使用自行管理的 SSL 憑證

自行管理的 SSL 憑證,也就是您自行取得、佈建及更新的憑證。您可以使用這個資源,確保用戶端與負載平衡器之間的通訊安全無虞。

自行管理的憑證可以是下列憑證類型的任意組合:

  • 網域驗證 (DV)
  • 機構驗證 (OV)
  • 擴充驗證 (EV)

自行管理憑證支援以下負載平衡器:

  • 全球憑證
    • 全域外部應用程式負載平衡器
    • 傳統版應用程式負載平衡器
    • 外部 Proxy 網路負載平衡器 (含目標 SSL Proxy)
  • 區域認證
    • 區域性外部應用程式負載平衡器
    • 區域性內部應用程式負載平衡器

本頁說明取得有效 Compute Engine 憑證的程序,以及上傳憑證以建立 Google CloudSSL 憑證資源。

如要使用 Certificate Manager 建立 Google 代管憑證,請參閱部署概覽

事前準備

權限

如要執行本指南中的任務,您必須能夠在專案中建立及修改 SSL 憑證。符合下列任一條件即可執行此操作:

  • 您是專案擁有者或編輯者 (roles/ownerroles/editor)。
  • 您在專案中同時具備Compute 安全性管理員角色 (compute.securityAdmin) 和Compute 網路管理員角色 (compute.networkAdmin)。
  • 您為專案建立了自訂角色,其中包含 compute.sslCertificates.* 權限,以及 compute.targetHttpsProxies.*compute.targetSslProxies.* 的其中一個或兩者 (視您使用的負載平衡器類型而定)。

步驟 1:建立私密金鑰和憑證

如果您已擁有私密金鑰和憑證授權單位 (CA) 提供的憑證,請略過本節,並前往「建立 SSL 憑證資源」。

選取或建立私密金鑰

Google Cloud SSL 憑證包含私密金鑰和憑證本身,兩者皆為 PEM 格式。您的私密金鑰必須符合下列條件:

  • 必須採用 PEM 格式
  • 無法透過密碼短句加以保護。 Google Cloud 會以其專屬的加密格式儲存私密金鑰。
  • 其加密演算法必須是 RSA-2048 或 ECDSA P-256。

如要建立新的私密金鑰,請使用下列 OpenSSL 指令之一。

  • 建立 RSA-2048 私密金鑰:

    openssl genrsa -out PRIVATE_KEY_FILE 2048
    
  • 建立 ECDSA P-256 私密金鑰:

    openssl ecparam -name prime256v1 -genkey -noout -out PRIVATE_KEY_FILE
    

PRIVATE_KEY_FILE 替換為新私密金鑰檔案的路徑和檔案名稱。

建立憑證簽署要求 (CSR)

取得私密金鑰後,您可以使用 OpenSSL 產生 PEM 格式的憑證簽署要求 (CSR)。CSR 必須符合下列條件:

  • 必須採用 PEM 格式。
  • 必須具有常見名稱 (CN) 或主題別名 (SAN) 屬性。實際上,憑證應包含 CNSAN 屬性,即使是單一網域也不例外。macOS 和 iOS 的現行版本等新式用戶端不會只依賴 CN 屬性。

如要建立 CSR,請按照下列步驟操作:

  1. 建立 OpenSSL 設定檔。在以下範例中,主體別名是在 [sans_list] 中定義。

    cat <<'EOF' >CONFIG_FILE
    [req]
    default_bits              = 2048
    req_extensions            = extension_requirements
    distinguished_name        = dn_requirements
    prompt                    = no
    
    [extension_requirements]
    basicConstraints          = CA:FALSE
    keyUsage                  = nonRepudiation, digitalSignature, keyEncipherment
    subjectAltName            = @sans_list
    
    [dn_requirements]
    countryName               = Country Name (2 letter code)
    stateOrProvinceName       = State or Province Name (full name)
    localityName              = Locality Name (eg, city)
    0.organizationName        = Organization Name (eg, company)
    organizationalUnitName    = Organizational Unit Name (eg, section)
    commonName                = Common Name (e.g. server FQDN or YOUR name)
    emailAddress              = Email Address
    
    [sans_list]
    DNS.1                     = SUBJECT_ALTERNATIVE_NAME_1
    DNS.2                     = SUBJECT_ALTERNATIVE_NAME_2
    
    EOF
    
  2. 執行下列 OpenSSL 指令,建立憑證簽署要求 (CSR) 檔案。這項指令是互動式指令,系統會提示您提供屬性,但主體別名除外,您在上一個步驟中已在 CONFIG_FILE[sans_list] 中定義主體別名。

    openssl req -new -key PRIVATE_KEY_FILE \
        -out CSR_FILE \
        -config CONFIG_FILE
    

