Search in sources :

Example 26 with CryptoKeyVersion

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");
    }
}
Also used : CryptoKeyName(com.google.cloud.kms.v1.CryptoKeyName) CryptoKeyVersion(com.google.cloud.kms.v1.CryptoKeyVersion) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient) TimeoutException(java.util.concurrent.TimeoutException)

Example 27 with CryptoKeyVersion

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());
            }
        }
    }
}
Also used : ListCryptoKeyVersionsRequest(com.google.cloud.kms.v1.ListCryptoKeyVersionsRequest) CryptoKey(com.google.cloud.kms.v1.CryptoKey) CryptoKeyVersion(com.google.cloud.kms.v1.CryptoKeyVersion) FieldMask(com.google.protobuf.FieldMask) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient) AfterClass(org.junit.AfterClass)

Example 28 with CryptoKeyVersion

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");
}
Also used : CryptoKeyVersion(com.google.cloud.kms.v1.CryptoKeyVersion) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 29 with CryptoKeyVersion

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());
    }
}
Also used : CryptoKeyVersionName(com.google.cloud.kms.v1.CryptoKeyVersionName) CryptoKeyVersion(com.google.cloud.kms.v1.CryptoKeyVersion) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Example 30 with CryptoKeyVersion

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();
    }
}
Also used : CryptoKeyVersion(com.google.cloud.kms.v1.CryptoKeyVersion) DestroyCryptoKeyVersionRequest(com.google.cloud.kms.v1.DestroyCryptoKeyVersionRequest) KeyManagementServiceClient(com.google.cloud.kms.v1.KeyManagementServiceClient)

Aggregations

CryptoKeyVersion (com.google.cloud.kms.v1.CryptoKeyVersion)43 KeyManagementServiceClient (com.google.cloud.kms.v1.KeyManagementServiceClient)39 CryptoKeyVersionName (com.google.cloud.kms.v1.CryptoKeyVersionName)13 Test (org.junit.Test)8 FieldMask (com.google.protobuf.FieldMask)7 ByteString (com.google.protobuf.ByteString)6 CryptoKeyName (com.google.cloud.kms.v1.CryptoKeyName)5 ListCryptoKeyVersionsPagedResponse (com.google.cloud.kms.v1.KeyManagementServiceClient.ListCryptoKeyVersionsPagedResponse)4 ListCryptoKeyVersionsRequest (com.google.cloud.kms.v1.ListCryptoKeyVersionsRequest)4 AbstractMessage (com.google.protobuf.AbstractMessage)4 CreateCryptoKeyVersionRequest (com.google.cloud.kms.v1.CreateCryptoKeyVersionRequest)2 CryptoKey (com.google.cloud.kms.v1.CryptoKey)2 DestroyCryptoKeyVersionRequest (com.google.cloud.kms.v1.DestroyCryptoKeyVersionRequest)2 GetCryptoKeyVersionRequest (com.google.cloud.kms.v1.GetCryptoKeyVersionRequest)2 ImportCryptoKeyVersionRequest (com.google.cloud.kms.v1.ImportCryptoKeyVersionRequest)2 KeyOperationAttestation (com.google.cloud.kms.v1.KeyOperationAttestation)2 RestoreCryptoKeyVersionRequest (com.google.cloud.kms.v1.RestoreCryptoKeyVersionRequest)2 UpdateCryptoKeyVersionRequest (com.google.cloud.kms.v1.UpdateCryptoKeyVersionRequest)2 TimeoutException (java.util.concurrent.TimeoutException)2 AfterClass (org.junit.AfterClass)2