/** * * * <pre> * The list of [CryptoKeys][google.cloud.kms.v1.CryptoKey]. * </pre> * * <code>repeated .google.cloud.kms.v1.CryptoKey crypto_keys = 1;</code> */ public Builder addCryptoKeys(com.google.cloud.kms.v1.CryptoKey.Builder builderForValue) { if (cryptoKeysBuilder_ == null) { ensureCryptoKeysIsMutable(); cryptoKeys_.add(builderForValue.build()); onChanged(); } else { cryptoKeysBuilder_.addMessage(builderForValue.build()); } return this; } /**
/** * * * <pre> * The list of [CryptoKeys][google.cloud.kms.v1.CryptoKey]. * </pre> * * <code>repeated .google.cloud.kms.v1.CryptoKey crypto_keys = 1;</code> */ public Builder addCryptoKeys( int index, com.google.cloud.kms.v1.CryptoKey.Builder builderForValue) { if (cryptoKeysBuilder_ == null) { ensureCryptoKeysIsMutable(); cryptoKeys_.add(index, builderForValue.build()); onChanged(); } else { cryptoKeysBuilder_.addMessage(index, builderForValue.build()); } return this; } /**
/** * * * <pre> * The list of [CryptoKeys][google.cloud.kms.v1.CryptoKey]. * </pre> * * <code>repeated .google.cloud.kms.v1.CryptoKey crypto_keys = 1;</code> */ public Builder setCryptoKeys( int index, com.google.cloud.kms.v1.CryptoKey.Builder builderForValue) { if (cryptoKeysBuilder_ == null) { ensureCryptoKeysIsMutable(); cryptoKeys_.set(index, builderForValue.build()); onChanged(); } else { cryptoKeysBuilder_.setMessage(index, builderForValue.build()); } return this; } /**
/** * * * <pre> * The list of [CryptoKeys][google.cloud.kms.v1.CryptoKey]. * </pre> * * <code>repeated .google.cloud.kms.v1.CryptoKey crypto_keys = 1;</code> */ public Builder addCryptoKeys( int index, com.google.cloud.kms.v1.CryptoKey.Builder builderForValue) { if (cryptoKeysBuilder_ == null) { ensureCryptoKeysIsMutable(); cryptoKeys_.add(index, builderForValue.build()); onChanged(); } else { cryptoKeysBuilder_.addMessage(index, builderForValue.build()); } return this; } /**
/** * * * <pre> * The list of [CryptoKeys][google.cloud.kms.v1.CryptoKey]. * </pre> * * <code>repeated .google.cloud.kms.v1.CryptoKey crypto_keys = 1;</code> */ public Builder setCryptoKeys( int index, com.google.cloud.kms.v1.CryptoKey.Builder builderForValue) { if (cryptoKeysBuilder_ == null) { ensureCryptoKeysIsMutable(); cryptoKeys_.set(index, builderForValue.build()); onChanged(); } else { cryptoKeysBuilder_.setMessage(index, builderForValue.build()); } return this; } /**
/** * * * <pre> * The list of [CryptoKeys][google.cloud.kms.v1.CryptoKey]. * </pre> * * <code>repeated .google.cloud.kms.v1.CryptoKey crypto_keys = 1;</code> */ public Builder addCryptoKeys(com.google.cloud.kms.v1.CryptoKey.Builder builderForValue) { if (cryptoKeysBuilder_ == null) { ensureCryptoKeysIsMutable(); cryptoKeys_.add(builderForValue.build()); onChanged(); } else { cryptoKeysBuilder_.addMessage(builderForValue.build()); } return this; } /**
/** * * * <pre> * [CryptoKey][google.cloud.kms.v1.CryptoKey] with updated values. * </pre> * * <code>.google.cloud.kms.v1.CryptoKey crypto_key = 1;</code> */ public Builder setCryptoKey(com.google.cloud.kms.v1.CryptoKey.Builder builderForValue) { if (cryptoKeyBuilder_ == null) { cryptoKey_ = builderForValue.build(); onChanged(); } else { cryptoKeyBuilder_.setMessage(builderForValue.build()); } return this; } /**
/** * * * <pre> * A [CryptoKey][google.cloud.kms.v1.CryptoKey] with initial field values. * </pre> * * <code>.google.cloud.kms.v1.CryptoKey crypto_key = 3;</code> */ public Builder setCryptoKey(com.google.cloud.kms.v1.CryptoKey.Builder builderForValue) { if (cryptoKeyBuilder_ == null) { cryptoKey_ = builderForValue.build(); onChanged(); } else { cryptoKeyBuilder_.setMessage(builderForValue.build()); } return this; } /**
/** * Creates an RSA encrypt/decrypt key pair with the given id. */ public static CryptoKey createAsymmetricKey(String projectId, String locationId, String keyRingId, String cryptoKeyId) throws IOException { try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) { String parent = KeyRingName.format(projectId, locationId, keyRingId); CryptoKeyPurpose purpose = CryptoKeyPurpose.ASYMMETRIC_DECRYPT; CryptoKeyVersionAlgorithm algorithm = CryptoKeyVersionAlgorithm.RSA_DECRYPT_OAEP_2048_SHA256; CryptoKeyVersionTemplate version = CryptoKeyVersionTemplate.newBuilder() .setAlgorithm(algorithm) .build(); CryptoKey cryptoKey = CryptoKey.newBuilder() .setPurpose(purpose) .setVersionTemplate(version) .build(); CryptoKey createdKey = client.createCryptoKey(parent, cryptoKeyId, cryptoKey); return createdKey; } } // [END kms_create_asymmetric_key]
/** * 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); // This will allow the API access to the key for encryption and decryption. CryptoKey cryptoKey = CryptoKey.newBuilder() .setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT) .build(); // Create the CryptoKey for your project. CryptoKey createdKey = client.createCryptoKey(parent, cryptoKeyId, cryptoKey); return createdKey; } } // [END kms_create_cryptokey]
String kmsKeyRingResourcePath = KeyRingName.of(projectId, location, keyRingName).toString(); CryptoKey cryptoKey = CryptoKey.newBuilder().setPurpose(CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT).build(); CreateCryptoKeyRequest createCryptoKeyRequest = CreateCryptoKeyRequest.newBuilder()
@Test @SuppressWarnings("all") public void listCryptoKeysTest() { String nextPageToken = ""; int totalSize = 705419236; CryptoKey cryptoKeysElement = CryptoKey.newBuilder().build(); List<CryptoKey> cryptoKeys = Arrays.asList(cryptoKeysElement); ListCryptoKeysResponse expectedResponse = ListCryptoKeysResponse.newBuilder() .setNextPageToken(nextPageToken) .setTotalSize(totalSize) .addAllCryptoKeys(cryptoKeys) .build(); mockKeyManagementService.addResponse(expectedResponse); KeyRingName parent = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); ListCryptoKeysPagedResponse pagedListResponse = client.listCryptoKeys(parent); List<CryptoKey> resources = Lists.newArrayList(pagedListResponse.iterateAll()); Assert.assertEquals(1, resources.size()); Assert.assertEquals(expectedResponse.getCryptoKeysList().get(0), resources.get(0)); List<GeneratedMessageV3> actualRequests = mockKeyManagementService.getRequests(); Assert.assertEquals(1, actualRequests.size()); ListCryptoKeysRequest actualRequest = (ListCryptoKeysRequest) actualRequests.get(0); Assert.assertEquals(parent, KeyRingName.parse(actualRequest.getParent())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
public void createCryptoKeyTest() { CryptoKeyName name = CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]"); CryptoKey expectedResponse = CryptoKey.newBuilder().setName(name.toString()).build(); mockKeyManagementService.addResponse(expectedResponse); .setNextRotationTime(nextRotationTime) .setRotationPeriod(rotationPeriod) .build();
@Test @SuppressWarnings("all") public void createCryptoKeyExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockKeyManagementService.addException(exception); try { KeyRingName parent = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]"); String cryptoKeyId = "my-app-key"; CryptoKey.CryptoKeyPurpose purpose = CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT; long seconds = 2147483647L; Timestamp nextRotationTime = Timestamp.newBuilder().setSeconds(seconds).build(); long seconds2 = 604800L; Duration rotationPeriod = Duration.newBuilder().setSeconds(seconds2).build(); CryptoKey cryptoKey = CryptoKey.newBuilder() .setPurpose(purpose) .setNextRotationTime(nextRotationTime) .setRotationPeriod(rotationPeriod) .build(); client.createCryptoKey(parent, cryptoKeyId, cryptoKey); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }
@Test @SuppressWarnings("all") public void updateCryptoKeyPrimaryVersionTest() { CryptoKeyName name2 = CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]"); CryptoKey expectedResponse = CryptoKey.newBuilder().setName(name2.toString()).build(); mockKeyManagementService.addResponse(expectedResponse); CryptoKeyName name = CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]"); String cryptoKeyVersionId = "cryptoKeyVersionId729489152"; CryptoKey actualResponse = client.updateCryptoKeyPrimaryVersion(name, cryptoKeyVersionId); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockKeyManagementService.getRequests(); Assert.assertEquals(1, actualRequests.size()); UpdateCryptoKeyPrimaryVersionRequest actualRequest = (UpdateCryptoKeyPrimaryVersionRequest) actualRequests.get(0); Assert.assertEquals(name, CryptoKeyName.parse(actualRequest.getName())); Assert.assertEquals(cryptoKeyVersionId, actualRequest.getCryptoKeyVersionId()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
@Test @SuppressWarnings("all") public void updateCryptoKeyTest() { CryptoKeyName name = CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]"); CryptoKey expectedResponse = CryptoKey.newBuilder().setName(name.toString()).build(); mockKeyManagementService.addResponse(expectedResponse); CryptoKey cryptoKey = CryptoKey.newBuilder().build(); FieldMask updateMask = FieldMask.newBuilder().build(); CryptoKey actualResponse = client.updateCryptoKey(cryptoKey, updateMask); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockKeyManagementService.getRequests(); Assert.assertEquals(1, actualRequests.size()); UpdateCryptoKeyRequest actualRequest = (UpdateCryptoKeyRequest) actualRequests.get(0); Assert.assertEquals(cryptoKey, actualRequest.getCryptoKey()); Assert.assertEquals(updateMask, actualRequest.getUpdateMask()); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
/** * * * <pre> * A [CryptoKey][google.cloud.kms.v1.CryptoKey] with initial field values. * </pre> * * <code>.google.cloud.kms.v1.CryptoKey crypto_key = 3;</code> */ public Builder setCryptoKey(com.google.cloud.kms.v1.CryptoKey.Builder builderForValue) { if (cryptoKeyBuilder_ == null) { cryptoKey_ = builderForValue.build(); onChanged(); } else { cryptoKeyBuilder_.setMessage(builderForValue.build()); } return this; } /**
@Test @SuppressWarnings("all") public void getCryptoKeyTest() { CryptoKeyName name2 = CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]"); CryptoKey expectedResponse = CryptoKey.newBuilder().setName(name2.toString()).build(); mockKeyManagementService.addResponse(expectedResponse); CryptoKeyName name = CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]"); CryptoKey actualResponse = client.getCryptoKey(name); Assert.assertEquals(expectedResponse, actualResponse); List<GeneratedMessageV3> actualRequests = mockKeyManagementService.getRequests(); Assert.assertEquals(1, actualRequests.size()); GetCryptoKeyRequest actualRequest = (GetCryptoKeyRequest) actualRequests.get(0); Assert.assertEquals(name, CryptoKeyName.parse(actualRequest.getName())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern())); }
/** * * * <pre> * [CryptoKey][google.cloud.kms.v1.CryptoKey] with updated values. * </pre> * * <code>.google.cloud.kms.v1.CryptoKey crypto_key = 1;</code> */ public Builder setCryptoKey(com.google.cloud.kms.v1.CryptoKey.Builder builderForValue) { if (cryptoKeyBuilder_ == null) { cryptoKey_ = builderForValue.build(); onChanged(); } else { cryptoKeyBuilder_.setMessage(builderForValue.build()); } return this; } /**
@Test @SuppressWarnings("all") public void updateCryptoKeyExceptionTest() throws Exception { StatusRuntimeException exception = new StatusRuntimeException(Status.INVALID_ARGUMENT); mockKeyManagementService.addException(exception); try { CryptoKey cryptoKey = CryptoKey.newBuilder().build(); FieldMask updateMask = FieldMask.newBuilder().build(); client.updateCryptoKey(cryptoKey, updateMask); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception } }