Skip to content

Commit ae7e20d

Browse files
author
Anirav Kareddy
committed
Fixed second round of PR comments
1 parent c4fc099 commit ae7e20d

File tree

9 files changed

+372
-271
lines changed

9 files changed

+372
-271
lines changed

src/main/java/software/amazon/encryption/s3/internal/ContentMetadata.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ public class ContentMetadata {
1515

1616
private final EncryptedDataKey _encryptedDataKey;
1717
private final String _encryptedDataKeyAlgorithm;
18+
19+
/**
20+
* This field stores either encryption context or material description.
21+
* We use a single field to store both in order to maintain backwards
22+
* compatibility with V2, which treated both as the same.
23+
*/
1824
private final Map<String, String> _encryptionContextOrMatDesc;
1925

2026
private final byte[] _contentIv;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public boolean isLegacy() {
9090

9191
@Override
9292
public EncryptionMaterials modifyMaterials(EncryptionMaterials materials) {
93-
return modifyMaterialHelper(materials);
93+
return modifyMaterialsForRawKeyring(materials);
9494
}
9595

9696
@Override

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
66
import software.amazon.awssdk.services.s3.model.S3Request;
7+
import software.amazon.encryption.s3.S3EncryptionClientException;
78
import software.amazon.encryption.s3.algorithms.AlgorithmSuite;
89
import software.amazon.encryption.s3.internal.CipherMode;
910
import software.amazon.encryption.s3.internal.CipherProvider;
@@ -187,6 +188,9 @@ public Builder plaintextLength(long plaintextLength) {
187188
}
188189

189190
public EncryptionMaterials build() {
191+
if (!_materialsDescription.isEmpty() && !_encryptionContext.isEmpty()) {
192+
throw new S3EncryptionClientException("MaterialsDescription and EncryptionContext cannot both be set!");
193+
}
190194
return new EncryptionMaterials(this);
191195
}
192196
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public String get(Object key) {
5454

5555
@Override
5656
public String put(String key, String value) {
57-
return materialsDescription.put(key, value);
57+
throw new UnsupportedOperationException("This map is immutable");
5858
}
5959

6060
@Override
@@ -64,7 +64,7 @@ public String remove(Object key) {
6464

6565
@Override
6666
public void putAll(Map<? extends String, ? extends String> m) {
67-
materialsDescription.putAll(m);
67+
throw new UnsupportedOperationException("This map is immutable");
6868
}
6969

7070
@Override
@@ -101,9 +101,7 @@ public Builder putAll(Map<String, String> description) {
101101
if (description == null) {
102102
throw new IllegalArgumentException("Description must not be null");
103103
}
104-
for (Map.Entry<String, String> entry : description.entrySet()) {
105-
put(entry.getKey(), entry.getValue());
106-
}
104+
materialsDescription.putAll(description);
107105
return this;
108106
}
109107
public MaterialsDescription build() {

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,46 @@
1010
*/
1111
public abstract class RawKeyring extends S3Keyring {
1212
protected final MaterialsDescription _materialsDescription;
13-
protected final boolean _reEncryptInstructionFile;
1413

1514
protected RawKeyring(Builder<?, ?> builder) {
1615
super(builder);
1716
_materialsDescription = builder._materialsDescription;
18-
_reEncryptInstructionFile = builder._reEncryptInstructionFile;
1917
}
20-
public MaterialsDescription getMaterialsDescription() {
21-
return _materialsDescription;
22-
}
23-
public boolean getReEncryptInstructionFile() {
24-
return _reEncryptInstructionFile;
25-
}
26-
public EncryptionMaterials modifyMaterialHelper(EncryptionMaterials materials) {
18+
19+
public EncryptionMaterials modifyMaterialsForRawKeyring(EncryptionMaterials materials) {
2720
warnIfEncryptionContextIsPresent(materials);
2821
if (_materialsDescription != null && !_materialsDescription.isEmpty()) {
2922
materials = materials.toBuilder()
3023
.materialsDescription(_materialsDescription)
3124
.build();
32-
return materials;
3325
}
34-
3526
return materials;
3627
}
28+
29+
/**
30+
* Checks if an encryption context is present in the EncryptionMaterials and issues a warning
31+
* if an encryption context is found.
32+
* <p>
33+
* Encryption context is not recommended for use with
34+
* non-KMS keyrings as it does not provide additional security benefits and is not stored.
35+
*
36+
* @param materials EncryptionMaterials
37+
*/
38+
3739
public void warnIfEncryptionContextIsPresent(EncryptionMaterials materials) {
3840
materials.s3Request().overrideConfiguration()
3941
.flatMap(overrideConfiguration ->
4042
overrideConfiguration.executionAttributes()
4143
.getOptionalAttribute(S3EncryptionClient.ENCRYPTION_CONTEXT))
42-
.ifPresent(ctx -> LogFactory.getLog(getClass()).warn("Usage of Encryption Context provides no security benefit in " + getClass().getSimpleName()));
44+
.ifPresent(ctx -> LogFactory.getLog(getClass()).warn("Usage of Encryption Context provides no " +
45+
"security benefit in " + getClass().getSimpleName() + ".Additionally, this Encryption Context WILL NOT be " +
46+
"stored in the material description. Provide a MaterialDescription in the Keyring's builder instead."));
4347
}
48+
4449
public static abstract class Builder<KeyringT extends RawKeyring, BuilderT extends Builder<KeyringT, BuilderT>>
4550
extends S3Keyring.Builder<KeyringT, BuilderT> {
4651

4752
protected MaterialsDescription _materialsDescription;
48-
protected boolean _reEncryptInstructionFile = false;
4953

5054
protected Builder() {
5155
super();
@@ -55,10 +59,5 @@ public BuilderT materialsDescription(MaterialsDescription materialsDescription)
5559
_materialsDescription = materialsDescription;
5660
return builder();
5761
}
58-
59-
public BuilderT reEncryptInstructionFile(boolean reEncryptInstructionFile) {
60-
_reEncryptInstructionFile = reEncryptInstructionFile;
61-
return builder();
62-
}
6362
}
6463
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public boolean isLegacy() {
9595

9696
@Override
9797
public EncryptionMaterials modifyMaterials(EncryptionMaterials materials) {
98-
return modifyMaterialHelper(materials);
98+
return modifyMaterialsForRawKeyring(materials);
9999
}
100100

101101
@Override

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

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,11 @@ abstract public class S3Keyring implements Keyring {
2323
protected final DataKeyGenerator _dataKeyGenerator;
2424
private final boolean _enableLegacyWrappingAlgorithms;
2525
private final SecureRandom _secureRandom;
26-
protected final MaterialsDescription _materialsDescription;
2726

2827
protected S3Keyring(Builder<?, ?> builder) {
2928
_enableLegacyWrappingAlgorithms = builder._enableLegacyWrappingAlgorithms;
3029
_secureRandom = builder._secureRandom;
3130
_dataKeyGenerator = builder._dataKeyGenerator;
32-
_materialsDescription = builder._materialsDescription;
3331
}
3432

3533
/**
@@ -130,8 +128,6 @@ abstract public static class Builder<KeyringT extends S3Keyring, BuilderT extend
130128
private boolean _enableLegacyWrappingAlgorithms = false;
131129
private SecureRandom _secureRandom;
132130
private DataKeyGenerator _dataKeyGenerator = new DefaultDataKeyGenerator();
133-
protected MaterialsDescription _materialsDescription;
134-
135131

136132
protected Builder() {}
137133

@@ -162,11 +158,6 @@ public BuilderT dataKeyGenerator(final DataKeyGenerator dataKeyGenerator) {
162158
_dataKeyGenerator = dataKeyGenerator;
163159
return builder();
164160
}
165-
public BuilderT materialsDescription(final MaterialsDescription materialsDescription) {
166-
_materialsDescription = materialsDescription;
167-
return builder();
168-
}
169-
170161
abstract public KeyringT build();
171162
}
172163
}

0 commit comments

Comments
 (0)