|
19 | 19 | package org.apache.hadoop.fs.s3a;
|
20 | 20 |
|
21 | 21 | import java.io.IOException;
|
| 22 | +import java.util.Map; |
| 23 | +import java.util.Optional; |
22 | 24 |
|
| 25 | +import org.apache.hadoop.fs.s3a.impl.HeaderProcessing; |
| 26 | +import org.assertj.core.api.Assertions; |
23 | 27 | import software.amazon.awssdk.services.s3.model.HeadObjectResponse;
|
24 | 28 |
|
25 | 29 | import org.apache.commons.codec.digest.DigestUtils;
|
|
28 | 32 | import org.apache.hadoop.fs.Path;
|
29 | 33 |
|
30 | 34 | import static org.apache.hadoop.fs.s3a.Constants.S3_ENCRYPTION_KEY;
|
| 35 | +import static org.apache.hadoop.fs.s3a.impl.HeaderProcessing.XA_ENCRYPTION_KEY_ID; |
| 36 | +import static org.apache.hadoop.fs.s3a.impl.HeaderProcessing.XA_SERVER_SIDE_ENCRYPTION; |
31 | 37 | import static org.assertj.core.api.Assertions.assertThat;
|
32 | 38 |
|
33 | 39 | public final class EncryptionTestUtils {
|
@@ -111,4 +117,31 @@ public static void assertEncrypted(S3AFileSystem fs,
|
111 | 117 | }
|
112 | 118 | }
|
113 | 119 |
|
| 120 | + /** |
| 121 | + * Assert that a path is encrypted with right encryption settings. |
| 122 | + * @param fs filesystem. |
| 123 | + * @param path path |
| 124 | + * @param algorithm encryption algorithm. |
| 125 | + * @param kmsKey full kms key if present. |
| 126 | + * @throws IOException any IOE. |
| 127 | + */ |
| 128 | + public static void validateEncryptionFileAttributes(S3AFileSystem fs, |
| 129 | + Path path, |
| 130 | + String algorithm, |
| 131 | + Optional<String> kmsKey) throws IOException { |
| 132 | + Map<String, byte[]> xAttrs = fs.getXAttrs(path); |
| 133 | + Assertions.assertThat(xAttrs.get(XA_SERVER_SIDE_ENCRYPTION)) |
| 134 | + .describedAs("Server side encryption must not be null") |
| 135 | + .isNotNull(); |
| 136 | + Assertions.assertThat(HeaderProcessing.decodeBytes(xAttrs.get(XA_SERVER_SIDE_ENCRYPTION))) |
| 137 | + .describedAs("Server side encryption algorithm must match") |
| 138 | + .isEqualTo(algorithm); |
| 139 | + Assertions.assertThat(xAttrs) |
| 140 | + .describedAs("Encryption key id should be present") |
| 141 | + .containsKey(XA_ENCRYPTION_KEY_ID); |
| 142 | + kmsKey.ifPresent(s -> Assertions |
| 143 | + .assertThat(HeaderProcessing.decodeBytes(xAttrs.get(XA_ENCRYPTION_KEY_ID))) |
| 144 | + .describedAs("Encryption key id should match with the kms key") |
| 145 | + .isEqualTo(s)); |
| 146 | + } |
114 | 147 | }
|
0 commit comments