Search in sources :

Example 11 with KmsException

use of software.amazon.awssdk.services.kms.model.KmsException in project aws-doc-sdk-examples by awsdocs.

the class SetKeyPolicy method createPolicy.

// snippet-start:[kms.java2_set_policy.main]
public static void createPolicy(KmsClient kmsClient, String keyId, String policyName) {
    String policy = "{" + "  \"Version\": \"2012-10-17\"," + "  \"Statement\": [{" + "    \"Effect\": \"Allow\"," + // Replace the following user ARN with one for a real user.
    "    \"Principal\": {\"AWS\": \"arn:aws:iam::814548047983:root\"}," + "    \"Action\": \"kms:*\"," + "    \"Resource\": \"*\"" + "  }]" + "}";
    try {
        PutKeyPolicyRequest keyPolicyRequest = PutKeyPolicyRequest.builder().keyId(keyId).policyName(policyName).policy(policy).build();
        kmsClient.putKeyPolicy(keyPolicyRequest);
        System.out.println("Done");
    } catch (KmsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : KmsException(software.amazon.awssdk.services.kms.model.KmsException) PutKeyPolicyRequest(software.amazon.awssdk.services.kms.model.PutKeyPolicyRequest)

Example 12 with KmsException

use of software.amazon.awssdk.services.kms.model.KmsException in project aws-doc-sdk-examples by awsdocs.

the class CreateCustomerKey method createKey.

// snippet-start:[kms.java2_create_key.main]
public static String createKey(KmsClient kmsClient, String keyDesc) {
    try {
        CreateKeyRequest keyRequest = CreateKeyRequest.builder().description(keyDesc).customerMasterKeySpec(CustomerMasterKeySpec.SYMMETRIC_DEFAULT).keyUsage("ENCRYPT_DECRYPT").build();
        CreateKeyResponse result = kmsClient.createKey(keyRequest);
        System.out.printf("Created a customer key with id \"%s\"%n", result.keyMetadata().arn());
        return result.keyMetadata().keyId();
    } catch (KmsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
    return "";
}
Also used : KmsException(software.amazon.awssdk.services.kms.model.KmsException) CreateKeyRequest(software.amazon.awssdk.services.kms.model.CreateKeyRequest) CreateKeyResponse(software.amazon.awssdk.services.kms.model.CreateKeyResponse)

Example 13 with KmsException

use of software.amazon.awssdk.services.kms.model.KmsException in project aws-doc-sdk-examples by awsdocs.

the class DeleteAlias method deleteSpecificAlias.

// snippet-start:[kms.java2_delete_alias.main]
public static void deleteSpecificAlias(KmsClient kmsClient, String aliasName) {
    try {
        DeleteAliasRequest deleteAliasRequest = DeleteAliasRequest.builder().aliasName(aliasName).build();
        kmsClient.deleteAlias(deleteAliasRequest);
    } catch (KmsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : KmsException(software.amazon.awssdk.services.kms.model.KmsException) DeleteAliasRequest(software.amazon.awssdk.services.kms.model.DeleteAliasRequest)

Example 14 with KmsException

use of software.amazon.awssdk.services.kms.model.KmsException in project aws-doc-sdk-examples by awsdocs.

the class EncryptDataKey method decryptData.

// snippet-end:[kms.java2_encrypt_data.main]
// snippet-start:[kms.java2_decrypt_data.main]
public static void decryptData(KmsClient kmsClient, SdkBytes encryptedData, String keyId) {
    try {
        DecryptRequest decryptRequest = DecryptRequest.builder().ciphertextBlob(encryptedData).keyId(keyId).build();
        DecryptResponse decryptResponse = kmsClient.decrypt(decryptRequest);
        decryptResponse.plaintext();
    } catch (KmsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : DecryptResponse(software.amazon.awssdk.services.kms.model.DecryptResponse) KmsException(software.amazon.awssdk.services.kms.model.KmsException) DecryptRequest(software.amazon.awssdk.services.kms.model.DecryptRequest)

Example 15 with KmsException

use of software.amazon.awssdk.services.kms.model.KmsException in project aws-doc-sdk-examples by awsdocs.

the class ListGrants method displayGrantIds.

// snippet-start:[kms.java2_list_grant.main]
public static void displayGrantIds(KmsClient kmsClient, String keyId) {
    try {
        ListGrantsRequest grantsRequest = ListGrantsRequest.builder().keyId(keyId).limit(15).build();
        ListGrantsResponse response = kmsClient.listGrants(grantsRequest);
        List<GrantListEntry> grants = response.grants();
        for (GrantListEntry grant : grants) {
            System.out.println("The grant Id is : " + grant.grantId());
        }
    } catch (KmsException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : ListGrantsRequest(software.amazon.awssdk.services.kms.model.ListGrantsRequest) ListGrantsResponse(software.amazon.awssdk.services.kms.model.ListGrantsResponse) KmsException(software.amazon.awssdk.services.kms.model.KmsException) GrantListEntry(software.amazon.awssdk.services.kms.model.GrantListEntry)

Aggregations

KmsException (software.amazon.awssdk.services.kms.model.KmsException)16 SdkBytes (software.amazon.awssdk.core.SdkBytes)3 KmsClient (software.amazon.awssdk.services.kms.KmsClient)2 DecryptRequest (software.amazon.awssdk.services.kms.model.DecryptRequest)2 DecryptResponse (software.amazon.awssdk.services.kms.model.DecryptResponse)2 EncryptRequest (software.amazon.awssdk.services.kms.model.EncryptRequest)2 EncryptResponse (software.amazon.awssdk.services.kms.model.EncryptResponse)2 AliasListEntry (software.amazon.awssdk.services.kms.model.AliasListEntry)1 CreateAliasRequest (software.amazon.awssdk.services.kms.model.CreateAliasRequest)1 CreateGrantRequest (software.amazon.awssdk.services.kms.model.CreateGrantRequest)1 CreateGrantResponse (software.amazon.awssdk.services.kms.model.CreateGrantResponse)1 CreateKeyRequest (software.amazon.awssdk.services.kms.model.CreateKeyRequest)1 CreateKeyResponse (software.amazon.awssdk.services.kms.model.CreateKeyResponse)1 DeleteAliasRequest (software.amazon.awssdk.services.kms.model.DeleteAliasRequest)1 DescribeKeyRequest (software.amazon.awssdk.services.kms.model.DescribeKeyRequest)1 DescribeKeyResponse (software.amazon.awssdk.services.kms.model.DescribeKeyResponse)1 DisableKeyRequest (software.amazon.awssdk.services.kms.model.DisableKeyRequest)1 EnableKeyRequest (software.amazon.awssdk.services.kms.model.EnableKeyRequest)1 GrantListEntry (software.amazon.awssdk.services.kms.model.GrantListEntry)1 KeyListEntry (software.amazon.awssdk.services.kms.model.KeyListEntry)1