針對這兩個步驟,請將下列內容替換為:

  • CONFIG_FILE:OpenSSL 設定檔的路徑,包括檔案名稱 (完成這個程序後,您可以刪除該檔案)
  • SUBJECT_ALTERNATIVE_NAME_1SUBJECT_ALTERNATIVE_NAME_2:憑證的主體別名

    如果憑證只適用於一個主機名稱,您只應定義一個與共用名稱相符的主體別名。如果您需要的對象別名超過兩個,請將這些別名新增至設定檔,並在 DNS 後面增加數字 (DNS.3DNS.4 等)。

  • PRIVATE_KEY_FILE:私密金鑰檔案的路徑

  • CSR_FILE:CSR 的路徑,包括檔案名稱

簽署 CSR

憑證授權單位 (CA) 簽署 CSR 時,會使用自己的私密金鑰建立憑證。請使用下列其中一種方法簽署 CSR:

使用公開信任的 CA

如果您要求公開信任的 CA 簽署 CSR,則產生的憑證會受到所有信任該公開 CA 的用戶端信任。如要產生已簽署的憑證,公開 CA 只需要您的 CSR。

使用自有的內部 CA

如果您管理自己的 CA,可以使用該 CA 簽署 CSR。使用 CA 簽署 CSR 時,如果客戶端也已設定為信任自己的 CA,系統就會建立內部信任的憑證。

使用自行簽署的憑證

如果您使用建立 CSR 時使用的私密金鑰來簽署 CSR,就會建立自行簽署憑證。您應僅將自行簽署的憑證用於測試。

Google Cloud 不支援用戶端對自行簽署的伺服器憑證進行驗證。因此,您必須將用戶端設定為略過憑證驗證程序。舉例來說,您可以建立網頁瀏覽器用戶端,顯示訊息詢問您是否要信任自行簽署的憑證。

如果您自行管理 CA,或是想要建立自行簽署的憑證進行測試,可以使用下列 OpenSSL 指令:

openssl x509 -req \
    -signkey PRIVATE_KEY_FILE \
    -in CSR_FILE \
    -out CERTIFICATE_FILE \
    -extfile CONFIG_FILE \
    -extensions extension_requirements \
    -days TERM

更改下列內容:

  • PRIVATE_KEY_FILE:CA 私密金鑰的路徑;如果您要建立自行簽署的憑證進行測試,這個私密金鑰會與用於建立 CSR 的私密金鑰相同
  • CSR_FILE:CSR 的路徑
  • CERTIFICATE_FILE:要建立的憑證檔案路徑
  • TERM:從現在起,在證書有效期間內,驗證端應視為有效的證書天數

一般名稱中的萬用字元

您自行管理的 SSL 憑證可以在一般名稱中使用萬用字元。舉例來說,一般名稱為 *.example.com. 的憑證符合主機名稱 www.example.comfoo.example.com,但不符合 a.b.example.comexample.com。負載平衡器選取憑證時,一律會為主機名稱優先選取沒有萬用字元的相符憑證,而不是有萬用字元的憑證。

不支援含有萬用字元片段的憑證,例如 f*.example.com

步驟 2:建立自行管理的 SSL 憑證資源

您必須先擁有私密金鑰和憑證,才能建立 Google Cloud SSL 憑證資源。如果您尚未建立或取得私密金鑰和憑證,請參閱「建立私密金鑰和憑證」。

建立憑證後,您無法將憑證的範圍從全球變更為區域,或從區域變更為全球。

主控台

您可以在 Google Cloud 控制台的「Classic Certificates」分頁中使用全球 SSL 憑證。您無法在 Google Cloud 控制台建立地區 SSL 憑證。使用 gcloud 或 REST API。

  1. 前往 Google Cloud 控制台的「Classic Certificate」分頁。
    前往傳統版憑證
  2. 按一下「建立 SSL 憑證」
  3. 輸入憑證名稱和說明 (選填)。
  4. 選取「上傳我的憑證」
  5. 貼上憑證,或點選「上傳」前往憑證檔案。
    您可以選擇在憑證相同的檔案中加入 CA 憑證鏈結。 Google Cloud 不會為您驗證憑證鏈結,驗證是您的責任。
  6. 貼上私密金鑰,或按一下「上傳」,前往私密金鑰檔案。
  7. 按一下 [建立]。

gcloud

如要建立全域 SSL 憑證,請使用 gcloud compute ssl-certificates create 指令搭配 --global 標記:

gcloud compute ssl-certificates create CERTIFICATE_NAME \
    --certificate=CERTIFICATE_FILE \
    --private-key=PRIVATE_KEY_FILE \
    --global

如要建立地區 SSL 憑證,請使用 gcloud compute ssl-certificates create 指令搭配 --region 標記:

gcloud compute ssl-certificates create CERTIFICATE_NAME \
    --certificate=CERTIFICATE_FILE \
    --private-key=PRIVATE_KEY_FILE \
    --region=REGION

更改下列內容:

  • CERTIFICATE_NAME:要建立的憑證資源名稱
  • CERTIFICATE_FILE:PEM 格式憑證檔案的路徑

    您可以選擇將 CA 憑證鏈結納入憑證所在的檔案。 Google Cloud 不會為您驗證憑證鏈結,您必須自行驗證。

  • PRIVATE_KEY_FILE:PEM 格式私密金鑰的路徑;私密金鑰無法使用密碼保護

  • REGION:如果適用,則是地區 SSL 憑證的區域

    如果此憑證資源適用於內部應用程式負載平衡器或區域性外部應用程式負載平衡器,則區域必須與負載平衡器的區域相同。

Java

如要使用 API 方法,您必須先讀取憑證和私密金鑰檔案,然後建立 SSL 憑證。這是因為 API 要求必須包含檔案內容。

以下範例說明如何使用 Java 執行這項操作。

如需全球 SSL 憑證,請使用 sslCertificates.insert API 方法:


import com.google.cloud.compute.v1.InsertSslCertificateRequest;
import com.google.cloud.compute.v1.SslCertificate;
import com.google.cloud.compute.v1.SslCertificatesClient;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateCertificate {

  public static void main(String[] args)
          throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to use.
    String project = "your-project-id";
    // The certificate you want to create in your project.
    String certificate = "your-certificate";
    // The private key you used to sign the certificate with.
    String privateKey = "your-private-key";
    // Name for the certificate once it's created in your project.
    String certificateName = "your-certificate-name";

    createCertificate(project, certificate, privateKey, certificateName);
  }

  // Create a global SSL self-signed certificate within your Google Cloud project.
  public static SslCertificate createCertificate(String project, String certificate,
                                                 String privateKey, String certificateName)
          throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (SslCertificatesClient client = SslCertificatesClient.create()) {
      SslCertificate certificateResource = SslCertificate.newBuilder()
              .setCertificate(certificate)
              .setPrivateKey(privateKey)
              .setName(certificateName)
              .build();

      InsertSslCertificateRequest request = InsertSslCertificateRequest.newBuilder()
              .setProject(project)
              .setSslCertificateResource(certificateResource)
              .build();

      client.insertCallable().futureCall(request).get(60, TimeUnit.SECONDS);

      // Wait for server update
      TimeUnit.SECONDS.sleep(1);

      SslCertificate sslCert = client.get(project, certificateName);

      System.out.printf("Certificate '%s' has been created successfully", sslCert.getName());

      return sslCert;
    }
  }
}

如需地區 SSL 憑證,請使用 regionSslCertificates.insert API 方法:


