use of com.google.api.services.cloudkms.v1.CloudKMS in project java-docs-samples by GoogleCloudPlatform.
the class Snippets method addMemberToKeyRingPolicy.
// [END kms_add_member_to_cryptokey_policy]
// [START kms_add_member_to_keyring_policy]
/**
* Adds the given member to the given keyring, with the given role.
*
* @param projectId The id of the project.
* @param locationId The location id of the key.
* @param keyRingId The id of the keyring.
* @param member The member to add. Must be in the proper format, eg:
*
* allUsers user:$userEmail serviceAccount:$serviceAccountEmail
*
* See https://g.co/cloud/kms/docs/reference/rest/v1/Policy#binding for more details.
* @param role Must be in one of the following formats: roles/[role]
* organizations/[organizationId]/roles/[role] projects/[projectId]/roles/[role]
*
* See https://g.co/cloud/iam/docs/understanding-roles for available values for [role].
*/
public static Policy addMemberToKeyRingPolicy(String projectId, String locationId, String keyRingId, String member, String role) throws IOException {
// Create the Cloud KMS client.
CloudKMS kms = createAuthorizedClient();
// The resource name of the keyring version
String keyring = String.format("projects/%s/locations/%s/keyRings/%s", projectId, locationId, keyRingId);
// Get the current IAM policy
Policy iamPolicy = getKeyRingPolicy(projectId, locationId, keyRingId);
// Add the new account to it.
Binding newBinding = new Binding().setRole(role).setMembers(Collections.singletonList(member));
List<Binding> bindings = iamPolicy.getBindings();
if (null == bindings) {
bindings = Collections.singletonList(newBinding);
} else {
bindings.add(newBinding);
}
iamPolicy.setBindings(bindings);
// Set the new IAM Policy.
Policy newIamPolicy = kms.projects().locations().keyRings().setIamPolicy(keyring, new SetIamPolicyRequest().setPolicy(iamPolicy)).execute();
System.out.println("Response: " + newIamPolicy);
return newIamPolicy;
}
use of com.google.api.services.cloudkms.v1.CloudKMS in project java-docs-samples by GoogleCloudPlatform.
the class Snippets method createCryptoKeyVersion.
// [END kms_create_cryptokey]
// [START kms_create_cryptokey_version]
/**
* Creates a new crypto key version for the given id.
*/
public static void createCryptoKeyVersion(String projectId, String locationId, String keyRingId, String cryptoKeyId) throws IOException {
// Create the Cloud KMS client.
CloudKMS kms = createAuthorizedClient();
// The resource name of the cryptoKey
String cryptoKeys = String.format("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", projectId, locationId, keyRingId, cryptoKeyId);
CryptoKeyVersion version = new CryptoKeyVersion();
CryptoKeyVersion newVersion = kms.projects().locations().keyRings().cryptoKeys().cryptoKeyVersions().create(cryptoKeys, version).execute();
System.out.println(newVersion);
}
use of com.google.api.services.cloudkms.v1.CloudKMS in project java-docs-samples by GoogleCloudPlatform.
the class Snippets method restoreCryptoKeyVersion.
// [END kms_destroy_cryptokey_version]
// [START kms_restore_cryptokey_version]
/**
* Restores the given version of a crypto key that is currently scheduled for destruction.
*/
public static CryptoKeyVersion restoreCryptoKeyVersion(String projectId, String locationId, String keyRingId, String cryptoKeyId, String version) throws IOException {
// Create the Cloud KMS client.
CloudKMS kms = createAuthorizedClient();
// The resource name of the cryptoKey version
String cryptoKeyVersion = String.format("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s/cryptoKeyVersions/%s", projectId, locationId, keyRingId, cryptoKeyId, version);
RestoreCryptoKeyVersionRequest restoreRequest = new RestoreCryptoKeyVersionRequest();
CryptoKeyVersion restored = kms.projects().locations().keyRings().cryptoKeys().cryptoKeyVersions().restore(cryptoKeyVersion, restoreRequest).execute();
System.out.println(restored);
return restored;
}
use of com.google.api.services.cloudkms.v1.CloudKMS in project java-docs-samples by GoogleCloudPlatform.
the class Snippets method removeMemberFromKeyRingPolicy.
// [END kms_remove_member_from_cryptokey_policy]
// [START kms_remove_member_from_keyring_policy]
/**
* Removes the given member from the given policy.
*/
public static Policy removeMemberFromKeyRingPolicy(String projectId, String locationId, String keyRingId, String member, String role) throws IOException {
// Create the Cloud KMS client.
CloudKMS kms = createAuthorizedClient();
// The resource name of the cryptoKey
String cryptoKey = String.format("projects/%s/locations/%s/keyRings/%s", projectId, locationId, keyRingId);
// Get the current IAM policy and add the new account to it.
Policy iamPolicy = getKeyRingPolicy(projectId, locationId, keyRingId);
// Filter out the given member
for (Binding b : iamPolicy.getBindings()) {
if (role.equals(b.getRole()) && b.getMembers().contains(member)) {
b.getMembers().remove(member);
break;
}
}
// Set the new IAM Policy.
Policy newIamPolicy = kms.projects().locations().keyRings().setIamPolicy(cryptoKey, new SetIamPolicyRequest().setPolicy(iamPolicy)).execute();
System.out.println("Response: " + newIamPolicy);
return newIamPolicy;
}
use of com.google.api.services.cloudkms.v1.CloudKMS in project java-docs-samples by GoogleCloudPlatform.
the class Snippets method createCryptoKey.
// [END kms_create_keyring]
// [START kms_create_cryptokey]
/**
* Creates a new crypto key with the given id.
*/
public static CryptoKey createCryptoKey(String projectId, String locationId, String keyRingId, String cryptoKeyId) throws IOException {
// Create the Cloud KMS client.
CloudKMS kms = createAuthorizedClient();
// The resource name of the location associated with the KeyRing.
String parent = String.format("projects/%s/locations/%s/keyRings/%s", projectId, locationId, keyRingId);
// This will allow the API access to the key for encryption and decryption.
String purpose = "ENCRYPT_DECRYPT";
CryptoKey cryptoKey = new CryptoKey();
cryptoKey.setPurpose(purpose);
// Create the CryptoKey for your project.
CryptoKey createdKey = kms.projects().locations().keyRings().cryptoKeys().create(parent, cryptoKey).setCryptoKeyId(cryptoKeyId).execute();
System.out.println(createdKey);
return createdKey;
}
Aggregations