销毁和恢复密钥版本

本页面介绍如何安排 Cloud Key Management Service 密钥版本进行永久销毁。在 Cloud KMS 中,用于加密、解密、签名和验证数据的加密密钥材料存储在密钥版本中。一个密钥具有零个或多个密钥版本。轮替密钥时,您会创建新的密钥版本。

销毁密钥版本意味着密钥材料将被永久删除。销毁密钥版本时,系统不会删除密钥名称和密钥版本号等其他详细信息。销毁密钥后,使用相应密钥版本加密的数据将无法解密。

唯一的例外情况是密钥重新导入,该功能可让您通过提供相同的原始密钥材料来恢复先前导入的密钥。

由于密钥销毁通常是不可逆的,因此 Cloud KMS 不允许您立即销毁密钥版本。而是安排销毁密钥版本。密钥版本会保持“已安排销毁”状态一段可配置的时间。在“已安排销毁”状态持续期间,您可以恢复密钥版本以取消销毁。

默认的预定销毁时长为 30 天。您可以在创建密钥时为密钥设置自定义的“安排销毁”时长。您的组织可以在组织政策中设置每个密钥的最短预定销毁时长限制,以强制实施最短预定销毁时长。

您还可以使用 Identity and Access Management (IAM) 管理对密钥的访问权限。IAM 操作会在数秒内达成一致。如需了解详情,请参阅使用 IAM

您还可以暂时停用密钥版本。我们建议您在安排销毁密钥版本之前先停用密钥版本,以确保密钥可以安全销毁。根据您的组织政策,您可能需要先停用密钥版本,然后才能安排销毁该密钥版本。如需详细了解如何使用组织政策控制密钥版本销毁,请参阅控制密钥版本销毁

在本文档的其余部分中,安排销毁密钥称为销毁密钥,即使销毁不是立即进行。

准备工作

了解相关风险

销毁密钥版本是一项永久性操作。销毁仍需要的密钥版本存在以下风险:

  • 服务中断:如果您销毁启动容器或实例所需的密钥,您的服务或应用可能会变得不可用。

  • 永久性数据丢失:如果您销毁曾用于加密数据的密钥,相应数据将无法再访问。使用已销毁的密钥加密的数据被视为加密粉碎。在某些情况下,销毁密钥可能会导致加密资源被永久删除。

  • 法规或合规性问题:如果您在保留期限结束之前销毁了访问受保留期限限制的数据所需的密钥,则可能会违反法规或合规性要求。

所需的角色

如需获得销毁和恢复密钥版本所需的权限,请让您的管理员为您授予密钥的 Cloud KMS Admin (roles/cloudkms.admin) IAM 角色。 如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

您也可以通过自定义角色或其他预定义角色来获取所需的权限。

检查密钥版本是否正在使用

在销毁密钥版本之前,请完成以下步骤,以查看该密钥版本是否正在使用:

  1. 查看密钥的使用情况跟踪详情。 如果任何资源受到您要销毁的密钥版本的保护,请使用其他密钥版本重新加密这些资源。

  2. 为可能使用相应密钥版本的任何服务或应用启用日志记录。

  3. 在包含密钥的 Cloud KMS 项目中启用日志记录。

  4. 停用密钥版本。停用密钥版本可防止使用该密钥版本。停用密钥版本后,任何尝试使用该密钥版本的操作都会失败。

  5. 监控日志,直到您确定没有应用或服务仍依赖于您停用的密钥版本。如果任何错误表明无法访问密钥版本,请配置应用或资源以使用其他密钥版本。

    在销毁密钥版本之前,您需要花多长时间来监控日志,这取决于密钥的类型、使用模式和敏感度级别。例如,在销毁用于每季度运行一次的进程中的密钥版本之前,请保持该密钥版本处于停用状态,直到该进程成功完成。

  6. 根据任何适用的合规性要求检查密钥的使用情况。 例如,密钥版本和使用该密钥版本加密的数据可能需要遵守数据保留期限。

