Search in sources :

Example 21 with CryptoKeyVersion

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());
            }
        }
    }
}
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 22 with CryptoKeyVersion

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

Example 23 with CryptoKeyVersion

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

Example 24 with CryptoKeyVersion

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

Example 25 with CryptoKeyVersion

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

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