Search in sources :

Example 1 with CloudKMS

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

the class Quickstart method main.

public static void main(String... args) throws Exception {
    String projectId = args[0];
    // The location of the Key Rings
    String location = args[1];
    // Create the Cloud KMS client.
    CloudKMS kms = createAuthorizedClient();
    // The resource name of the cryptoKey
    String keyRingPath = String.format("projects/%s/locations/%s", projectId, location);
    // Make the RPC call
    ListKeyRingsResponse response = kms.projects().locations().keyRings().list(keyRingPath).execute();
    // Print the returned key rings
    if (null != response.getKeyRings()) {
        System.out.println("Key Rings: ");
        for (KeyRing keyRing : response.getKeyRings()) {
            System.out.println(keyRing.getName());
        }
    } else {
        System.out.println("No key rings defined.");
    }
}
Also used : KeyRing(com.google.api.services.cloudkms.v1.model.KeyRing) CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS) ListKeyRingsResponse(com.google.api.services.cloudkms.v1.model.ListKeyRingsResponse)

Example 2 with CloudKMS

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

the class Snippets method listCryptoKeys.

/**
 * Prints all crypto keys in the given key ring.
 */
public static void listCryptoKeys(String projectId, String locationId, String keyRingId) throws IOException {
    // Create the Cloud KMS client.
    CloudKMS kms = createAuthorizedClient();
    // The resource name of the cryptoKey
    String keyRingPath = String.format("projects/%s/locations/%s/keyRings/%s", projectId, locationId, keyRingId);
    ListCryptoKeysResponse cryptoKeys = null;
    do {
        // Print every page of keys
        cryptoKeys = kms.projects().locations().keyRings().cryptoKeys().list(keyRingPath).setPageToken(cryptoKeys != null ? cryptoKeys.getNextPageToken() : null).execute();
        for (CryptoKey key : cryptoKeys.getCryptoKeys()) {
            System.out.println(key);
        }
    } while (cryptoKeys.getNextPageToken() != null);
}
Also used : ListCryptoKeysResponse(com.google.api.services.cloudkms.v1.model.ListCryptoKeysResponse) CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS) CryptoKey(com.google.api.services.cloudkms.v1.model.CryptoKey)

Example 3 with CloudKMS

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

the class Snippets method setPrimaryVersion.

/**
 * Sets a version as the primary version for a crypto key.
 */
public static void setPrimaryVersion(String projectId, String locationId, String keyRingId, String cryptoKeyId, String versionId) throws IOException {
    // Create the Cloud KMS client.
    CloudKMS kms = createAuthorizedClient();
    // Resource name of the key version.
    String resourceName = String.format("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", projectId, locationId, keyRingId, cryptoKeyId);
    CryptoKey key = kms.projects().locations().keyRings().cryptoKeys().updatePrimaryVersion(resourceName, new UpdateCryptoKeyPrimaryVersionRequest().setCryptoKeyVersionId(versionId)).execute();
    System.out.println(key);
}
Also used : CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS) CryptoKey(com.google.api.services.cloudkms.v1.model.CryptoKey) UpdateCryptoKeyPrimaryVersionRequest(com.google.api.services.cloudkms.v1.model.UpdateCryptoKeyPrimaryVersionRequest)

Example 4 with CloudKMS

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

the class Snippets method listKeyRings.

// [END kms_remove_member_from_keyring_policy]
/**
 * Prints all the key rings in the given project.
 */
public static void listKeyRings(String projectId, String locationId) throws IOException {
    // Create the Cloud KMS client.
    CloudKMS kms = createAuthorizedClient();
    // The resource name of the cryptoKey
    String keyRingPath = String.format("projects/%s/locations/%s", projectId, locationId);
    // Make the RPC call
    ListKeyRingsResponse response = kms.projects().locations().keyRings().list(keyRingPath).execute();
    // Print the returned key rings
    if (null != response.getKeyRings()) {
        System.out.println("Key Rings: ");
        for (KeyRing keyRing : response.getKeyRings()) {
            System.out.println(keyRing.getName());
        }
    } else {
        System.out.println("No keyrings defined.");
    }
}
Also used : KeyRing(com.google.api.services.cloudkms.v1.model.KeyRing) CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS) ListKeyRingsResponse(com.google.api.services.cloudkms.v1.model.ListKeyRingsResponse)

Example 5 with CloudKMS

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

the class Snippets method addMemberToCryptoKeyPolicy.

// [END kms_get_keyring_policy]
// [START kms_add_member_to_cryptokey_policy]
/**
 * Adds the given member to the given key, 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 cryptoKeyId The id of the crypto key.
 * @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 addMemberToCryptoKeyPolicy(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 version
    String cryptoKey = String.format("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", projectId, locationId, keyRingId, cryptoKeyId);
    // Get the current IAM policy
    Policy iamPolicy = getCryptoKeyPolicy(projectId, locationId, keyRingId, cryptoKeyId);
    // 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().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)

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