21.2. 使用 postgresql RHEL 系统角色,使用 IdM 发布的 TLS 证书配置 PostgreSQL
如果应用程序需要 PostgreSQL 数据库服务器,则您可以配置带有 TLS 加密的 PostgreSQL 服务,以在应用和数据库之间启用安全通信。如果 PostgreSQL 主机是 Red Hat Identity Management (IdM)域的成员,certmonger
服务可以管理证书请求和将来的续订。
通过使用 postgresql
RHEL 系统角色,您可以自动化此过程。您可以远程安装和配置带有 TLS 加密的 PostgreSQL,postgresql
角色使用 certificate
RHEL 系统角色配置 certmonger
,并从 IdM 请求证书。
postgresql
角色无法在 firewalld
服务中打开端口。要允许远程访问 PostgreSQL 服务器,请在使用 firewall
RHEL 系统角色的 playbook 中添加一个任务。
先决条件
- 您已准备好控制节点和受管节点。
- 您以可在受管主机上运行 playbook 的用户身份登录到控制节点。
-
您用于连接到受管节点的帐户对它们具有
sudo
权限。 - 您在 IdM 域中注册了受管节点。
流程
将敏感变量存储在加密的文件中:
创建 vault :
ansible-vault create ~/vault.yml
$ ansible-vault create ~/vault.yml New Vault password: <vault_password> Confirm New Vault password: <vault_password>
Copy to Clipboard Copied! 在
ansible-vault create
命令打开编辑器后,以<key>: <value>
格式输入敏感数据:pwd: <password>
pwd: <password>
Copy to Clipboard Copied! - 保存更改,并关闭编辑器。Ansible 加密 vault 中的数据。
创建一个包含以下内容的 playbook 文件,如
~/playbook.yml
:--- - name: Installing and configuring PostgreSQL hosts: managed-node-01.example.com vars_files: - ~/vault.yml tasks: - name: PostgreSQL with certificates issued by IdM ansible.builtin.include_role: name: redhat.rhel_system_roles.postgresql vars: postgresql_version: "16" postgresql_password: "{{ pwd }}" postgresql_ssl_enable: true postgresql_certificates: - name: postgresql_cert dns: "{{ inventory_hostname }}" ca: ipa principal: "postgresql/{{ inventory_hostname }}@EXAMPLE.COM" postgresql_server_conf: listen_addresses: "'*'" password_encryption: scram-sha-256 postgresql_pg_hba_conf: - type: local database: all user: all auth_method: scram-sha-256 - type: hostssl database: all user: all address: '127.0.0.1/32' auth_method: scram-sha-256 - type: hostssl database: all user: all address: '::1/128' auth_method: scram-sha-256 - type: hostssl database: all user: all address: '192.0.2.0/24' auth_method: scram-sha-256 - name: Open the PostgresQL port in firewalld ansible.builtin.include_role: name: redhat.rhel_system_roles.firewall vars: firewall: - service: postgresql state: enabled
--- - name: Installing and configuring PostgreSQL hosts: managed-node-01.example.com vars_files: - ~/vault.yml tasks: - name: PostgreSQL with certificates issued by IdM ansible.builtin.include_role: name: redhat.rhel_system_roles.postgresql vars: postgresql_version: "16" postgresql_password: "{{ pwd }}" postgresql_ssl_enable: true postgresql_certificates: - name: postgresql_cert dns: "{{ inventory_hostname }}" ca: ipa principal: "postgresql/{{ inventory_hostname }}@EXAMPLE.COM" postgresql_server_conf: listen_addresses: "'*'" password_encryption: scram-sha-256 postgresql_pg_hba_conf: - type: local database: all user: all auth_method: scram-sha-256 - type: hostssl database: all user: all address: '127.0.0.1/32' auth_method: scram-sha-256 - type: hostssl database: all user: all address: '::1/128' auth_method: scram-sha-256 - type: hostssl database: all user: all address: '192.0.2.0/24' auth_method: scram-sha-256 - name: Open the PostgresQL port in firewalld ansible.builtin.include_role: name: redhat.rhel_system_roles.firewall vars: firewall: - service: postgresql state: enabled
Copy to Clipboard Copied! 示例 playbook 中指定的设置包括如下:
postgresql_version: <version>
设置要安装的 PostgreSQL 的版本。您可以设置的版本取决于在受管节点上运行的 Red Hat Enterprise Linux 中提供的 PostgreSQL 版本。
您无法通过更改
postgresql_version
变量并再次运行 playbook 来升级或降级 PostgreSQL。postgresql_password: <password>
设置
postgres
数据库超级用户的密码。您无法通过更改
postgresql_password
变量并再次运行 playbook 来更改密码。postgresql_certificates: <certificate_role_settings>
-
包含
certificate
角色设置的 YAML 字典的列表。 postgresql_server_conf: <list_of_settings>
定义您要角色设置的
postgresql.conf
设置。角色将这些设置添加到/etc/postgresql/system-roles.conf
文件中,并在/var/lib/pgsql/data/postgresql.conf
的末尾包含此文件。因此,postgresql_server_conf
变量中的设置会覆盖/var/lib/pgsql/data/postgresql.conf
中的设置。使用
postgresql_server_conf
中的不同设置重新运行 playbook 会使用新设置覆盖/etc/postgresql/system-roles.conf
文件。postgresql_pg_hba_conf: <list_of_authentication_entries>
在
/var/lib/pgsql/data/pg_hba.conf
文件中配置客户端身份验证条目。详情请查看 PostgreSQL 文档。示例允许以下到 PostgreSQL 的连接:
- 使用本地 UNIX 域套接字的未加密的连接。
- 到 IPv4 和 IPv6 localhost 地址的 TLS 加密的连接。
-
来自 192.0.2.0/24 子网的 TLS 加密的连接。请注意,只有在您也在
postgresql_server_conf
变量中适当配置了listen_addresses
设置,才能从远程地址进行访问。
使用
postgresql_pg_hba_conf
中的不同设置重新运行 playbook 会使用新设置覆盖/var/lib/pgsql/data/pg_hba.conf
文件。
有关 playbook 中使用的所有变量的详情,请查看控制节点上的
/usr/share/ansible/roles/rhel-system-roles.postgresql/README.md
文件。验证 playbook 语法:
ansible-playbook --ask-vault-pass --syntax-check ~/playbook.yml
$ ansible-playbook --ask-vault-pass --syntax-check ~/playbook.yml
Copy to Clipboard Copied! 请注意,这个命令只验证语法,不能防止错误的、但有效的配置。
运行 playbook:
ansible-playbook --ask-vault-pass ~/playbook.yml
$ ansible-playbook --ask-vault-pass ~/playbook.yml
Copy to Clipboard Copied!
验证
使用
postgres
超级用户连接到 PostgreSQL 服务器,并执行\conninfo
元命令:psql "postgresql://postgres@managed-node-01.example.com:5432" -c '\conninfo'
# psql "postgresql://postgres@managed-node-01.example.com:5432" -c '\conninfo' Password for user postgres: You are connected to database "postgres" as user "postgres" on host "192.0.2.1" at port "5432". SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off)
Copy to Clipboard Copied! 如果输出显示 TLS 协议版本和密码详情,则连接可以正常工作,且 TLS 加密已启用。