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);
}
}
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 "";
}
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);
}
}
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);
}
}
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);
}
}
Aggregations