|
21 | 21 | import java.io.FileWriter;
|
22 | 22 | import java.io.IOException;
|
23 | 23 | import java.io.InputStream;
|
| 24 | +import java.net.URI; |
24 | 25 | import java.nio.file.Files;
|
25 | 26 | import java.nio.file.Path;
|
26 | 27 | import java.nio.file.StandardOpenOption;
|
|
74 | 75 |
|
75 | 76 | public class CloudFrontUtilitiesIntegrationTest extends IntegrationTestBase {
|
76 | 77 | private static final Base64.Encoder ENCODER = Base64.getEncoder();
|
77 |
| - private static final String RESOURCE_PREFIX = "do-not-delete-cf-test-"; |
| 78 | + private static final String RESOURCE_PREFIX = "do-not-delete-cf-test-v2"; |
78 | 79 | private static final String CALLER_REFERENCE = UUID.randomUUID().toString();
|
79 | 80 | private static final String S3_OBJECT_KEY = "s3ObjectKey";
|
| 81 | + private static final String S3_OBJECT_KEY_ON_SUB_PATH = "foo/specific-file"; |
| 82 | + private static final String S3_OBJECT_KEY_ON_SUB_PATH_OTHER = "foo/other-file"; |
| 83 | + |
80 | 84 |
|
81 | 85 | private static String bucket;
|
82 | 86 | private static String domainName;
|
@@ -267,6 +271,114 @@ void getCookiesForCustomPolicy_withFutureActiveDate_shouldReturn403Response() th
|
267 | 271 | assertThat(response.httpResponse().statusCode()).isEqualTo(expectedStatus);
|
268 | 272 | }
|
269 | 273 |
|
| 274 | + @Test |
| 275 | + void getSignedUrlWithCustomPolicy_shouldAllowQueryParametersWhenUsingWildcard() throws Exception { |
| 276 | + Instant expirationDate = LocalDate.of(2050, 1, 1) |
| 277 | + .atStartOfDay() |
| 278 | + .toInstant(ZoneOffset.of("Z")); |
| 279 | + |
| 280 | + Instant activeDate = LocalDate.of(2022, 1, 1) |
| 281 | + .atStartOfDay() |
| 282 | + .toInstant(ZoneOffset.of("Z")); |
| 283 | + |
| 284 | + CustomSignerRequest request = CustomSignerRequest.builder() |
| 285 | + .resourceUrl(resourceUrl) |
| 286 | + .privateKey(keyFilePath) |
| 287 | + .keyPairId(keyPairId) |
| 288 | + .resourceUrlPattern(resourceUrl + "*") |
| 289 | + .activeDate(activeDate) |
| 290 | + .expirationDate(expirationDate) |
| 291 | + .build(); |
| 292 | + |
| 293 | + SignedUrl signedUrl = cloudFrontUtilities.getSignedUrlWithCustomPolicy(request); |
| 294 | + |
| 295 | + String urlWithDynamicParam = signedUrl.url() + "&foo=bar"; |
| 296 | + URI modifiedUri = URI.create(urlWithDynamicParam); |
| 297 | + |
| 298 | + |
| 299 | + SdkHttpClient client = ApacheHttpClient.create(); |
| 300 | + HttpExecuteResponse response = client.prepareRequest(HttpExecuteRequest.builder() |
| 301 | + .request(SdkHttpRequest.builder() |
| 302 | + .encodedPath(modifiedUri.getRawPath() + "?" + modifiedUri.getRawQuery()) |
| 303 | + .host(modifiedUri.getHost()) |
| 304 | + .method(SdkHttpMethod.GET) |
| 305 | + .protocol("https") |
| 306 | + .build()) |
| 307 | + .build()).call(); |
| 308 | + assertThat(response.httpResponse().statusCode()).isEqualTo(200); |
| 309 | + } |
| 310 | + |
| 311 | + @Test |
| 312 | + void getSignedUrlWithCustomPolicy_wildCardPath() throws Exception { |
| 313 | + String resourceUri = "https://" + domainName; |
| 314 | + Instant expirationDate = LocalDate.of(2050, 1, 1) |
| 315 | + .atStartOfDay() |
| 316 | + .toInstant(ZoneOffset.of("Z")); |
| 317 | + |
| 318 | + Instant activeDate = LocalDate.of(2022, 1, 1) |
| 319 | + .atStartOfDay() |
| 320 | + .toInstant(ZoneOffset.of("Z")); |
| 321 | + |
| 322 | + CustomSignerRequest request = CustomSignerRequest.builder() |
| 323 | + .resourceUrl(resourceUri + "/foo/specific-file") |
| 324 | + .privateKey(keyFilePath) |
| 325 | + .keyPairId(keyPairId) |
| 326 | + .resourceUrlPattern(resourceUri + "/foo/*") |
| 327 | + .activeDate(activeDate) |
| 328 | + .expirationDate(expirationDate) |
| 329 | + .build(); |
| 330 | + |
| 331 | + SignedUrl signedUrl = cloudFrontUtilities.getSignedUrlWithCustomPolicy(request); |
| 332 | + |
| 333 | + |
| 334 | + URI modifiedUri = URI.create(signedUrl.url().replace("/specific-file","/other-file")); |
| 335 | + SdkHttpClient client = ApacheHttpClient.create(); |
| 336 | + HttpExecuteResponse response = client.prepareRequest(HttpExecuteRequest.builder() |
| 337 | + .request(SdkHttpRequest.builder() |
| 338 | + .encodedPath(modifiedUri.getRawPath() + "?" + modifiedUri.getRawQuery()) |
| 339 | + .host(modifiedUri.getHost()) |
| 340 | + .method(SdkHttpMethod.GET) |
| 341 | + .protocol("https") |
| 342 | + .build()) |
| 343 | + .build()).call(); |
| 344 | + assertThat(response.httpResponse().statusCode()).isEqualTo(200); |
| 345 | + } |
| 346 | + |
| 347 | + @Test |
| 348 | + void getSignedUrlWithCustomPolicy_wildCardPolicyResource_allowsAnyPath() throws Exception { |
| 349 | + Instant expirationDate = LocalDate.of(2050, 1, 1) |
| 350 | + .atStartOfDay() |
| 351 | + .toInstant(ZoneOffset.of("Z")); |
| 352 | + |
| 353 | + Instant activeDate = LocalDate.of(2022, 1, 1) |
| 354 | + .atStartOfDay() |
| 355 | + .toInstant(ZoneOffset.of("Z")); |
| 356 | + |
| 357 | + CustomSignerRequest request = CustomSignerRequest.builder() |
| 358 | + .resourceUrl(resourceUrl) |
| 359 | + .privateKey(keyFilePath) |
| 360 | + .keyPairId(keyPairId) |
| 361 | + .resourceUrlPattern("*") |
| 362 | + .activeDate(activeDate) |
| 363 | + .expirationDate(expirationDate) |
| 364 | + .build(); |
| 365 | + |
| 366 | + SignedUrl signedUrl = cloudFrontUtilities.getSignedUrlWithCustomPolicy(request); |
| 367 | + |
| 368 | + |
| 369 | + URI modifiedUri = URI.create(signedUrl.url().replace("/s3ObjectKey","/foo/other-file")); |
| 370 | + SdkHttpClient client = ApacheHttpClient.create(); |
| 371 | + HttpExecuteResponse response = client.prepareRequest(HttpExecuteRequest.builder() |
| 372 | + .request(SdkHttpRequest.builder() |
| 373 | + .encodedPath(modifiedUri.getRawPath() + "?" + modifiedUri.getRawQuery()) |
| 374 | + .host(modifiedUri.getHost()) |
| 375 | + .method(SdkHttpMethod.GET) |
| 376 | + .protocol("https") |
| 377 | + .build()) |
| 378 | + .build()).call(); |
| 379 | + assertThat(response.httpResponse().statusCode()).isEqualTo(200); |
| 380 | + } |
| 381 | + |
270 | 382 | private static void initStaticFields() throws Exception {
|
271 | 383 | initializeKeyFileAndPair();
|
272 | 384 | originAccessId = getOrCreateOriginAccessIdentity();
|
@@ -409,7 +521,11 @@ private static String getOrCreateBucket() throws IOException {
|
409 | 521 | s3Client.waiter().waitUntilBucketExists(r -> r.bucket(newBucketName));
|
410 | 522 |
|
411 | 523 | File content = new RandomTempFile("testFile", 1000L);
|
| 524 | + File content2 = new RandomTempFile("testFile2", 500L); |
412 | 525 | s3Client.putObject(PutObjectRequest.builder().bucket(newBucketName).key(S3_OBJECT_KEY).build(), RequestBody.fromFile(content));
|
| 526 | + s3Client.putObject(PutObjectRequest.builder().bucket(newBucketName).key(S3_OBJECT_KEY_ON_SUB_PATH).build(), RequestBody.fromFile(content2)); |
| 527 | + s3Client.putObject(PutObjectRequest.builder().bucket(newBucketName).key(S3_OBJECT_KEY_ON_SUB_PATH_OTHER).build(), RequestBody.fromFile(content2)); |
| 528 | + |
413 | 529 |
|
414 | 530 | String bucketPolicy = "{\n"
|
415 | 531 | + "\"Version\":\"2012-10-17\",\n"
|
|
0 commit comments