$npx -y skills add giuseppe-trisciuoglio/developer-kit --skill aws-sdk-java-v2-kmsProvides AWS Key Management Service (KMS) patterns using AWS SDK for Java 2.x. Use when creating/managing encryption keys, encrypting/decrypting data, generating data keys, digital signing, key rotation, or integrating encryption into Spring Boot applications.
| 1 | # AWS SDK for Java 2.x - AWS KMS (Key Management Service) |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Provides AWS KMS patterns using AWS SDK for Java 2.x. Covers key management, encryption/decryption, envelope encryption, digital signatures, and Spring Boot integration. |
| 6 | |
| 7 | ## Instructions |
| 8 | |
| 9 | 1. **Set Up IAM Permissions** - Grant kms:* actions with least privilege |
| 10 | 2. **Create KMS Client** - Instantiate KmsClient with region and credentials |
| 11 | 3. **Create Keys** - Use createKey() → **Verify key state is ENABLED before proceeding** |
| 12 | 4. **Set Key Policies** - Define key usage permissions → **Test access before production** |
| 13 | 5. **Encrypt Data** - Use encrypt() for data <4KB; **Verify ciphertext is not empty** |
| 14 | 6. **Envelope Encryption** - For larger data, use generateDataKey() → **Verify data key generation succeeded** |
| 15 | 7. **Digital Signatures** - Create signing keys → **Verify signatureValid=true after sign/verify** |
| 16 | 8. **Key Rotation** - Enable auto-rotation → **Confirm rotation schedule is active** |
| 17 | |
| 18 | ## When to Use |
| 19 | |
| 20 | - Creating/managing symmetric encryption keys for data protection |
| 21 | - Implementing envelope encryption for large data |
| 22 | - Generating data keys for local encryption with KMS-managed keys |
| 23 | - Setting up digital signatures with asymmetric keys |
| 24 | - Integrating encryption into Spring Boot applications |
| 25 | |
| 26 | ## Dependencies |
| 27 | |
| 28 | ### Maven |
| 29 | |
| 30 | ```xml |
| 31 | <dependency> |
| 32 | <groupId>software.amazon.awssdk</groupId> |
| 33 | <artifactId>kms</artifactId> |
| 34 | </dependency> |
| 35 | ``` |
| 36 | |
| 37 | ### Gradle |
| 38 | |
| 39 | ```groovy |
| 40 | implementation 'software.amazon.awssdk:kms:2.x.x' |
| 41 | ``` |
| 42 | |
| 43 | ## Client Setup |
| 44 | |
| 45 | ### Basic Synchronous Client |
| 46 | |
| 47 | ```java |
| 48 | import software.amazon.awssdk.regions.Region; |
| 49 | import software.amazon.awssdk.services.kms.KmsClient; |
| 50 | |
| 51 | KmsClient kmsClient = KmsClient.builder() |
| 52 | .region(Region.US_EAST_1) |
| 53 | .build(); |
| 54 | ``` |
| 55 | |
| 56 | ### Basic Asynchronous Client |
| 57 | |
| 58 | ```java |
| 59 | import software.amazon.awssdk.services.kms.KmsAsyncClient; |
| 60 | |
| 61 | KmsAsyncClient kmsAsyncClient = KmsAsyncClient.builder() |
| 62 | .region(Region.US_EAST_1) |
| 63 | .build(); |
| 64 | ``` |
| 65 | |
| 66 | ### Advanced Client Configuration |
| 67 | |
| 68 | ```java |
| 69 | KmsClient kmsClient = KmsClient.builder() |
| 70 | .region(Region.of(System.getenv("AWS_REGION"))) |
| 71 | .credentialsProvider(DefaultCredentialsProvider.create()) |
| 72 | .overrideConfiguration(c -> c.retryPolicy(RetryPolicy.builder() |
| 73 | .numRetries(3) |
| 74 | .build())) |
| 75 | .build(); |
| 76 | ``` |
| 77 | |
| 78 | ## Basic Key Management |
| 79 | |
| 80 | ### Create Encryption Key |
| 81 | |
| 82 | ```java |
| 83 | public String createEncryptionKey(KmsClient kmsClient, String description) { |
| 84 | CreateKeyRequest request = CreateKeyRequest.builder() |
| 85 | .description(description) |
| 86 | .keyUsage(KeyUsageType.ENCRYPT_DECRYPT) |
| 87 | .build(); |
| 88 | |
| 89 | CreateKeyResponse response = kmsClient.createKey(request); |
| 90 | return response.keyMetadata().keyId(); |
| 91 | } |
| 92 | ``` |
| 93 | |
| 94 | ### Describe Key |
| 95 | |
| 96 | ```java |
| 97 | public KeyMetadata getKeyMetadata(KmsClient kmsClient, String keyId) { |
| 98 | DescribeKeyRequest request = DescribeKeyRequest.builder() |
| 99 | .keyId(keyId) |
| 100 | .build(); |
| 101 | |
| 102 | return kmsClient.describeKey(request).keyMetadata(); |
| 103 | } |
| 104 | ``` |
| 105 | |
| 106 | ### Enable/Disable Key |
| 107 | |
| 108 | ```java |
| 109 | public void toggleKeyState(KmsClient kmsClient, String keyId, boolean enable) { |
| 110 | if (enable) { |
| 111 | kmsClient.enableKey(EnableKeyRequest.builder().keyId(keyId).build()); |
| 112 | } else { |
| 113 | kmsClient.disableKey(DisableKeyRequest.builder().keyId(keyId).build()); |
| 114 | } |
| 115 | } |
| 116 | ``` |
| 117 | |
| 118 | ## Basic Encryption and Decryption |
| 119 | |
| 120 | ### Encrypt Data |
| 121 | |
| 122 | ```java |
| 123 | public String encryptData(KmsClient kmsClient, String keyId, String plaintext) { |
| 124 | SdkBytes plaintextBytes = SdkBytes.fromString(plaintext, StandardCharsets.UTF_8); |
| 125 | |
| 126 | EncryptRequest request = EncryptRequest.builder() |
| 127 | .keyId(keyId) |
| 128 | .plaintext(plaintextBytes) |
| 129 | .build(); |
| 130 | |
| 131 | EncryptResponse response = kmsClient.encrypt(request); |
| 132 | return Base64.getEncoder().encodeToString( |
| 133 | response.ciphertextBlob().asByteArray()); |
| 134 | } |
| 135 | ``` |
| 136 | |
| 137 | ### Decrypt Data |
| 138 | |
| 139 | ```java |
| 140 | public String decryptData(KmsClient kmsClient, String ciphertextBase64) { |
| 141 | byte[] ciphertext = Base64.getDecoder().decode(ciphertextBase64); |
| 142 | SdkBytes ciphertextBytes = SdkBytes.fromByteArray(ciphertext); |
| 143 | |
| 144 | DecryptRequest request = DecryptRequest.builder() |
| 145 | .ciphertextBlob(ciphertextBytes) |
| 146 | .build(); |
| 147 | |
| 148 | DecryptResponse response = kmsClient.decrypt(request); |
| 149 | return response.plaintext().asString(StandardCharsets.UTF_8); |
| 150 | } |
| 151 | ``` |
| 152 | |
| 153 | ## Envelope Encryption Pattern |
| 154 | |
| 155 | ### Generate and Use Data Key |
| 156 | |
| 157 | ```java |
| 158 | public DataKeyResult encryptWithEnvelope(KmsClient kmsClient, String masterKeyId, byte[] data) { |
| 159 | try { |
| 160 | GenerateDataKeyRequest keyRequest = GenerateDataKeyRequest.builder() |
| 161 | .keyId(masterKeyId) |
| 162 | .keySpec(DataKeySpec.AES_256) |
| 163 | .build(); |
| 164 | |
| 165 | Gene |