这些步骤有助于您确定是否可能仍需要某个密钥,但无法保证不再需要某个密钥版本。组织应实施相关程序和准则,以确保密钥版本销毁不会造成负面影响。

销毁密钥版本

您可以销毁已启用或已停用的密钥版本。

控制台

  1. 在 Google Cloud 控制台中,前往密钥管理页面。

    前往 Key Management

  2. 选中要安排销毁的密钥版本旁边的复选框。

  3. 点击标头中的销毁

  4. 在确认提示中输入密钥名称,然后点击安排销毁

gcloud

如需在命令行上使用 Cloud KMS,请先安装或升级到最新版本的 Google Cloud CLI

gcloud kms keys versions destroy KEY_VERSION \
    --key KEY_NAME \
    --keyring KEY_RING \
    --location LOCATION

替换以下内容:

  • KEY_VERSION:您要销毁的密钥版本的版本号。
  • KEY_NAME:您要销毁密钥版本的密钥的名称。
  • KEY_RING:包含密钥的密钥环的名称。
  • LOCATION:密钥环的 Cloud KMS 位置。

如需了解所有标志和可能值,请使用 --help 标志运行命令。

C#

要运行此代码,请先设置 C# 开发环境安装 Cloud KMS C# SDK


using Google.Cloud.Kms.V1;

public class DestroyKeyVersionSample
{
    public CryptoKeyVersion DestroyKeyVersion(
      string projectId = "my-project", string locationId = "us-east1", string keyRingId = "my-key-ring", string keyId = "my-key", string keyVersionId = "123")
    {
        // Create the client.
        KeyManagementServiceClient client = KeyManagementServiceClient.Create();

        // Build the key version name.
        CryptoKeyVersionName keyVersionName = new CryptoKeyVersionName(projectId, locationId, keyRingId, keyId, keyVersionId);

        // Call the API.
        CryptoKeyVersion result = client.DestroyCryptoKeyVersion(keyVersionName);

        // Return the result.
        return result;
    }
}

Go

要运行此代码,请先设置 Go 开发环境安装 Cloud KMS Go SDK

import (
	"context"
	"fmt"
	"io"

	kms "cloud.google.com/go/kms/apiv1"
	"cloud.google.com/go/kms/apiv1/kmspb"
)

// destroyKeyVersion marks a specified key version for deletion. The key can be
// restored if requested within 24 hours.
func destroyKeyVersion(w io.Writer, name string) error {
	// name := "projects/my-project/locations/us-east1/keyRings/my-key-ring/cryptoKeys/my-key/cryptoKeyVersions/123"

	// Create the client.
	ctx := context.Background()
	client, err := kms.NewKeyManagementClient(ctx)
	if err != nil {
		return fmt.Errorf("failed to create kms client: %w", err)
	}
	defer client.Close()

	// Build the request.
	req := &kmspb.DestroyCryptoKeyVersionRequest{
		Name: name,
	}

	// Call the API.
	result, err := client.DestroyCryptoKeyVersion(ctx, req)
	if err != nil {
		return fmt.Errorf("failed to destroy key version: %w", err)
	}
	fmt.Fprintf(w, "Destroyed key version: %s\n", result)
	return nil
}

Java

要运行此代码,请先设置 Java 开发环境安装 Cloud KMS Java SDK

import com.google.cloud.kms.v1.CryptoKeyVersion;
import com.google.cloud.kms.v1.CryptoKeyVersionName;
import com.google.cloud.kms.v1.KeyManagementServiceClient;
import java.io.IOException;

public class DestroyKeyVersion {

