use of com.google.cloud.kms.v1.CryptoKeyVersion in project java-docs-samples by GoogleCloudPlatform.
the class SnippetsIT method createKeyVersion.
private static CryptoKeyVersion createKeyVersion(String keyId) throws IOException, InterruptedException, TimeoutException {
try (KeyManagementServiceClient client = KeyManagementServiceClient.create()) {
CryptoKeyName keyName = CryptoKeyName.of(PROJECT_ID, LOCATION_ID, KEY_RING_ID, keyId);
CryptoKeyVersion keyVersion = CryptoKeyVersion.newBuilder().build();
CryptoKeyVersion createdVersion = client.createCryptoKeyVersion(keyName, keyVersion);
for (int i = 1; i <= 5; i++) {
CryptoKeyVersion gotVersion = client.getCryptoKeyVersion(createdVersion.getName());
if (gotVersion.getState() == CryptoKeyVersionState.ENABLED) {
return gotVersion;
}
Thread.sleep(500 * i);
}
throw new TimeoutException("key version not ready in timeout");
}
}
use of com.google.cloud.kms.v1.CryptoKeyVersion in project java-docs-samples by GoogleCloudPlatform.
the class DestroyKeyVersion method destroyKeyVersion.
// Schedule destruction of the given key version.
public void destroyKeyVersion(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);
// Destroy the key version.
CryptoKeyVersion response = client.destroyCryptoKeyVersion(keyVersionName);
System.out.printf("Destroyed key version: %s%n", response.getName());
}
}
use of com.google.cloud.kms.v1.CryptoKeyVersion in project java-docs-samples by GoogleCloudPlatform.
the class EnableKeyVersion method enableKeyVersion.
// Enable a disabled key version to be used again.
public void enableKeyVersion(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 enabled.
CryptoKeyVersion keyVersion = CryptoKeyVersion.newBuilder().setName(keyVersionName.toString()).setState(CryptoKeyVersionState.ENABLED).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("Enabled key version: %s%n", response.getName());
}
}
use of com.google.cloud.kms.v1.CryptoKeyVersion in project java-kms by googleapis.
the class KeyManagementServiceClientTest method listCryptoKeyVersionsTest2.
@Test
public void listCryptoKeyVersionsTest2() throws Exception {
CryptoKeyVersion responsesElement = CryptoKeyVersion.newBuilder().build();
ListCryptoKeyVersionsResponse expectedResponse = ListCryptoKeyVersionsResponse.newBuilder().setNextPageToken("").addAllCryptoKeyVersions(Arrays.asList(responsesElement)).build();
mockKeyManagementService.addResponse(expectedResponse);
String parent = "parent-995424086";
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, actualRequest.getParent());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
use of com.google.cloud.kms.v1.CryptoKeyVersion in project java-kms by googleapis.
the class SnippetsIT method testDestroyRestoreKeyVersion.
@Test
public void testDestroyRestoreKeyVersion() throws IOException, InterruptedException, TimeoutException {
CryptoKeyVersion keyVersion = createKeyVersion(ASYMMETRIC_DECRYPT_KEY_ID);
String name = keyVersion.getName();
String keyVersionId = name.substring(name.lastIndexOf('/') + 1);
new DestroyKeyVersion().destroyKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_DECRYPT_KEY_ID, keyVersionId);
assertThat(stdOut.toString()).contains("Destroyed key version");
new RestoreKeyVersion().restoreKeyVersion(PROJECT_ID, LOCATION_ID, KEY_RING_ID, ASYMMETRIC_DECRYPT_KEY_ID, keyVersionId);
assertThat(stdOut.toString()).contains("Restored key version");
}
Aggregations