import com.google.cloud.compute.v1.InsertRegionSslCertificateRequest;
import com.google.cloud.compute.v1.RegionSslCertificatesClient;
import com.google.cloud.compute.v1.SslCertificate;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateRegionalCertificate {
  public static void main(String[] args)
          throws IOException, InterruptedException, ExecutionException, TimeoutException {
    // TODO(developer): Replace these variables before running the sample.
    // Project ID or project number of the Google Cloud project you want to use.
    String project = "your-project-id";
    // The certificate you want to create in your project.
    String certificate = "your-certificate";
    // The private key you used to sign the certificate with.
    String privateKey = "your-private-key";
    // Name for the certificate once it's created in your project.
    String certificateName = "your-certificate-name";
    // Name of the region you want to use.
    String region = "your-region";

    createRegionCertificate(project, certificate, region, privateKey, certificateName);
  }

  // Create a regional SSL self-signed certificate within your Google Cloud project.
  public static SslCertificate createRegionCertificate(String project, String certificate,
                                                       String region, String privateKey,
                                                       String certificateName)
          throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests.
    try (RegionSslCertificatesClient client = RegionSslCertificatesClient.create()) {
      SslCertificate certificateResource = SslCertificate.newBuilder()
              .setCertificate(certificate)
              .setPrivateKey(privateKey)
              .setName(certificateName)
              .build();

      InsertRegionSslCertificateRequest request = InsertRegionSslCertificateRequest.newBuilder()
              .setProject(project)
              .setRegion(region)
              .setSslCertificateResource(certificateResource)
              .build();

      client.insertCallable().futureCall(request).get(60, TimeUnit.SECONDS);

      // Wait for server update
      TimeUnit.SECONDS.sleep(1);

      SslCertificate sslCert = client.get(project, region, certificateName);

      System.out.printf("Regional cert '%s' has been created successfully", sslCert.getName());

      return sslCert;
    }
  }
}

如需其他程式碼範例,請參閱 API 參考資料頁面

Python

如要使用 API 方法,您必須先讀取憑證和私密金鑰檔案,然後建立 SSL 憑證。這是因為 API 要求必須包含檔案內容。

以下範例說明如何使用 Python 執行這項操作。

如需全球 SSL 憑證,請使用 sslCertificates.insert API 方法:

from pathlib import Path
from pprint import pprint
from typing import Union

from googleapiclient import discovery


def create_certificate(
    project_id: str,
    certificate_file: Union[str, Path],
    private_key_file: Union[str, Path],
    certificate_name: str,
    description: str = "Certificate created from a code sample.",
) -> dict:
    """
    Create a global SSL self-signed certificate within your Google Cloud project.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        certificate_file: path to the file with the certificate you want to create in your project.
        private_key_file: path to the private key you used to sign the certificate with.
        certificate_name: name for the certificate once it's created in your project.
        description: description of the certificate.

    Returns:
        Dictionary with information about the new global SSL self-signed certificate.
    """
    service = discovery.build("compute", "v1")

    # Read the cert into memory
    with open(certificate_file) as f:
        _temp_cert = f.read()

    # Read the private_key into memory
    with open(private_key_file) as f:
        _temp_key = f.read()

    # Now that the certificate and private key are in memory, you can create the
    # certificate resource
    ssl_certificate_body = {
        "name": certificate_name,
        "description": description,
        "certificate": _temp_cert,
        "privateKey": _temp_key,
    }
    request = service.sslCertificates().insert(
        project=project_id, body=ssl_certificate_body
    )
    response = request.execute()
    pprint(response)
    return response

如需地區 SSL 憑證,請使用 regionSslCertificates.insert API 方法:

from pathlib import Path
from pprint import pprint
from typing import Union

from googleapiclient import discovery


def create_regional_certificate(
    project_id: str,
    region: str,
    certificate_file: Union[str, Path],
    private_key_file: Union[str, Path],
    certificate_name: str,
    description: str = "Certificate created from a code sample.",
) -> dict:
    """
    Create a regional SSL self-signed certificate within your Google Cloud project.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        region: name of the region you want to use.
        certificate_file: path to the file with the certificate you want to create in your project.
        private_key_file: path to the private key you used to sign the certificate with.
        certificate_name: name for the certificate once it's created in your project.
        description: description of the certificate.

        Returns:
        Dictionary with information about the new regional SSL self-signed certificate.
    """
    service = discovery.build("compute", "v1")

    # Read the cert into memory
    with open(certificate_file) as f:
        _temp_cert = f.read()

    # Read the private_key into memory
    with open(private_key_file) as f:
        _temp_key = f.read()

    # Now that the certificate and private key are in memory, you can create the
    # certificate resource
    ssl_certificate_body = {
        "name": certificate_name,
        "description": description,
        "certificate": _temp_cert,
        "privateKey": _temp_key,
    }
    request = service.regionSslCertificates().insert(
        project=project_id, region=region, body=ssl_certificate_body
    )
    response = request.execute()
    pprint(response)

    return response

如需其他程式碼範例,請參閱 API 參考資料頁面

步驟 3:將 SSL 憑證與目標 Proxy 建立關聯

您必須將至少一個 SSL 憑證與每個目標 HTTPS 或 SSL Proxy 建立關聯。您最多可以為目標 Proxy 設定 每個目標 HTTPS 或目標 SSL Proxy 的 SSL 憑證數量上限。您可以在同一個目標 Proxy 上參照多個自行管理的憑證。

主控台

使用 Google Cloud 控制台編輯現有負載平衡器時,系統會自動將 SSL 憑證與適當的目標 Proxy 建立關聯。

gcloud

如要將全域 SSL 憑證與目標 HTTPS Proxy 建立關聯,請使用 gcloud compute target-https-proxies update 指令搭配 --global--global-ssl-certificates 旗標:

gcloud compute target-https-proxies update TARGET_PROXY_NAME \
    --global \
    --ssl-certificates=SSL_CERTIFICATE_LIST \
    --global-ssl-certificates

如要將全球性 SSL 憑證與目標 SSL Proxy 建立關聯,請使用 gcloud compute target-ssl-proxies update 指令:

gcloud compute target-ssl-proxies update TARGET_PROXY_NAME \
    --ssl-certificates=SSL_CERTIFICATE_LIST

如要將地區安全資料傳輸層 (SSL) 憑證與目標 HTTPS Proxy 建立關聯,請使用 gcloud compute target-https-proxies update 指令搭配 --region--ssl-certificates-region 旗標:

gcloud compute target-https-proxies update TARGET_PROXY_NAME \
    --region=REGION \
    --ssl-certificates=SSL_CERTIFICATE_LIST \
    --ssl-certificates-region=REGION

更改下列內容:

  • TARGET_PROXY_NAME:負載平衡器的目標 Proxy 名稱
  • REGION (如適用):區域目標 Proxy 和區域 SSL 憑證的地區;地區必須一致
  • SSL_CERTIFICATE_LIST:以半形逗號分隔的Google Cloud SSL 憑證名稱清單

    請確認參照憑證清單包含所有舊版有效 SSL 憑證,以及新的 SSL 憑證。gcloud compute target-ssl-proxies update 指令會使用新值覆寫 --ssl-certificates 的原始值。

API

如要將全域 SSL 憑證與目標 HTTPS Proxy 建立關聯,請對 targetHttpsProxies.insert 方法提出 POST 要求,並將 PROJECT_ID 替換為專案 ID。

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/targetHttpsProxy

{
"name": "l7-xlb-proxy",
"urlMap": "projects/PROJECT_ID/global/urlMaps/l7-xlb-map",
"sslCertificates": /projectsPROJECT_IDglobal/sslCertificates/SSL_CERT_NAME
}

如要將全域 SSL 憑證與目標 HTTPS Proxy 建立關聯,請對 targetSslProxies.insert 方法提出 POST 要求,並將 PROJECT_ID 替換為專案 ID。

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/targetSslProxy

{
"name": "l7-ssl-proxy",
"sslCertificates": /projectsPROJECT_IDglobal/sslCertificates/SSL_CERT_NAME
}

如要將地區安全資料傳輸層 (SSL) 憑證與目標 HTTPS Proxy 建立關聯,請向 targetHttpsProxies.insert 方法提出 POST 要求,並將 PROJECT_ID 替換為您的專案 ID。

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/global/targetHttpsProxy

{
"name": "l7-xlb-proxy",
"urlMap": "projects/PROJECT_ID/global/urlMaps/l7-ilb-map",
"region": "us-west1"
"sslCertificates": /projectsPROJECT_IDregions/us-west1/sslCertificates/SSL_CERT_NAME
}

