Search in sources :

Example 6 with CryptoKey

use of com.google.api.services.cloudkms.v1.model.CryptoKey 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);
}
Also used : CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS) CryptoKeyVersion(com.google.api.services.cloudkms.v1.model.CryptoKeyVersion)

Example 7 with CryptoKey

use of com.google.api.services.cloudkms.v1.model.CryptoKey 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;
}
Also used : CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS) RestoreCryptoKeyVersionRequest(com.google.api.services.cloudkms.v1.model.RestoreCryptoKeyVersionRequest) CryptoKeyVersion(com.google.api.services.cloudkms.v1.model.CryptoKeyVersion)

Example 8 with CryptoKey

use of com.google.api.services.cloudkms.v1.model.CryptoKey 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;
}
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 9 with CryptoKey

use of com.google.api.services.cloudkms.v1.model.CryptoKey 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;
}
Also used : CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS) CryptoKey(com.google.api.services.cloudkms.v1.model.CryptoKey)

Example 10 with CryptoKey

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

the class CryptFile method decrypt.

// [END kms_encrypt]
// [START kms_decrypt]
/**
 * Decrypts the provided ciphertext with the specified crypto key.
 */
public static byte[] decrypt(String projectId, String locationId, String keyRingId, String cryptoKeyId, byte[] ciphertext) throws IOException {
    // Create the Cloud KMS client.
    CloudKMS kms = createAuthorizedClient();
    // The resource name of the cryptoKey
    String cryptoKeyName = String.format("projects/%s/locations/%s/keyRings/%s/cryptoKeys/%s", projectId, locationId, keyRingId, cryptoKeyId);
    DecryptRequest request = new DecryptRequest().encodeCiphertext(ciphertext);
    DecryptResponse response = kms.projects().locations().keyRings().cryptoKeys().decrypt(cryptoKeyName, request).execute();
    return response.decodePlaintext();
}
Also used : DecryptResponse(com.google.api.services.cloudkms.v1.model.DecryptResponse) CloudKMS(com.google.api.services.cloudkms.v1.CloudKMS) DecryptRequest(com.google.api.services.cloudkms.v1.model.DecryptRequest)

Aggregations

CloudKMS (com.google.api.services.cloudkms.v1.CloudKMS)17 CryptoKeyVersion (com.google.api.services.cloudkms.v1.model.CryptoKeyVersion)6 Policy (com.google.api.services.cloudkms.v1.model.Policy)4 Binding (com.google.api.services.cloudkms.v1.model.Binding)3 CryptoKey (com.google.api.services.cloudkms.v1.model.CryptoKey)3 SetIamPolicyRequest (com.google.api.services.cloudkms.v1.model.SetIamPolicyRequest)3 KeyRing (com.google.api.services.cloudkms.v1.model.KeyRing)2 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