Call ACL Authorize for command before processing. Call ACL Authenticate when auth command is triggered.

This commit is contained in:
Kelvin Clement Mwinuka
2023-12-14 18:43:56 +08:00
parent 19c9b40b4c
commit 39bf074e33
3 changed files with 23 additions and 4 deletions

View File

@@ -173,8 +173,19 @@ func (server *Server) handleConnection(ctx context.Context, conn net.Conn) {
synchronize := command.Command.Sync synchronize := command.Command.Sync
if subCommand, ok := utils.GetSubCommand(command, cmd).(utils.SubCommand); ok { subCommand, ok := utils.GetSubCommand(command, cmd).(utils.SubCommand)
if ok {
synchronize = subCommand.Sync synchronize = subCommand.Sync
err = server.ACL.AuthorizeConnection(&conn, cmd, command.Command, subCommand)
} else {
err = server.ACL.AuthorizeConnection(&conn, cmd, command.Command, nil)
}
if err != nil {
connRW.WriteString(fmt.Sprintf("-%s\r\n\n", err.Error()))
connRW.Flush()
continue
} }
if !server.IsInCluster() || !synchronize { if !server.IsInCluster() || !synchronize {

View File

@@ -2,6 +2,7 @@ package acl
import ( import (
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"github.com/kelvinmwinuka/memstore/src/utils" "github.com/kelvinmwinuka/memstore/src/utils"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@@ -133,10 +134,11 @@ func (acl *ACL) RegisterConnection(conn *net.Conn) {
} }
func (acl *ACL) AuthenticateConnection(conn *net.Conn, cmd []string) error { func (acl *ACL) AuthenticateConnection(conn *net.Conn, cmd []string) error {
return nil return errors.New("could not authenticate user")
} }
func (acl *ACL) AuthorizeConnection(conn *net.Conn, cmd []string) error { func (acl *ACL) AuthorizeConnection(conn *net.Conn, cmd []string, command utils.Command, subCommand interface{}) error {
fmt.Println("SUBCOMMAND: ", subCommand)
return nil return nil
} }

View File

@@ -63,7 +63,13 @@ func (p Plugin) HandleCommand(ctx context.Context, cmd []string, server utils.Se
} }
func (p Plugin) handleAuth(ctx context.Context, cmd []string, server utils.Server, conn *net.Conn) ([]byte, error) { func (p Plugin) handleAuth(ctx context.Context, cmd []string, server utils.Server, conn *net.Conn) ([]byte, error) {
return nil, errors.New("AUTH not implemented") if len(cmd) < 2 || len(cmd) > 3 {
return nil, errors.New("wrong number of arguments")
}
if err := p.acl.AuthenticateConnection(conn, cmd); err != nil {
return nil, err
}
return []byte("+OK\r\n\n"), nil
} }
func (p Plugin) handleGetUser(ctx context.Context, cmd []string, server utils.Server, conn *net.Conn) ([]byte, error) { func (p Plugin) handleGetUser(ctx context.Context, cmd []string, server utils.Server, conn *net.Conn) ([]byte, error) {