Как установить уровень защиты для связки ключей с помощью API Java KMS? - PullRequest
0 голосов
/ 31 октября 2019

Мне нужно установить ProtectionLevel в HSM для набора ключей для обоих случаев при создании и для существующего.

Я пытаюсь использовать этот же способ, чтобы установить этот параметр, как и любой другой параметр:

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

Как я могу сделать это с помощью этого API?

1 Ответ

0 голосов
/ 12 ноября 2019

Уровень защиты HSM не указан на уровне ключей.

При создании связки ключей (то есть с ключами HSM) нужно просто учитывать области, поддерживаемые HSM ProtectionLevel

Для связки ключейДля создания вам нужен только родитель (местоположение), keyring_id (имя) и объект keyRing, В документации приведен следующий пример для 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;
  }
}

И затем вы переходите кСоздайте свой ключ KMS, чтобы добавить уровень защиты HSM, вам потребуется создать новый шаблон версии CryptoKey и установить его в Crypto Key Builder. Это пример кода, который я уже попробовал и подтвердил, что он работает:

  /**
   * 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;
    }
  }

Зависимости, которые вам понадобятся:

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;
...