Redis 6.0 ACL
期待已久的ACL終於來了,大家知道在redis集群中只有一個db,在多項目操作時可以通過key前綴來區分,但是還是能獲取其它key,這樣就帶來了安全風險.
Access Control Lists ACL
在之前 登錄可以AUTH pwd
有了ACL后AUTH user pwd
ACL LIST
我們可以使用 ACL LIST 命令來檢查當前活動的 ACL
> ACL LIST
1) "user default on nopass ~* +@all"
user 代表是用戶 default 代表默認用戶(反之 為自己創建的用戶) on 代表激活(反之off,默認新增的為off) nopass 代表不需要密碼 ~* 代表可以訪問的key +@all 代表可以操作的command
key
~<pattern> ~* 所有key
allkeys 別名 ~*
resetkeys 刷新允許的鍵模式列表
可以多個一起使用,如:
~foo:* ~bar:* resetkeys ~objects:*
只能訪問 ~objects:*
command
+<command> 添加命令
-<command> 移除命令
+@<category> 添加一類命令,如:@admin, @set, @sortedset, ... 可以ACL CAT 查看
如:+@geo -@readonly 會排除geo的只讀命令
-@<category> 移除一類命令
+<command>|subcommand 允許一個特定的子命令,沒有-<command>|subcommand
allcommands 所有cmd 別名 +@all
nocommands 別名 -@all
配置密碼
>pwd 添加密碼列表
<pwd 移除密碼列表
#hash 添加SHA-256值
!hash 移除SHA-256值
nopass 移除所有密碼
resetpass 刷新密碼列表,並且也不會回到nopass 狀態,之后一定要添加密碼
沒有使用nopass標記的用戶並且沒有有效密碼列表 是不能登錄的
reset 重置用戶 實際上是執行 resetpass, resetkeys, off, -@all
另外,在默認用戶的特殊情況下,擁有 nopass 規則意味着新的連接將通過默認用戶的自動身份驗證,而不需要任何顯式的 AUTH 調用。
ACL CAT
第一大類
127.0.0.1:0>ACL CAT
1) "keyspace"
2) "read"
3) "write"
4) "set"
5) "sortedset"
6) "list"
7) "hash"
8) "string"
9) "bitmap"
10) "hyperloglog"
11) "geo"
12) "stream"
13) "pubsub"
14) "admin"
15) "fast"
16) "slow"
17) "blocking"
18) "dangerous"
19) "connection"
20) "transaction"
21) "scripting"
127.0.0.1:0>ACL CAT set
1) "spop"
2) "sunionstore"
3) "sinter"
4) "sdiffstore"
5) "sscan"
6) "sunion"
7) "smembers"
8) "sinterstore"
9) "sismember"
10) "smove"
11) "srem"
12) "sdiff"
13) "srandmember"
14) "sort"
15) "sadd"
16) "scard"
ACL SETUSER
添加用戶 - 區分大小寫
ACL SETUSER alice
檢查一下默認的用戶狀態:
> ACL LIST
1) "user alice off -@all"
2) "user default on nopass ~* +@all"
設置密碼
ACL SETUSER alice on >p1pp0 ~cached:* +get
> AUTH alice p1pp0
OK
> GET foo
(error) NOPERM this user has no permissions to access one of the keys used as arguments
> GET cached:1234
(nil)
> SET cached:1234 zap
(error) NOPERM this user has no permissions to run the 'set' command or its subcommnad
查看單獨用戶
打開一個新的連接
> ACL GETUSER alice
1) "flags"
2) 1) "on"
3) "passwords"
4) 1) "2d9c75..."
5) "commands"
6) "-@all +get"
7) "keys"
8) 1) "cached:*"
返回 field-value 數組
如果使用RESP3
> ACL GETUSER alice
1# "flags" => 1~ "on"
2# "passwords" => 1) "2d9c75..."
3# "commands" => "-@all +get"
4# "keys" => 1) "cached:*"
新增權限
ACL SETUSER alice ~objects:* ~items:* ~public:*
> ACL LIST
1) "user alice on #2d9c75... ~cached:* ~objects:* ~items:* ~public:* -@all +get"
2) "user default on nopass ~* +@all"
ACL SETUSER alice +set
ACL SETUSER alice +get
> ACL LIST
1) "user alice on #2d9c75... ~cached:* ~objects:* ~items:* ~public:* -@all +get +set"
2) "user default on nopass ~* +@all"
其它
ACL GENPASS
生成隨機數、密碼
redis.conf
配置文件中可以保存用戶信息 如: user worker +@list +@connection ~jobs:* on >ffa9203c493aa99 也可以指定外部acl文件信息 如:aclfile /etc/redis/users.acl
ACL LOAD 重置加載acl文件(如果你修改了acl文件) ACL SAVE 保存acl文件
哨兵和副本
略...