刪除憑證授權單位

憑證授權單位服務可讓您刪除現有的憑證授權單位 (CA)。在啟動刪除程序後,憑證授權單位還有 30 天的寬限期,寬限期結束後便會永久刪除憑證授權單位。寬限期過後,CA 服務會永久刪除 CA 和所有巢狀構件,例如憑證和憑證撤銷清單 (CRL)。

遭刪除的 CA 所使用的任何客戶代管資源 (例如 Cloud Storage bucket 或 Cloud Key Management Service 金鑰) 都不會遭到刪除。 Google Cloud 如要進一步瞭解 Google 代管和客戶代管的資源,請參閱「管理資源」。

在寬限期內,系統不會針對已刪除的 CA 收費。不過,如果還原 CA,系統會按照 CA 的帳單層級,針對 CA 處於 DELETED 狀態的時間收費。

事前準備

  • 確認您具備 CA 服務作業管理員 (roles/privateca.caManager) 或 CA 服務管理員 (roles/privateca.admin) Identity and Access Management (IAM) 角色。如要進一步瞭解 CA 服務的預先定義 IAM 角色,請參閱「使用 IAM 控管存取權」。

    如要瞭解如何授予身分與存取權管理角色,請參閱「授予單一角色」。

  • 請確認 CA 符合下列條件:

    • CA 必須處於 AWAITING_USER_ACTIVATIONDISABLEDSTAGED 狀態。詳情請參閱「憑證授權單位狀態」。
    • CA 不得含有使用中的憑證。如果憑證符合下列條件,即視為有效:

    • 憑證的「從」和「到」日期有效。

    • 憑證尚未撤銷。

    • 使用憑證的裝置或系統信任核發憑證的憑證授權單位 (CA)。

    刪除 CA 前,請務必撤銷 CA 核發的所有有效憑證。您無法撤銷已刪除 CA 的憑證。

刪除 CA

如要啟動 CA 刪除作業,請按照下列步驟操作:

控制台

  1. 前往 Google Cloud 控制台的「憑證授權單位」頁面。

    前往「憑證授權單位」

  2. 在 CA 清單中,選取要刪除的 CA。
  3. 按一下 「Delete」(刪除)。系統會隨即顯示「Delete Certificate Authority」(刪除憑證授權單位) 對話方塊。
  4. 選用:如果符合下列條件,請選取一或兩個核取方塊:
    • 刪除這個 CA (即使憑證有效)

      這個選項可讓您刪除含有有效憑證的 CA。刪除含有有效憑證的 CA 可能會導致依賴這些憑證的網站、應用程式或系統發生故障。建議您先撤銷 CA 核發的所有有效憑證,再刪除 CA。

    • 略過 30 天的寬限期,立即刪除這個 CA

      30 天的寬限期可讓您撤銷這個 CA 核發的所有憑證,並確認沒有任何系統依賴這個 CA。建議您「只」在非實際工作或測試環境中使用這個選項,以免發生潛在的服務中斷和資料遺失問題。

  5. 按一下「確認」

gcloud

  1. 檢查 CA 狀態,確認已停用。您只能刪除處於 DISABLED 狀態的 CA。

    gcloud privateca roots describe CA_ID --pool=POOL_ID --location=LOCATION --format="value(state)"
    

    更改下列內容:

    • CA_ID:CA 的專屬 ID
    • POOL_ID:包含 CA 的 CA 集區名稱
    • LOCATION:CA 集區的位置。如需完整的位置清單,請參閱「位置」。

    如要進一步瞭解 gcloud privateca roots describe 指令,請參閱 gcloud privateca roots describe

  2. 如果 CA 未停用,請執行下列指令來停用 CA。

    gcloud privateca roots disable CA_ID --pool=POOL_ID --location=LOCATION
    

    如要進一步瞭解 gcloud privateca roots disable 指令,請參閱 gcloud privateca roots disable

  3. 刪除 CA。

    gcloud privateca roots delete CA_ID --pool=POOL_ID --location=LOCATION
    

    即使 CA 含有有效憑證,您仍可在 gcloud 指令中加入 --ignore-active-certificates 旗標,藉此刪除 CA。

    如要進一步瞭解 gcloud privateca roots delete 指令,請參閱 gcloud privateca roots delete

  4. 系統提示時,請確認要刪除 CA。

    確認後,系統會排定刪除憑證授權單位,並開始計算 30 天的寬限期。指令會輸出預計刪除 CA 的日期和時間。

        Deleted Root CA [projects/PROJECT_ID/locations/us-west1/caPools/POOL_ID/certificateAuthorities/CA_ID] can be undeleted until 2020-08-14T19:28:39Z.
    

Go

如要向 CA Service 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

import (
	"context"
	"fmt"
	"io"

	privateca "cloud.google.com/go/security/privateca/apiv1"
	"cloud.google.com/go/security/privateca/apiv1/privatecapb"
)

