Skip to content

Commit fcc0007

Browse files
committed
add ACL{SetUser,DelUser,List} commands
1 parent 3d4310a commit fcc0007

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

acl_commands.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ type ACLCmdable interface {
66
ACLDryRun(ctx context.Context, username string, command ...interface{}) *StringCmd
77
ACLLog(ctx context.Context, count int64) *ACLLogCmd
88
ACLLogReset(ctx context.Context) *StatusCmd
9+
ACLSetUser(ctx context.Context, username string, rules ...string) *StatusCmd
10+
ACLDelUser(ctx context.Context, username string) *IntCmd
11+
ACLList(ctx context.Context) *StringSliceCmd
912
}
1013

1114
func (c cmdable) ACLDryRun(ctx context.Context, username string, command ...interface{}) *StringCmd {
@@ -33,3 +36,32 @@ func (c cmdable) ACLLogReset(ctx context.Context) *StatusCmd {
3336
_ = c(ctx, cmd)
3437
return cmd
3538
}
39+
40+
func (c cmdable) ACLDelUser(ctx context.Context, username string) *IntCmd {
41+
args := make([]interface{}, 3, 3)
42+
args[0] = "acl"
43+
args[1] = "deluser"
44+
args[2] = username
45+
cmd := NewIntCmd(ctx, args...)
46+
_ = c(ctx, cmd)
47+
return cmd
48+
}
49+
50+
func (c cmdable) ACLSetUser(ctx context.Context, username string, rules ...string) *StatusCmd {
51+
args := make([]interface{}, 3+len(rules), 3+len(rules))
52+
args[0] = "acl"
53+
args[1] = "setuser"
54+
args[2] = username
55+
for i, rule := range rules {
56+
args[i+3] = rule
57+
}
58+
cmd := NewStatusCmd(ctx, args...)
59+
_ = c(ctx, cmd)
60+
return cmd
61+
}
62+
63+
func (c cmdable) ACLList(ctx context.Context) *StringSliceCmd {
64+
cmd := NewStringSliceCmd(ctx, "acl", "list")
65+
_ = c(ctx, cmd)
66+
return cmd
67+
}

acl_commands_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package redis_test
2+
3+
import (
4+
"context"
5+
6+
"github.com/redis/go-redis/v9"
7+
8+
. "github.com/bsm/ginkgo/v2"
9+
. "github.com/bsm/gomega"
10+
)
11+
12+
var TestUserName string = "goredis"
13+
14+
var _ = Describe("ACL Commands", func() {
15+
var client *redis.Client
16+
var ctx context.Context
17+
18+
BeforeEach(func() {
19+
ctx = context.Background()
20+
client = redis.NewClient(redisOptions())
21+
})
22+
23+
AfterEach(func() {
24+
_, err := client.ACLDelUser(context.Background(), TestUserName).Result()
25+
Expect(client.Close()).NotTo(HaveOccurred())
26+
Expect(err).NotTo(HaveOccurred())
27+
})
28+
29+
It("list only default user", func() {
30+
res, err := client.ACLList(ctx).Result()
31+
Expect(err).NotTo(HaveOccurred())
32+
Expect(res).To(HaveLen(1))
33+
Expect(res[0]).To(ContainSubstring("default"))
34+
})
35+
36+
It("setuser and deluser", func() {
37+
res, err := client.ACLList(ctx).Result()
38+
Expect(err).NotTo(HaveOccurred())
39+
Expect(res).To(HaveLen(1))
40+
Expect(res[0]).To(ContainSubstring("default"))
41+
42+
add, err := client.ACLSetUser(ctx, TestUserName, "nopass", "on", "allkeys", "+set", "+get").Result()
43+
Expect(err).NotTo(HaveOccurred())
44+
Expect(add).To(Equal("OK"))
45+
46+
resAfter, err := client.ACLList(ctx).Result()
47+
Expect(err).NotTo(HaveOccurred())
48+
Expect(resAfter).To(HaveLen(2))
49+
Expect(resAfter[1]).To(ContainSubstring(TestUserName))
50+
51+
deletedN, err := client.ACLDelUser(ctx, TestUserName).Result()
52+
Expect(err).NotTo(HaveOccurred())
53+
Expect(deletedN).To(BeNumerically("==", 1))
54+
55+
resAfterDeletion, err := client.ACLList(ctx).Result()
56+
Expect(err).NotTo(HaveOccurred())
57+
Expect(resAfterDeletion).To(HaveLen(1))
58+
Expect(resAfterDeletion[0]).To(BeEquivalentTo(res[0]))
59+
})
60+
})

0 commit comments

Comments
 (0)