zoukankan      html  css  js  c++  java
  • 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)
  • 相关阅读:
    一个没调好的程序
    bzoj1214 [HNOI2004]FTP服务器
    bzoj4514 [Sdoi2016]数字配对(网络流)
    二分图最大权匹配模板(pascal)
    CSS控制文字,超出部分显示省略号
    新型智慧城市顶层设计经验分享
    移动端浏览器前端优化
    桌面浏览器前端优化
    关于ie8下disabled属性:字体颜色问题
    win10永久激活
  • 原文地址:https://www.cnblogs.com/architectforest/p/12836843.html
Copyright © 2011-2022 走看看