|
7 | 7 | import software.amazon.awssdk.services.s3.S3Client; |
8 | 8 | import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
9 | 9 | import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| 10 | +import software.amazon.encryption.s3.materials.CryptographicMaterialsManager; |
| 11 | +import software.amazon.encryption.s3.materials.DefaultCryptoMaterialsManager; |
| 12 | +import software.amazon.encryption.s3.materials.KmsKeyring; |
10 | 13 | import software.amazon.encryption.s3.utils.BoundedZerosInputStream; |
11 | 14 |
|
12 | 15 | import javax.crypto.KeyGenerator; |
@@ -185,6 +188,67 @@ public void defaultModeWithLargeObjectFails() throws IOException { |
185 | 188 | v3Client.close(); |
186 | 189 | } |
187 | 190 |
|
| 191 | + |
| 192 | + @Test |
| 193 | + public void s3EncryptionClientWithKeyringFromKmsKeyIdSucceeds() { |
| 194 | + final String objectKey = "keyring-from-kms-key-id"; |
| 195 | + |
| 196 | + KmsKeyring keyring = KmsKeyring.builder().wrappingKeyId(KMS_KEY_ID).build(); |
| 197 | + |
| 198 | + S3Client v3Client = S3EncryptionClient.builder() |
| 199 | + .keyring(keyring) |
| 200 | + .build(); |
| 201 | + |
| 202 | + simpleV3RoundTrip(v3Client, objectKey); |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + public void s3EncryptionClientWithCmmFromKmsKeyIdSucceeds() { |
| 207 | + final String objectKey = "cmm-from-kms-key-id"; |
| 208 | + |
| 209 | + KmsKeyring keyring = KmsKeyring.builder().wrappingKeyId(KMS_KEY_ID).build(); |
| 210 | + |
| 211 | + CryptographicMaterialsManager cmm = DefaultCryptoMaterialsManager.builder() |
| 212 | + .keyring(keyring) |
| 213 | + .build(); |
| 214 | + |
| 215 | + S3Client v3Client = S3EncryptionClient.builder() |
| 216 | + .cryptoMaterialsManager(cmm) |
| 217 | + .build(); |
| 218 | + |
| 219 | + simpleV3RoundTrip(v3Client, objectKey); |
| 220 | + } |
| 221 | + |
| 222 | + @Test |
| 223 | + public void s3EncryptionClientWithWrappedS3ClientSucceeds() { |
| 224 | + final String objectKey = "wrapped-s3-client-with-kms-key-id"; |
| 225 | + |
| 226 | + S3Client wrappedClient = S3Client.builder().build(); |
| 227 | + |
| 228 | + S3Client wrappingClient = S3EncryptionClient.builder() |
| 229 | + .wrappedClient(wrappedClient) |
| 230 | + .kmsKeyId(KMS_KEY_ID) |
| 231 | + .build(); |
| 232 | + |
| 233 | + simpleV3RoundTrip(wrappingClient, objectKey); |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * S3EncryptionClient implements S3Client, so it can be passed into the builder as a wrappedClient. |
| 238 | + * However, is not a supported use case, and the builder should throw an exception if this happens. |
| 239 | + */ |
| 240 | + @Test |
| 241 | + public void s3EncryptionClientWithWrappedS3EncryptionClientFails() { |
| 242 | + S3Client wrappedClient = S3EncryptionClient.builder() |
| 243 | + .kmsKeyId(KMS_KEY_ID) |
| 244 | + .build(); |
| 245 | + |
| 246 | + assertThrows(S3EncryptionClientException.class, () -> S3EncryptionClient.builder() |
| 247 | + .wrappedClient(wrappedClient) |
| 248 | + .kmsKeyId(KMS_KEY_ID) |
| 249 | + .build()); |
| 250 | + } |
| 251 | + |
188 | 252 | /** |
189 | 253 | * A simple, reusable round-trip (encryption + decryption) using a given |
190 | 254 | * S3Client. Useful for testing client configuration. |
|
0 commit comments