步驟 4:更新 DNS A 和 AAAA 記錄,指向負載平衡器的 IP 位址

在您的註冊商網站、DNS 主機或網際網路服務供應商 (ISP) (視管理 DNS 記錄的位置而定),新增或更新網域和任何子網域的 DNS A 記錄 (適用於 IPv4) 和 DNS AAAA 記錄 (適用於 IPv6),使其指向與負載平衡器轉送規則相關聯的 IP 位址。

如果您使用的是 Cloud DNSCloud Domains,請設定網域更新名稱伺服器

如果您為單一憑證使用多個網域,則必須為所有網域和任何子網域新增或更新 DNS 記錄,以便這些網域都指向負載平衡器的 IP 位址。

等待 DNS 傳播完成後,您可以執行 dig 指令來驗證設定。舉例來說,假設您的網域是 www.example.com。執行下列 dig 指令:

dig www.example.com
; <<>> DiG 9.10.6 <<>> www.example.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31748
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;www.example.com.           IN  A

;; ANSWER SECTION:
www.example.com.        1742    IN  CNAME   www.example.com.edgekey.net.
www.example.com.edgekey.net. 21330 IN   CNAME   www.example.com.edgekey.net.globalredir.akadns.net.
www.example.com.edgekey.net.globalredir.akadns.net. 3356 IN CNAME   e6858.dsce9.akamaiedge.net.
e6858.dsce9.akamaiedge.net. 19  IN  A   203.0.113.5

;; Query time: 43 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Wed Jun 03 16:54:44 PDT 2020
;; MSG SIZE  rcvd: 193

在這個範例中,203.0.113.5 是負載平衡器的 IP 位址。

步驟 5:使用 OpenSSL 進行測試

負載平衡器最多可能需要 30 分鐘才會開始使用自行管理的 SSL 憑證。

如要進行測試,請執行下列 OpenSSL 指令,並將 DOMAIN 替換為 DNS 名稱,將 IP_ADDRESS 替換為負載平衡器的 IP 位址。

echo | openssl s_client -showcerts -servername DOMAIN -connect IP_ADDRESS:443 -verify 99 -verify_return_error

這個指令會輸出負載平衡器向用戶端提供的憑證。除了其他詳細資訊外,輸出內容應包含憑證鏈結和 Verify return code: 0 (ok)

使用自行管理的 SSL 憑證

以下各節將說明如何列出、查看、刪除及取代 SSL 憑證資源。

列出 SSL 憑證

主控台

您可以在「Certificate Manager」頁面的「Classic Certificates」分頁中,查看全球 SSL 憑證的狀態。您無法在 Google Cloud 控制台維護地區 SSL 憑證。使用 gcloud 或 REST API。

  1. 前往 Google Cloud 控制台的「傳統憑證」分頁。
    前往傳統版憑證
  2. (選用) 篩選 SSL 憑證清單。

gcloud

如要列出全球 SSL 憑證,請使用 gcloud compute ssl-certificates list 指令搭配 --global 標記:

gcloud compute ssl-certificates list \
   --global

如要列出地區 SSL 憑證,請使用 gcloud compute ssl-certificates list 指令搭配 region 篩選器:

gcloud compute ssl-certificates list \
   --filter="region:(REGION ...)"

更改下列內容:

  • REGION: Google Cloud 區域;請以空格分隔多個區域

說明 SSL 憑證

主控台

如要查看全球 SSL 憑證的其他詳細資料,請前往「Certificate Manager」頁面,然後點選「Classic Certificates」分頁。

  1. 前往 Google Cloud 控制台的「傳統憑證」頁面。
    前往傳統版憑證
  2. (選用) 篩選 SSL 憑證清單。
  3. 如要查看更多詳細資料,請按一下證照名稱。

gcloud

如要說明全球 SSL 憑證,請使用 gcloud compute ssl-certificates describe 指令搭配 --global 標記:

gcloud  compute ssl-certificates describe CERTIFICATE_NAME \
   --global

如要說明地區 SSL 憑證,請使用 gcloud compute ssl-certificates describe 指令搭配 --region 標記:

gcloud compute ssl-certificates describe CERTIFICATE_NAME \
   --region=REGION

