|
1 | 1 | package software.amazon.encryption.s3; |
2 | 2 |
|
| 3 | +import com.amazonaws.services.s3.AmazonS3EncryptionClientV2; |
| 4 | +import com.amazonaws.services.s3.AmazonS3EncryptionV2; |
| 5 | +import com.amazonaws.services.s3.model.CryptoConfigurationV2; |
| 6 | +import com.amazonaws.services.s3.model.CryptoMode; |
| 7 | +import com.amazonaws.services.s3.model.CryptoStorageMode; |
| 8 | +import com.amazonaws.services.s3.model.EncryptionMaterials; |
| 9 | +import com.amazonaws.services.s3.model.EncryptionMaterialsProvider; |
| 10 | +import com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider; |
3 | 11 | import java.security.SecureRandom; |
4 | 12 |
|
5 | 13 | import org.junit.jupiter.api.BeforeAll; |
|
9 | 17 | import software.amazon.awssdk.core.sync.RequestBody; |
10 | 18 | import software.amazon.awssdk.services.s3.S3Client; |
11 | 19 | import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
| 20 | +import software.amazon.awssdk.services.s3.model.ObjectIdentifier; |
12 | 21 | import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| 22 | +import software.amazon.awssdk.services.s3.model.S3Exception; |
13 | 23 | import software.amazon.encryption.s3.materials.AesKeyring; |
14 | 24 | import software.amazon.encryption.s3.materials.CryptographicMaterialsManager; |
15 | 25 | import software.amazon.encryption.s3.materials.DefaultCryptoMaterialsManager; |
|
23 | 33 | import java.security.KeyPair; |
24 | 34 | import java.security.KeyPairGenerator; |
25 | 35 | import java.security.NoSuchAlgorithmException; |
| 36 | +import java.util.ArrayList; |
26 | 37 | import java.util.HashMap; |
| 38 | +import java.util.List; |
27 | 39 | import java.util.Map; |
28 | 40 |
|
29 | | -import static org.junit.jupiter.api.Assertions.*; |
| 41 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
30 | 44 | import static org.mockito.ArgumentMatchers.any; |
31 | 45 | import static org.mockito.Mockito.mock; |
32 | 46 | import static org.mockito.Mockito.never; |
@@ -60,6 +74,104 @@ public static void setUp() throws NoSuchAlgorithmException { |
60 | 74 | RSA_KEY_PAIR = keyPairGen.generateKeyPair(); |
61 | 75 | } |
62 | 76 |
|
| 77 | + @Test |
| 78 | + public void deleteObjectWithInstructionFileSuccess() { |
| 79 | + final String objectKey = "delete-object-with-instruction-file"; |
| 80 | + |
| 81 | + // V2 Client |
| 82 | + EncryptionMaterialsProvider materialsProvider = |
| 83 | + new StaticEncryptionMaterialsProvider(new EncryptionMaterials(AES_KEY)); |
| 84 | + CryptoConfigurationV2 cryptoConfig = |
| 85 | + new CryptoConfigurationV2(CryptoMode.StrictAuthenticatedEncryption) |
| 86 | + .withStorageMode(CryptoStorageMode.InstructionFile); |
| 87 | + AmazonS3EncryptionV2 v2Client = AmazonS3EncryptionClientV2.encryptionBuilder() |
| 88 | + .withCryptoConfiguration(cryptoConfig) |
| 89 | + .withEncryptionMaterialsProvider(materialsProvider) |
| 90 | + .build(); |
| 91 | + |
| 92 | + // V3 Client |
| 93 | + S3Client v3Client = S3EncryptionClient.builder() |
| 94 | + .aesKey(AES_KEY) |
| 95 | + .build(); |
| 96 | + final String input = "DeleteObjectWithInstructionFileSuccess"; |
| 97 | + v2Client.putObject(BUCKET, objectKey, input); |
| 98 | + |
| 99 | + // Delete Object |
| 100 | + v3Client.deleteObject(builder -> builder.bucket(BUCKET).key(objectKey)); |
| 101 | + |
| 102 | + S3Client s3Client = S3Client.builder().build(); |
| 103 | + // Assert throw NoSuchKeyException when getObject for objectKey |
| 104 | + assertThrows(S3Exception.class, () -> s3Client.getObject(builder -> builder |
| 105 | + .bucket(BUCKET) |
| 106 | + .key(objectKey))); |
| 107 | + assertThrows(S3Exception.class, () -> s3Client.getObject(builder -> builder |
| 108 | + .bucket(BUCKET) |
| 109 | + .key(objectKey + ".instruction"))); |
| 110 | + |
| 111 | + // Cleanup |
| 112 | + v3Client.close(); |
| 113 | + s3Client.close(); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void deleteObjectsWithInstructionFilesSuccess() { |
| 118 | + final String[] objectKeys = {"delete-object-with-instruction-file-1", |
| 119 | + "delete-object-with-instruction-file-2", |
| 120 | + "delete-object-with-instruction-file-3"}; |
| 121 | + |
| 122 | + // V2 Client |
| 123 | + EncryptionMaterialsProvider materialsProvider = |
| 124 | + new StaticEncryptionMaterialsProvider(new EncryptionMaterials(AES_KEY)); |
| 125 | + CryptoConfigurationV2 cryptoConfig = |
| 126 | + new CryptoConfigurationV2(CryptoMode.StrictAuthenticatedEncryption) |
| 127 | + .withStorageMode(CryptoStorageMode.InstructionFile); |
| 128 | + AmazonS3EncryptionV2 v2Client = AmazonS3EncryptionClientV2.encryptionBuilder() |
| 129 | + .withCryptoConfiguration(cryptoConfig) |
| 130 | + .withEncryptionMaterialsProvider(materialsProvider) |
| 131 | + .build(); |
| 132 | + |
| 133 | + // V3 Client |
| 134 | + S3Client v3Client = S3EncryptionClient.builder() |
| 135 | + .aesKey(AES_KEY) |
| 136 | + .build(); |
| 137 | + final String input = "DeleteObjectsWithInstructionFileSuccess"; |
| 138 | + List<ObjectIdentifier> objects = new ArrayList<>(); |
| 139 | + for (String objectKey : objectKeys) { |
| 140 | + v2Client.putObject(BUCKET, objectKey, input); |
| 141 | + objects.add(ObjectIdentifier.builder().key(objectKey).build()); |
| 142 | + } |
| 143 | + |
| 144 | + // Delete Objects from S3 Buckets |
| 145 | + v3Client.deleteObjects(builder -> builder |
| 146 | + .bucket(BUCKET) |
| 147 | + .delete(builder1 -> builder1.objects(objects))); |
| 148 | + |
| 149 | + S3Client s3Client = S3Client.builder().build(); |
| 150 | + // Assert throw NoSuchKeyException when getObject for any of objectKeys |
| 151 | + assertThrows(S3Exception.class, () -> s3Client.getObject(builder -> builder |
| 152 | + .bucket(BUCKET) |
| 153 | + .key(objectKeys[0]))); |
| 154 | + assertThrows(S3Exception.class, () -> s3Client.getObject(builder -> builder |
| 155 | + .bucket(BUCKET) |
| 156 | + .key(objectKeys[0] + ".instruction"))); |
| 157 | + |
| 158 | + // Cleanup |
| 159 | + v3Client.close(); |
| 160 | + s3Client.close(); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + public void deleteObjectWithWrongObjectKeySuccess() { |
| 165 | + // V3 Client |
| 166 | + S3Client v3Client = S3EncryptionClient.builder() |
| 167 | + .aesKey(AES_KEY) |
| 168 | + .build(); |
| 169 | + assertDoesNotThrow(() -> v3Client.deleteObject(builder -> builder.bucket(BUCKET).key("InvalidKey"))); |
| 170 | + |
| 171 | + // Cleanup |
| 172 | + v3Client.close(); |
| 173 | + } |
| 174 | + |
63 | 175 | @Test |
64 | 176 | public void s3EncryptionClientWithMultipleKeyringsFails() { |
65 | 177 | assertThrows(S3EncryptionClientException.class, () -> S3EncryptionClient.builder() |
@@ -361,6 +473,7 @@ public void s3EncryptionClientFromAESKeyringUsesDifferentSecureRandomThanKeyring |
361 | 473 | /** |
362 | 474 | * A simple, reusable round-trip (encryption + decryption) using a given |
363 | 475 | * S3Client. Useful for testing client configuration. |
| 476 | + * |
364 | 477 | * @param v3Client the client under test |
365 | 478 | */ |
366 | 479 | private void simpleV3RoundTrip(final S3Client v3Client, final String objectKey) { |
|
0 commit comments