// Delete a Certificate Authority from the specified CA pool.
// Before deletion, the CA must be disabled or staged and must not contain any active certificates.
func deleteCa(w io.Writer, projectId string, location string, caPoolId string, caId string) error {
	// projectId := "your_project_id"
	// location := "us-central1"	// For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations.
	// caPoolId := "ca-pool-id"		// The id of the CA pool under which the CA is present.
	// caId := "ca-id"				// The id of the CA to be deleted.

	ctx := context.Background()
	caClient, err := privateca.NewCertificateAuthorityClient(ctx)
	if err != nil {
		return fmt.Errorf("NewCertificateAuthorityClient creation failed: %w", err)
	}
	defer caClient.Close()

	fullCaName := fmt.Sprintf("projects/%s/locations/%s/caPools/%s/certificateAuthorities/%s",
		projectId, location, caPoolId, caId)

	// Check if the CA is disabled or staged.
	// See https://pkg.go.dev/cloud.google.com/go/security/privateca/apiv1/privatecapb#GetCertificateAuthorityRequest.
	caReq := &privatecapb.GetCertificateAuthorityRequest{Name: fullCaName}
	caResp, err := caClient.GetCertificateAuthority(ctx, caReq)
	if err != nil {
		return fmt.Errorf("GetCertificateAuthority failed: %w", err)
	}

	if caResp.State != privatecapb.CertificateAuthority_DISABLED &&
		caResp.State != privatecapb.CertificateAuthority_STAGED {
		return fmt.Errorf("you can only delete disabled or staged Certificate Authorities. %s is not disabled", caId)
	}

	// Create the DeleteCertificateAuthorityRequest.
	// Setting the IgnoreActiveCertificates to True will delete the CA
	// even if it contains active certificates. Care should be taken to re-anchor
	// the certificates to new CA before deleting.
	// See https://pkg.go.dev/cloud.google.com/go/security/privateca/apiv1/privatecapb#DeleteCertificateAuthorityRequest.
	req := &privatecapb.DeleteCertificateAuthorityRequest{
		Name:                     fullCaName,
		IgnoreActiveCertificates: false,
	}

	op, err := caClient.DeleteCertificateAuthority(ctx, req)
	if err != nil {
		return fmt.Errorf("DeleteCertificateAuthority failed: %w", err)
	}

	if caResp, err = op.Wait(ctx); err != nil {
		return fmt.Errorf("DeleteCertificateAuthority failed during wait: %w", err)
	}

	if caResp.State != privatecapb.CertificateAuthority_DELETED {
		return fmt.Errorf("unable to delete Certificate Authority. Current state: %s", caResp.State.String())
	}

	fmt.Fprintf(w, "Successfully deleted Certificate Authority: %s.", caId)
	return nil
}

Java

如要向 CA Service 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。


import com.google.api.core.ApiFuture;
import com.google.cloud.security.privateca.v1.CertificateAuthority.State;
import com.google.cloud.security.privateca.v1.CertificateAuthorityName;
import com.google.cloud.security.privateca.v1.CertificateAuthorityServiceClient;
import com.google.cloud.security.privateca.v1.DeleteCertificateAuthorityRequest;
import com.google.longrunning.Operation;
import java.io.IOException;
import java.util.concurrent.ExecutionException;

public class DeleteCertificateAuthority {

  public static void main(String[] args)
      throws InterruptedException, ExecutionException, IOException {
    // TODO(developer): Replace these variables before running the sample.
    // location: For a list of locations, see:
    // https://cloud.google.com/certificate-authority-service/docs/locations
    // poolId: The id of the CA pool under which the CA is present.
    // certificateAuthorityName: The name of the CA to be deleted.
    String project = "your-project-id";
    String location = "ca-location";
    String poolId = "ca-pool-id";
    String certificateAuthorityName = "certificate-authority-name";
    deleteCertificateAuthority(project, location, poolId, certificateAuthorityName);
  }

