透過憑證授權單位服務部署全球 Google 代管憑證

本教學課程說明如何使用 Certificate Manager,透過憑證授權單位服務部署 Google 代管的全域憑證。

下列全域負載平衡器支援 Google 代管憑證和憑證授權單位服務:

  • 全域外部應用程式負載平衡器
  • 傳統版應用程式負載平衡器
  • 全域外部 Proxy 網路負載平衡器
  • 傳統版 Proxy 網路負載平衡器

如要部署至跨區域負載平衡器或區域性負載平衡器,請參閱下列內容:

目標

本教學課程將說明如何完成下列工作:

  • 使用 Certificate Manager,透過 CA 服務建立 Google 代管憑證。
  • 使用目標 HTTPS Proxy,將憑證部署至支援的負載平衡器。

事前準備

  1. 登入 Google Cloud 帳戶。如果您未曾使用過 Google Cloud,歡迎 建立帳戶,親自體驗實際使用我們產品的成效。新客戶還能獲得價值 $300 美元的免費抵免額,用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Compute Engine, Certificate Manager, Certificate Authority APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Compute Engine, Certificate Manager, Certificate Authority APIs, if any are not already enabled.

    Roles required to enable APIs

    To enable APIs, you need the serviceusage.services.enable permission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.

    Enable the APIs

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init
  14. 啟用 CA Service API。
  15. 建立 CA 集區。您必須在這個 CA 集區中建立並啟用至少一個 CA。

按照本教學課程的步驟操作前,請注意下列事項:

  • 使用憑證授權單位服務建立的憑證不受公開信任。如要核發公開信任的憑證,請使用 DNS 授權或負載平衡器授權建立憑證。
  • 如果目標 CA 集區已啟用憑證核發政策,憑證佈建作業可能會因下列任一原因而失敗:

    • 憑證核發政策已封鎖要求的憑證。在這種情況下,由於憑證尚未核發,因此您不需付費。
    • 政策已對憑證套用 Certificate Manager 不支援的變更。在這種情況下,即使憑證與 Certificate Manager 不完全相容,您仍須支付憑證核發費用。
  • 為確保相容性,請設定目標 CA 集區的憑證核發政策,允許終端實體 (葉) 伺服器 TLS 憑證。

  • 您可以設定全域 Google 代管 TLS 憑證,從任何區域的 CA 集區核發。

必要的角色

請確認您具備下列角色,才能完成本教學課程中的工作:

  • Certificate Manager 擁有者 (roles/certificatemanager.owner)

    建立及管理 Certificate Manager 資源。

  • Compute 負載平衡器管理員 (roles/compute.loadBalancerAdmin) 或 Compute 網路管理員 (roles/compute.networkAdmin)

    必須具備這項權限,才能建立及管理 HTTPS 目標 Proxy。

  • CA 服務管理員 (roles/privateca.admin)

    必須具備這個角色,才能在 CA 服務中執行動作。

如要瞭解詳情,請參考下列資源:

建立負載平衡器

本教學課程假設您已建立並設定負載平衡器的後端、健康狀態檢查、後端服務和網址對應。如果您已建立外部應用程式負載平衡器,請記下網址對應的名稱,因為稍後會用到。

如果您尚未建立負載平衡器,請參閱下列頁面來建立:

設定 CA 服務與 Certificate Manager 的整合

