TiDB之Prometheus未授权访问漏洞修复

思路:
将密码认证添加到 TiDB 的 Prometheus 实例中
,可以按照以下步骤操作

步骤一:生成 bcrypt 哈希密码

  1. 安装 python3-bcrypt(假设你使用的是 Debian 系统):

    sudo apt install python3-bcrypt
    
  2. 创建并运行 gen-pass.py 脚本生成密码哈希

    创建 gen-pass.py 脚本并运行它:

    import getpass
    import bcrypt
    
    password = getpass.getpass("password: ")
    hashed_password = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt())
    print(hashed_password.decode())
    

    运行脚本:

    python3 gen-pass.py
    

    输入你的密码(例如 “test”)后,脚本会输出类似于 $2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay 的哈希值。

步骤二:创建 web.yml 文件

  1. 创建 web.yml 文件,内容如下(使用生成的哈希密码):

    basic_auth_users:
      admin: $2b$12$hNf2lSsxfm0.i4a.1kVpSOVyBCfIB51VRjgBUyv6kdnyTlgWj81Ay
    
  2. 验证 web.yml 文件

    promtool check web-config web.yml
    

    如果验证成功,会输出 web.yml SUCCESS

步骤三:配置 Prometheus

  1. 编辑 prometheus.yml 文件(位于 TiDB 的 Prometheus 配置目录中),添加以下内容来加载 web.yml 文件:

    global:
      scrape_interval: 15s
      evaluation_interval: 15s
    
    scrape_configs:
      - job_name: 'prometheus'
        static_configs:
          - targets: ['localhost:9090']
    
    # 添加 web 配置 (如果报错 直接在启动脚本中添加)
    web_config:
      file: /path/to/web.yml
    

    确保 file 路径指向正确的 web.yml 文件位置。

  2. 重启 Prometheus

    tiup cluster restart <cluster-name> -R prometheus
    tiup cluster start <cluster-name> -R prometheus
    tiup cluster stop <cluster-name> -R prometheus
    

步骤四:测试认证

  1. 使用 curl 进行测试

    尝试未认证访问(应返回 401 Unauthorized):

    curl --head http://localhost:9090/graph
    

    使用基本认证访问(输入正确的用户名和密码):

    curl -u admin http://localhost:9090/metrics
    

    输入密码后,你应能看到 Prometheus 的指标输出。

参考资料:

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值