简体   繁体   English

如何使用 java KMS API 设置密钥环的保护级别?

[英]How to set protection level for key ring using java KMS API?

I need to set ProtectionLevel to HSM for a key ring for both cases during creation and for an existing one.我需要将 ProtectionLevel 设置为 HSM,以便在创建期间和现有情况下为密钥环设置密钥环。

I am trying to use the same way to set this option as any other option:我正在尝试使用与任何其他选项相同的方式将此选项设置为:

CreateKeyRingRequest.newBuilder().//I see nothing to set ProtectionLevel here.

How can I do this using this API?如何使用此 API 执行此操作?

The HSM ProtectionLevel is not specified on the Key Ring level. HSM ProtectionLevel 未在密钥环级别上指定。

When creating a Key Ring (that is meant to have HSM keys) you just need to take in consideration the regions supported by the HSM ProtectionLevel创建密钥环(即具有 HSM 密钥)时,您只需要考虑HSM ProtectionLevel 支持的区域

For the Key Ring creation you just need a parent (location), keyring_id (the name), and the keyRing object, the documentation gives the following example for Java:对于密钥环创建,您只需要一个父项(位置)、keyring_id(名称)和密钥环 object, 文档为 Java 提供了以下示例:

/**
 * Creates a new key ring with the given id.
 */
public static KeyRing createKeyRing(String projectId, String locationId, String keyRingId)
    throws IOException {
  // Create the Cloud KMS client.
  try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {

    // The resource name of the location associated with the KeyRing.
    String parent = LocationName.format(projectId, locationId);

    // Create the KeyRing for your project.
    KeyRing keyRing = client.createKeyRing(parent, keyRingId, KeyRing.newBuilder().build());

    return keyRing;
  }
}

And then you proceed to create your KMS key, to add the HSM Protection Level you will need to create a new CryptoKey Version Template and set the template to the Crypto Key Builder.然后您继续创建您的 KMS 密钥,添加 HSM 保护级别,您需要创建一个新的 CryptoKey 版本模板并将该模板设置为 Crypto Key Builder。 This is a sample code which I have already tried and confirmed that it works:这是我已经尝试过并确认它有效的示例代码:

  /**
   * Creates a new crypto key with the given id.
   */
  public static CryptoKey createCryptoKey(String projectId, String locationId, String keyRingId,
      String cryptoKeyId)
      throws IOException {

    // Create the Cloud KMS client.
    try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
      // The resource name of the location associated with the KeyRing.
      String parent = KeyRingName.format(projectId, locationId, keyRingId);
      ProtectionLevel protectionLevel = ProtectionLevel.HSM;

      // creating the template with the right protection level
      CryptoKeyVersionTemplate template = CryptoKeyVersionTemplate.newBuilder()
            .setProtectionLevel(protectionLevel)
            .build();

      // This will allow the API access to the key for encryption and decryption and also the HSM PL.
      CryptoKey cryptoKey = CryptoKey.newBuilder()
          .setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT)
          .setVersionTemplate(template)
          .build();

      // Create the CryptoKey for your project.
      CryptoKey createdKey = client.createCryptoKey(parent, cryptoKeyId, cryptoKey);

      return createdKey;
    }
  }

The dependencies you will need:您将需要的依赖项:

import com.google.cloud.kms.v1.CryptoKey;
import com.google.cloud.kms.v1.CryptoKey.CryptoKeyPurpose;
import com.google.cloud.kms.v1.ProtectionLevel;
import com.google.cloud.kms.v1.CryptoKeyName;
import com.google.cloud.kms.v1.CryptoKeyVersionTemplate;
import com.google.cloud.kms.v1.KeyManagementServiceClient;
import com.google.cloud.kms.v1.LocationName;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android(使用Java)中,如何设置铃声音量(指定的数量)? - In Android (using Java), how can I set the ring volume (to a specified number)? 如何通过 gRPC 发送网络钓鱼防护 API 密钥? - How do I send Phishing Protection API key over gRPC? JAVA:如何使用密码保护将私钥保存在pem文件中 - JAVA: How to save a private key in a pem file with password protection 如何使用 Api Key In Java Language 设置 Google Recaptcha Enterprise 验证? - How to set Google Recaptcha Enterprise verification Using Api Key In Java Language? 使用 Java API TransmissionWithRecipientArray 对象,如何设置像键值数组(Sparkpost)这样的元素 - Using Java API TransmissionWithRecipientArray object, how can I set an element like a key value array ( Sparkpost ) java中的程序包级别保护有什么用? - What is the use of package level protection in java? 如何在Java中设置日志记录级别? - How to set logging level in java? 如何使用Java在Hibernate 3 + Oracle中设置查询级别超时 - how to set query level time out in hibernate 3 + oracle using java 如何使用 Cassandra Java 驱动程序为所有查询设置一致性级别? - How to set consistency level for all queries using Cassandra Java Driver? 如何在不使用Java的物理密钥环的情况下使用公共密钥OpenPGP(GPG) - How to use public key OpenPGP(GPG) without access to physical key ring in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM