|
| 1 | +package software.amazon.encryption.s3.examples; |
| 2 | + |
| 3 | +import java.security.NoSuchAlgorithmException; |
| 4 | + |
| 5 | +import software.amazon.awssdk.core.ResponseBytes; |
| 6 | +import software.amazon.awssdk.core.sync.RequestBody; |
| 7 | +import software.amazon.awssdk.services.kms.KmsClient; |
| 8 | +import software.amazon.awssdk.services.s3.S3Client; |
| 9 | +import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
| 10 | +import software.amazon.encryption.s3.S3EncryptionClient; |
| 11 | +import software.amazon.encryption.s3.S3EncryptionClientException; |
| 12 | +import software.amazon.encryption.s3.internal.InstructionFileConfig; |
| 13 | +import software.amazon.encryption.s3.materials.KmsKeyring; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 17 | +import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.appendTestSuffix; |
| 18 | +import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.deleteObject; |
| 19 | + |
| 20 | +public class InstructionFileExample { |
| 21 | + |
| 22 | + public static void main(final String[] args) throws NoSuchAlgorithmException { |
| 23 | + final String bucket = args[0]; |
| 24 | + final String kmsKeyId = args.length > 1 ? args[1] : null; |
| 25 | + |
| 26 | + if (kmsKeyId != null) { |
| 27 | + InstructionFileExample.simpleKmsKeyringUseInstructionFile(bucket, kmsKeyId); |
| 28 | + } |
| 29 | + } |
| 30 | + /** |
| 31 | + * This example demonstrates using Instruction Files. |
| 32 | + * |
| 33 | + * @param bucket The name of the Amazon S3 bucket to perform operations on. |
| 34 | + * @param kmsKeyId The KMS key ID used for encryption |
| 35 | + */ |
| 36 | + public static void simpleKmsKeyringUseInstructionFile( |
| 37 | + final String bucket, |
| 38 | + final String kmsKeyId |
| 39 | + ) { |
| 40 | + // Set up the S3 object key and content to be encrypted |
| 41 | + final String objectKey = appendTestSuffix( |
| 42 | + "kms-instruction-file-test" |
| 43 | + ); |
| 44 | + final String input = |
| 45 | + "Testing encryption of instruction file with KMS Keyring"; |
| 46 | + |
| 47 | + // Create a KMS client for key operations |
| 48 | + KmsClient kmsClient = KmsClient.create(); |
| 49 | + |
| 50 | + // Create the original KMS keyring with the first KMS key |
| 51 | + KmsKeyring originalKeyring = KmsKeyring |
| 52 | + .builder() |
| 53 | + .kmsClient(kmsClient) |
| 54 | + .wrappingKeyId(kmsKeyId) |
| 55 | + .build(); |
| 56 | + |
| 57 | + // Create a default S3 client for instruction file operations |
| 58 | + S3Client wrappedClient = S3Client.create(); |
| 59 | + |
| 60 | + // Create the S3 Encryption Client with instruction file support enabled |
| 61 | + // The client can perform both putObject and getObject operations using the KMS key |
| 62 | + ResponseBytes<GetObjectResponse> decryptedObject; |
| 63 | + try (S3EncryptionClient s3ec = S3EncryptionClient |
| 64 | + .builderV4() |
| 65 | + .keyring(originalKeyring) |
| 66 | + .instructionFileConfig( |
| 67 | + InstructionFileConfig |
| 68 | + .builder() |
| 69 | + .instructionFileClient(wrappedClient) |
| 70 | + .enableInstructionFilePutObject(true) |
| 71 | + .build() |
| 72 | + ).build()) { |
| 73 | + |
| 74 | + // Upload both the encrypted object and instruction file to the specified bucket in S3 |
| 75 | + s3ec.putObject( |
| 76 | + builder -> builder.bucket(bucket).key(objectKey).build(), |
| 77 | + RequestBody.fromString(input) |
| 78 | + ); |
| 79 | + |
| 80 | + // Verify that the client can successfully decrypt the object |
| 81 | + decryptedObject = s3ec.getObjectAsBytes(builder -> |
| 82 | + builder.bucket(bucket).key(objectKey).build() |
| 83 | + ); |
| 84 | + // Assert that the decrypted object's content matches the original input |
| 85 | + assertEquals(input, decryptedObject.asUtf8String()); |
| 86 | + |
| 87 | + // Call deleteObject to delete the object and instruction file |
| 88 | + // from given S3 Bucket |
| 89 | + deleteObject(bucket, objectKey, s3ec); |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments