centos8平台:舉例講解redis6的ACL功能(redis6.0.1)


一,為什么redis6要增加acl功能模塊?

什么是acl?

訪問控制列表(ACL)是一種基於包過濾的訪問控制技術,

它可以根據設定的條件對接口上的數據包進行過濾,允許其通過或丟棄

 

redis6增加了acl功能模塊后,極大的提高了redis的安全性,

使redis更適用於企業級的業務場景

 

說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest

         對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/

說明:作者:劉宏締 郵箱: 371125307@qq.com

 

二,linux平台redis6的安裝:

請參見這一篇:

https://www.cnblogs.com/architectforest/p/12830056.html

 

三,查看redis6中acl的幫助

1,acl的命令列表

[root@centos8 bin]# ./redis-cli 
127.0.0.1:6379> acl help
 1) ACL <subcommand> arg arg ... arg. Subcommands are:
 2) LOAD                             -- Reload users from the ACL file.
 3) SAVE                             -- Save the current config to the ACL file.
 4) LIST                             -- Show user details in config file format.
 5) USERS                            -- List all the registered usernames.
 6) SETUSER <username> [attribs ...] -- Create or modify a user.
 7) GETUSER <username>               -- Get the user details.
 8) DELUSER <username> [...]         -- Delete a list of users.
 9) CAT                              -- List available categories.
10) CAT <category>                   -- List commands inside category.
11) GENPASS [<bits>]                 -- Generate a secure user password.
12) WHOAMI                           -- Return the current connection username.
13) LOG [<count> | RESET]            -- Show the ACL log entries.

 

以上各個命令的說明:

LOAD:從acl文件加載規則(說明:此文件在redis.conf中有定義)

SAVE:保存規則到acl文件

LIST                  列出所有的acl規則

USERS              列出所有的用戶

SETUSER          設置/創建一個用戶

GETUSER          查看一個用戶

DELUSER            刪除一個用戶

CAT                      查看所有的命令分類

CAT <category>    列出一個命令分類下的命令

GENPASS   生成密碼

WHOAMI     查看當前用戶

LOG    日志操作

 

2,acl命令的參數:密碼

>pwd  添加密碼列表

<pwd  移除密碼列表

nopass 移除當前用戶的密碼

 

3,acl命令的參數: command

+<command> 添加命令 

-<command> 移除命令 

+@<category> 添加一個命令分類

-@<category> 移除一個命令分類

allcommands 所有命令可用 別名 +@all 

nocommands 所有命令不可用 別名 -@all

 

說明:關於​命令分類

所有命令分類可以用 acl cat 查看

命令分類下的命令可以用 acl cat <category>查看

​ 

4,acl命令的參數: key

~<pattern>  

~* 所有key 

allkeys 別名 ~*

 

四,redis6的acl使用例子:

1,列出當前的acl規則:

127.0.0.1:6379> acl list
1) "user default on nopass ~* +@all"

acl的字段說明:

user  :用戶
default  :默認用戶(反之 為自己創建的用戶)
on         :狀態是激活,未激活是off
nopass  :不需要密碼
~*           :可訪問的key
+@all      :可操作的command

 

2,列出所有的命令分類:

127.0.0.1:6379> 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"

 

3,列出一個命令分類下面所有的命令:

127.0.0.1:6379> acl cat set
 1) "smembers"
 2) "sinter"
 3) "srandmember"
 4) "sdiff"
 5) "sismember"
 6) "sinterstore"
 7) "scard"
 8) "sadd"
 9) "sort"
10) "srem"
11) "sdiffstore"
12) "sunionstore"
13) "spop"
14) "smove"
15) "sscan"
16) "sunion"

 

4,創建用戶/查看用戶

創建用戶

127.0.0.1:6379> acl setuser laoliu
OK

 

查看用戶

127.0.0.1:6379> acl getuser laoliu
1) "flags"
2) 1) "off"
3) "passwords"
4) (empty array)
5) "commands"
6) "-@all"
7) "keys"
8) (empty array)

說明:可以看到,如果未明確指定,

         用戶無權使用任何命令,也無權訪問任何key

 

查看添加用戶后的acl

127.0.0.1:6379> acl list
1) "user default on nopass ~* +@all"
2) "user laoliu off -@all"

可以看到默認添加的用戶狀態是off,

需要把賬號設置為激活狀態:

127.0.0.1:6379> acl setuser laoliu on
OK

 

5,給laoliu賬號配置權限:

127.0.0.1:6379> acl setuser laoliu on >lhdpass1 >lhdpass2 >lhdpass3 +@all ~*
OK
127.0.0.1:6379> acl getuser laoliu
1) "flags"
2) 1) "on"
   2) "allkeys"
   3) "allcommands"
