轮替密钥

本页面介绍如何自动或手动轮替密钥。如需了解有关密钥轮替的一般信息,请参阅密钥轮替

所需的角色

如需获得轮替密钥所需的权限,请让您的管理员为您授予密钥的以下 IAM 角色:

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

这些预定义角色包含轮换密钥所需的权限。如需查看所需的确切权限,请展开所需权限部分:

所需权限

如需轮换密钥,您需要具备以下权限:

  • 更改主密钥版本: cloudkms.cryptoKeys.update
  • 更改或停用自动屏幕旋转: cloudkms.cryptoKeys.update
  • 创建新的密钥版本: cloudkms.cryptoKeyVersions.create
  • 停用旧密钥版本: cloudkms.cryptoKeyVersions.update
  • 重新加密数据:
    • cloudkms.cryptoKeyVersions.useToDecrypt
    • cloudkms.cryptoKeyVersions.useToEncrypt

您也可以使用自定义角色或其他预定义角色来获取这些权限。

如果单个用户拥有包含所有这些权限的自定义角色,则可以自行轮换密钥并重新加密数据。Cloud KMS 管理员角色和 Cloud KMS CryptoKey Encrypter/Decrypter 角色中的用户可以共同轮换密钥并重新加密数据。在分配角色时,请遵循最小权限原则。如需了解详情,请参阅权限和角色

轮替密钥时,使用先前的密钥版本加密的数据不会自动重新加密。如需了解详情,请参阅解密和重新加密。轮替密钥不会自动停用销毁任何现有密钥版本。销毁不再需要的密钥版本有助于降低费用。

配置自动轮替

使用自定义轮替计划创建新密钥

要在创建新密钥时配置自动轮替,请执行以下操作:

控制台

使用 Google Cloud 控制台创建密钥时,Cloud KMS 会自动设置轮替周期和下次轮替的时间。您可以选择使用默认值,也可以指定其他值。

如需在创建密钥期间,但在点击创建按钮之前指定其他轮替周期和开始时间,请执行以下操作:

  1. 对于密钥轮替周期,请选择一个选项。

  2. 对于开始于,请选择您希望首次自动轮换发生的日期。您可以将开始时间保留为默认值,以便在您创建密钥后经过一个密钥轮替周期时开始第一次自动轮替。

gcloud

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

gcloud kms keys create KEY_NAME \
    --keyring KEY_RING \
    --location LOCATION \
    --purpose "encryption" \
    --rotation-period ROTATION_PERIOD \
    --next-rotation-time NEXT_ROTATION_TIME

替换以下内容:

  • KEY_NAME:密钥的名称。
  • KEY_RING:包含密钥的密钥环的名称。
  • LOCATION:密钥环的 Cloud KMS 位置。
  • ROTATION_PERIOD:轮替密钥的时间间隔,例如 30d 表示每 30 天轮替一次密钥。轮替周期必须至少为 1 天,最长为 100 年。如需了解详情,请参阅 CryptoKey.rotationPeriod
  • NEXT_ROTATION_TIME:完成首次轮换的时间戳,例如 2023-01-01T01:02:03。您可以省略 --next-rotation-time,以将首次轮替安排在运行命令后的一个轮替周期。如需了解详情,请参阅 CryptoKey.nextRotationTime

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

C#

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


using Google.Cloud.Kms.V1;
using Google.Protobuf.WellKnownTypes;
using System;

public class CreateKeyRotationScheduleSample
{
    public CryptoKey CreateKeyRotationSchedule(
      string projectId = "my-project", string locationId = "us-east1", string keyRingId = "my-key-ring",
      string id = "my-key-with-rotation-schedule")
    {
        // Create the client.
        KeyManagementServiceClient client = KeyManagementServiceClient.Create();

        // Build the parent key ring name.
        KeyRingName keyRingName = new KeyRingName(projectId, locationId, keyRingId);

        // Build the key.
        CryptoKey key = new CryptoKey
        {
            Purpose = CryptoKey.Types.CryptoKeyPurpose.EncryptDecrypt,
            VersionTemplate = new CryptoKeyVersionTemplate
            {
                Algorithm = CryptoKeyVersion.Types.CryptoKeyVersionAlgorithm.GoogleSymmetricEncryption,
            },

            // Rotate the key every 30 days.
            RotationPeriod = new Duration
            {
                Seconds = 60 * 60 * 24 * 30, // 30 days
            },

            // Start the first rotation in 24 hours.
            NextRotationTime = new Timestamp
            {
                Seconds = new DateTimeOffset(DateTime.UtcNow.AddHours(24)).ToUnixTimeSeconds(),
            }
        };

        // Call the API.
        CryptoKey result = client.CreateCryptoKey(keyRingName, id, key);

        // Return the result.
        return result;
    }
}

Go

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

import (
	"context"
	"fmt"
	"io"
	"time"

	kms "cloud.google.com/go/kms/apiv1"
	"cloud.google.com/go/kms/apiv1/kmspb"
	"google.golang.org/protobuf/types/known/durationpb"
	"google.golang.org/protobuf/types/known/timestamppb"
)

// createKeyRotationSchedule creates a key with a rotation schedule.
func createKeyRotationSchedule(w io.Writer, parent, id string) error {
	// name := "projects/my-project/locations/us-east1/keyRings/my-key-ring"
	// id := "my-key-with-rotation-schedule"

	// Create the client.
	ctx := context.Background()
	client, err := kms.NewKeyManagementClient(ctx)
	if err