AlloyDB Omni 中的 Active Directory 設定為選用功能,預設為停用。只有使用 Active Directory 伺服器進行驗證的環境,才能使用這項設定機制。
事前準備
整合 Active Directory 前,請確認您符合下列需求:
- 確認已設定 Active Directory。
- 取得 Active Directory 伺服器的
REALM
。 - 取得 Active Directory 伺服器的 Key Distribution Center (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)。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
。如要進一步瞭解以 ID 為基礎的驗證,請參閱「ID 地圖」。
使用下列指令重新載入 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 整合問題。