AlloyDB Omni 中的 Active Directory 配置是可选项,默认处于停用状态。只有使用 Active Directory 服务器进行身份验证的环境才能使用此配置机制。
准备工作
在集成 Active Directory 之前,请先确保满足以下要求:
- 确保已设置 Active Directory。
- 获取 Active Directory 服务器的
REALM
。 - 获取 Active Directory 服务器的密钥分发中心 (KDC) 主机名。该主机名存储在 Docker 映像中。
- 获取 Active Directory 服务器的管理服务器主机名。此主机名存储在 Docker 映像中。
- 确保您有权访问 Active Directory 服务器,以便生成
.keytab
文件。 - 选择用于测试和登录的 Active Directory 用户。
- 从现有 Active Directory 服务器获取
.keytab
文件。
从现有 Active Directory 服务器获取 .keytab 文件
如需从 Active Directory 服务器获取 keytab,请按以下步骤操作:
- 以管理员身份登录 Active Directory 服务器的 PowerShell 终端。
- 运行以下命令或使用 Active Directory 界面创建名为
postgres
的用户。 - 生成映射到此 Active Directory 服务器的服务主账号 keytab。
- 将 keytab 文件复制到 Linux 机器。
New-ADUser -Name "postgres" ` -SamAccountName "postgres" ` -UserPrincipalName "postgres@REALM" ` -Description "Service Account for AlloyDB Omni PostgreSQL Kerberos Authentication" ` -AccountPassword (Read-Host -AsSecureString "Set a strong password for the postgres service account") ` -Enabled $true ` -PasswordNeverExpires $true
ktpass /princ postgres/ALLOYDB_HOST_NAME@REALM /Pass ChangeMe123 /mapuser postgres /crypto ALL /ptype KRB5_NT_PRINCIPAL /mapOp set /out C:\Users\Public\postgres.keytab
其中,<HOST>
是您计划在其中部署 AlloyDB Omni 的服务器的完全限定域名,例如 alloydb-server.ad.example.com
。您必须在网域领域映射的 krb5.conf
文件中配置相同的主机。
启用 Active Directory 身份验证
如需在 AlloyDB Omni 中启用 Active Directory 身份验证,请按照以下步骤操作,其中包括配置通用安全服务应用编程接口 (GSSAPI),该接口是一种可让程序访问安全服务的应用编程接口。
将以下条目添加到
/var/lib/postgresql/data/pg_hba.conf
文件中,放在
host all all all scram-sha-256
条目之前。运行以下 Docker 命令,以在容器内添加
gss
:docker exec CONTAINER_NAME sed -i 's;^host all all all scram-sha-256$;hostgssenc all all 0.0.0.0/0 gss map=gssmap\n&;' /var/lib/postgresql/data/pg_hba.conf
运行以下 Docker 命令,以验证
pg_hba.conf
文件是否在容器内:docker exec CONTAINER_NAME cat /var/lib/postgresql/data/pg_hba.conf
验证以下条目是否在该文件中:
hostgssenc all all 0.0.0.0/0 gss map=gssmap
如需了解详情,请参阅 pg_hba.conf 文件。
将 keytab 文件复制到 AlloyDB Omni 映像内的数据目录。
docker cp PATH TO KEYTAB FILE CONTAINER_NAME:/var/lib/postgresql/data/alloydb.keytab docker exec CONTAINER_NAME chmod 600 /var/lib/postgresql/data/alloydb.keytab docker exec CONTAINER_NAME chown postgres:postgres /var/lib/postgresql/data/alloydb.keytab
keytab 文件由 Kerberos 为 PostgreSQL 服务器生成。如需详细了解身份验证,请参阅 GSSAPI 身份验证。
向
/var/lib/postgresql/data/DATA_DIR/postgresql.conf
文件添加 keytab 文件的相应条目。运行以下 Docker 命令,以在容器内添加相应条目:
docker exec CONTAINER_NAME sed -i '$akrb_server_keyfile='"'"'/var/lib/postgresql/data/alloydb.keytab'"'" /var/lib/postgresql/data/postgresql.conf
运行以下 Docker 命令,以验证容器内的
postgresql.conf
文件:docker exec CONTAINER_NAME tail /var/lib/postgresql/data/postgresql.conf
确保以下条目在该文件中:
krb_server_keyfile=/var/lib/postgresql/data/alloydb.keytab
如需了解详情,请参阅 krb_server_keyfile。
可选:向
/var/lib/postgresql/data/DATA_DIR/pg_ident.conf
文件添加相应条目。当您使用 GSSAPI 等外部身份验证系统时,发起连接的操作系统用户的名称可能与您要使用的数据库用户(角色)的名称不同。
在这种情况下,请在
/var/lib/postgresql/data/DATA_DIR/pg_ident.conf
文件中指定系统用户与 PostgreSQL 用户之间的映射:docker exec -it CONTAINER_NAME bash $ echo -e " gssmap /^(.*)@EXAMPLE\.COM$ \1 gssmap /^(.*)@example\.com$ \1 " | column -t | tee -a /var/lib/postgresql/data/pg_ident.conf
如需实现用户名映射,请在
pg_hba.conf
文件的选项字段中指定map=gssmap
。如需详细了解基于 ident 的身份验证,请参阅 Ident 映射。
使用以下命令重新加载 PostgreSQL 配置:
docker exec -it CONTAINER_NAME psql -h localhost -U postgres psql (16.3) Type "help" for help. postgres=# select pg_reload_conf();
测试 Active Directory 身份验证
如需验证 Active Directory 身份验证是否正常,请按照以下步骤操作:
- 使用
kinit
登录 Active Directory。 从您通常运行
kinit
的机器运行以下psql
命令:root@4f6414ad02ef:/# kinit AD_USER_NAME Password for user1@YOUR.REALM: root@4f6414ad02ef:/# psql --h ALLOYDB_SERVER_HOST_NAME -U AD_USER_NAME psql (16.6 (Ubuntu 16.6-0ubuntu0.24.04.1), server 16.3) GSSAPI-encrypted connection Type "help" for help. user1=#
停用 Active Directory 身份验证
如需在 AlloyDB Omni 中停用 Active Directory 身份验证,请按照以下步骤操作,这些步骤会停用 GSSAPI:
移除
pg_hba.conf
文件中指向gss
身份验证方法的条目:docker exec CONTAINER_NAME sed -i '/hostgssenc all all 0.0.0.0\/0 gss map=gssmap/d' /var/lib/postgresql/data/pg_hba.conf
使用以下命令重新加载 PostgreSQL 配置:
docker exec -it CONTAINER_NAME psql -h localhost -U postgres psql (16.3) Type "help" for help. postgres=# select pg_reload_conf();
后续步骤
- 在 Kubernetes 上集成 Active Directory 用户支持。
- 在 AlloyDB Omni 中排查 Active Directory 问题。
- 将 Active Directory 群组支持与 AlloyDB Omni 集成。
- 在 Kubernetes 上集成 Active Directory 群组支持。
- 在 AlloyDB Omni 中排查 Active Directory 集成问题。