Уровень защиты 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;