如要將 CA 服務與 Certificate Manager 整合,請按照下列步驟操作:

  1. 在目標 Google Cloud 專案中,建立 Certificate Manager 服務帳戶:

    gcloud beta services identity create --service=certificatemanager.googleapis.com \
        --project=PROJECT_ID
    

    將 PROJECT_ID 替換為目標Google Cloud 專案的 ID。

    指令會傳回建立的服務身分名稱。請參閱以下範例:

    service-520498234@gcp-sa-certificatemanager.iam.gserviceaccount.com
    
  2. 在目標 CA 集區中,將 CA 服務憑證要求者角色 (roles/privateca.certificateRequester) 授予 Certificate Manager 服務帳戶:

    gcloud privateca pools add-iam-policy-binding CA_POOL \
        --location LOCATION \
        --member "serviceAccount:SERVICE_ACCOUNT" \
        --role roles/privateca.certificateRequester
    

    更改下列內容:

    • CA_POOL:目標 CA 集區的 ID。
    • LOCATION:目標 Google Cloud 位置。
    • SERVICE_ACCOUNT:您在步驟 1 建立的服務帳戶全名。
  3. 為 CA 集區建立憑證核發設定資源:

    控制台

    1. 前往 Google Cloud 控制台,然後點選「Certificate Manager」頁面的「Issuance configs」(核發設定) 分頁標籤。

      前往「Certificate Manager」

    2. 點選「建立」。系統會顯示「Create a Certificate Issuance Config」(建立憑證核發設定) 頁面。

    3. 在「Name」(名稱) 欄位中,輸入憑證核發設定的專屬名稱。

    4. 選用:在「Description」(說明) 欄位中,輸入核發設定的說明。

    5. 在「位置」部分,選取「全域」。

    6. 選用:在「Lifetime」(效期) 欄位中,指定核發憑證的效期 (以天為單位)。值必須介於 21 至 30 天 (含) 之間。

    7. 選用:在「Rotation window percentage」(輪替時間範圍百分比) 中,指定憑證效期達到多少百分比時,開始更新程序。如要查看有效值的範圍,請參閱「生命週期和輪換時間範圍百分比」。

    8. 選用:從「Key algorithm」(金鑰演算法) 清單中,選取產生私密金鑰時要使用的金鑰演算法。

    9. 從「CA pool」(CA 集區) 清單中,選取要指派給這項憑證核發設定資源的 CA 集區名稱。

    10. 在「Labels」(標籤) 欄位中,指定要與憑證建立關聯的標籤。如要新增標籤,請按一下「Add label」(新增標籤),然後指定標籤的鍵和值。

    11. 點選「建立」。

    gcloud

    gcloud certificate-manager issuance-configs create ISSUANCE_CONFIG_NAME \
        --ca-pool=CA_POOL
    

    更改下列內容:

    • ISSUANCE_CONFIG_NAME:憑證核發設定資源的名稱。
    • CA_POOL:要指派給這個憑證核發設定資源的 CA 集區完整資源路徑和名稱。

    如要進一步瞭解憑證核發設定資源,請參閱「管理憑證核發設定資源」。

建立由 CA 服務執行個體核發的 Google 代管憑證

如要建立由 CA 服務執行個體核發的 Google 代管憑證,請按照下列步驟操作:

控制台

  1. 前往 Google Cloud 控制台的「Certificate Manager」頁面。

    前往「Certificate Manager」

  2. 在「憑證」分頁中,按一下「新增憑證」。

  3. 在「Certificate name」(憑證名稱) 欄位中,輸入憑證的專屬名稱。

  4. 選用:在「Description」(說明) 欄位中輸入憑證說明。說明可用於識別憑證。

  5. 在「位置」部分,選取「全域」。

  6. 在「Scope」(範圍) 中選取「Default」(預設值)。

  7. 在「Certificate type」(憑證類型) 部分,選取「Create Google-managed certificate」(建立 Google 代管的憑證)。

  8. 在「Certificate Authority type」(憑證授權單位類型) 部分,選取「Private」(私人)。

  9. 在「Domain Names」(網域名稱) 欄位中,指定以半形逗號分隔的憑證網域名稱清單。每個網域名稱都必須是完整網域名稱,例如 myorg.example.com。

  10. 在「Select a certificate issuance config」(選取憑證核發設定) 部分,選取參照目標 CA 集區的憑證核發設定資源名稱。

  11. 在「Labels」(標籤) 欄位中,指定要與憑證建立關聯的標籤。如要新增標籤,請按一下「Add label」(新增標籤),然後指定標籤的鍵和值。

  12. 點選「建立」。

    新的憑證會顯示在憑證清單中。

gcloud

如要使用憑證授權單位服務建立全域 Google 代管憑證,請使用 certificate-manager certificates create 指令搭配 issuance-config 旗標:

gcloud certificate-manager certificates create CERTIFICATE_NAME \
    --domains="DOMAIN_NAMES" \
    --issuance-config=ISSUANCE_CONFIG_NAME

更改下列內容:

  • CERTIFICATE_NAME:憑證名稱。
  • DOMAIN_NAME:目標網域的名稱。網域名稱必須是完整網域名稱,例如 myorg.example.com。
  • ISSUANCE_CONFIG_NAME:參照目標 CA 集區的憑證核發設定資源名稱。

API

如要建立憑證,請向 certificates.create 方法發出 POST 要求,如下所示:

POST /v1/projects/PROJECT_ID/locations/global/certificates?certificate_id=CERTIFICATE_NAME"
{
 "managed": {
  "domains": ["DOMAIN_NAME"],
  "issuanceConfig": "ISSUANCE_CONFIG_NAME",
 }
}

更改下列內容:

  • PROJECT_ID: Google Cloud 專案 ID。
  • CERTIFICATE_NAME:憑證名稱。
  • DOMAIN_NAME:目標網域的名稱。網域名稱必須是完整網域名稱,例如 myorg.example.com。
  • ISSUANCE_CONFIG_NAME:參照目標 CA 集區的憑證核發設定資源名稱。

驗證憑證狀態

將憑證部署至負載平衡器之前,請先確認憑證已啟用。憑證狀態可能需要幾分鐘才會變更為 ACTIVE。

控制台

  1. 前往 Google Cloud 控制台的「Certificate Manager」頁面。

    前往「Certificate Manager」

  2. 在「Certificates」(憑證) 分頁中,查看憑證的「Status」(狀態) 欄。

gcloud

如要確認憑證狀態,請執行下列指令:

gcloud certificate-manager certificates describe CERTIFICATE_NAME

將 CERTIFICATE_NAME 替換為目標 Google 代管憑證的名稱。

輸出結果會與下列內容相似:

createTime: '2021-10-20T12:19:53.370778666Z'
expireTime: '2022-05-07T05:03:49Z'
managed:
  domains:
  - myorg.example.com
  issuanceConfig: projects/myproject/locations/global/issuanceConfigs/myissuanceConfig
  state: ACTIVE
name: projects/myproject/locations/global/certificates/mycertificate
pemCertificate: |
  -----BEGIN CERTIFICATE-----
  [...]
  -----END CERTIFICATE-----
sanDnsnames:
  - myorg.example.com
updateTime: '2021-10-20T12:19:55.083385630Z'

如需更多疑難排解步驟,請參閱「排解 Certificate Manager 問題」。

將憑證部署至負載平衡器

如要部署全域 Google 代管憑證,請使用憑證對應組合。

建立憑證對應組合

建立憑證對應組合,參照與憑證相關聯的憑證對應關係項目:

gcloud

如要建立憑證對應組合,請使用 gcloud certificate-manager maps create 指令:

gcloud certificate-manager maps create CERTIFICATE_MAP_NAME

將 CERTIFICATE_MAP_NAME 替換為目標憑證對應組合的名稱。

Terraform

如要建立憑證對應組合,可以使用 google_certificate_manager_certificate_map 資源。

resource "google_certificate_manager_certificate_map" "certificate_map" {
  name        = "${local.name}-certmap-${random_id.tf_prefix.hex}"
  description = "${local.domain} certificate map"
  labels = {
    "terraform" : true
  }
}

建立憑證對應關係

建立憑證對應關係,並與憑證和憑證對應組合建立關聯:

gcloud

如要建立憑證對應關係,請使用 gcloud certificate-manager maps entries create 指令:

gcloud certificate-manager maps entries create CERTIFICATE_MAP_ENTRY_NAME \
    --map="CERTIFICATE_MAP_NAME" \
    --certificates="CERTIFICATE_NAME" \
    --hostname="HOSTNAME"