  public void destroyKeyVersion() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String locationId = "us-east1";
    String keyRingId = "my-key-ring";
    String keyId = "my-key";
    String keyVersionId = "123";
    destroyKeyVersion(projectId, locationId, keyRingId, keyId, keyVersionId);
  }

  // Schedule destruction of the given key version.
  public void destroyKeyVersion(
      String projectId, String locationId, String keyRingId, String keyId, String keyVersionId)
      throws IOException {
    // 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 "close" method on the client to
    // safely clean up any remaining background resources.
    try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
      // Build the key version name from the project, location, key ring, key,
      // and key version.
      CryptoKeyVersionName keyVersionName =
          CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);

      // Destroy the key version.
      CryptoKeyVersion response = client.destroyCryptoKeyVersion(keyVersionName);
      System.out.printf("Destroyed key version: %s%n", response.getName());
    }
  }
}

Node.js

要运行此代码,请先设置 Node.js 开发环境安装 Cloud KMS Node.js SDK

//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-east1';
// const keyRingId = 'my-key-ring';
// const keyId = 'my-key';
// const versionId = '123';

// Imports the Cloud KMS library
const {KeyManagementServiceClient} = require('@google-cloud/kms');

// Instantiates a client
const client = new KeyManagementServiceClient();

// Build the key version name
const versionName = client.cryptoKeyVersionPath(
  projectId,
  locationId,
  keyRingId,
  keyId,
  versionId
);

async function destroyKeyVersion() {
  const [version] = await client.destroyCryptoKeyVersion({
    name: versionName,
  });

  console.log(`Destroyed key version: ${version.name}`);
  return version;
}

return destroyKeyVersion();

PHP

如需运行此代码,请先了解如何在 Google Cloud上使用 PHP安装 Cloud KMS PHP SDK

use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\DestroyCryptoKeyVersionRequest;

function destroy_key_version(
    string $projectId = 'my-project',
    string $locationId = 'us-east1',
    string $keyRingId = 'my-key-ring',
    string $keyId = 'my-key',
    string $versionId = '123'
) {
    // Create the Cloud KMS client.
    $client = new KeyManagementServiceClient();

    // Build the key version name.
    $keyVersionName = $client->cryptoKeyVersionName($projectId, $locationId, $keyRingId, $keyId, $versionId);

    // Call the API.
    $destroyCryptoKeyVersionRequest = (new DestroyCryptoKeyVersionRequest())
        ->setName($keyVersionName);
    $destroyedVersion = $client->destroyCryptoKeyVersion($destroyCryptoKeyVersionRequest);
    printf('Destroyed key version: %s' . PHP_EOL, $destroyedVersion->getName());

    return $destroyedVersion;
}

Python

要运行此代码,请先设置 Python 开发环境安装 Cloud KMS Python SDK

from google.cloud import kms


def destroy_key_version(
    project_id: str, location_id: str, key_ring_id: str, key_id: str, version_id: str
) -> kms.CryptoKeyVersion:
    """
    Schedule destruction of the given key version.

    Args:
        project_id (string): Google Cloud project ID (e.g. 'my-project').
        location_id (string): Cloud KMS location (e.g. 'us-east1').
        key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring').
        key_id (string): ID of the key to use (e.g. 'my-key').
        version_id (string): ID of the key version to destroy (e.g. '1').

    Returns:
        CryptoKeyVersion: The version.

    """

    # Create the client.
    client = kms.KeyManagementServiceClient()

    # Build the key version name.
    key_version_name = client.crypto_key_version_path(
        project_id, location_id, key_ring_id, key_id, version_id
    )

    # Call the API.
    destroyed_version = client.destroy_crypto_key_version(
        request={"name": key_version_name}
    )
    print(f"Destroyed key version: {destroyed_version.name}")
    return destroyed_version

Ruby

要运行此代码,请先设置 Ruby 开发环境安装 Cloud KMS Ruby SDK

# TODO(developer): uncomment these values before running the sample.
# project_id  = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id      = "my-key"
# version_id  = "123"

# Require the library.
require "google/cloud/kms"

# Create the client.
client = Google::Cloud::Kms.key_management_service

