Cloud HSM

本文档概述了 Cloud HSM,并介绍了如何在 Cloud Key Management Service 中创建和使用 HSM 保护的加密密钥。

什么是 Cloud HSM?

Cloud HSM 是一种云托管的硬件安全模块 (HSM) 服务,可让您在 FIPS 140-2 Level 3 认证的 HSM 集群中托管加密密钥并执行加密操作。Google 会为您管理 HSM 集群,因此您无需担心创建集群、伸缩或修补事宜。由于 Cloud HSM 使用 Cloud KMS 作为其前端,因此您可以利用 Cloud KMS 提供的所有便利和功能。

创建密钥环

创建密钥时,您将其添加到给定 Google Cloud位置中的密钥环。您可以创建新的密钥环,也可以使用现有的密钥环。在本主题中,您将创建一个新的密钥环,并向其添加新密钥。

在支持 Cloud HSM 的 Google Cloud 位置创建密钥环。

控制台

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

    前往“密钥管理”

  2. 点击创建密钥环

  3. 对于密钥环名称,请输入密钥环的名称。

  4. 对于密钥环位置,选择一个位置,例如 "us-east1"

  5. 点击创建

gcloud

  1. 在 Google Cloud 控制台中,激活 Cloud Shell。

    激活 Cloud Shell

  2. 在您的环境中,运行 gcloud kms keyrings create 命令:

    gcloud kms keyrings create KEY_RING \
        --location LOCATION
    

    替换以下内容:

    • KEY_RING:包含密钥的密钥环的名称。
    • LOCATION:密钥环的 Cloud KMS 位置。

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

C#

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


using Google.Api.Gax.ResourceNames;
using Google.Cloud.Kms.V1;

public class CreateKeyRingSample
{
    public KeyRing CreateKeyRing(
      string projectId = "my-project", string locationId = "us-east1",
      string id = "my-key-ring")
    {
        // Create the client.
        KeyManagementServiceClient client = KeyManagementServiceClient.Create();

        // Build the parent location name.
        LocationName locationName = new LocationName(projectId, locationId);

        // Build the key ring.
        KeyRing keyRing = new KeyRing { };

        // Call the API.
        KeyRing result = client.CreateKeyRing(locationName, id, keyRing);

        // 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"
)

// createKeyRing creates a new ring to store keys on KMS.
func createKeyRing(w io.Writer, parent, id string) error {
	// parent := "projects/PROJECT_ID/locations/global"
	// id := "my-key-ring"

	// 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.CreateKeyRingRequest{
		Parent:    parent,
		KeyRingId: id,
	}

	// Call the API.
	result, err := client.CreateKeyRing(ctx, req)
	if err != nil {
		return fmt.Errorf("failed to create key ring: %w", err)
	}
	fmt.Fprintf(w, "Created key ring: %s\n", result.Name)
	return nil
}

Java

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

import com.google.cloud.kms.v1.KeyManagementServiceClient;
import com.google.cloud.kms.v1.KeyRing;
import com.google.cloud.kms.v1.LocationName;
import java.io.IOException;

public class CreateKeyRing {

  public void createKeyRing() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String locationId = "us-east1";
    String id = "my-asymmetric-signing-key";
    createKeyRing(projectId, locationId, id);
  }

  // Create a new key ring.
  public void createKeyRing(String projectId, String locationId, String id) 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 parent name from the project and location.
      LocationName locationName = LocationName.of(projectId, locationId);

      // Build the key ring to create.
      KeyRing keyRing = KeyRing.newBuilder().build();

      // Create the key ring.
      KeyRing createdKeyRing = client.createKeyRing(locationName,