更改下列內容:

  • CERTIFICATE_MAP_ENTRY_NAME:憑證對應項目名稱。
  • CERTIFICATE_MAP_NAME:與憑證對應關係相關聯的憑證對應組合名稱。
  • CERTIFICATE_NAME:要與憑證對應關係建立關聯的憑證名稱。
  • HOSTNAME:要與憑證對應關係建立關聯的主機名稱。

    如要建立涵蓋萬用字元網域和根網域的憑證,請指定含有根網域和萬用字元的主機名稱,例如 example.com 和 *.example.com。此外,您必須指定兩個憑證對應關係,一個用於 example.com,另一個用於 *.example.com。

Terraform

如要建立具有根網域的憑證對應關係,請使用 google_certificate_manager_certificate_map_entry 資源。

resource "google_certificate_manager_certificate_map_entry" "first_entry" {
  name        = "${local.name}-first-entry-${random_id.tf_prefix.hex}"
  description = "example certificate map entry"
  map         = google_certificate_manager_certificate_map.certificate_map.name
  labels = {
    "terraform" : true
  }
  certificates = [google_certificate_manager_certificate.root_cert.id]
  hostname     = local.domain
}

如要建立含有萬用字元網域的憑證對應關係,請使用 google_certificate_manager_certificate_map_entry 資源。

resource "google_certificate_manager_certificate_map_entry" "second_entry" {
  name        = "${local.name}-second-entity-${random_id.tf_prefix.hex}"
  description = "example certificate map entry"
  map         = google_certificate_manager_certificate_map.certificate_map.name
  labels = {
    "terraform" : true
  }
  certificates = [google_certificate_manager_certificate.root_cert.id]
  hostname     = "*.${local.domain}"
}

確認憑證對應關係已啟用

將對應的憑證對應組合附加至目標 Proxy 之前,請先確認憑證對應關係已啟用。

如要驗證憑證對應關係,請使用 gcloud certificate-manager maps entries describe 指令:

gcloud certificate-manager maps entries describe CERTIFICATE_MAP_ENTRY_NAME \
    --map="CERTIFICATE_MAP_NAME"

更改下列內容:

  • CERTIFICATE_MAP_ENTRY_NAME:憑證對應項目名稱。
  • CERTIFICATE_NAME:要與憑證對應關係建立關聯的憑證名稱。

輸出結果會與下列內容相似:

certificates:
createTime: '2021-09-06T10:01:56.229472109Z'
hostname: example.com
name: projects/my-project/locations/global/certificateMaps/myCertMap/certificateMapEntries/myCertMapEntry
state: ACTIVE
updateTime: '2021-09-06T10:01:58.277031787Z'

將憑證對應組合附加至目標 Proxy

您可以將憑證對應組合附加至新的或現有的目標 Proxy。

控制台

如果目標 Proxy 屬於現有負載平衡器,您可以編輯負載平衡器的前端設定,附加憑證對應組合。

  1. 前往 Google Cloud 控制台的「Load balancing」(負載平衡) 頁面。

    前往「Load balancing」(負載平衡)

  2. 按一下要更新的負載平衡器名稱,然後點選「Edit」(編輯)。

  3. 按一下「Frontend configuration」(前端設定),然後選取要編輯的前端 IP 位址和通訊埠設定。

  4. 如果畫面上顯示「Choose certificate repository」(選擇憑證存放區),請選取「Use certificate map」(使用憑證對應關係)。

  5. 在「Select a certificate map」(選取憑證對應組合) 清單中,選取憑證對應組合。

  6. 按一下「Done」(完成),然後按一下「Update」(更新)。

gcloud

如要將憑證對應組合附加至新的目標 Proxy,請使用 gcloud compute target-https-proxies create 指令:

gcloud compute target-https-proxies create PROXY_NAME \
    --certificate-map="CERTIFICATE_MAP_NAME" \
    --url-map="URL_MAP" \
    --global

更改下列內容:

  • PROXY_NAME:目標 Proxy 的名稱。
  • CERTIFICATE_MAP_NAME:參照憑證對應關係和相關聯憑證的憑證對應組合名稱。
  • URL_MAP:網址對應的名稱

如要將憑證對應組合附加至現有的目標 HTTPS Proxy,請使用 gcloud compute target-https-proxies update 指令。如果您不知道現有目標 Proxy 的名稱,請前往「Target proxies」(目標 Proxy) 頁面,記下目標 Proxy 的名稱。

