Skip to content

Commit dc1c843

Browse files
committed
Add integration tests
1 parent 10f588a commit dc1c843

File tree

5 files changed

+507
-16
lines changed

5 files changed

+507
-16
lines changed

pom.xml

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,35 +61,54 @@
6161

6262
<dependencies>
6363
<dependency>
64-
<groupId>org.bouncycastle</groupId>
65-
<artifactId>bcprov-jdk18on</artifactId>
66-
<version>1.71</version>
64+
<groupId>software.amazon.awssdk</groupId>
65+
<artifactId>s3</artifactId>
66+
<version>2.17.204</version>
6767
<optional>true</optional>
6868
</dependency>
6969

7070
<dependency>
71-
<groupId>com.amazonaws</groupId>
72-
<artifactId>aws-java-sdk-kms</artifactId>
71+
<groupId>software.amazon.awssdk</groupId>
72+
<artifactId>kms</artifactId>
73+
<version>2.17.204</version>
74+
<optional>true</optional>
75+
</dependency>
76+
77+
<!-- Test Dependencies -->
78+
<dependency>
79+
<groupId>org.junit.jupiter</groupId>
80+
<artifactId>junit-jupiter</artifactId>
81+
<version>5.9.0</version>
82+
<scope>test</scope>
7383
</dependency>
7484

7585
<dependency>
7686
<groupId>com.amazonaws</groupId>
77-
<artifactId>aws-java-sdk-s3</artifactId>
87+
<artifactId>aws-java-sdk-kms</artifactId>
88+
<scope>test</scope>
7889
</dependency>
7990

8091
<dependency>
81-
<groupId>software.amazon.awssdk</groupId>
82-
<artifactId>s3</artifactId>
83-
<version>2.17.204</version>
84-
<optional>true</optional>
92+
<groupId>com.amazonaws</groupId>
93+
<artifactId>aws-java-sdk-s3</artifactId>
94+
<scope>test</scope>
8595
</dependency>
8696

8797
<dependency>
88-
<groupId>software.amazon.awssdk</groupId>
89-
<artifactId>kms</artifactId>
90-
<version>2.17.204</version>
91-
<optional>true</optional>
98+
<groupId>org.bouncycastle</groupId>
99+
<artifactId>bcprov-jdk18on</artifactId>
100+
<version>1.71</version>
101+
<scope>test</scope>
92102
</dependency>
93103
</dependencies>
94104

105+
<build>
106+
<plugins>
107+
<plugin>
108+
<artifactId>maven-surefire-plugin</artifactId>
109+
<version>2.22.2</version>
110+
</plugin>
111+
</plugins>
112+
</build>
113+
95114
</project>

src/main/java/software/amazon/encryption/s3/materials/EncryptDataKeyStrategy.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22

33
import java.security.GeneralSecurityException;
44
import java.security.SecureRandom;
5+
import java.util.Map;
56

67
public interface EncryptDataKeyStrategy {
78
String keyProviderId();
89

10+
default EncryptionMaterials modifyMaterials(EncryptionMaterials materials) {
11+
return materials;
12+
}
13+
914
byte[] encryptDataKey(
1015
SecureRandom secureRandom,
1116
EncryptionMaterials materials

src/main/java/software/amazon/encryption/s3/materials/KmsKeyring.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package software.amazon.encryption.s3.materials;
22

33
import java.security.SecureRandom;
4+
import java.util.Collections;
45
import java.util.HashMap;
56
import java.util.Map;
67
import java.util.TreeMap;
@@ -69,14 +70,23 @@ public String keyProviderId() {
6970
}
7071

7172
@Override
72-
public byte[] encryptDataKey(SecureRandom secureRandom, EncryptionMaterials materials) {
73+
public EncryptionMaterials modifyMaterials(EncryptionMaterials materials) {
7374
if (materials.encryptionContext().containsKey(ENCRYPTION_CONTEXT_ALGORITHM_KEY)) {
7475
throw new S3EncryptionClientException(ENCRYPTION_CONTEXT_ALGORITHM_KEY + " is a reserved key for the S3 encryption client");
7576
}
7677

77-
TreeMap<String, String> encryptionContext = new TreeMap<>(materials.encryptionContext());
78+
Map<String, String> encryptionContext = new HashMap<>(materials.encryptionContext());
7879
encryptionContext.put(ENCRYPTION_CONTEXT_ALGORITHM_KEY, materials.algorithmSuite().cipherName());
7980

81+
return materials.toBuilder()
82+
.encryptionContext(encryptionContext)
83+
.build();
84+
}
85+
86+
@Override
87+
public byte[] encryptDataKey(SecureRandom secureRandom, EncryptionMaterials materials) {
88+
// Convert to TreeMap for sorting of keys
89+
TreeMap<String, String> encryptionContext = new TreeMap<>(materials.encryptionContext());
8090
EncryptRequest request = EncryptRequest.builder()
8191
.keyId(_wrappingKeyId)
8292
.encryptionContext(encryptionContext)

src/main/java/software/amazon/encryption/s3/materials/S3Keyring.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public EncryptionMaterials onEncrypt(EncryptionMaterials materials) {
3434

3535
EncryptDataKeyStrategy encryptStrategy = encryptStrategy();
3636
try {
37+
// Allow encrypt strategy to modify the materials if necessary
38+
materials = encryptStrategy.modifyMaterials(materials);
39+
3740
byte[] ciphertext = encryptStrategy.encryptDataKey(_secureRandom, materials);
3841
EncryptedDataKey encryptedDataKey = EncryptedDataKey.builder()
3942
.keyProviderId(encryptStrategy.keyProviderId())

0 commit comments

Comments
 (0)