# Build the key version name.
key_version_name = client.crypto_key_version_path project:            project_id,
                                                  location:           location_id,
                                                  key_ring:           key_ring_id,
                                                  crypto_key:         key_id,
                                                  crypto_key_version: version_id

# Call the API.
destroyed_version = client.destroy_crypto_key_version name: key_version_name
puts "Destroyed key version: #{destroyed_version.name}"

API

这些示例使用 curl 作为 HTTP 客户端来演示如何使用 API。如需详细了解访问权限控制,请参阅访问 Cloud KMS API

通过调用 CryptoKeyVersions.destroy 方法销毁密钥版本。

curl "https://cloudkms.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/keyRings/KEY_RING/cryptoKeys/KEY_NAME/cryptoKeyVersions/KEY_VERSION:destroy" \
    --request "POST" \
    --header "authorization: Bearer TOKEN"

如果您无法销毁某个密钥版本,可能是因为贵组织要求在销毁密钥版本之前先停用密钥版本。尝试在销毁密钥版本之前将其停用。

提交销毁请求后,密钥版本的状态将变为“已安排销毁”。在密钥的配置的计划销毁时长过去后,密钥版本的状态会变为“已销毁”,这意味着已开始从活跃系统中逻辑删除密钥材料,并且客户无法恢复密钥材料。密钥材料可在 Google 系统中保留长达 45 天(自预定销毁时间起)。

如需在密钥版本安排销毁后接收提醒,请参阅将 Cloud Monitoring 与 Cloud KMS 搭配使用

已销毁的密钥版本不是计费资源。

销毁外部密钥

如需永久移除 Cloud EKM 密钥与外部密钥之间的关联,您可以销毁密钥版本。已安排销毁状态的期限过后,密钥会被销毁。 密钥版本销毁后,您就无法再加密数据或解密使用 Cloud EKM 密钥版本加密的数据。

在 Cloud KMS 中销毁手动管理的密钥版本不会修改外部密钥管理器中的相应密钥。我们建议您先销毁 Google Cloud中的密钥或密钥版本。销毁 Cloud EKM 密钥版本后,您可以销毁外部密钥管理器中的密钥材料。

在 Cloud KMS 中销毁协同外部密钥版本时,系统会先在 Google Cloud中销毁密钥版本,然后向 EKM 发送销毁请求,以销毁外部密钥材料。

恢复密钥版本

在密钥版本的状态为“已安排销毁”期间,您可以通过提交恢复请求来恢复密钥版本。

控制台

  1. 前往 Google Cloud 控制台中的密钥管理页面。

    前往“密钥管理”页面

  2. 点击您将恢复其密钥版本的密钥所在密钥环的名称。

  3. 点击您要恢复其密钥版本的密钥。

  4. 选中要恢复的密钥版本旁边的复选框。

  5. 点击标头中的恢复

  6. 在确认提示中,点击恢复

gcloud

如需在命令行上使用 Cloud KMS,请先安装或升级到最新版本的 Google Cloud CLI

gcloud kms keys versions restore key-version \
    --key key \
    --keyring key-ring \
    --location location

key-version 替换为要恢复的密钥的版本。将 key 替换为密钥的名称。将 key-ring 替换为密钥所在的密钥环的名称。将 location 替换为密钥环的 Cloud KMS 位置。

如需了解所有标志和可能值,请使用 --help 标志运行命令。

C#

要运行此代码,请先设置 C# 开发环境安装 Cloud KMS C# SDK


using Google.Cloud.Kms.V1;

public class RestoreKeyVersionSample
{
    public CryptoKeyVersion RestoreKeyVersion(string projectId = "my-project", string locationId = "us-east1", string keyRingId = "my-key-ring", string keyId = "my-key", string keyVersionId = "123")
    {
        // Create the client.
        KeyManagementServiceClient client = KeyManagementServiceClient.Create();

        // Build the key version name.
        CryptoKeyVersionName cryptoKeyVersionName = new CryptoKeyVersionName(projectId, locationId, keyRingId, keyId, keyVersionId);

        // Call the API.
        CryptoKeyVersion result = client.RestoreCryptoKeyVersion(cryptoKeyVersionName);

        // Return the result.
        return result;
    }
}

