|
| 1 | +package software.amazon.encryption.s3; |
| 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.EncryptionMaterialsProvider; |
| 9 | +import com.amazonaws.services.s3.model.KMSEncryptionMaterials; |
| 10 | +import com.amazonaws.services.s3.model.StaticEncryptionMaterialsProvider; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | +import software.amazon.awssdk.core.ResponseBytes; |
| 13 | +import software.amazon.awssdk.core.sync.RequestBody; |
| 14 | +import software.amazon.awssdk.services.s3.S3Client; |
| 15 | +import software.amazon.awssdk.services.s3.model.GetObjectResponse; |
| 16 | +import software.amazon.awssdk.services.s3.model.NoSuchKeyException; |
| 17 | +import software.amazon.encryption.s3.internal.InstructionFileConfig; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 21 | +import static org.junit.jupiter.api.Assertions.fail; |
| 22 | +import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.BUCKET; |
| 23 | +import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.KMS_KEY_ID; |
| 24 | +import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.appendTestSuffix; |
| 25 | +import static software.amazon.encryption.s3.utils.S3EncryptionClientTestResources.deleteObject; |
| 26 | + |
| 27 | +public class S3EncryptionClientInstructionFileTest { |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testInstructionFileExists() { |
| 31 | + final String objectKey = appendTestSuffix("instruction-file-put-object"); |
| 32 | + final String input = "SimpleTestOfV3EncryptionClient"; |
| 33 | + S3Client wrappedClient = S3Client.create(); |
| 34 | + S3Client s3Client = S3EncryptionClient.builder() |
| 35 | + .instructionFileConfig(InstructionFileConfig.builder() |
| 36 | + .instructionFileClient(wrappedClient) |
| 37 | + .enableInstructionFilePutObject(true) |
| 38 | + .build()) |
| 39 | + .kmsKeyId(KMS_KEY_ID) |
| 40 | + .build(); |
| 41 | + |
| 42 | + s3Client.putObject(builder -> builder |
| 43 | + .bucket(BUCKET) |
| 44 | + .key(objectKey) |
| 45 | + .build(), RequestBody.fromString(input)); |
| 46 | + |
| 47 | + // Get the instruction file separately using a default client |
| 48 | + S3Client defaultClient = S3Client.create(); |
| 49 | + ResponseBytes<GetObjectResponse> directInstGetResponse = defaultClient.getObjectAsBytes(builder -> builder |
| 50 | + .bucket(BUCKET) |
| 51 | + .key(objectKey + ".instruction") |
| 52 | + .build()); |
| 53 | + // Ensure its metadata identifies it as such |
| 54 | + assertTrue(directInstGetResponse.response().metadata().containsKey("x-amz-crypto-instr-file")); |
| 55 | + |
| 56 | + // Ensure decryption succeeds |
| 57 | + ResponseBytes<GetObjectResponse> objectResponse = s3Client.getObjectAsBytes(builder -> builder |
| 58 | + .bucket(BUCKET) |
| 59 | + .key(objectKey) |
| 60 | + .build()); |
| 61 | + String output = objectResponse.asUtf8String(); |
| 62 | + assertEquals(input, output); |
| 63 | + |
| 64 | + deleteObject(BUCKET, objectKey, s3Client); |
| 65 | + s3Client.close(); |
| 66 | + defaultClient.close(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testDisabledClientFails() { |
| 71 | + final String objectKey = appendTestSuffix("instruction-file-put-object"); |
| 72 | + final String input = "SimpleTestOfV3EncryptionClient"; |
| 73 | + S3Client wrappedClient = S3Client.create(); |
| 74 | + S3Client s3Client = S3EncryptionClient.builder() |
| 75 | + .instructionFileConfig(InstructionFileConfig.builder() |
| 76 | + .instructionFileClient(wrappedClient) |
| 77 | + .enableInstructionFilePutObject(true) |
| 78 | + .build()) |
| 79 | + .kmsKeyId(KMS_KEY_ID) |
| 80 | + .build(); |
| 81 | + |
| 82 | + // Put with Instruction File |
| 83 | + s3Client.putObject(builder -> builder |
| 84 | + .bucket(BUCKET) |
| 85 | + .key(objectKey) |
| 86 | + .build(), RequestBody.fromString(input)); |
| 87 | + |
| 88 | + // Disabled client should fail |
| 89 | + S3Client s3ClientDisabledInstructionFile = S3EncryptionClient.builder() |
| 90 | + .wrappedClient(wrappedClient) |
| 91 | + .instructionFileConfig(InstructionFileConfig.builder() |
| 92 | + .disableInstructionFile(true) |
| 93 | + .build()) |
| 94 | + .kmsKeyId(KMS_KEY_ID) |
| 95 | + .build(); |
| 96 | + |
| 97 | + try { |
| 98 | + s3ClientDisabledInstructionFile.getObjectAsBytes(builder -> builder |
| 99 | + .bucket(BUCKET) |
| 100 | + .key(objectKey) |
| 101 | + .build()); |
| 102 | + fail("expected exception"); |
| 103 | + } catch (S3EncryptionClientException exception) { |
| 104 | + assertTrue(exception.getMessage().contains("Exception encountered while fetching Instruction File.")); |
| 105 | + } |
| 106 | + |
| 107 | + deleteObject(BUCKET, objectKey, s3Client); |
| 108 | + s3Client.close(); |
| 109 | + s3ClientDisabledInstructionFile.close(); |
| 110 | + } |
| 111 | + |
| 112 | + |
| 113 | + /** |
| 114 | + * This test is somewhat redundant given deletion itself is tested in |
| 115 | + * e.g. deleteObjectWithInstructionFileSuccess, but is included anyway to be thorough |
| 116 | + */ |
| 117 | + @Test |
| 118 | + public void testInstructionFileDelete() { |
| 119 | + final String objectKey = appendTestSuffix("instruction-file-put-object"); |
| 120 | + final String input = "SimpleTestOfV3EncryptionClient"; |
| 121 | + S3Client wrappedClient = S3Client.create(); |
| 122 | + S3Client s3Client = S3EncryptionClient.builder() |
| 123 | + .instructionFileConfig(InstructionFileConfig.builder() |
| 124 | + .instructionFileClient(wrappedClient) |
| 125 | + .enableInstructionFilePutObject(true) |
| 126 | + .build()) |
| 127 | + .kmsKeyId(KMS_KEY_ID) |
| 128 | + .build(); |
| 129 | + |
| 130 | + s3Client.putObject(builder -> builder |
| 131 | + .bucket(BUCKET) |
| 132 | + .key(objectKey) |
| 133 | + .build(), RequestBody.fromString(input)); |
| 134 | + |
| 135 | + // Get the instruction file separately using a default client |
| 136 | + S3Client defaultClient = S3Client.create(); |
| 137 | + ResponseBytes<GetObjectResponse> directInstGetResponse = defaultClient.getObjectAsBytes(builder -> builder |
| 138 | + .bucket(BUCKET) |
| 139 | + .key(objectKey + ".instruction") |
| 140 | + .build()); |
| 141 | + // Ensure its metadata identifies it as such |
| 142 | + assertTrue(directInstGetResponse.response().metadata().containsKey("x-amz-crypto-instr-file")); |
| 143 | + |
| 144 | + // Ensure decryption succeeds |
| 145 | + ResponseBytes<GetObjectResponse> objectResponse = s3Client.getObjectAsBytes(builder -> builder |
| 146 | + .bucket(BUCKET) |
| 147 | + .key(objectKey) |
| 148 | + .build()); |
| 149 | + String output = objectResponse.asUtf8String(); |
| 150 | + assertEquals(input, output); |
| 151 | + |
| 152 | + deleteObject(BUCKET, objectKey, s3Client); |
| 153 | + |
| 154 | + try { |
| 155 | + defaultClient.getObjectAsBytes(builder -> builder |
| 156 | + .bucket(BUCKET) |
| 157 | + .key(objectKey + ".instruction") |
| 158 | + .build()); |
| 159 | + fail("expected exception!"); |
| 160 | + } catch (NoSuchKeyException e) { |
| 161 | + // expected |
| 162 | + } |
| 163 | + |
| 164 | + s3Client.close(); |
| 165 | + defaultClient.close(); |
| 166 | + } |
| 167 | + @Test |
| 168 | + public void testPutWithInstructionFile() { |
| 169 | + final String objectKey = appendTestSuffix("instruction-file-put-object"); |
| 170 | + final String objectKeyV2 = appendTestSuffix("instruction-file-put-object-v2"); |
| 171 | + final String input = "SimpleTestOfV3EncryptionClient"; |
| 172 | + S3Client wrappedClient = S3Client.create(); |
| 173 | + S3Client s3Client = S3EncryptionClient.builder() |
| 174 | + .instructionFileConfig(InstructionFileConfig.builder() |
| 175 | + .instructionFileClient(wrappedClient) |
| 176 | + .enableInstructionFilePutObject(true) |
| 177 | + .build()) |
| 178 | + .kmsKeyId(KMS_KEY_ID) |
| 179 | + .build(); |
| 180 | + |
| 181 | + s3Client.putObject(builder -> builder |
| 182 | + .bucket(BUCKET) |
| 183 | + .key(objectKey) |
| 184 | + .build(), RequestBody.fromString(input)); |
| 185 | + |
| 186 | + // Get the instruction file separately using a default client |
| 187 | + S3Client defaultClient = S3Client.create(); |
| 188 | + ResponseBytes<GetObjectResponse> directInstGetResponse = defaultClient.getObjectAsBytes(builder -> builder |
| 189 | + .bucket(BUCKET) |
| 190 | + .key(objectKey + ".instruction") |
| 191 | + .build()); |
| 192 | + assertTrue(directInstGetResponse.response().metadata().containsKey("x-amz-crypto-instr-file")); |
| 193 | + |
| 194 | + ResponseBytes<GetObjectResponse> objectResponse = s3Client.getObjectAsBytes(builder -> builder |
| 195 | + .bucket(BUCKET) |
| 196 | + .key(objectKey) |
| 197 | + .build()); |
| 198 | + String output = objectResponse.asUtf8String(); |
| 199 | + assertEquals(input, output); |
| 200 | + |
| 201 | + // Temporary - Generate an instruction file in V2 to compare against V3 |
| 202 | + // TODO: do this for other keyrings as well |
| 203 | + // TODO: Instead, make a V3ToV2 test |
| 204 | + EncryptionMaterialsProvider materialsProvider = |
| 205 | + new StaticEncryptionMaterialsProvider(new KMSEncryptionMaterials(KMS_KEY_ID)); |
| 206 | + CryptoConfigurationV2 cryptoConfig = |
| 207 | + new CryptoConfigurationV2(CryptoMode.StrictAuthenticatedEncryption) |
| 208 | + .withStorageMode(CryptoStorageMode.InstructionFile); |
| 209 | + |
| 210 | + AmazonS3EncryptionV2 v2Client = AmazonS3EncryptionClientV2.encryptionBuilder() |
| 211 | + .withCryptoConfiguration(cryptoConfig) |
| 212 | + .withEncryptionMaterialsProvider(materialsProvider) |
| 213 | + .build(); |
| 214 | + |
| 215 | + v2Client.putObject(BUCKET, objectKeyV2, input); |
| 216 | + |
| 217 | + // Cleanup |
| 218 | +// deleteObject(BUCKET, objectKey, s3Client); |
| 219 | + s3Client.close(); |
| 220 | + } |
| 221 | +} |
0 commit comments