更改下列內容:

  • CERTIFICATE_NAME:SSL 憑證的名稱
  • REGION: Google Cloud 區域

刪除 SSL 憑證

您必須先更新參考憑證的每個目標 Proxy,才能刪除 SSL 憑證。針對每個目標 Proxy,請執行適當的 gcloud update 指令來更新目標 Proxy 的 SSL_CERTIFICATE_LIST,以便不再包含您需要刪除的 SSL 憑證。每個目標 SSL Proxy 或目標 HTTPS Proxy 都必須參照至少一個 SSL 憑證。

更新目標 Proxy 後,您可以刪除 SSL 憑證。

主控台

您可以在「Certificate Manager」頁面的「Classic Certificates」分頁中刪除全球 SSL 憑證。

  1. 前往 Google Cloud 控制台的「傳統憑證」分頁。
    前往傳統版憑證
  2. 選取要刪除的 SSL 憑證。
  3. 按一下 「Delete」(刪除)
  4. 再按一下「刪除」 確認。

gcloud

如要刪除全球 SSL 憑證,請使用 gcloud compute ssl-certificates delete 指令搭配 --global 指令:

gcloud compute ssl-certificates delete CERTIFICATE_NAME \
    --global

如要刪除地區 SSL 憑證,請使用 gcloud compute ssl-certificates delete 指令搭配 --region 指令:

gcloud compute ssl-certificates delete CERTIFICATE_NAME \
    --region=REGION

更改下列內容:

  • CERTIFICATE_NAME:SSL 憑證的名稱
  • REGION: Google Cloud 區域

在 SSL 憑證到期前更換或續訂

如需替換、續約或輪替 SSL 憑證,請按照下列步驟操作:

  1. 針對目前的憑證執行 gcloud compute ssl-certificates describe 指令,檢查憑證是否即將到期。

  2. 建立新的 SSL 憑證資源。新的 SSL 憑證在專案中必須使用不重複的名稱。

  3. 更新目標 Proxy,以便卸除舊的 SSL 憑證並新增新的憑證。請務必加入您要保留的任何其他現有 SSL 憑證。

    為避免服務中斷,請搭配 --ssl-certificates 標記執行單一 gcloud 指令。例如:

    全域外部應用程式負載平衡器:

    使用加上 --global 旗標的 gcloud compute target-https-proxies update 指令。

    gcloud compute target-https-proxies update TARGET_PROXY_NAME \
       --global \
       --ssl-certificates=new-ssl-cert,other-certificates \
       --global-ssl-certificates
    

    區域性外部應用程式負載平衡器和區域性內部應用程式負載平衡器:

    使用加上 --region 旗標的 gcloud compute target-https-proxies update 指令。

    gcloud compute target-https-proxies update TARGET_PROXY_NAME \
       --region=REGION \
       --ssl-certificates=new-ssl-cert,other-certificates \
       --ssl-certificates-region=REGION
    

    外部 Proxy 網路負載平衡器:

    使用加上 --backend-service 旗標的 gcloud compute target-ssl-proxies update 指令。

    gcloud compute target-ssl-proxies update TARGET_PROXY_NAME \
       --ssl-certificates=new-ssl-cert,other-certificates
    
  4. 執行下列 OpenSSL 指令,確認負載平衡器是否提供替換憑證:

    echo | openssl s_client -showcerts -connect IP_ADDRESS:443 -verify 99 -verify_return_error
    
  5. 請等待 15 分鐘,確保替換作業已傳播至所有 Google Front End (GFE)。

  6. (選用) 刪除舊的 SSL 憑證

定期輪替 SSL 憑證

這個範例解決方案會定期檢查與 Google Cloud 負載平衡器搭配使用的憑證狀態,並在憑證使用壽命達到指定百分比時輪替憑證。這項工具會使用透過憑證授權單位服務設定的 CA。

本解決方案可搭配下列負載平衡器使用:

  • 全域外部應用程式負載平衡器
  • 傳統版應用程式負載平衡器
  • 區域性外部應用程式負載平衡器
  • 內部應用程式負載平衡器
  • 外部 Proxy 網路負載平衡器 (含 SSL Proxy)

後續步驟