Go

要运行此代码,请先设置 Go 开发环境安装 Cloud KMS Go SDK

import (
	"context"
	"fmt"
	"io"

	kms "cloud.google.com/go/kms/apiv1"
	"cloud.google.com/go/kms/apiv1/kmspb"
)

// restoreKeyVersion attempts to recover a key that has been marked for
// destruction in the past 24h.
func restoreKeyVersion(w io.Writer, name string) error {
	// name := "projects/my-project/locations/us-east1/keyRings/my-key-ring/cryptoKeys/my-key/cryptoKeyVersions/123"

	// Create the client.
	ctx := context.Background()
	client, err := kms.NewKeyManagementClient(ctx)
	if err != nil {
		return fmt.Errorf("failed to create kms client: %w", err)
	}
	defer client.Close()

	// Build the request.
	req := &kmspb.RestoreCryptoKeyVersionRequest{
		Name: name,
	}

	// Call the API.
	result, err := client.RestoreCryptoKeyVersion(ctx, req)
	if err != nil {
		return fmt.Errorf("failed to restore key version: %w", err)
	}
	fmt.Fprintf(w, "Restored key version: %s\n", result)
	return nil
}

Java

要运行此代码,请先设置 Java 开发环境安装 Cloud KMS Java SDK

import com.google.cloud.kms.v1.CryptoKeyVersion;
import com.google.cloud.kms.v1.CryptoKeyVersionName;
import com.google.cloud.kms.v1.KeyManagementServiceClient;
import java.io.IOException;

public class RestoreKeyVersion {

  public void restoreKeyVersion() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String locationId = "us-east1";
    String keyRingId = "my-key-ring";
    String keyId = "my-key";
    String keyVersionId = "123";
    restoreKeyVersion(projectId, locationId, keyRingId, keyId, keyVersionId);
  }

  // Schedule destruction of the given key version.
  public void restoreKeyVersion(
      String projectId, String locationId, String keyRingId, String keyId, String keyVersionId)
      throws IOException {
    // 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 "close" method on the client to
    // safely clean up any remaining background resources.
    try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
      // Build the key version name from the project, location, key ring, key,
      // and key version.
      CryptoKeyVersionName keyVersionName =
          CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);

      // Restore the key version.
      CryptoKeyVersion response = client.restoreCryptoKeyVersion(keyVersionName);
      System.out.printf("Restored key version: %s%n", response.getName());
    }
  }
}

Node.js

要运行此代码,请先设置 Node.js 开发环境安装 Cloud KMS Node.js SDK

//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-east1';
// const keyRingId = 'my-key-ring';
// const keyId = 'my-key';
// const versionId = '123';

// Imports the Cloud KMS library
const {KeyManagementServiceClient} = require('@google-cloud/kms');

// Instantiates a client
const client = new KeyManagementServiceClient();

// Build the key version name
const versionName = client.cryptoKeyVersionPath(
  projectId,
  locationId,
  keyRingId,
  keyId,
  versionId
);

async function restoreKeyVersion() {
  const [version] = await client.restoreCryptoKeyVersion({
    name: versionName,
  });

  console.log(`Restored key version: ${version.name}`);
  return version;
}

return restoreKeyVersion();

PHP

如需运行此代码,请先了解如何在 Google Cloud上使用 PHP安装 Cloud KMS PHP SDK

use Google\Cloud\Kms\V1\Client\KeyManagementServiceClient;
use Google\Cloud\Kms\V1\RestoreCryptoKeyVersionRequest;

