Skip to content

Commit de00ef1

Browse files
authored
test(examples): Instruction Files (#505)
1 parent 371e58c commit de00ef1

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package software.amazon.encryption.s3.examples;
2+
3+
import org.junit.jupiter.api.Test;
4+
import software.amazon.encryption.s3.utils.S3EncryptionClientTestResources;
5+
6+
import static org.junit.jupiter.api.Assertions.fail;
7+
8+
public class InstructionFileExampleTest {
9+
10+
@Test
11+
public void testInstructionFileExample() {
12+
final String bucket = S3EncryptionClientTestResources.BUCKET;
13+
final String kmsKeyId = S3EncryptionClientTestResources.KMS_KEY_ID;
14+
try {
15+
InstructionFileExample.simpleKmsKeyringUseInstructionFile(bucket, kmsKeyId);
16+
} catch (Throwable exception) {
17+
exception.printStackTrace();
18+
fail("Instruction File Example Test Failed!!", exception);
19+
}
20+
}
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package software.amazon.encryption.s3.examples;
2+
3+
import org.junit.jupiter.api.Test;
4+
import software.amazon.encryption.s3.utils.S3EncryptionClientTestResources;
5+
6+
import static org.junit.jupiter.api.Assertions.fail;
7+
8+
public class ReEncryptInstructionFileExampleTest {
9+
10+
@Test
11+
public void testSimpleAesKeyringReEncryptInstructionFile() {
12+
final String bucket = S3EncryptionClientTestResources.BUCKET;
13+
try {
14+
ReEncryptInstructionFileExample.simpleAesKeyringReEncryptInstructionFile(bucket);
15+
} catch (Throwable exception) {
16+
exception.printStackTrace();
17+
fail("AES Keyring ReEncrypt Instruction File Test Failed!!", exception);
18+
}
19+
}
20+
21+
@Test
22+
public void testSimpleRsaKeyringReEncryptInstructionFile() {
23+
final String bucket = S3EncryptionClientTestResources.BUCKET;
24+
try {
25+
ReEncryptInstructionFileExample.simpleRsaKeyringReEncryptInstructionFile(bucket);
26+
} catch (Throwable exception) {
27+
exception.printStackTrace();
28+
fail("RSA Keyring ReEncrypt Instruction File Test Failed!!", exception);
29+
}
30+
}
31+
32+
@Test
33+
public void testSimpleRsaKeyringReEncryptInstructionFileWithCustomSuffix() {
34+
final String bucket = S3EncryptionClientTestResources.BUCKET;
35+
try {
36+
ReEncryptInstructionFileExample.simpleRsaKeyringReEncryptInstructionFileWithCustomSuffix(bucket);
37+
} catch (Throwable exception) {
38+
exception.printStackTrace();
39+
fail("RSA Keyring ReEncrypt Instruction File With Custom Suffix Test Failed!!", exception);
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)