Search in sources :

Example 16 with CloudKMS

use of com.google.api.services.cloudkms.v1.CloudKMS in project java-docs-samples by GoogleCloudPlatform.

the class Snippets method getCryptoKeyPolicy.

// [END kms_restore_cryptokey_version]
// [START kms_get_cryptokey_policy]
/**
 * Retrieves the IAM policy for the given crypto key.
 */
public static Policy getCryptoKeyPolicy(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 cryptoKey = String.format("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", projectId, locationId, keyRingId, cryptoKeyId);
    // Get the current IAM policy and add the new account to it.
    Policy iamPolicy = kms.projects().locations().keyRings().cryptoKeys().getIamPolicy(cryptoKey).execute();
    System.out.println(iamPolicy.getBindings());
    return iamPolicy;
}
Also used : Policy(com.google.api.services.cloudkms.v1.model.Policy) CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS)

Example 17 with CloudKMS

use of com.google.api.services.cloudkms.v1.CloudKMS in project java-docs-samples by GoogleCloudPlatform.

the class Snippets method destroyCryptoKeyVersion.

// [END kms_enable_cryptokey_version]
// [START kms_destroy_cryptokey_version]
/**
 * Marks the given version of a crypto key to be destroyed at a scheduled future point.
 */
public static CryptoKeyVersion destroyCryptoKeyVersion(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);
    DestroyCryptoKeyVersionRequest destroyRequest = new DestroyCryptoKeyVersionRequest();
    CryptoKeyVersion destroyed = kms.projects().locations().keyRings().cryptoKeys().cryptoKeyVersions().destroy(cryptoKeyVersion, destroyRequest).execute();
    System.out.println(destroyed);
    return destroyed;
}
Also used : CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS) CryptoKeyVersion(com.google.api.services.cloudkms.v1.model.CryptoKeyVersion) DestroyCryptoKeyVersionRequest(com.google.api.services.cloudkms.v1.model.DestroyCryptoKeyVersionRequest)

Example 18 with CloudKMS

use of com.google.api.services.cloudkms.v1.CloudKMS in project java-docs-samples by GoogleCloudPlatform.

the class Snippets method createKeyRing.

// [START kms_create_keyring]
/**
 * Creates a new key ring with the given id.
 */
public static KeyRing createKeyRing(String projectId, String locationId, String keyRingId) 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", projectId, locationId);
    // Create the KeyRing for your project.
    KeyRing keyring = kms.projects().locations().keyRings().create(parent, new KeyRing()).setKeyRingId(keyRingId).execute();
    System.out.println(keyring);
    return keyring;
}
Also used : KeyRing(com.google.api.services.cloudkms.v1.model.KeyRing) CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS)

Example 19 with CloudKMS

use of com.google.api.services.cloudkms.v1.CloudKMS in project java-docs-samples by GoogleCloudPlatform.

the class Snippets method removeMemberFromCryptoKeyPolicy.

// [END kms_add_member_to_keyring_policy]
// [START kms_remove_member_from_cryptokey_policy]
/**
 * Removes the given member from the given policy.
 */
public static Policy removeMemberFromCryptoKeyPolicy(String projectId, String locationId, String keyRingId, String cryptoKeyId, 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/cryptoKeys/%s", projectId, locationId, keyRingId, cryptoKeyId);
    // Get the current IAM policy and add the new account to it.
    Policy iamPolicy = getCryptoKeyPolicy(projectId, locationId, keyRingId, cryptoKeyId);
    if (null == iamPolicy.getBindings()) {
        // Nothing to remove
        return null;
    }
    // Filter out the given member
    for (Binding b : iamPolicy.getBindings()) {
        if (role.equals(b.getRole()) && b.getMembers().contains(member)) {
            b.getMembers().removeAll(Collections.singletonList(member));
            break;
        }
    }
    // Set the new IAM Policy.
    Policy newIamPolicy = kms.projects().locations().keyRings().cryptoKeys().setIamPolicy(cryptoKey, new SetIamPolicyRequest().setPolicy(iamPolicy)).execute();
    System.out.println("Response: " + newIamPolicy);
    return newIamPolicy;
}
Also used : Policy(com.google.api.services.cloudkms.v1.model.Policy) Binding(com.google.api.services.cloudkms.v1.model.Binding) CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS) SetIamPolicyRequest(com.google.api.services.cloudkms.v1.model.SetIamPolicyRequest)

Example 20 with CloudKMS

use of com.google.api.services.cloudkms.v1.CloudKMS in project java-docs-samples by GoogleCloudPlatform.

the class Snippets method enableCryptoKeyVersion.

// [END kms_disable_cryptokey_version]
// [START kms_enable_cryptokey_version]
/**
 * Enables the given version of the crypto key.
 */
public static CryptoKeyVersion enableCryptoKeyVersion(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);
    CryptoKeyVersion newVersionState = new CryptoKeyVersion().setState("ENABLED");
    CryptoKeyVersion response = kms.projects().locations().keyRings().cryptoKeys().cryptoKeyVersions().patch(cryptoKeyVersion, newVersionState).setUpdateMask("state").execute();
    System.out.println(response);
    return response;
}
Also used : CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS) CryptoKeyVersion(com.google.api.services.cloudkms.v1.model.CryptoKeyVersion)

Aggregations

CloudKMS (com.google.api.services.cloudkms.v1.CloudKMS)20 CryptoKeyVersion (com.google.api.services.cloudkms.v1.model.CryptoKeyVersion)6 Policy (com.google.api.services.cloudkms.v1.model.Policy)6 Binding (com.google.api.services.cloudkms.v1.model.Binding)4 SetIamPolicyRequest (com.google.api.services.cloudkms.v1.model.SetIamPolicyRequest)4 CryptoKey (com.google.api.services.cloudkms.v1.model.CryptoKey)3 KeyRing (com.google.api.services.cloudkms.v1.model.KeyRing)3 ListKeyRingsResponse (com.google.api.services.cloudkms.v1.model.ListKeyRingsResponse)2 DecryptRequest (com.google.api.services.cloudkms.v1.model.DecryptRequest)1 DecryptResponse (com.google.api.services.cloudkms.v1.model.DecryptResponse)1 DestroyCryptoKeyVersionRequest (com.google.api.services.cloudkms.v1.model.DestroyCryptoKeyVersionRequest)1 EncryptRequest (com.google.api.services.cloudkms.v1.model.EncryptRequest)1 EncryptResponse (com.google.api.services.cloudkms.v1.model.EncryptResponse)1 ListCryptoKeyVersionsResponse (com.google.api.services.cloudkms.v1.model.ListCryptoKeyVersionsResponse)1 ListCryptoKeysResponse (com.google.api.services.cloudkms.v1.model.ListCryptoKeysResponse)1 RestoreCryptoKeyVersionRequest (com.google.api.services.cloudkms.v1.model.RestoreCryptoKeyVersionRequest)1 UpdateCryptoKeyPrimaryVersionRequest (com.google.api.services.cloudkms.v1.model.UpdateCryptoKeyPrimaryVersionRequest)1