AlloyDB Omni 中的 Active Directory 設定為選用功能,預設為停用。只有使用 Active Directory 伺服器進行驗證的環境,才能使用這項設定機制。
事前準備
將 AlloyDB Omni 與 Active Directory 實作項目整合前,請務必完成下列步驟:
- 設定及設定 Active Directory 伺服器。
- 確認您在 Active Directory 中擁有產生及管理 Kerberos 金鑰表或金鑰表檔案的權限。
啟用 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 ( EOF )
如要實作使用者名稱對應,請在
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 alloydb_docs psql -h localhost -U postgres psql (16.3) Type "help" for help. postgres=# select pg_reload_conf();