将 Active Directory 用户支持与 AlloyDB Omni 集成

选择文档版本:

本页面介绍如何将 AlloyDB Omni 与现有的 Active Directory 实现集成,以便您可以使用现有的用户名和密码访问 AlloyDB Omni 数据库。Active Directory 集成提供 Kerberos 作为与 AlloyDB Omni 通信的默认身份验证机制。如需了解详情,请参阅 Active Directory 概览

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,请按以下步骤操作:

  1. 以管理员身份登录 Active Directory 服务器的 PowerShell 终端。
  2. 运行以下命令或使用 Active Directory 界面创建名为 postgres 的用户。
  3.   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
      
  4. 生成映射到此 Active Directory 服务器的服务主账号 keytab。
  5.   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 文件中配置相同的主机。

  6. 将 keytab 文件复制到 Linux 机器。

启用 Active Directory 身份验证

如需在 AlloyDB Omni 中启用 Active Directory 身份验证,请按照以下步骤操作,其中包括配置通用安全服务应用编程接口 (GSSAPI),该接口是一种可让程序访问安全服务的应用编程接口。

  1. 将以下条目添加到 /var/lib/postgresql/data/pg_hba.conf 文件中,放在
    host all all all scram-sha-256 条目之前。

    1. 运行以下 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
      
    2. 运行以下 Docker 命令,以验证 pg_hba.conf 文件是否在容器内:

      docker exec CONTAINER_NAME cat /var/lib/postgresql/data/pg_hba.conf
      
    3. 验证以下条目是否在该文件中:

      hostgssenc all all 0.0.0.0/0 gss map=gssmap
      

      如需了解详情,请参阅 pg_hba.conf 文件

  2. 将 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 身份验证

  3. /var/lib/postgresql/data/DATA_DIR/postgresql.conf 文件添加 keytab 文件的相应条目。

    1. 运行以下 Docker 命令,以在容器内添加相应条目:

      docker exec CONTAINER_NAME sed -i '$akrb_server_keyfile='"'"'/var/lib/postgresql/data/alloydb.keytab'"'" /var/lib/postgresql/data/postgresql.conf
      
    2. 运行以下 Docker 命令,以验证容器内的 postgresql.conf 文件:

      docker exec CONTAINER_NAME tail  /var/lib/postgresql/data/postgresql.conf
      
    3. 确保以下条目在该文件中:

      krb_server_keyfile=/var/lib/postgresql/data/alloydb.keytab
      

      如需了解详情,请参阅 krb_server_keyfile

  4. 可选:向 /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 映射

  5. 使用以下命令重新加载 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 身份验证是否正常,请按照以下步骤操作:

  1. 使用 kinit 登录 Active Directory。
  2. 从您通常运行 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:

  1. 移除 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
    
  2. 使用以下命令重新加载 PostgreSQL 配置:

    docker exec -it CONTAINER_NAME psql -h localhost -U postgres
    psql (16.3)
    Type "help" for help.
    postgres=# select pg_reload_conf();
    

后续步骤