Skip to content

Commit e2f6700

Browse files
destinyoooondyakov
andauthored
feat(acl): add acl support and test (#3576)
* feat: add acl support and command test * validate client name before kill it --------- Co-authored-by: Nedyalko Dyakov <1547186+ndyakov@users.noreply.github.com>
1 parent 71bb3ca commit e2f6700

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

acl_commands.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ type ACLCmdable interface {
88
ACLLog(ctx context.Context, count int64) *ACLLogCmd
99
ACLLogReset(ctx context.Context) *StatusCmd
1010

11+
ACLGenPass(ctx context.Context, bit int) *StringCmd
12+
1113
ACLSetUser(ctx context.Context, username string, rules ...string) *StatusCmd
1214
ACLDelUser(ctx context.Context, username string) *IntCmd
15+
ACLUsers(ctx context.Context) *StringSliceCmd
16+
ACLWhoAmI(ctx context.Context) *StringCmd
1317
ACLList(ctx context.Context) *StringSliceCmd
1418

1519
ACLCat(ctx context.Context) *StringSliceCmd
@@ -65,6 +69,24 @@ func (c cmdable) ACLSetUser(ctx context.Context, username string, rules ...strin
6569
return cmd
6670
}
6771

72+
func (c cmdable) ACLGenPass(ctx context.Context, bit int) *StringCmd {
73+
cmd := NewStringCmd(ctx, "acl", "genpass")
74+
_ = c(ctx, cmd)
75+
return cmd
76+
}
77+
78+
func (c cmdable) ACLUsers(ctx context.Context) *StringSliceCmd {
79+
cmd := NewStringSliceCmd(ctx, "acl", "users")
80+
_ = c(ctx, cmd)
81+
return cmd
82+
}
83+
84+
func (c cmdable) ACLWhoAmI(ctx context.Context) *StringCmd {
85+
cmd := NewStringCmd(ctx, "acl", "whoami")
86+
_ = c(ctx, cmd)
87+
return cmd
88+
}
89+
6890
func (c cmdable) ACLList(ctx context.Context) *StringSliceCmd {
6991
cmd := NewStringSliceCmd(ctx, "acl", "list")
7092
_ = c(ctx, cmd)

acl_commands_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ var _ = Describe("ACL user commands", Label("NonRedisEnterprise"), func() {
9292
Expect(err).NotTo(HaveOccurred())
9393
Expect(res).To(HaveLen(1))
9494
Expect(res[0]).To(ContainSubstring("default"))
95+
96+
res, err = client.ACLUsers(ctx).Result()
97+
Expect(err).NotTo(HaveOccurred())
98+
Expect(res).To(HaveLen(1))
99+
Expect(res[0]).To(Equal("default"))
100+
101+
res1, err := client.ACLWhoAmI(ctx).Result()
102+
Expect(err).NotTo(HaveOccurred())
103+
Expect(res1).To(Equal("default"))
104+
})
105+
106+
It("gen password", func() {
107+
password, err := client.ACLGenPass(ctx, 0).Result()
108+
Expect(err).NotTo(HaveOccurred())
109+
Expect(password).NotTo(BeEmpty())
95110
})
96111

97112
It("setuser and deluser", func() {

commands_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,29 @@ var _ = Describe("Commands", func() {
199199
Expect(r.Val()).To(Equal(int64(0)))
200200
})
201201

202+
It("should ClientKillByFilter with kill myself", func() {
203+
opt := redisOptions()
204+
opt.ClientName = "killmyid"
205+
db := redis.NewClient(opt)
206+
Expect(db.Ping(ctx).Err()).NotTo(HaveOccurred())
207+
208+
defer func() {
209+
Expect(db.Close()).NotTo(HaveOccurred())
210+
}()
211+
val, err := client.ClientList(ctx).Result()
212+
Expect(err).NotTo(HaveOccurred())
213+
Expect(val).Should(ContainSubstring("name=killmyid"))
214+
215+
myid := db.ClientID(ctx).Val()
216+
killed := client.ClientKillByFilter(ctx, "ID", strconv.FormatInt(myid, 10))
217+
Expect(killed.Err()).NotTo(HaveOccurred())
218+
Expect(killed.Val()).To(BeNumerically("==", 1))
219+
220+
val, err = client.ClientList(ctx).Result()
221+
Expect(err).NotTo(HaveOccurred())
222+
Expect(val).ShouldNot(ContainSubstring("name=killmyid"))
223+
})
224+
202225
It("should ClientKillByFilter with MAXAGE", Label("NonRedisEnterprise"), func() {
203226
SkipBeforeRedisVersion(7.4, "doesn't work with older redis stack images")
204227
var s []string

0 commit comments

Comments
 (0)