|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 | package software.amazon.encryption.s3; |
4 | 4 |
|
| 5 | +import ch.qos.logback.classic.Logger; |
| 6 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 7 | +import ch.qos.logback.core.read.ListAppender; |
5 | 8 | import com.amazonaws.services.kms.AWSKMS; |
6 | 9 | import com.amazonaws.services.kms.AWSKMSClientBuilder; |
7 | 10 | import com.amazonaws.services.s3.AmazonS3Encryption; |
|
15 | 18 | import com.amazonaws.services.s3.model.EncryptedPutObjectRequest; |
16 | 19 | import com.amazonaws.services.s3.model.EncryptionMaterials; |
17 | 20 | import com.amazonaws.services.s3.model.EncryptionMaterialsProvider; |
18 | | -import com.amazonaws.services.s3.model.GetObjectMetadataRequest; |
19 | | -import com.amazonaws.services.s3.model.InitiateMultipartUploadRequest; |
20 | | -import com.amazonaws.services.s3.model.InitiateMultipartUploadResult; |
21 | 21 | import com.amazonaws.services.s3.model.KMSEncryptionMaterials; |
22 | 22 | import com.amazonaws.services.s3.model.KMSEncryptionMaterialsProvider; |
23 | | -import com.amazonaws.services.s3.model.ObjectMetadata; |
24 | 23 | import com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider; |
25 | | -import com.amazonaws.services.s3.model.StorageClass; |
26 | | -import com.amazonaws.services.s3.model.UploadObjectRequest; |
27 | 24 | import org.junit.jupiter.api.BeforeAll; |
28 | 25 | import org.junit.jupiter.api.Test; |
| 26 | +import org.slf4j.LoggerFactory; |
29 | 27 | import software.amazon.awssdk.core.ResponseBytes; |
30 | 28 | import software.amazon.awssdk.core.sync.RequestBody; |
31 | 29 | import software.amazon.awssdk.services.s3.S3Client; |
|
44 | 42 | import javax.crypto.SecretKey; |
45 | 43 | import java.io.ByteArrayInputStream; |
46 | 44 | import java.io.IOException; |
47 | | -import java.io.InputStream; |
48 | 45 | import java.nio.charset.StandardCharsets; |
49 | 46 | import java.security.KeyPair; |
50 | 47 | import java.security.KeyPairGenerator; |
51 | 48 | import java.security.NoSuchAlgorithmException; |
52 | 49 | import java.util.HashMap; |
53 | 50 | import java.util.Map; |
54 | | -import java.util.concurrent.ExecutionException; |
55 | 51 |
|
56 | 52 | import static org.junit.jupiter.api.Assertions.assertEquals; |
57 | | -import static org.junit.jupiter.api.Assertions.assertNotEquals; |
| 53 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
58 | 54 | import static org.junit.jupiter.api.Assertions.assertThrows; |
59 | 55 | import static org.junit.jupiter.api.Assertions.assertTrue; |
60 | 56 |
|
@@ -983,69 +979,158 @@ public void nullMaterialDescriptionV3() { |
983 | 979 | } |
984 | 980 |
|
985 | 981 | @Test |
986 | | - public void validateAgainstSettingLegacyWrappingOnClientWithAesKeyringPassedV1toV3() { |
987 | | - try { |
988 | | - AesKeyring aesKeyring = AesKeyring.builder() |
989 | | - .wrappingKey(AES_KEY) |
990 | | - .build(); |
991 | | - |
992 | | - S3Client wrappedClient = S3Client.create(); |
993 | | - S3Client v3Client = S3EncryptionClient.builder() |
994 | | - .keyring(aesKeyring) |
995 | | - .wrappedClient(wrappedClient) |
996 | | - .enableLegacyWrappingAlgorithms(true) |
997 | | - .enableLegacyUnauthenticatedModes(true) |
998 | | - .build(); |
999 | | - throw new RuntimeException("Expected failure"); |
1000 | | - } catch (S3EncryptionClientException e) { |
1001 | | - assertTrue(e.getMessage().contains("Legacy wrapping algorithms are not enabled for this keyring")); |
1002 | | - } |
| 982 | + public void LegacyWrappingEnabledOnClientButNotOnAesKeyring() { |
| 983 | + Logger logger = (Logger) LoggerFactory.getLogger(S3EncryptionClient.class); |
| 984 | + ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); |
| 985 | + listAppender.start(); |
| 986 | + logger.addAppender(listAppender); |
| 987 | + |
| 988 | + AesKeyring aesKeyring = AesKeyring.builder() |
| 989 | + .wrappingKey(AES_KEY) |
| 990 | + .build(); |
| 991 | + |
| 992 | + S3Client wrappedClient = S3Client.create(); |
| 993 | + S3Client v3Client = S3EncryptionClient.builder() |
| 994 | + .keyring(aesKeyring) |
| 995 | + .wrappedClient(wrappedClient) |
| 996 | + .enableLegacyWrappingAlgorithms(true) |
| 997 | + .enableLegacyUnauthenticatedModes(true) |
| 998 | + .build(); |
1003 | 999 |
|
| 1000 | + assertTrue(listAppender.list.stream().anyMatch(event -> event.getMessage().contains("enableLegacyWrappingAlgorithms is set on the client, but is not set on the keyring provided. In order to enable legacy wrapping algorithms, set enableLegacyWrappingAlgorithms to true in the keyring's builder."))); |
| 1001 | + logger.detachAppender(listAppender); |
1004 | 1002 | } |
1005 | 1003 |
|
1006 | 1004 | @Test |
1007 | | - public void validateAgainstSettingLegacyWrappingOnClientWithRsaKeyringPassedV1toV3() { |
1008 | | - try { |
1009 | | - PartialRsaKeyPair partialRsaKeyPair = PartialRsaKeyPair.builder() |
1010 | | - .publicKey(RSA_KEY_PAIR.getPublic()) |
1011 | | - .privateKey(RSA_KEY_PAIR.getPrivate()) |
1012 | | - .build(); |
1013 | | - |
1014 | | - RsaKeyring rsaKeyring = RsaKeyring.builder() |
1015 | | - .wrappingKeyPair(partialRsaKeyPair) |
1016 | | - .build(); |
1017 | | - |
1018 | | - S3Client wrappedClient = S3Client.create(); |
1019 | | - S3Client v3Client = S3EncryptionClient.builder() |
1020 | | - .keyring(rsaKeyring) |
1021 | | - .wrappedClient(wrappedClient) |
1022 | | - .enableLegacyWrappingAlgorithms(true) |
1023 | | - .enableLegacyUnauthenticatedModes(true) |
1024 | | - .build(); |
1025 | | - throw new RuntimeException("Expected failure"); |
1026 | | - } catch (S3EncryptionClientException e) { |
1027 | | - assertTrue(e.getMessage().contains("Legacy wrapping algorithms are not enabled for this keyring")); |
1028 | | - } |
| 1005 | + public void LegacyWrappingEnabledOnClientButNotOnRsaKeyring() { |
| 1006 | + Logger logger = (Logger) LoggerFactory.getLogger(S3EncryptionClient.class); |
| 1007 | + ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); |
| 1008 | + listAppender.start(); |
| 1009 | + logger.addAppender(listAppender); |
| 1010 | + |
| 1011 | + PartialRsaKeyPair partialRsaKeyPair = PartialRsaKeyPair.builder() |
| 1012 | + .publicKey(RSA_KEY_PAIR.getPublic()) |
| 1013 | + .privateKey(RSA_KEY_PAIR.getPrivate()) |
| 1014 | + .build(); |
| 1015 | + |
| 1016 | + RsaKeyring rsaKeyring = RsaKeyring.builder() |
| 1017 | + .wrappingKeyPair(partialRsaKeyPair) |
| 1018 | + .build(); |
| 1019 | + |
| 1020 | + S3Client wrappedClient = S3Client.create(); |
| 1021 | + S3Client v3Client = S3EncryptionClient.builder() |
| 1022 | + .keyring(rsaKeyring) |
| 1023 | + .wrappedClient(wrappedClient) |
| 1024 | + .enableLegacyWrappingAlgorithms(true) |
| 1025 | + .enableLegacyUnauthenticatedModes(true) |
| 1026 | + .build(); |
| 1027 | + |
| 1028 | + assertTrue(listAppender.list.stream().anyMatch(event -> event.getMessage().contains("enableLegacyWrappingAlgorithms is set on the client, but is not set on the keyring provided. In order to enable legacy wrapping algorithms, set enableLegacyWrappingAlgorithms to true in the keyring's builder."))); |
| 1029 | + logger.detachAppender(listAppender); |
| 1030 | + |
1029 | 1031 | } |
1030 | 1032 |
|
1031 | 1033 | @Test |
1032 | | - public void validateAgainstSettingLegacyWrappingOnClientWithKmsKeyringPassedV1toV3() { |
1033 | | - try { |
1034 | | - KmsKeyring kmsKeyring = KmsKeyring.builder() |
1035 | | - .wrappingKeyId(KMS_KEY_ID) |
1036 | | - .build(); |
1037 | | - |
1038 | | - S3Client wrappedClient = S3Client.create(); |
1039 | | - S3Client v3Client = S3EncryptionClient.builder() |
1040 | | - .keyring(kmsKeyring) |
1041 | | - .wrappedClient(wrappedClient) |
1042 | | - .enableLegacyWrappingAlgorithms(true) |
1043 | | - .enableLegacyUnauthenticatedModes(true) |
1044 | | - .build(); |
1045 | | - throw new RuntimeException("Expected failure"); |
1046 | | - } catch (S3EncryptionClientException e) { |
1047 | | - assertTrue(e.getMessage().contains("Legacy wrapping algorithms are not enabled for this keyring")); |
| 1034 | + public void LegacyWrappingEnabledOnClientButNotOnKmsKeyring() { |
| 1035 | + Logger logger = (Logger) LoggerFactory.getLogger(S3EncryptionClient.class); |
| 1036 | + ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); |
| 1037 | + listAppender.start(); |
| 1038 | + logger.addAppender(listAppender); |
| 1039 | + |
| 1040 | + KmsKeyring kmsKeyring = KmsKeyring.builder() |
| 1041 | + .wrappingKeyId(KMS_KEY_ID) |
| 1042 | + .build(); |
| 1043 | + |
| 1044 | + S3Client wrappedClient = S3Client.create(); |
| 1045 | + S3Client v3Client = S3EncryptionClient.builder() |
| 1046 | + .keyring(kmsKeyring) |
| 1047 | + .wrappedClient(wrappedClient) |
| 1048 | + .enableLegacyWrappingAlgorithms(true) |
| 1049 | + .enableLegacyUnauthenticatedModes(true) |
| 1050 | + .build(); |
| 1051 | + |
| 1052 | + assertTrue(listAppender.list.stream().anyMatch(event -> event.getMessage().contains("enableLegacyWrappingAlgorithms is set on the client, but is not set on the keyring provided. In order to enable legacy wrapping algorithms, set enableLegacyWrappingAlgorithms to true in the keyring's builder."))); |
| 1053 | + logger.detachAppender(listAppender); |
| 1054 | + |
1048 | 1055 | } |
| 1056 | + |
| 1057 | + @Test |
| 1058 | + public void LegacyWrappingEnabledOnBothClientAndAesKeyring() { |
| 1059 | + Logger logger = (Logger) LoggerFactory.getLogger(S3EncryptionClient.class); |
| 1060 | + ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); |
| 1061 | + listAppender.start(); |
| 1062 | + logger.addAppender(listAppender); |
| 1063 | + |
| 1064 | + AesKeyring aesKeyring = AesKeyring.builder() |
| 1065 | + .wrappingKey(AES_KEY) |
| 1066 | + .enableLegacyWrappingAlgorithms(true) |
| 1067 | + .build(); |
| 1068 | + |
| 1069 | + S3Client wrappedClient = S3Client.create(); |
| 1070 | + S3Client v3Client = S3EncryptionClient.builder() |
| 1071 | + .keyring(aesKeyring) |
| 1072 | + .wrappedClient(wrappedClient) |
| 1073 | + .enableLegacyWrappingAlgorithms(true) |
| 1074 | + .enableLegacyUnauthenticatedModes(true) |
| 1075 | + .build(); |
| 1076 | + |
| 1077 | + assertFalse(listAppender.list.stream() |
| 1078 | + .anyMatch(event -> event.getMessage().contains("enableLegacyWrappingAlgorithms is set on the client, but is not set on the keyring provided. In order to enable legacy wrapping algorithms, set enableLegacyWrappingAlgorithms to true in the keyring's builder."))); |
| 1079 | + logger.detachAppender(listAppender); |
| 1080 | + } |
| 1081 | + |
| 1082 | + @Test |
| 1083 | + public void LegacyWrappingEnabledOnBothClientAndRsaKeyring() { |
| 1084 | + Logger logger = (Logger) LoggerFactory.getLogger(S3EncryptionClient.class); |
| 1085 | + ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); |
| 1086 | + listAppender.start(); |
| 1087 | + logger.addAppender(listAppender); |
| 1088 | + |
| 1089 | + PartialRsaKeyPair partialRsaKeyPair = PartialRsaKeyPair.builder() |
| 1090 | + .publicKey(RSA_KEY_PAIR.getPublic()) |
| 1091 | + .privateKey(RSA_KEY_PAIR.getPrivate()) |
| 1092 | + .build(); |
| 1093 | + |
| 1094 | + RsaKeyring rsaKeyring = RsaKeyring.builder() |
| 1095 | + .wrappingKeyPair(partialRsaKeyPair) |
| 1096 | + .enableLegacyWrappingAlgorithms(true) |
| 1097 | + .build(); |
| 1098 | + |
| 1099 | + S3Client wrappedClient = S3Client.create(); |
| 1100 | + S3Client v3Client = S3EncryptionClient.builder() |
| 1101 | + .keyring(rsaKeyring) |
| 1102 | + .wrappedClient(wrappedClient) |
| 1103 | + .enableLegacyWrappingAlgorithms(true) |
| 1104 | + .enableLegacyUnauthenticatedModes(true) |
| 1105 | + .build(); |
| 1106 | + |
| 1107 | + assertFalse(listAppender.list.stream() |
| 1108 | + .anyMatch(event -> event.getMessage().contains("enableLegacyWrappingAlgorithms is set on the client, but is not set on the keyring provided. In order to enable legacy wrapping algorithms, set enableLegacyWrappingAlgorithms to true in the keyring's builder."))); |
| 1109 | + logger.detachAppender(listAppender); |
1049 | 1110 | } |
1050 | 1111 |
|
| 1112 | + @Test |
| 1113 | + public void LegacyWrappingEnabledOnBothClientAndKmsKeyring() { |
| 1114 | + Logger logger = (Logger) LoggerFactory.getLogger(S3EncryptionClient.class); |
| 1115 | + ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); |
| 1116 | + listAppender.start(); |
| 1117 | + logger.addAppender(listAppender); |
| 1118 | + |
| 1119 | + KmsKeyring kmsKeyring = KmsKeyring.builder() |
| 1120 | + .wrappingKeyId(KMS_KEY_ID) |
| 1121 | + .enableLegacyWrappingAlgorithms(true) |
| 1122 | + .build(); |
| 1123 | + |
| 1124 | + S3Client wrappedClient = S3Client.create(); |
| 1125 | + S3Client v3Client = S3EncryptionClient.builder() |
| 1126 | + .keyring(kmsKeyring) |
| 1127 | + .wrappedClient(wrappedClient) |
| 1128 | + .enableLegacyWrappingAlgorithms(true) |
| 1129 | + .enableLegacyUnauthenticatedModes(true) |
| 1130 | + .build(); |
| 1131 | + |
| 1132 | + assertFalse(listAppender.list.stream() |
| 1133 | + .anyMatch(event -> event.getMessage().contains("enableLegacyWrappingAlgorithms is set on the client, but is not set on the keyring provided. In order to enable legacy wrapping algorithms, set enableLegacyWrappingAlgorithms to true in the keyring's builder."))); |
| 1134 | + logger.detachAppender(listAppender); |
| 1135 | + } |
1051 | 1136 | } |
0 commit comments