Search in sources :

Example 1 with PutKeyPolicyRequest

use of software.amazon.awssdk.services.kms.model.PutKeyPolicyRequest 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)

Aggregations

KmsException (software.amazon.awssdk.services.kms.model.KmsException)1 PutKeyPolicyRequest (software.amazon.awssdk.services.kms.model.PutKeyPolicyRequest)1