mirror of
https://github.com/EchoVault/SugarDB.git
synced 2025-10-09 18:00:23 +08:00
Added command categories in utils/types.go and in each of the current modules and commands
This commit is contained in:
@@ -176,7 +176,8 @@ func (acl *ACL) AuthenticateConnection(conn *net.Conn, cmd []string) error {
|
||||
for _, userPassword := range user.Passwords {
|
||||
for _, password := range passwords {
|
||||
if strings.EqualFold(userPassword.PasswordType, password.PasswordType) &&
|
||||
userPassword.PasswordValue == password.PasswordValue {
|
||||
userPassword.PasswordValue == password.PasswordValue &&
|
||||
user.Enabled {
|
||||
// Set the current connection to the selected user and set them as authenticated
|
||||
acl.Connections[conn] = Connection{
|
||||
Authenticated: true,
|
||||
@@ -191,6 +192,13 @@ func (acl *ACL) AuthenticateConnection(conn *net.Conn, cmd []string) error {
|
||||
}
|
||||
|
||||
func (acl *ACL) AuthorizeConnection(conn *net.Conn, cmd []string, command utils.Command, subCommand interface{}) error {
|
||||
// 1. Check if password is required and if we're authorized
|
||||
// 2. Check if commands category is in IncludedCommands
|
||||
// 3. Check if commands category is in ExcludedCommands
|
||||
// 4. Check if commands is in IncludedCommands
|
||||
// 5. Check if commands is in ExcludedCommands
|
||||
// 6. Check if keys are in IncludedKeys
|
||||
// 7. Check if keys are in ExcludedKeys
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@@ -122,7 +122,7 @@ func NewModule(acl *ACL) Plugin {
|
||||
commands: []utils.Command{
|
||||
{
|
||||
Command: "auth",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ConnectionCategory, utils.SlowCategory},
|
||||
Description: "(AUTH [username] password) Authenticates the connection",
|
||||
Sync: false,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -137,8 +137,8 @@ func NewModule(acl *ACL) Plugin {
|
||||
SubCommands: []utils.SubCommand{
|
||||
{
|
||||
Command: "cat",
|
||||
Categories: []string{},
|
||||
Description: "(ACL CAT) List all the categories and commands inside a category.",
|
||||
Categories: []string{utils.SlowCategory},
|
||||
Description: "(ACL CAT [category]) List all the categories and commands inside a category.",
|
||||
Sync: false,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
return []string{}, nil
|
||||
@@ -146,8 +146,8 @@ func NewModule(acl *ACL) Plugin {
|
||||
},
|
||||
{
|
||||
Command: "users",
|
||||
Categories: []string{},
|
||||
Description: "(ACL LIST) List all ACL users",
|
||||
Categories: []string{utils.AdminCategory, utils.SlowCategory, utils.DangerousCategory},
|
||||
Description: "(ACL USERS) List all usersnames of the configured ACL users",
|
||||
Sync: false,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
return []string{}, nil
|
||||
@@ -155,7 +155,7 @@ func NewModule(acl *ACL) Plugin {
|
||||
},
|
||||
{
|
||||
Command: "setuser",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.AdminCategory, utils.SlowCategory, utils.DangerousCategory},
|
||||
Description: "(ACL SETUSER) Configure a new or existing user",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -164,7 +164,7 @@ func NewModule(acl *ACL) Plugin {
|
||||
},
|
||||
{
|
||||
Command: "getuser",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.AdminCategory, utils.SlowCategory, utils.DangerousCategory},
|
||||
Description: "(ACL GETUSER) List the ACL rules of a user",
|
||||
Sync: false,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -173,7 +173,7 @@ func NewModule(acl *ACL) Plugin {
|
||||
},
|
||||
{
|
||||
Command: "deluser",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.AdminCategory, utils.SlowCategory, utils.DangerousCategory},
|
||||
Description: "(ACL DELUSER) Deletes users and terminates their connections",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -182,7 +182,7 @@ func NewModule(acl *ACL) Plugin {
|
||||
},
|
||||
{
|
||||
Command: "whoami",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.FastCategory},
|
||||
Description: "(ACL WHOAMI) Returns the authenticated user of the current connection",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -191,7 +191,7 @@ func NewModule(acl *ACL) Plugin {
|
||||
},
|
||||
{
|
||||
Command: "list",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.AdminCategory, utils.SlowCategory, utils.DangerousCategory},
|
||||
Description: "(ACL LIST) Dumps effective acl rules in acl config file format",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -200,7 +200,7 @@ func NewModule(acl *ACL) Plugin {
|
||||
},
|
||||
{
|
||||
Command: "load",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.AdminCategory, utils.SlowCategory, utils.DangerousCategory},
|
||||
Description: "(ACL LOAD) Reloads the rules from the configured ACL config file",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -209,7 +209,7 @@ func NewModule(acl *ACL) Plugin {
|
||||
},
|
||||
{
|
||||
Command: "save",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.AdminCategory, utils.SlowCategory, utils.DangerousCategory},
|
||||
Description: "(ACL SAVE) Saves the effective ACL rules the configured ACL config file",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
|
@@ -153,11 +153,11 @@ func handleMSet(ctx context.Context, cmd []string, s utils.Server) ([]byte, erro
|
||||
|
||||
func NewModule() Plugin {
|
||||
SetModule := Plugin{
|
||||
name: "SetCommands",
|
||||
name: "OtherCommands",
|
||||
commands: []utils.Command{
|
||||
{
|
||||
Command: "set",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.WriteCategory, utils.SlowCategory},
|
||||
Description: "(SET key value) Set the value of a key, considering the value's type.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -169,7 +169,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "setnx",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.WriteCategory, utils.SlowCategory},
|
||||
Description: "(SETNX key value) Set the key/value only if the key doesn't exist.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -181,7 +181,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "mset",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.WriteCategory, utils.SlowCategory},
|
||||
Description: "(MSET key value [key value ...]) Automatically etc or modify multiple key/value pairs.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
|
@@ -98,7 +98,7 @@ func NewModule() Plugin {
|
||||
commands: []utils.Command{
|
||||
{
|
||||
Command: "get",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ReadCategory, utils.FastCategory},
|
||||
Description: "(GET key) Get the value at the specified key.",
|
||||
Sync: false,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -110,7 +110,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "mget",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ReadCategory, utils.FastCategory},
|
||||
Description: "(MGET key1 [key2]) Get multiple values from the specified keys.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
|
@@ -508,7 +508,7 @@ func NewModule() Plugin {
|
||||
commands: []utils.Command{
|
||||
{
|
||||
Command: "lpush",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ListCategory, utils.WriteCategory, utils.FastCategory},
|
||||
Description: "(LPUSH key value1 [value2]) Prepends one or more values to the beginning of a list, creates the list if it does not exist.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -520,7 +520,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "lpushx",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ListCategory, utils.WriteCategory, utils.FastCategory},
|
||||
Description: "(LPUSHX key value) Prepends a value to the beginning of a list only if the list exists.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -532,7 +532,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "lpop",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ListCategory, utils.WriteCategory, utils.FastCategory},
|
||||
Description: "(LPOP key) Removes and returns the first element of a list.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -544,7 +544,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "llen",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ListCategory, utils.ReadCategory, utils.FastCategory},
|
||||
Description: "(LLEN key) Return the length of a list.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -556,7 +556,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "lrange",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ListCategory, utils.ReadCategory, utils.SlowCategory},
|
||||
Description: "(LRANGE key start end) Return a range of elements between the given indices.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -568,7 +568,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "lindex",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ListCategory, utils.ReadCategory, utils.SlowCategory},
|
||||
Description: "(LINDEX key index) Gets list element by index.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -580,7 +580,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "lset",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ListCategory, utils.WriteCategory, utils.SlowCategory},
|
||||
Description: "(LSET key index value) Sets the value of an element in a list by its index.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -592,7 +592,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "ltrim",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ListCategory, utils.WriteCategory, utils.SlowCategory},
|
||||
Description: "(LTRIM key start end) Trims a list to the specified range.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -604,7 +604,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "lrem",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ListCategory, utils.WriteCategory, utils.SlowCategory},
|
||||
Description: "(LREM key count value) Remove elements from list.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -616,7 +616,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "lmove",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ListCategory, utils.WriteCategory, utils.SlowCategory},
|
||||
Description: "(LMOVE source destination <LEFT | RIGHT> <LEFT | RIGHT>) Move element from one list to the other specifying left/right for both lists.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -628,7 +628,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "rpop",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ListCategory, utils.WriteCategory, utils.FastCategory},
|
||||
Description: "(RPOP key) Removes and gets the last element in a list.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -640,7 +640,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "rpush",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ListCategory, utils.WriteCategory, utils.FastCategory},
|
||||
Description: "(RPUSH key value [value2]) Appends one or multiple elements to the end of a list.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -652,7 +652,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "rpushx",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.ListCategory, utils.WriteCategory, utils.FastCategory},
|
||||
Description: "(RPUSHX key value) Appends an element to the end of a list, only if the list exists.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
|
@@ -60,7 +60,7 @@ func NewModule() Plugin {
|
||||
commands: []utils.Command{
|
||||
{
|
||||
Command: "ping",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.FastCategory, utils.ConnectionCategory},
|
||||
Description: "(PING [value]) Ping the server. If a value is provided, the value will be echoed.",
|
||||
Sync: false,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
|
@@ -96,7 +96,7 @@ func NewModule(pubsub *PubSub) Plugin {
|
||||
commands: []utils.Command{
|
||||
{
|
||||
Command: "publish",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.PubSubCategory, utils.FastCategory},
|
||||
Description: "(PUBLISH channel message) Publish a message to the specified channel.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -109,7 +109,7 @@ func NewModule(pubsub *PubSub) Plugin {
|
||||
},
|
||||
{
|
||||
Command: "subscribe",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.PubSubCategory, utils.SlowCategory},
|
||||
Description: "(SUBSCRIBE channel [consumer_group]) Subscribe to a channel with an option to join a consumer group on the channel.",
|
||||
Sync: false,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -122,7 +122,7 @@ func NewModule(pubsub *PubSub) Plugin {
|
||||
},
|
||||
{
|
||||
Command: "unsubscribe",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.PubSubCategory, utils.SlowCategory},
|
||||
Description: "(UNSUBSCRIBE channel) Unsubscribe from a channel.",
|
||||
Sync: false,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
|
@@ -204,7 +204,7 @@ func NewModule() Plugin {
|
||||
commands: []utils.Command{
|
||||
{
|
||||
Command: "setrange",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.StringCategory, utils.WriteCategory, utils.SlowCategory},
|
||||
Description: "(SETRANGE key offset value) Overwrites part of a string value with another by offset. Creates the key if it doesn't exist.",
|
||||
Sync: true,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -216,7 +216,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "strlen",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.StringCategory, utils.ReadCategory, utils.FastCategory},
|
||||
Description: "(STRLEN key) Returns length of the key's value if it's a string.",
|
||||
Sync: false,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -228,7 +228,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "substr",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.StringCategory, utils.ReadCategory, utils.SlowCategory},
|
||||
Description: "(SUBSTR key start end) Returns a substring from the string value.",
|
||||
Sync: false,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
@@ -240,7 +240,7 @@ func NewModule() Plugin {
|
||||
},
|
||||
{
|
||||
Command: "getrange",
|
||||
Categories: []string{},
|
||||
Categories: []string{utils.StringCategory, utils.ReadCategory, utils.SlowCategory},
|
||||
Description: "(GETRANGE key start end) Returns a substring from the string value.",
|
||||
Sync: false,
|
||||
KeyExtractionFunc: func(cmd []string) ([]string, error) {
|
||||
|
@@ -60,3 +60,29 @@ type Plugin interface {
|
||||
Description() string
|
||||
HandleCommand(ctx context.Context, cmd []string, server Server, conn *net.Conn) ([]byte, error)
|
||||
}
|
||||
|
||||
type CommandCategory string
|
||||
|
||||
const (
|
||||
AdminCategory = "admin"
|
||||
BitmapCategory = "bitmap"
|
||||
BlockingCategory = "blocking"
|
||||
ConnectionCategory = "connection"
|
||||
DangerousCategory = "dangerous"
|
||||
GeoCategory = "geo"
|
||||
HashCategory = "hash"
|
||||
HyperLogLogCategory = "hyperloglog"
|
||||
FastCategory = "fast"
|
||||
KeyspaceCategory = "keyspace"
|
||||
ListCategory = "list"
|
||||
PubSubCategory = "pubsub"
|
||||
ReadCategory = "read"
|
||||
ScriptingCategory = "scripting"
|
||||
SetCategory = "set"
|
||||
SortedSetCategory = "sortedset"
|
||||
SlowCategory = "slow"
|
||||
StreamCategory = "stream"
|
||||
StringCategory = "string"
|
||||
TransactionCategory = "transaction"
|
||||
WriteCategory = "write"
|
||||
)
|
||||
|
Reference in New Issue
Block a user