gcloud compute target-https-proxies update PROXY_NAME \
    --certificate-map="CERTIFICATE_MAP_NAME" \
    --global

建立或更新目標 Proxy 後,請執行下列指令進行驗證:

gcloud compute target-https-proxies list

Terraform

如要將憑證對應組合附加至目標 Proxy,可以使用 google_compute_target_https_proxy 資源。

設定目標 Proxy 時,如果您直接附加 TLS (SSL) 憑證,同時也透過憑證對應組合附加,Proxy 會使用憑證對應組合參照的憑證,並忽略直接附加的 TLS (SSL) 憑證。

排解 CA 服務核發的憑證問題

如需疑難排解步驟,請參閱「與 CA 服務執行個體核發憑證相關的問題」。

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取這個教學課程所用資源的費用,請刪除這些資源。

  1. 刪除負載平衡器及其資源。

    請參閱「清除負載平衡設定」。

  2. 刪除憑證對應組合,或將其與 Proxy 卸離。

    如要刪除憑證對應組合,請執行下列指令:

    gcloud compute target-https-proxies delete PROXY_NAME
    

    如要保留目標 HTTPS Proxy,請將憑證對應組合與 Proxy 卸離。

    • 如果 Proxy 直接附加任何 TLS (SSL) 憑證,則卸載憑證對應組合會導致 Proxy 繼續使用這些直接附加的 TLS (SSL) 憑證。
    • 如果沒有直接附加至 Proxy 的 TLS (SSL) 憑證,就無法從 Proxy 卸載憑證對應組合。您必須先將至少一個 TLS (SSL) 憑證直接附加至 Proxy,才能卸載憑證對應組合。

    如要卸載憑證對應組合,請執行下列指令:

    gcloud compute target-https-proxies update PROXY_NAME \
        --clear-certificate-map
    

    將 PROXY_NAME 替換為目標 Proxy 的名稱。

  3. 從憑證對應組合中刪除憑證對應關係:

    gcloud certificate-manager maps entries delete CERTIFICATE_MAP_ENTRY_NAME \
        --map="CERTIFICATE_MAP_NAME"
    

    更改下列內容:

    • CERTIFICATE_MAP_ENTRY_NAME:憑證對應項目名稱。
    • CERTIFICATE_MAP_NAME:憑證對應組合的名稱。
  4. 刪除憑證對應組合:

    gcloud certificate-manager maps delete CERTIFICATE_MAP_NAME
    

    將 CERTIFICATE_MAP_NAME 替換為憑證對應組合的名稱。

  5. 刪除 Google 代管的憑證:

    控制台

    1. 前往 Google Cloud 控制台的「Certificate Manager」頁面。

      前往「Certificate Manager」

    2. 在「憑證」分頁中,勾選憑證的核取方塊。

    3. 點選「刪除」。

    4. 在出現的對話方塊中,按一下 [Delete] (刪除) 以進行確認。

    gcloud

    gcloud certificate-manager certificates delete CERTIFICATE_NAME
    

    將 CERTIFICATE_NAME 替換為目標憑證名稱。

  6. 刪除憑證核發設定資源:

    控制台

    1. 前往 Google Cloud 控制台,然後點選「Certificate Manager」頁面的「Issuance configs」(核發設定) 分頁標籤。

      前往「Certificate Manager」

    2. 找出要刪除的核發設定資源,然後勾選對應的核取方塊。

    3. 點選「刪除」。

    4. 在出現的對話方塊中,按一下 [Delete] (刪除) 以進行確認。

    gcloud

     gcloud certificate-manager issuance-configs delete ISSUANCE_CONFIG_NAME
     

    將 ISSUANCE_CONFIG_NAME 替換為目標憑證核發設定資源的名稱。

  7. 刪除 CA 集區。

    如要刪除 CA 集區,或停用憑證核發設定資源參照的 CA 集區中最後一個啟用的 CA,請刪除參照該 CA 集區的所有憑證核發設定資源。詳情請參閱「刪除 CA 集區」。

後續步驟