use of com.google.cloud.kms.v1.CryptoKeyVersion in project java-docs-samples by GoogleCloudPlatform.
the class SnippetsIT method afterAll.
@AfterClass
public static void afterAll() throws IOException {
Assert.assertFalse("missing GOOGLE_CLOUD_PROJECT", Strings.isNullOrEmpty(PROJECT_ID));
// Iterate over each key ring's key's crypto key versions and destroy.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
for (CryptoKey key : client.listCryptoKeys(getKeyRingName()).iterateAll()) {
if (key.hasRotationPeriod() || key.hasNextRotationTime()) {
CryptoKey keyWithoutRotation = CryptoKey.newBuilder().setName(key.getName()).build();
FieldMask fieldMask = FieldMaskUtil.fromString("rotation_period,next_rotation_time");
client.updateCryptoKey(keyWithoutRotation, fieldMask);
}
ListCryptoKeyVersionsRequest listVersionsRequest = ListCryptoKeyVersionsRequest.newBuilder().setParent(key.getName()).setFilter("state != DESTROYED AND state != DESTROY_SCHEDULED").build();
for (CryptoKeyVersion version : client.listCryptoKeyVersions(listVersionsRequest).iterateAll()) {
client.destroyCryptoKeyVersion(version.getName());
}
}
}
}
use of com.google.cloud.kms.v1.CryptoKeyVersion in project java-docs-samples by GoogleCloudPlatform.
the class SnippetsIT method testDisableEnableKeyVersion.
@Test
public void testDisableEnableKeyVersion() throws Exception {
CryptoKeyVersion keyVersion = createKeyVersion(ASYMMETRIC_DECRYPT_KEY_ID);
String name = keyVersion.getName();
String keyVersionId = name.substring(name.lastIndexOf('/') + 1);
new DisableKeyVersion().disableKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_DECRYPT_KEY_ID, keyVersionId);
assertThat(stdOut.toString()).contains("Disabled key version");
new EnableKeyVersion().enableKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_DECRYPT_KEY_ID, keyVersionId);
assertThat(stdOut.toString()).contains("Enabled key version");
}
use of com.google.cloud.kms.v1.CryptoKeyVersion in project java-docs-samples by GoogleCloudPlatform.
the class CreateKeyVersion method createKeyVersion.
// Create a new key version.
public void createKeyVersion(String projectId, String locationId, String keyRingId, String keyId) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the parent name from the project, location, and key ring.
CryptoKeyName cryptoKeyName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);
// Build the key version to create.
CryptoKeyVersion keyVersion = CryptoKeyVersion.newBuilder().build();
// Create the key.
CryptoKeyVersion createdVersion = client.createCryptoKeyVersion(cryptoKeyName, keyVersion);
System.out.printf("Created key version %s%n", createdVersion.getName());
}
}
use of com.google.cloud.kms.v1.CryptoKeyVersion in project java-docs-samples by GoogleCloudPlatform.
the class DisableKeyVersion method disableKeyVersion.
// Disable a key version from use.
public void disableKeyVersion(String projectId, String locationId, String keyRingId, String keyId, String keyVersionId) throws IOException {
// safely clean up any remaining background resources.
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
// Build the key version name from the project, location, key ring, key,
// and key version.
CryptoKeyVersionName keyVersionName = CryptoKeyVersionName.of(projectId, locationId, keyRingId, keyId, keyVersionId);
// Build the updated key version, setting it to disbaled.
CryptoKeyVersion keyVersion = CryptoKeyVersion.newBuilder().setName(keyVersionName.toString()).setState(CryptoKeyVersionState.DISABLED).build();
// Create a field mask of updated values.
FieldMask fieldMask = FieldMaskUtil.fromString("state");
// Destroy the key version.
CryptoKeyVersion response = client.updateCryptoKeyVersion(keyVersion, fieldMask);
System.out.printf("Disabled key version: %s%n", response.getName());
}
}
use of com.google.cloud.kms.v1.CryptoKeyVersion in project java-kms by googleapis.
the class KeyManagementServiceClientTest method listCryptoKeyVersionsTest.
@Test
public void listCryptoKeyVersionsTest() throws Exception {
CryptoKeyVersion responsesElement = CryptoKeyVersion.newBuilder().build();
ListCryptoKeyVersionsResponse expectedResponse = ListCryptoKeyVersionsResponse.newBuilder().setNextPageToken("").addAllCryptoKeyVersions(Arrays.asList(responsesElement)).build();
mockKeyManagementService.addResponse(expectedResponse);
CryptoKeyName parent = CryptoKeyName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]");
ListCryptoKeyVersionsPagedResponse pagedListResponse = client.listCryptoKeyVersions(parent);
List<CryptoKeyVersion> resources = Lists.newArrayList(pagedListResponse.iterateAll());
Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getCryptoKeyVersionsList().get(0), resources.get(0));
List<AbstractMessage> actualRequests = mockKeyManagementService.getRequests();
Assert.assertEquals(1, actualRequests.size());
ListCryptoKeyVersionsRequest actualRequest = ((ListCryptoKeyVersionsRequest) actualRequests.get(0));
Assert.assertEquals(parent.toString(), actualRequest.getParent());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Aggregations