3) "passwords"
4) 1) "7ff6ac0bf1fc54b1eb01f65e306a757b91f84a0ea421508945fa3e00917d3fef"
   2) "c989e79ee6baa5bfa95af56810ae3e57be9432e8700771b7bc1fbb3b7e75158b"
   3) "2e634d78bd89dce200e61c6e1d1c97e22607105a01c0b84cd3ee4251e6769bb8"
5) "commands"
6) "+@all"
7) "keys"
8) 1) "*"

說明:

on     用戶為激活狀態

>lhdpass1 >lhdpass2 >lhdpass3

添加三個密碼,每個密碼都可以使用

+@all   可以使用所有權限

~*       可以訪問所有的key

 

6,查看當前用戶/切換當前用戶

#whoami:   查看當前用戶

127.0.0.1:6379> acl whoami
"default"
127.0.0.1:6379> auth laoliu lhdpass2
OK
127.0.0.1:6379> acl whoami
"laoliu"

說明:5.x的auth命令無需用戶名,

         6.x的auth命令必須指定用戶名

 

7,查看用戶列表/刪除用戶/關閉(凍結)用戶/激活用戶

#acl users: 列出所有用戶

127.0.0.1:6379> acl users
1) "default"
2) "help"
3) "laoliu"

 

#deluser: 刪除用戶

127.0.0.1:6379> acl deluser help
(integer) 1
127.0.0.1:6379> acl users
1) "default"
2) "laoliu"

 

#關閉(凍結)用戶:設置狀態為off即可

127.0.0.1:6379> acl setuser laoliu off
OK
127.0.0.1:6379> auth laoliu lhdpass1
(error) WRONGPASS invalid username-password pair

說明:關閉用戶后已經不能再以此用戶登錄

 

#激活一個關閉的用戶:設置狀態為on即可

127.0.0.1:6379> acl setuser laoliu on
OK
127.0.0.1:6379> auth laoliu lhdpass1
OK

 

8,保存當前的acl

首先在redis.conf中配置acl文件的啟用:

[root@centos8 conf]# vi redis.conf 

編輯內容:

aclfile /usr/local/soft/redis6/conf/users.acl

說明:這個acl文件的路徑指定,應該用絕對路徑

啟動redis前,如果users.acl不存在,則先手動生成一個users.acl空文件

[root@centos8 conf]# touch /usr/local/soft/redis6/conf/users.acl

重新啟動redis

[root@centos8 bin]# systemctl restart redis6

再次用redis-cli連接到server,

[root@centos8 bin]# ./redis-cli 
127.0.0.1:6379> acl whoami
"default"
127.0.0.1:6379> acl list
1) "user default on nopass ~* +@all"
127.0.0.1:6379> acl save
OK

 

查看外部文件中所保存的規則:

[root@centos8 conf]# more users.acl 
user default on nopass ~* +@all

可以看到,已保存成功

 

說明:如果當前沒有配置使用acl外部文件,

則在保存acl規則時redis會給出報錯:

127.0.0.1:6379> acl save
(error) ERR This Redis instance is not configured to use an ACL file. 
You may want to specify users via the ACL SETUSER command and 
then issue a CONFIG REWRITE (assuming you have a Redis configuration file set) 
in order to store users in the Redis configuration.

 

9,給缺省用戶添加密碼:

127.0.0.1:6379> acl setuser default on >lhdpass1 >lhdpass2 >lhdpass3 +@all ~*
OK
127.0.0.1:6379> acl getuser default
1) "flags"
2) 1) "on"
   2) "allkeys"
   3) "allcommands"
3) "passwords"
4) 1) "7ff6ac0bf1fc54b1eb01f65e306a757b91f84a0ea421508945fa3e00917d3fef"
   2) "c989e79ee6baa5bfa95af56810ae3e57be9432e8700771b7bc1fbb3b7e75158b"
   3) "2e634d78bd89dce200e61c6e1d1c97e22607105a01c0b84cd3ee4251e6769bb8"
5) "commands"
6) "+@all"
7) "keys"
8) 1) "*"

然后保存到acl

127.0.0.1:6379> acl save
OK

查看保存的acl內容

[root@centos8 conf]# more users.acl 
user default on #7ff6ac0bf1fc54b1eb01f65e306a757b91f84a0ea421508945fa3e00917d3fef 
#c989e79ee6baa5bfa95af56810ae3e57be9432e8700771b7bc1fbb3b7e75158b
#2e634d78bd89dce200e61c6e1d1c97e22607105a01c0b84cd3ee4251e6769bb8
~* +@all

因為default加了密碼,重啟后需要auth才能訪問

[root@centos8 bin]# systemctl restart redis6.service 
[root@centos8 bin]# ./redis-cli 
127.0.0.1:6379> get a
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth default lhdpass2
OK
127.0.0.1:6379> get a
"aaaa"

 

10,添加一個生產環境的用戶例子:

給予指定的命令和訪問指定的內容

127.0.0.1:6379> acl setuser api on >apipass1 +get +set ~goods:* ~home:*
OK

說明:創建了用戶api,

         密碼: apipass1

         可用的命令有 get set 

         可以訪問的key有 分別以goods:/home:作前綴的兩類key

新建3個key供測試用

127.0.0.1:6379> auth laoliu lhdpass1
OK
127.0.0.1:6379> set goods:123 cup
OK
127.0.0.1:6379> set home:345 ad
OK
127.0.0.1:6379> set list:789 allgoods
OK

切換到新用戶

127.0.0.1:6379> auth api apipass1
OK

注意acl的whoami命令當前用戶無權使用,因為未做授權

127.0.0.1:6379> acl whoami
(error) NOPERM this user has no permissions to run the 'acl' command or its subcommand

可以用get/set命令訪問goods:作前綴的key

127.0.0.1:6379> get goods:123
"cup"
127.0.0.1:6379> set goods:123 shoes
OK
127.0.0.1:6379> get goods:123
"shoes"

未被授權的key不能訪問

127.0.0.1:6379> get list:789
(error) NOPERM this user has no permissions to access one of the keys used as arguments

 

11,command/key/password的增加和減少

查看當前用戶:

127.0.0.1:6379> acl getuser api
1) "flags"
2) 1) "on"
3) "passwords"
4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
5) "commands"
6) "-@all +get +set"
7) "keys"
8) 1) "goods:*"
   2) "home:*"

 

減少命令/增加命令

127.0.0.1:6379> acl setuser api -set
OK
127.0.0.1:6379> acl setuser api +hget
OK
127.0.0.1:6379> acl getuser api
1) "flags"
2) 1) "on"
3) "passwords"
4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
5) "commands"
6) "-@all +hget +get"
7) "keys"
8) 1) "goods:*"
   2) "home:*"

 

減少key/增加key

減少已添加的key,用resetkeys,后跟需要保留的key

127.0.0.1:6379> acl setuser api resetkeys ~home:*
OK
127.0.0.1:6379> acl getuser api
1) "flags"
2) 1) "on"
3) "passwords"
4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
5) "commands"
6) "-@all +hget +get"
7) "keys"
8) 1) "home:*"

 

添加可訪問的key:直接添加就可以

127.0.0.1:6379> acl setuser api ~list:*
OK
127.0.0.1:6379> acl getuser api
1) "flags"
2) 1) "on"
3) "passwords"
4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
5) "commands"
6) "-@all +hget +get"
7) "keys"
8) 1) "home:*"
   2) "list:*"

 

添加密碼

# >apipass2 :表示添加一個密碼:apipass2

127.0.0.1:6379> acl setuser api >apipass2
OK
127.0.0.1:6379> acl getuser api
1) "flags"
2) 1) "on"
3) "passwords"
4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
   2) "a5255a6217658703a1a427ce3e6690aa335f8ca8f089dd629f54be45c2db3187"
5) "commands"
6) "-@all +hget +get"
7) "keys"
8) 1) "home:*"
   2) "list:*"

 

刪除密碼

# <apipass2 :表示刪除apipass2這個密碼

127.0.0.1:6379> acl setuser api <apipass2
OK
127.0.0.1:6379> acl getuser api
1) "flags"
2) 1) "on"
3) "passwords"
4) 1) "9e82332c8e2177f60f216351be4cb656ad2b06bea105d1ac21d1a1fba92e667d"
5) "commands"
6) "-@all +hget +get"
7) "keys"
8) 1) "home:*"
   2) "list:*"

 

12,acl日志的操作:

清空日志

#log reset: 清空已記錄的日志

127.0.0.1:6379> acl log reset
OK

#log count: 查看指定條數的日志

127.0.0.1:6379> acl log  5

 

五,查看redis的版本

[root@centos8 bin]# /usr/local/soft/redis6/bin/redis-server --version
Redis server v=6.0.1 sha=00000000:0 malloc=jemalloc-5.1.0 bits=64 build=0

 

六,查看centos的版本

[root@centos8 bin]# cat /etc/redhat-release 
CentOS Linux release 8.1.1911 (Core)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM