|
25 | 25 | import java.nio.file.Files; |
26 | 26 | import java.nio.file.Path; |
27 | 27 | import java.nio.file.StandardOpenOption; |
28 | | -import java.security.Key; |
29 | 28 | import java.security.KeyPair; |
30 | 29 | import java.security.KeyPairGenerator; |
31 | 30 | import java.security.PrivateKey; |
|
66 | 65 | import software.amazon.awssdk.services.cloudfront.model.DistributionConfig; |
67 | 66 | import software.amazon.awssdk.services.cloudfront.model.DistributionSummary; |
68 | 67 | import software.amazon.awssdk.services.cloudfront.model.GetKeyGroupResponse; |
69 | | -import software.amazon.awssdk.services.cloudfront.model.KeyGroup; |
70 | 68 | import software.amazon.awssdk.services.cloudfront.model.KeyGroupConfig; |
71 | 69 | import software.amazon.awssdk.services.cloudfront.model.KeyGroupSummary; |
72 | 70 | import software.amazon.awssdk.services.cloudfront.model.Origin; |
@@ -319,6 +317,108 @@ void getCookiesForCustomPolicy_withFutureActiveDate_shouldReturn403Response() th |
319 | 317 | assertThat(response.httpResponse().statusCode()).isEqualTo(expectedStatus); |
320 | 318 | } |
321 | 319 |
|
| 320 | + @ParameterizedTest(name = "{0}") |
| 321 | + @MethodSource("keyCases") |
| 322 | + void getCookiesForCustomPolicy_shouldAllowQueryParametersWhenUsingWildcard(KeyTestCase testCase) throws Exception { |
| 323 | + Instant expirationDate = LocalDate.of(2050, 1, 1) |
| 324 | + .atStartOfDay() |
| 325 | + .toInstant(ZoneOffset.of("Z")); |
| 326 | + |
| 327 | + Instant activeDate = LocalDate.of(2022, 1, 1) |
| 328 | + .atStartOfDay() |
| 329 | + .toInstant(ZoneOffset.of("Z")); |
| 330 | + |
| 331 | + CookiesForCustomPolicy cookies = cloudFrontUtilities.getCookiesForCustomPolicy(r -> r.resourceUrl(resourceUrl) |
| 332 | + .privateKey(testCase.privateKey) |
| 333 | + .keyPairId(testCase.keyPairId) |
| 334 | + .resourceUrlPattern(resourceUrl + "*") |
| 335 | + .activeDate(activeDate) |
| 336 | + .expirationDate(expirationDate)); |
| 337 | + |
| 338 | + // Request the same resource with an additional query parameter - should still be allowed by the wildcard policy |
| 339 | + URI uri = URI.create(resourceUrl + "?foo=bar"); |
| 340 | + SdkHttpClient client = ApacheHttpClient.create(); |
| 341 | + HttpExecuteResponse response = client.prepareRequest(HttpExecuteRequest.builder() |
| 342 | + .request(SdkHttpRequest.builder() |
| 343 | + .uri(uri) |
| 344 | + .appendHeader("Cookie", cookies.policyHeaderValue()) |
| 345 | + .appendHeader("Cookie", cookies.signatureHeaderValue()) |
| 346 | + .appendHeader("Cookie", cookies.keyPairIdHeaderValue()) |
| 347 | + .method(SdkHttpMethod.GET) |
| 348 | + .build()) |
| 349 | + .build()).call(); |
| 350 | + assertThat(response.httpResponse().statusCode()).isEqualTo(200); |
| 351 | + } |
| 352 | + |
| 353 | + @ParameterizedTest(name = "{0}") |
| 354 | + @MethodSource("keyCases") |
| 355 | + void getCookiesForCustomPolicy_wildCardPath(KeyTestCase testCase) throws Exception { |
| 356 | + String resourceUri = "https://" + domainName; |
| 357 | + Instant expirationDate = LocalDate.of(2050, 1, 1) |
| 358 | + .atStartOfDay() |
| 359 | + .toInstant(ZoneOffset.of("Z")); |
| 360 | + |
| 361 | + Instant activeDate = LocalDate.of(2022, 1, 1) |
| 362 | + .atStartOfDay() |
| 363 | + .toInstant(ZoneOffset.of("Z")); |
| 364 | + |
| 365 | + CookiesForCustomPolicy cookies = cloudFrontUtilities.getCookiesForCustomPolicy( |
| 366 | + r -> r.resourceUrl(resourceUri + "/foo/specific-file") |
| 367 | + .privateKey(testCase.privateKey) |
| 368 | + .keyPairId(testCase.keyPairId) |
| 369 | + .resourceUrlPattern(resourceUri + "/foo/*") |
| 370 | + .activeDate(activeDate) |
| 371 | + .expirationDate(expirationDate)); |
| 372 | + |
| 373 | + // Use the cookies to access a different file under the same wildcard path |
| 374 | + URI otherFileUri = URI.create(resourceUri + "/foo/other-file"); |
| 375 | + SdkHttpClient client = ApacheHttpClient.create(); |
| 376 | + HttpExecuteResponse response = client.prepareRequest(HttpExecuteRequest.builder() |
| 377 | + .request(SdkHttpRequest.builder() |
| 378 | + .uri(otherFileUri) |
| 379 | + .appendHeader("Cookie", cookies.policyHeaderValue()) |
| 380 | + .appendHeader("Cookie", cookies.signatureHeaderValue()) |
| 381 | + .appendHeader("Cookie", cookies.keyPairIdHeaderValue()) |
| 382 | + .method(SdkHttpMethod.GET) |
| 383 | + .build()) |
| 384 | + .build()).call(); |
| 385 | + assertThat(response.httpResponse().statusCode()).isEqualTo(200); |
| 386 | + } |
| 387 | + |
| 388 | + @ParameterizedTest(name = "{0}") |
| 389 | + @MethodSource("keyCases") |
| 390 | + void getCookiesForCustomPolicy_wildCardPolicyResource_allowsAnyPath(KeyTestCase testCase) throws Exception { |
| 391 | + Instant expirationDate = LocalDate.of(2050, 1, 1) |
| 392 | + .atStartOfDay() |
| 393 | + .toInstant(ZoneOffset.of("Z")); |
| 394 | + |
| 395 | + Instant activeDate = LocalDate.of(2022, 1, 1) |
| 396 | + .atStartOfDay() |
| 397 | + .toInstant(ZoneOffset.of("Z")); |
| 398 | + |
| 399 | + CookiesForCustomPolicy cookies = cloudFrontUtilities.getCookiesForCustomPolicy( |
| 400 | + r -> r.resourceUrl(resourceUrl) |
| 401 | + .privateKey(testCase.privateKey) |
| 402 | + .keyPairId(testCase.keyPairId) |
| 403 | + .resourceUrlPattern("*") |
| 404 | + .activeDate(activeDate) |
| 405 | + .expirationDate(expirationDate)); |
| 406 | + |
| 407 | + // Use the cookies to access a completely different path - the "*" pattern should allow any path |
| 408 | + URI differentPathUri = URI.create(resourceUrl.replace("/s3ObjectKey", "/foo/other-file")); |
| 409 | + SdkHttpClient client = ApacheHttpClient.create(); |
| 410 | + HttpExecuteResponse response = client.prepareRequest(HttpExecuteRequest.builder() |
| 411 | + .request(SdkHttpRequest.builder() |
| 412 | + .uri(differentPathUri) |
| 413 | + .appendHeader("Cookie", cookies.policyHeaderValue()) |
| 414 | + .appendHeader("Cookie", cookies.signatureHeaderValue()) |
| 415 | + .appendHeader("Cookie", cookies.keyPairIdHeaderValue()) |
| 416 | + .method(SdkHttpMethod.GET) |
| 417 | + .build()) |
| 418 | + .build()).call(); |
| 419 | + assertThat(response.httpResponse().statusCode()).isEqualTo(200); |
| 420 | + } |
| 421 | + |
322 | 422 | @ParameterizedTest(name = "{0}") |
323 | 423 | @MethodSource("keyCases") |
324 | 424 | void getSignedUrlWithCustomPolicy_shouldAllowQueryParametersWhenUsingWildcard(KeyTestCase testCase) throws Exception { |
|
0 commit comments