• Redis 访问控制列表(ACL)


    关于 Redis ACL

    Redis ACL (访问控制列表) 是 Access Control List 的缩写,它允许某些连接在可以执行的命令和可以访问的密钥方面受到限制。它的工作方式是,在连接后,客户端需要提供 username/用户名 和有效 password/密码 来进行 authenticate/身份验证。如果身份验证成功,则连接将与给定用户和该用户的限制相关联。Redis 可以配置为新连接已经通过 “default” 用户的身份验证(这是默认配置)。配置默认用户的副作用是,可以仅为未明确验证的连接提供特定的功能子集。

    与旧版本兼容

    在默认配置中,Redis 6.0(第一个拥有 ACL 的版本)的工作方式与旧版本的 Redis 完全相同。每个新连接都能够调用每一个可能的命令并访问每一个密钥,因此 ACL 功能与旧客户端和应用程序向后兼容。此外,使用 requirepass 配置指令配置密码的旧方法仍然可以正常工作。但是,它现在为 “default” 用户设置了一个密码。

    • Redis6.0 之前的版本中,登陆 Redis Server 只需要输入密码(前提配置了 密码/requirepass )即可,不需要输入 username/用户名,而且密码也是明文配置到配置文件中,安全性不高。并且应用连接也使用该密码,导致应用有所有权限处理数据,风险也极高。
    • Redis6.0 有了 ACL 之后,终于解决了这些不安全的因素,可以按照不同的需求设置相关的用户和权限。

    Redis ACL 是向后兼容的,即默认情况下用户为 default,使用的是 requirepass 配置的密码。要是不使用ACL 功能,对旧版客户端来说完全一样。Redis Auth可以有 2 种方式进行工作:

    # 旧版本的使用方式,默认用户。兼容旧版本 Redis 的支持
    AUTH <password>
    # 新版本的使用方式,还需要验证用户名
    AUTH <username> <password>
    
    • 1
    • 2
    • 3
    • 4
    --user <username>  验证用户名
    --pass <password>  验证密码,是参数 -a 的别名;配合 --user 使用
    --askpass          强制用户输入带有 STDIN 掩码的密码
    
    • 1
    • 2
    • 3

    如果还按照老版本进行配置,那么新版的 redis 还是能够直接通过 密码 字符串来连接,以便和旧版本兼容。通过执行命令 ACL LIST 可以看出系统中有一个名称叫 default 的用户。默认情况下只输入 密码 就能通过 default 这个用户来连接 redis-server

    127.0.0.1:6379> ACL LIST
    1) "user default on #eb1c66c230df28518559872a792755e1bd7558cb35d58ae9c52689b3dc9ef335 ~* &* +@all"
    
    • 1
    • 2

    ACL 描述规则说明:

    参数规则说明
    user用户(关键字)
    default默认用户名称,此处可自定义
    on是否启用该用户,默认为 off(禁用)
    #…用户密码, nopass 表示不需要密码
    ~*可以访问的key(正则匹配)
    +@all授权类型,授权方式说明如下介绍

    授权方式说明:

    参数说明
    +授权用户操作命令权限
    -回收用户操作命令权限
    +@添加一类命令
    -@回收一类命令
    allcommands/+@all允许所有命令执行
    nocommands/-@all不运行所有命令操作执行

    由于 Redis 是高性能的数据库,正常情况下每秒可以接收百万级别的请求,因此我们的 用户/密码 一定要是非常复杂的组合,否则很容易就会被暴利跑字典给破解了。

    ACL help

    现在开始来说明如何在 Redis 中根据 ACL 来定制需要的用户权限。

    首先看 ACLhelp,了解大致的使用方法:ACL help

    # 进入 redis 容器
    docker container exec -it redis6479 /bin/sh
    
    # 连接 redis 服务
    redis-cli -c -h 192.168.48.190 -p 6479 -a '123456' --raw
    
    # 查看 ACL 帮助信息
    192.168.48.190:6479> ACL help
    ACL <subcommand> [<arg> [value] [opt] ...]. Subcommands are:
    CAT [<category>]
        List all commands that belong to <category>, or all command categories
        when no category is specified.
    DELUSER <username> [<username> ...]
        Delete a list of users.
    DRYRUN <username> <command> [<arg> ...]
        Returns whether the user can execute the given command without executing the command.
    GETUSER <username>
        Get the user's details.
    GENPASS [<bits>]
        Generate a secure 256-bit user password. The optional `bits` argument can
        be used to specify a different size.
    LIST
        Show users details in config file format.
    LOAD
        Reload users from the ACL file.
    LOG [<count> | RESET]
        Show the ACL log entries.
    SAVE
        Save the current config to the ACL file.
    SETUSER <username> <attribute> [<attribute> ...]
        Create or modify a user with the specified attributes.
    USERS
        List all the registered usernames.
    WHOAMI
        Return the current connection username.
    HELP
        Print this help.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    ACL help 翻译说明:

    • ACL<子命令>〔<arg>〔value〕〔opt〕…〕。子命令包括:
      CAT [< category > ]
      列出属于 < category > 或所有命令类别的所有命令,当没有指定类别时。

    • DELUSER<用户名>〔<用户名>…〕
      删除用户列表。

    • DRYRUN<用户名><命令>〔<arg>…〕
      返回用户是否可以在不执行给定命令的情况下执行该命令。

    • GEUSER<用户名>
      获取用户的详细信息。

    • GENPASS[]
      生成一个安全的256位用户密码。可选的“bits”参数可以用于指定不同的大小。

    • LIST
      以配置文件格式显示用户详细信息。

    • LOAD
      从 ACL 文件重新加载用户。

    • LOG [< count >|RESET]
      显示ACL日志条目。

    • SAVE
      将当前配置保存到 ACL 文件中。

    • SETUSER<用户名><属性>〔<属性>…〕
      创建或修改具有指定属性的用户。

    • USERS
      列出所有注册的用户名。

    • WHOAMI
      返回当前连接用户名。

    • HELP
      打印此帮助。

    ACL 配置模式

    ACL存储模式

    在介绍 ACLs 之前,我们先开启 ACL 配置。配置 ACL 的方式有两种:

    1. 用户可以直接在 redis.conf 文件中指定(推荐简单用例);
    2. 可以指定外部 ACL 文件(推荐复杂用例);

    由于两种方式是相互不兼容的,所以 Redis 会要求你使用其中的一种。在 redis.conf 中指定用户适用于简单的用例。在复杂的环境中,当需要定义多个用户时,建议使用外部 ACL file,扩展性更佳。

    redis.conf 配置模式

    redis.conf 和外部 ACL file 中使用的格式是完全相同的,所以从一种方式切换到另一种方式的情况,不需要关注 ACL 命令格式的变化,如下所示:

    user <username> ... acl rules ...
    
    • 1

    举例:

    user worker +@list +@connection ~jobs:* on >efa7303c493aa09
    
    • 1

    注意:两种配置方式,对于存储新用户配置所使用的命令有所不同。

    • redis.conf 方式:使用 CONFIG REWRITE 来通过重写文件来存储新的用户配置。
    • ACL file 方式:使用 ACL SAVE 来通过重写文件来存储新的用户配置。

    外部 ACL File 配置模式

    1. 如果之前使用了 redis.conf 模式,redis.conf 中会有之前已经生效的 DSL,我们需要注释掉。例如:
    #user default on nopass ~* &* +@all
    
    • 1
    1. 可能我们的 redis.conf 文件中还会有 requirepass 配置,当我们开启 ACL 之后,requirepass 将不在生效,这里我们也注释掉,当然如果不注释也不会有影响:
    #requirepass default123
    
    • 1
    1. config 文件中配置 aclfile 的路径,需要在 redis.conf 中添加:
    # Using an external ACL file
    #
    # Instead of configuring users here in this file, it is possible to use
    # a stand-alone file just listing users. The two methods cannot be mixed:
    # if you configure users here and at the same time you activate the external
    # ACL file, the server will refuse to start.
    #
    # The format of the external ACL user file is exactly the same as the
    # format that is used inside redis.conf to describe users.
    #
    aclfile /data/redis7/users.acl
    
    # 这里需要注意如果是 docker 方式部署,引用的是 docker 容器中的挂载路径
    aclfile /home/redis/conf/users.acl
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    注意:对应的 users.acl 文件要先建立好,不然重启 redis 时会报错。

    创建 users.acl 文件并授权,执行如下命令:

    touch /home/redis/conf/users.acl && chmod -R 777 /home/redis/conf/
    
    • 1
    1. 重启 redis 即生效。(配置完成之后,default 用户属于无密码状态)

    说明:

    • ACL 命令行 中设置的用户权限,不能持久化,redis 重启后就失效了。
    • 开启外部 aclfile 之后,不能使用 redis-cli -a xxx 登陆,必须使用 redis-cli --user xxx --pass yyy 来登陆。
    • aclfile 中的命令配置全部正确才会生效,反之会使用缓存上一次成功的信息。

    关于 redis.conf 配置,请查看相关文档:

    • Redis Configuractionhttps://redis.io/docs/management/config/
    • Redis v7.2.0 的 redis.conf 详细配置信息https://raw.githubusercontent.com/redis/redis/7.2/redis.conf
    • Redis 集群配置文件 redis.conf 解析https://blog.csdn.net/qq_39677803/article/details/116239553

    ACL 规则

    关于 ACL 的规则列表,请自行查看官方文档:

    • Redis ACLhttps://redis.io/docs/management/security/acl/#acl-rules
    • Redis Configure-aclshttps://docs.redis.com/latest/rc/security/access-control/data-access-control/configure-acls/

    相关博文推荐:

    • Redis Cluster 7.0 用户管理与 ACLs 权限控制,https://blog.csdn.net/weixin_40147979/article/details/128485222

    总结

    Redis 是一种高性能的缓存数据库,每秒可处理百万级的请求,如果没有很好的 ACL 控制,很可能会被暴力破解;在生产环境中,这是一种重大的安全影响因素,然而 Redis 6.0 扩展的 Auth 得以弥补这一隐患,助力安全生产。如果是新版本的 redis ≥ 6.0,推荐大家尝试外部 ACL file 方式配置来管理用户访问控制权限。

    Redis ACL 用户管理方面,其中 key 的配置因为支持 正则表达式 ,所以还能给出各种不同的表达式,这个正则表达式就推荐更多的小伙伴自行去尝试吧。

  • 相关阅读:
    LeetCode-146. LRU Cache [C++][Java]
    K-近邻算法(KNN)
    SCI论文还迟迟动不了笔?2/3区仅1个月25天录用,看看经验之谈
    Vue 3的新特性包括
    Python 自动化详解(pyautogui)
    Ubuntu 16.04 LTS third maintenance update release
    Unity Webgl发布的一些注意的点
    表格数据管理的新视角:JVS低代码的两大应用场景
    c语言-输入输出详解
    不同版本vue安装vue-router
  • 原文地址:https://blog.csdn.net/ChaITSimpleLove/article/details/134468073