use of com.google.cloud.kms.v1.KeyRingName in project java-docs-samples by GoogleCloudPlatform.
the class CreateKeySymmetricEncryptDecrypt method createKeySymmetricEncryptDecrypt.
// Create a new key that is used for symmetric encryption and decryption.
public void createKeySymmetricEncryptDecrypt(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 symmetric key to create.
CryptoKey key = CryptoKey.newBuilder().setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT).setVersionTemplate(CryptoKeyVersionTemplate.newBuilder().setAlgorithm(CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION)).build();
// Create the key.
CryptoKey createdKey = client.createCryptoKey(keyRingName, id, key);
System.out.printf("Created symmetric key %s%n", createdKey.getName());
}
}
use of com.google.cloud.kms.v1.KeyRingName in project java-docs-samples by GoogleCloudPlatform.
the class IamRemoveMember method iamRemoveMember.
// Remove the given IAM membership on the resource, if it exists.
public void iamRemoveMember(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);
// Search through the bindings and remove matches.
String roleToFind = "roles/cloudkms.cryptoKeyEncrypterDecrypter";
for (Binding binding : policy.getBindingsList()) {
if (binding.getRole().equals(roleToFind) && binding.getMembersList().contains(member)) {
binding.getMembersList().remove(member);
}
}
client.setIamPolicy(resourceName, policy);
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 CreateKeyAsymmetricSign method createKeyAsymmetricSign.
// Create a new asymmetric key for the purpose of signing and verifying data.
public void createKeyAsymmetricSign(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_SIGN).setVersionTemplate(CryptoKeyVersionTemplate.newBuilder().setAlgorithm(CryptoKeyVersionAlgorithm.RSA_SIGN_PKCS1_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-kms by googleapis.
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-kms by googleapis.
the class CreateKeyLabels method createKeyLabels.
// Create a new key with labels.
public void createKeyLabels(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 key to create with labels.
CryptoKey key = CryptoKey.newBuilder().setPurpose(CryptoKeyPurpose.ENCRYPT_DECRYPT).setVersionTemplate(CryptoKeyVersionTemplate.newBuilder().setAlgorithm(CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION)).putLabels("team", "alpha").putLabels("cost_center", "cc1234").build();
// Create the key.
CryptoKey createdKey = client.createCryptoKey(keyRingName, id, key);
System.out.printf("Created key with labels %s%n", createdKey.getName());
}
}
Aggregations