🦋
Butterfly 用户手册
  • Introduction
  • 一 前言
  • 二 开始
    • 安装部署
    • 五分钟体验指南
    • 单机使用手册
    • 应用规范
      • handler specs
      • middleware specs
      • xingqiao_plugin specs
      • yiqiu_program specs
  • 三 客户端功能
    • MySQL 原生协议
    • MySQL ORM
    • Redis 原生协议
      • redis_config
      • redis_tls
    • Redis ORM
    • Redis mcpack
    • Localcache
    • Kazoo
  • 四 应用(通用服务)
    • API JSON 规范
    • 异步任务 BaiChuan(百川)
    • 任务调度 RuQi(如期)
    • 任务编排 XingQiao(星桥)
    • 配置管理 WuXing(五行)
    • 运筹决策 BaiCe(百策)
  • 五 部署运维
    • 单机容器化部署
    • 监控
    • 异常排查
      • CPU Load spike every 7 hours
    • 升级
    • 安全
    • 其他
  • 六 前端
    • butterfly_template
    • butterfly_fe
    • butterfly-admin(json2web)
      • amis
      • sso
      • pangu
    • NoahV
    • PyWebIO
  • 七 潘多拉魔盒
    • 装饰器
      • localcache_decorator
      • retry_decorator
      • custom_decorator
      • command2http_decorator
    • 算法
      • 算法-分位数
      • 算法-变异系数
    • 实用工具
      • host_util
      • shell_util
      • http_util
      • time_util
      • random_util
      • concurrent
      • jsonschema
      • blinker
      • toml
      • command_util
      • config_util
      • picobox
      • 对称加密
        • des
        • aes
      • ascii_art
        • ttable
        • chart
      • business_rules
      • python-mysql-replication
      • dict_util
    • 中间件
      • middleware_status
      • middleware_whitelist
    • test_handler.py
  • 八 最佳实践
    • 分布式架构
    • Code practice
    • Log practice
    • Daemon process
  • 附录
Powered by GitBook
On this page
  • 1 TLS 说明
  • 1.1 单向认证和双向认证
  • 2 服务端启用 TLS
  • 2.1 创建私钥
  • 2.2 通过私钥生成一个自签名证书
  • 2.3 修改配置
  • 3 客户端连接
  • 4 常见报错
  • 4.1 服务端未配置 TLS
  • 4.2 客户端证书过期
  1. 三 客户端功能
  2. Redis 原生协议

redis_tls

1 TLS 说明

TLS(Transport Layer Security)加密协议,TLS协议具有比SSL(Secure Sockets Layer)协议更好的加密技术和更高级别的安全性,可进一步保障数据通信安全。

TLS协议是SSL协议的升级版,当前已成为互联网加密通信的标准协议,在现代网络中被广泛使用。以下是TLS相对SSL的一些优势:

  • 加密强度:TLS协议使用更强大的加密技术,例如AES(Advanced Encryption Standard)算法。

  • 安全性:TLS协议采用更安全的算法和协议,例如SHA-2(Secure Hash Algorithm 2)算法。

  • 兼容性:TLS协议是更现代化的协议,更加兼容现代浏览器和服务器,且支持更广泛的加密协议和密码套件。

  • 安全更新:TLS协议支持实时升级加密算法和协议。

1.1 单向认证和双向认证

在单向认证中,只有 Redis 服务器需要提供证书,客户端会验证服务器的身份,而服务器不会对客户端进行身份验证。

双向认证要求客户端和服务器都提供证书,并且双方都会验证对方的身份。这种认证方式提供了更高的安全性,确保只有经过授权的客户端才能连接到 Redis 服务器。

下面例子中为单向认证例子。

2 服务端启用 TLS

2.1 创建私钥

openssl genrsa -out redis.key 2048

2.2 通过私钥生成一个自签名证书

openssl req -new -x509 -days 365 -key redis.key -out redis.crt

2.3 修改配置

单向认证 Redis Server 仅需要配置 tls-cert-file、tls-key-file

双向认证 Redis Server 需要配置 tls-cert-file、tls-key-file、tls-ca-cert-file

tls-port 6379
tls-cert-file /path/to/redis.crt
tls-key-file /path/to/redis.key

tls-cert-file /path/to/redis.crt

  • 作用:指定 Redis 服务器的 TLS 证书文件路径。

  • 证书内容:

    • 该文件应包含 Redis 服务器的公钥证书(通常为 PEM 格式)。

    • 证书需由受信任的证书颁发机构(CA)签发,或为自签名证书。

  • 用途:

    • 客户端通过此证书验证 Redis 服务器的身份。

    • 证书中的 Subject Alternative Name (SAN) 或 Common Name (CN) 需与 Redis 服务器的域名或 IP 匹配。

tls-key-file /path/to/redis.key

  • 作用:指定 Redis 服务器的 TLS 私钥文件路径。

  • 私钥内容:

    • 该文件应包含与证书(redis.crt)对应的私钥(PEM 格式)。

    • 私钥必须严格保密,仅限 Redis 进程访问。

  • 用途:

    • 用于加密客户端与服务器之间的通信。

    • 在 TLS 握手过程中,服务器使用私钥证明对证书的所有权。

tls-ca-cert-file /path/to/ca.crt

  • 作用:指定受信任的 CA 证书文件路径。

  • CA 证书内容:

    • 该文件应包含签发 Redis 服务器证书的根 CA 或中间 CA 的公钥证书(PEM 格式)。

    • 如果是自签名证书,需将自签名证书作为 CA 证书配置。

  • 用途:

    • Redis 服务器使用此 CA 证书验证客户端证书(如果启用双向认证)。

    • 客户端也会使用此 CA 证书验证服务器证书(除非客户端配置了其他信任链)。

3 客户端连接

#!/bin/python
from xlib.db import redis

# 设置连接信息,分别将host、port、password的值分别替换为实例的连接地址、端口号、密码。
# ApsaraDB-CA-Chain.pem为证书文件名称。
client = redis.Redis(host="127.0.0.1", port=6379,
                     password="xxxx", ssl=True,
                    ssl_cert_reqs="required",
                    ssl_ca_certs="ca.pem")
                    
client.set("hello", "world")
print client.get("hello")

4 常见报错

4.1 服务端未配置 TLS

Traceback (most recent call last):
  ...
  File "/butterfly/xlib/db/redis/connection.py", line 641, in connect
    raise ConnectionError(self._error_message(e))
xlib.db.redis.exceptions.ConnectionError: Error 1 connecting to 127.0.0.1:6379. _ssl.c:504: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol.

4.2 客户端证书过期

Traceback (most recent call last):
  ...
  File "/butterfly/xlib/db/redis/connection.py", line 641, in connect
    raise ConnectionError(self._error_message(e))
xlib.db.redis.exceptions.ConnectionError: Error 1 connecting to 127.0.0.1:6379. _ssl.c:504: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed.
Previousredis_configNextRedis ORM

Last updated 24 days ago

https://redis.io/docs/latest/operate/oss_and_stack/management/security/encryption/