  // Delete the Certificate Authority from the specified CA pool.
  // Before deletion, the CA must be disabled and must not contain any active certificates.
  public static void deleteCertificateAuthority(
      String project, String location, String poolId, String certificateAuthorityName)
      throws IOException, ExecutionException, InterruptedException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the `certificateAuthorityServiceClient.close()` method on the client to safely
    // clean up any remaining background resources.
    try (CertificateAuthorityServiceClient certificateAuthorityServiceClient =
        CertificateAuthorityServiceClient.create()) {
      // Create the Certificate Authority Name.
      CertificateAuthorityName certificateAuthorityNameParent =
          CertificateAuthorityName.newBuilder()
              .setProject(project)
              .setLocation(location)
              .setCaPool(poolId)
              .setCertificateAuthority(certificateAuthorityName)
              .build();

      // Check if the CA is enabled.
      State caState =
          certificateAuthorityServiceClient
              .getCertificateAuthority(certificateAuthorityNameParent)
              .getState();
      if (caState == State.ENABLED) {
        System.out.println(
            "Please disable the Certificate Authority before deletion ! Current state: " + caState);
        return;
      }

      // Create the DeleteCertificateAuthorityRequest.
      // Setting the setIgnoreActiveCertificates() to true, will delete the CA
      // even if it contains active certificates. Care should be taken to re-anchor
      // the certificates to new CA before deleting.
      DeleteCertificateAuthorityRequest deleteCertificateAuthorityRequest =
          DeleteCertificateAuthorityRequest.newBuilder()
              .setName(certificateAuthorityNameParent.toString())
              .setIgnoreActiveCertificates(false)
              .build();

      // Delete the Certificate Authority.
      ApiFuture<Operation> futureCall =
          certificateAuthorityServiceClient
              .deleteCertificateAuthorityCallable()
              .futureCall(deleteCertificateAuthorityRequest);
      Operation response = futureCall.get();

      if (response.hasError()) {
        System.out.println("Error while deleting Certificate Authority !" + response.getError());
        return;
      }

      // Check if the CA has been deleted.
      caState =
          certificateAuthorityServiceClient
              .getCertificateAuthority(certificateAuthorityNameParent)
              .getState();
      if (caState == State.DELETED) {
        System.out.println(
            "Successfully deleted Certificate Authority : " + certificateAuthorityName);
      } else {
        System.out.println(
            "Unable to delete Certificate Authority. Please try again ! Current state: " + caState);
      }
    }
  }
}

Python

如要向 CA Service 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

import google.cloud.security.privateca_v1 as privateca_v1


def delete_certificate_authority(
    project_id: str, location: str, ca_pool_name: str, ca_name: str
) -> None:
    """
    Delete the Certificate Authority from the specified CA pool.
    Before deletion, the CA must be disabled and must not contain any active certificates.

    Args:
        project_id: project ID or project number of the Cloud project you want to use.
        location: location you want to use. For a list of locations, see: https://cloud.google.com/certificate-authority-service/docs/locations.
        ca_pool_name: the name of the CA pool under which the CA is present.
        ca_name: the name of the CA to be deleted.
    """

    caServiceClient = privateca_v1.CertificateAuthorityServiceClient()
    ca_path = caServiceClient.certificate_authority_path(
        project_id, location, ca_pool_name, ca_name
    )

    # Check if the CA is enabled.
    ca_state = caServiceClient.get_certificate_authority(name=ca_path).state
    if ca_state != privateca_v1.CertificateAuthority.State.DISABLED:
        print(
            "Please disable the Certificate Authority before deletion ! Current state:",
            ca_state,
        )
        raise RuntimeError(
            f"You can only delete disabled Certificate Authorities. "
            f"{ca_name} is not disabled!"
        )

    # Create the DeleteCertificateAuthorityRequest.
    # Setting the ignore_active_certificates to True will delete the CA
    # even if it contains active certificates. Care should be taken to re-anchor
    # the certificates to new CA before deleting.
    request = privateca_v1.DeleteCertificateAuthorityRequest(
        name=ca_path, ignore_active_certificates=False
    )

    # Delete the Certificate Authority.
    operation = caServiceClient.delete_certificate_authority(request=request)
    result = operation.result()

    print("Operation result", result)

    # Get the current CA state.
    ca_state = caServiceClient.get_certificate_authority(name=ca_path).state

    # Check if the CA has been deleted.
    if ca_state == privateca_v1.CertificateAuthority.State.DELETED:
        print("Successfully deleted Certificate Authority:", ca_name)
    else:
        print(
            "Unable to delete Certificate Authority. Please try again ! Current state:",
            ca_state,
        )

查看已刪除 CA 的到期日

如要查看 CA 的永久刪除時間,請按照下列步驟操作:

控制台

  1. 按一下「CA pool manager」(CA 集區管理員) 分頁標籤。
  2. 按一下包含您刪除的 CA 的 CA 集區名稱。

您可以在「CA pool」(CA 集區) 頁面的表格中查看 CA 的到期日。

查看已刪除 CA 的到期日。

gcloud

如要查看 CA 的預期刪除時間,請執行下列指令:

gcloud privateca roots describe CA_ID \
    --pool=POOL_ID \
    --location=LOCATION \
    --format="value(expireTime.date())"

更改下列內容:

  • CA_ID:CA 的名稱
  • POOL_ID:包含 CA 的 CA 集區名稱
  • LOCATION:CA 集區的位置。如需完整的位置清單,請參閱「位置」。

指令會傳回 CA Service 刪除 CA 的預計日期和時間。

2020-08-14T19:28:39

如要確認 CA 已永久刪除,請執行下列指令:

gcloud privateca roots describe CA_ID --pool=POOL_ID --location=LOCATION

如果 CA 成功刪除,指令會傳回下列錯誤。

ERROR: (gcloud.privateca.roots.describe) NOT_FOUND: Resource 'projects/PROJECT_ID/locations/LOCATION/caPools/POOL_ID/certificateAuthorities/CA_ID' was not found

後續步驟