use of com.google.cloud.kms.v1.CryptoKeyVersion in project java-kms by googleapis.
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-kms by googleapis.
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-kms by googleapis.
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-kms by googleapis.
the class RestoreKeyVersion method restoreKeyVersion.
// Schedule destruction of the given key version.
public void restoreKeyVersion(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);
// Restore the key version.
CryptoKeyVersion response = client.restoreCryptoKeyVersion(keyVersionName);
System.out.printf("Restored key version: %s%n", response.getName());
}
}
use of com.google.cloud.kms.v1.CryptoKeyVersion in project gapic-generator-java by googleapis.
the class AsyncDestroyCryptoKeyVersion method asyncDestroyCryptoKeyVersion.
public static void asyncDestroyCryptoKeyVersion() throws Exception {
// It may require modifications to work in your environment.
try (KeyManagementServiceClient keyManagementServiceClient = KeyManagementServiceClient.create()) {
DestroyCryptoKeyVersionRequest request = DestroyCryptoKeyVersionRequest.newBuilder().setName(CryptoKeyVersionName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]", "[CRYPTO_KEY]", "[CRYPTO_KEY_VERSION]").toString()).build();
ApiFuture<CryptoKeyVersion> future = keyManagementServiceClient.destroyCryptoKeyVersionCallable().futureCall(request);
// Do something.
CryptoKeyVersion response = future.get();
}
}
Aggregations