function restore_key_version(
    string $projectId = 'my-project',
    string $locationId = 'us-east1',
    string $keyRingId = 'my-key-ring',
    string $keyId = 'my-key',
    string $versionId = '123'
) {
    // Create the Cloud KMS client.
    $client = new KeyManagementServiceClient();

    // Build the key version name.
    $keyVersionName = $client->cryptoKeyVersionName($projectId, $locationId, $keyRingId, $keyId, $versionId);

    // Call the API.
    $restoreCryptoKeyVersionRequest = (new RestoreCryptoKeyVersionRequest())
        ->setName($keyVersionName);
    $restoredVersion = $client->restoreCryptoKeyVersion($restoreCryptoKeyVersionRequest);
    printf('Restored key version: %s' . PHP_EOL, $restoredVersion->getName());

    return $restoredVersion;
}

Python

要运行此代码,请先设置 Python 开发环境安装 Cloud KMS Python SDK

from google.cloud import kms


def restore_key_version(
    project_id: str, location_id: str, key_ring_id: str, key_id: str, version_id: str
) -> kms.CryptoKeyVersion:
    """
    Restore a key version scheduled for destruction.

    Args:
        project_id (string): Google Cloud project ID (e.g. 'my-project').
        location_id (string): Cloud KMS location (e.g. 'us-east1').
        key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring').
        key_id (string): ID of the key to use (e.g. 'my-key').
        version_id (string): ID of the version to use (e.g. '1').

    Returns:
        CryptoKeyVersion: Restored Cloud KMS key version.

    """

    # Create the client.
    client = kms.KeyManagementServiceClient()

    # Build the key version name.
    key_version_name = client.crypto_key_version_path(
        project_id, location_id, key_ring_id, key_id, version_id
    )

    # Call the API.
    restored_version = client.restore_crypto_key_version(
        request={"name": key_version_name}
    )
    print(f"Restored key version: {restored_version.name}")
    return restored_version

Ruby

要运行此代码,请先设置 Ruby 开发环境安装 Cloud KMS Ruby SDK

# TODO(developer): uncomment these values before running the sample.
# project_id  = "my-project"
# location_id = "us-east1"
# key_ring_id = "my-key-ring"
# key_id      = "my-key"
# version_id  = "123"

# Require the library.
require "google/cloud/kms"

# Create the client.
client = Google::Cloud::Kms.key_management_service

# Build the key version name.
key_version_name = client.crypto_key_version_path project:            project_id,
                                                  location:           location_id,
                                                  key_ring:           key_ring_id,
                                                  crypto_key:         key_id,
                                                  crypto_key_version: version_id

# Call the API.
restored_version = client.restore_crypto_key_version name: key_version_name
puts "Restored key version: #{restored_version.name}"

API

这些示例使用 curl 作为 HTTP 客户端来演示如何使用 API。如需详细了解访问权限控制,请参阅访问 Cloud KMS API

通过调用 CryptoKeyVersions.restore 方法恢复密钥版本。

curl "https://cloudkms.googleapis.com/v1/projects/project-id/locations/location-id/keyRings/key-ring-id/cryptoKeys/crypto-key-id/cryptoKeyVersions/version-id:restore" \
    --request "POST" \
    --header "authorization: Bearer token"

恢复请求完成后,密钥版本的状态将变为“已停用”。您必须先启用密钥,然后才能使用它。

必需的 IAM 权限

如需销毁密钥版本,调用者需要拥有密钥、密钥环或项目、文件夹或组织的 cloudkms.cryptoKeyVersions.destroy IAM 权限。

要恢复密钥版本,调用者需要 cloudkms.cryptoKeyVersions.restore 权限。

这两种权限都授予了 Cloud KMS Admin 角色 (roles/cloudkms.admin)。

删除时间轴

Cloud KMS 承诺在安排的销毁时间后的 45 天内从所有 Google 基础架构中删除客户密钥材料。这包括从活跃系统和数据中心备份中移除数据。其他客户数据受标准 Google Cloud 删除时间表(180 天)的约束。