Skip to content

Commit 5d66063

Browse files
committed
HADOOP-19197. S3A: KMS Encryption Context support: follow-up
Followup the main HADOOP-19197 patch to address serialization and compilation issues * Recreate serialization ID * Restore two arg constructor * Define DEFAULT_S3_ENCRYPTION_CONTEXT to specify what the default value is (just "", but being explicit) * Tests
1 parent 3d905f9 commit 5d66063

File tree

5 files changed

+60
-8
lines changed

5 files changed

+60
-8
lines changed

hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/Constants.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,13 @@ private Constants() {
785785
public static final String S3_ENCRYPTION_CONTEXT =
786786
"fs.s3a.encryption.context";
787787

788+
/**
789+
* Default S3-SSE encryption context.
790+
* value:{@value}
791+
*/
792+
public static final String DEFAULT_S3_ENCRYPTION_CONTEXT =
793+
"";
794+
788795
/**
789796
* Client side encryption (CSE-CUSTOM) with custom cryptographic material manager class name.
790797
* Custom keyring class name for CSE-KMS.

hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/auth/delegation/EncryptionSecrets.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.apache.hadoop.io.Text;
3232
import org.apache.hadoop.io.Writable;
3333

34+
import static org.apache.hadoop.fs.s3a.Constants.DEFAULT_S3_ENCRYPTION_CONTEXT;
35+
3436
/**
3537
* Encryption options in a form which can serialized or marshalled as a hadoop
3638
* Writeable.
@@ -54,7 +56,10 @@ public class EncryptionSecrets implements Writable, Serializable {
5456

5557
public static final int MAX_SECRET_LENGTH = 2048;
5658

57-
private static final long serialVersionUID = 1208329045511296375L;
59+
/**
60+
* Change this after any change to the payload: {@value}.
61+
*/
62+
private static final long serialVersionUID = 8834417969966697162L;
5863

5964
/**
6065
* Encryption algorithm to use: must match one in
@@ -70,7 +75,7 @@ public class EncryptionSecrets implements Writable, Serializable {
7075
/**
7176
* Encryption context: base64-encoded UTF-8 string.
7277
*/
73-
private String encryptionContext = "";
78+
private String encryptionContext = DEFAULT_S3_ENCRYPTION_CONTEXT;
7479

7580
/**
7681
* This field isn't serialized/marshalled; it is rebuilt from the
@@ -86,7 +91,24 @@ public EncryptionSecrets() {
8691
}
8792

8893
/**
89-
* Create a pair of secrets.
94+
* Create a tuple of secrets. The encryption context is set to "".
95+
* This constructor is used in external implementations of S3A delegation
96+
* tokens, sp MUST be retained even if there is no use in our own
97+
* production code.
98+
* @param encryptionAlgorithm algorithm enumeration.
99+
* @param encryptionKey key/key reference.
100+
* @throws IOException failure to initialize.
101+
* @deprecated use {@link #EncryptionSecrets(S3AEncryptionMethods, String, String)}
102+
* which takes an encryption context.
103+
*/
104+
public EncryptionSecrets(final S3AEncryptionMethods encryptionAlgorithm,
105+
final String encryptionKey) throws IOException {
106+
this(encryptionAlgorithm.getMethod(), encryptionKey,
107+
DEFAULT_S3_ENCRYPTION_CONTEXT);
108+
}
109+
110+
/**
111+
* Create a 3/tuple of secrets.
90112
* @param encryptionAlgorithm algorithm enumeration.
91113
* @param encryptionKey key/key reference.
92114
* @param encryptionContext base64-encoded string with the encryption context key-value pairs.
@@ -99,7 +121,7 @@ public EncryptionSecrets(final S3AEncryptionMethods encryptionAlgorithm,
99121
}
100122

101123
/**
102-
* Create a pair of secrets.
124+
* Create a 3/tuple of secrets.
103125
* @param encryptionAlgorithm algorithm name
104126
* @param encryptionKey key/key reference.
105127
* @param encryptionContext base64-encoded string with the encryption context key-value pairs.

hadoop-tools/hadoop-aws/src/main/java/org/apache/hadoop/fs/s3a/impl/S3AEncryption.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.hadoop.conf.Configuration;
3232
import org.apache.hadoop.fs.s3a.S3AUtils;
3333

34+
import static org.apache.hadoop.fs.s3a.Constants.DEFAULT_S3_ENCRYPTION_CONTEXT;
3435
import static org.apache.hadoop.fs.s3a.Constants.S3_ENCRYPTION_CONTEXT;
3536

3637
/**
@@ -61,7 +62,7 @@ public static String getS3EncryptionContext(String bucket, Configuration conf)
6162
}
6263
if (encryptionContext == null) {
6364
// no encryption context, return ""
64-
return "";
65+
return DEFAULT_S3_ENCRYPTION_CONTEXT;
6566
}
6667
return encryptionContext;
6768
}

hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/auth/TestMarshalledCredentials.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.net.URI;
2222
import java.net.URISyntaxException;
2323

24+
import org.assertj.core.api.Assertions;
2425
import software.amazon.awssdk.auth.credentials.AwsCredentials;
2526
import org.junit.jupiter.api.BeforeEach;
2627
import org.junit.jupiter.api.Test;
@@ -78,13 +79,33 @@ public void testRoundTripNoSessionData() throws Throwable {
7879

7980
@Test
8081
public void testRoundTripEncryptionData() throws Throwable {
82+
final String context = "encryptionContext";
8183
EncryptionSecrets secrets = new EncryptionSecrets(
8284
S3AEncryptionMethods.SSE_KMS,
8385
"key",
84-
"encryptionContext");
86+
context);
8587
EncryptionSecrets result = S3ATestUtils.roundTrip(secrets,
8688
new Configuration());
8789
assertEquals(secrets, result, "round trip");
90+
Assertions.assertThat(result .getEncryptionContext())
91+
.describedAs("encryptionContext")
92+
.isEqualTo(context);
93+
}
94+
95+
@Test
96+
public void testRoundTripEncryptionSecretsNoContext() throws Throwable {
97+
EncryptionSecrets secrets = new EncryptionSecrets(
98+
S3AEncryptionMethods.SSE_KMS,
99+
"key");
100+
EncryptionSecrets result = S3ATestUtils.roundTrip(secrets,
101+
new Configuration());
102+
assertEquals(secrets, result, "round trip");
103+
// not equal to secrets with a context
104+
Assertions.assertThat(result)
105+
.isNotEqualTo(new EncryptionSecrets(
106+
S3AEncryptionMethods.SSE_KMS,
107+
"key",
108+
"encryptionContext"));
88109
}
89110

90111
@Test

hadoop-tools/hadoop-aws/src/test/java/org/apache/hadoop/fs/s3a/impl/TestRequestFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.apache.hadoop.test.AbstractHadoopTestBase;
5757

5858
import static org.apache.hadoop.fs.s3a.Constants.DEFAULT_PART_UPLOAD_TIMEOUT;
59+
import static org.apache.hadoop.fs.s3a.Constants.DEFAULT_S3_ENCRYPTION_CONTEXT;
5960
import static org.apache.hadoop.fs.s3a.impl.PutObjectOptions.defaultOptions;
6061
import static org.apache.hadoop.test.LambdaTestUtils.intercept;
6162
import static org.assertj.core.api.Assertions.assertThat;
@@ -89,7 +90,7 @@ public void testRequestFactoryWithEncryption() throws Throwable {
8990
.withBucket("bucket")
9091
.withEncryptionSecrets(
9192
new EncryptionSecrets(S3AEncryptionMethods.SSE_KMS,
92-
"kms:key", ""))
93+
"kms:key", DEFAULT_S3_ENCRYPTION_CONTEXT))
9394
.build();
9495
createFactoryObjects(factory);
9596
}
@@ -329,7 +330,7 @@ public void testCompleteMultipartUploadRequestWithChecksumAlgorithmAndSSEC() thr
329330
.encodeToString(encryptionKey);
330331
final String encryptionKeyMd5 = Md5Utils.md5AsBase64(encryptionKey);
331332
final EncryptionSecrets encryptionSecrets = new EncryptionSecrets(S3AEncryptionMethods.SSE_C,
332-
encryptionKeyBase64, null);
333+
encryptionKeyBase64);
333334
RequestFactory factory = RequestFactoryImpl.builder()
334335
.withBucket("bucket")
335336
.withChecksumAlgorithm(ChecksumAlgorithm.CRC32_C)

0 commit comments

Comments
 (0)