將 Active Directory 與 AlloyDB Omni 整合

選取說明文件版本:

本頁說明如何整合 AlloyDB Omni 與現有的 Active Directory 實作項目,以便使用現有的使用者名稱和密碼存取 AlloyDB Omni 資料庫。整合 Active Directory 後,系統會預設使用 Kerberos 做為驗證機制,與 AlloyDB Omni 通訊。

AlloyDB Omni 中的 Active Directory 設定為選用功能,預設為停用。只有使用 Active Directory 伺服器進行驗證的環境,才能使用這項設定機制。

事前準備

將 AlloyDB Omni 與 Active Directory 實作項目整合前,請務必完成下列步驟:

  • 設定及設定 Active Directory 伺服器。
  • 確認您在 Active Directory 中擁有產生及管理 Kerberos 金鑰表或金鑰表檔案的權限。

啟用 Active Directory 驗證

如要在 AlloyDB Omni 中啟用 Active Directory 驗證,請按照下列步驟操作,包括設定一般安全性服務應用程式設計介面 (GSSAPI)。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
    ( EOF )
    

    如要實作使用者名稱對應,請在 pg_hba.conf 檔案的選項欄位中指定 map=gssmap

    如要進一步瞭解以 ID 為基礎的驗證,請參閱「ID 地圖」。

  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 alloydb_docs psql -h localhost -U postgres
    psql (16.3)
    Type "help" for help.
    postgres=# select pg_reload_conf();
    

後續步驟