Search in sources :

Example 11 with CryptoKeyVersion

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");
    }
}
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 12 with CryptoKeyVersion

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

Example 13 with CryptoKeyVersion

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

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

Example 15 with CryptoKeyVersion

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