Skip to content

Commit

Permalink
let's reduce the complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
ncode committed Nov 27, 2023
1 parent 9b035a2 commit 498031e
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions pkg/aclmanager/aclmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/redis/go-redis/v9"
"regexp"
"slices"
"strings"
)

Expand Down Expand Up @@ -155,35 +154,42 @@ func listAcls(client *redis.Client) (acls []string, err error) {

// syncAcls returns a list of acls in the cluster based on the redis acl list command
func syncAcls(source *redis.Client, destination *redis.Client) (deleted []string, err error) {
aclsToSync, err := listAcls(source)
sourceAcls, err := listAcls(source)
if err != nil {
return deleted, fmt.Errorf("error listing master acls: %v", err)
return nil, fmt.Errorf("error listing master acls: %v", err)
}

currentAcls, err := listAcls(destination)
destinationAcls, err := listAcls(destination)
if err != nil {
return deleted, fmt.Errorf("error listing current acls: %v", err)
return nil, fmt.Errorf("error listing current acls: %v", err)
}

for _, acl := range currentAcls {
acl := acl
if pos := slices.Index(aclsToSync, acl); pos != -1 {
aclsToSync = slices.Delete(aclsToSync, pos, pos+1)
continue
}
err = destination.Do(context.Background(), "ACL", "DELUSER", acl).Err()
if err != nil {
return deleted, fmt.Errorf("error deleting acl: %v", err)
// Map to keep track of ACLs to add
toAdd := make(map[string]struct{})
for _, acl := range sourceAcls {
toAdd[acl] = struct{}{}
}

// Delete ACLs not in source and remove from the toAdd map if present in destination
for _, acl := range destinationAcls {
if _, found := toAdd[acl]; found {
// If found in source, don't need to add, so remove from map
delete(toAdd, acl)
} else {
// If not found in source, delete from destination
if err := destination.Do(context.Background(), "ACL", "DELUSER", acl).Err(); err != nil {
return deleted, fmt.Errorf("error deleting acl: %v", err)
}
deleted = append(deleted, acl)
}
deleted = append(deleted, acl)
}

for _, acl := range aclsToSync {
err = destination.Do(context.Background(), "ACL", "SETUSER", acl).Err()
if err != nil {
// Add remaining ACLs from source
for acl := range toAdd {
if err := destination.Do(context.Background(), "ACL", "SETUSER", acl).Err(); err != nil {
return deleted, fmt.Errorf("error setting acl: %v", err)
}
}

return deleted, err
return deleted, nil
}

0 comments on commit 498031e

Please sign in to comment.