Search in sources :

Example 21 with KeyRingName

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

Example 22 with KeyRingName

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

Example 23 with KeyRingName

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

Example 24 with KeyRingName

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

Example 25 with KeyRingName

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

Aggregations

KeyManagementServiceClient (com.google.cloud.kms.v1.KeyManagementServiceClient)22 KeyRingName (com.google.cloud.kms.v1.KeyRingName)17 CryptoKey (com.google.cloud.kms.v1.CryptoKey)16 CryptoKeyName (com.google.cloud.kms.v1.CryptoKeyName)7 Binding (com.google.iam.v1.Binding)6 Policy (com.google.iam.v1.Policy)6 AbstractMessage (com.google.protobuf.AbstractMessage)4 Test (org.junit.Test)4 ListCryptoKeysPagedResponse (com.google.cloud.kms.v1.KeyManagementServiceClient.ListCryptoKeysPagedResponse)2 ListImportJobsPagedResponse (com.google.cloud.kms.v1.KeyManagementServiceClient.ListImportJobsPagedResponse)2 KeyManagementServiceBlockingStub (com.google.cloud.kms.v1.KeyManagementServiceGrpc.KeyManagementServiceBlockingStub)2 KeyRing (com.google.cloud.kms.v1.KeyRing)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 NotFoundException (com.google.api.gax.rpc.NotFoundException)1 CreateCryptoKeyRequest (com.google.cloud.kms.v1.CreateCryptoKeyRequest)1 CreateKeyRingRequest (com.google.cloud.kms.v1.CreateKeyRingRequest)1 GetCryptoKeyRequest (com.google.cloud.kms.v1.GetCryptoKeyRequest)1 GetKeyRingRequest (com.google.cloud.kms.v1.GetKeyRingRequest)1 ImportJob (com.google.cloud.kms.v1.ImportJob)1 KeyManagementServiceGrpc (com.google.cloud.kms.v1.KeyManagementServiceGrpc)1