【问题标题】:Securely create role in Postgres using SQL script and environment variables使用 SQL 脚本和环境变量在 Postgres 中安全地创建角色
【发布时间】:2022-07-15 02:37:18
【问题描述】:

将密码安全地传递到 Postgres SQL 脚本以创建 DB ROLE 的最佳方法是什么?

【问题讨论】:

    标签: postgresql passwords


    【解决方案1】:

    在开发自动化 Postgres 架构部署期间,我试图找到安全创建 dbuser 密码的方法,将其传递给部署脚本并将其存储在秘密管理器中,不管哪种方式。

    最难的部分是在 SQL 脚本中传递和使用环境变量。 我发现的方式看起来不错而且很安全。

    使用 openssl 生成的密码:

    export ROLE_PASSWORD=$(openssl rand -base64 12)
    

    psql 命令如下:

    psql -v password_to_save=$ROLE_PASSWORD -a -h localhost -d postgres -U postgres -f test.sql
    

    SQL 脚本:

    \set ON_ERROR_STOP on
    -- \echo :password_to_save
    
    CREATE ROLE dbuser WITH
        LOGIN
        NOSUPERUSER
        NOCREATEDB
        NOCREATEROLE
        NOINHERIT
        NOREPLICATION
        CONNECTION LIMIT -1
        PASSWORD :'password_to_save';
    

    因此,密码一直受到保护。

    【讨论】:

      猜你喜欢
      • 2014-05-05
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      • 2016-05-11
      • 2013-09-14
      • 1970-01-01
      相关资源
      最近更新 更多