use of com.google.cloud.kms.v1.KeyRingName in project java-docs-samples by GoogleCloudPlatform.
the class IamAddMember method iamAddMember.
// Add the given IAM member to the key.
public void iamAddMember(String projectId, String locationId, String keyRingId, String keyId, String member) 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.
CryptoKeyName resourceName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);
// The resource name could also be a key ring.
// KeyRingName resourceName = KeyRingName.of(projectId, locationId, keyRingId);
// Get the current policy.
Policy policy = client.getIamPolicy(resourceName);
// Create a new IAM binding for the member and role.
Binding binding = Binding.newBuilder().setRole("roles/cloudkms.cryptoKeyEncrypterDecrypter").addMembers(member).build();
// Add the binding to the policy.
Policy newPolicy = policy.toBuilder().addBindings(binding).build();
client.setIamPolicy(resourceName, newPolicy);
System.out.printf("Updated IAM policy for %s%n", resourceName.toString());
}
}
use of com.google.cloud.kms.v1.KeyRingName in project java-docs-samples by GoogleCloudPlatform.
the class IamGetPolicy method iamGetPolicy.
// Get the IAM policy for the given key.
public void iamGetPolicy(String projectId, String locationId, String keyRingId, String keyId) 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.
CryptoKeyName resourceName = CryptoKeyName.of(projectId, locationId, keyRingId, keyId);
// The resource name could also be a key ring.
// KeyRingName resourceName = KeyRingName.of(projectId, locationId, keyRingId);
// Get the current policy.
Policy policy = client.getIamPolicy(resourceName);
// Print the policy.
System.out.printf("IAM policy:%n");
for (Binding binding : policy.getBindingsList()) {
System.out.printf("%s%n", binding.getRole());
for (String member : binding.getMembersList()) {
System.out.printf("- %s%n", member);
}
}
}
}
use of com.google.cloud.kms.v1.KeyRingName in project java-docs-samples by GoogleCloudPlatform.
the class CreateKeyAsymmetricDecrypt method createKeyAsymmetricDecrypt.
// Create a new asymmetric key for the purpose of encrypting and decrypting
// data.
public void createKeyAsymmetricDecrypt(String projectId, String locationId, String keyRingId, String id) 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.
KeyRingName keyRingName = KeyRingName.of(projectId, locationId, keyRingId);
// Build the asymmetric key to create.
CryptoKey key = CryptoKey.newBuilder().setPurpose(CryptoKeyPurpose.ASYMMETRIC_DECRYPT).setVersionTemplate(CryptoKeyVersionTemplate.newBuilder().setAlgorithm(CryptoKeyVersionAlgorithm.RSA_DECRYPT_OAEP_2048_SHA256)).build();
// Create the key.
CryptoKey createdKey = client.createCryptoKey(keyRingName, id, key);
System.out.printf("Created asymmetric key %s%n", createdKey.getName());
}
}
use of com.google.cloud.kms.v1.KeyRingName in project java-docs-samples by GoogleCloudPlatform.
the class CreateKeyHsm method createKeyHsm.
// Create a new key that is stored in an HSM.
public void createKeyHsm(String projectId, String locationId, String keyRingId, String id) 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.
KeyRingName keyRingName = KeyRingName.of(projectId, locationId, keyRingId);
// Build the hsm key to create.
CryptoKey key = CryptoKey.newBuilder().setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT).setVersionTemplate(CryptoKeyVersionTemplate.newBuilder().setProtectionLevel(ProtectionLevel.HSM).setAlgorithm(CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION)).build();
// Create the key.
CryptoKey createdKey = client.createCryptoKey(keyRingName, id, key);
System.out.printf("Created hsm key %s%n", createdKey.getName());
}
}
use of com.google.cloud.kms.v1.KeyRingName in project java-kms by googleapis.
the class KeyManagementServiceClientTest method listImportJobsTest.
@Test
public void listImportJobsTest() throws Exception {
ImportJob responsesElement = ImportJob.newBuilder().build();
ListImportJobsResponse expectedResponse = ListImportJobsResponse.newBuilder().setNextPageToken("").addAllImportJobs(Arrays.asList(responsesElement)).build();
mockKeyManagementService.addResponse(expectedResponse);
KeyRingName parent = KeyRingName.of("[PROJECT]", "[LOCATION]", "[KEY_RING]");
ListImportJobsPagedResponse pagedListResponse = client.listImportJobs(parent);
List<ImportJob> resources = Lists.newArrayList(pagedListResponse.iterateAll());
Assert.assertEquals(1, resources.size());
Assert.assertEquals(expectedResponse.getImportJobsList().get(0), resources.get(0));
List<AbstractMessage> actualRequests = mockKeyManagementService.getRequests();
Assert.assertEquals(1, actualRequests.size());
ListImportJobsRequest actualRequest = ((ListImportJobsRequest) actualRequests.get(0));
Assert.assertEquals(parent.toString(), actualRequest.getParent());
Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Aggregations