|
| 1 | +/* |
| 2 | + * Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.services.ec2.transform.internal; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.mockito.Mockito.when; |
| 20 | +import static software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute.AWS_CREDENTIALS; |
| 21 | +import java.net.URI; |
| 22 | +import java.time.Clock; |
| 23 | +import java.time.Instant; |
| 24 | +import java.time.ZoneId; |
| 25 | +import java.time.ZonedDateTime; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | +import org.mockito.Mock; |
| 29 | +import org.mockito.runners.MockitoJUnitRunner; |
| 30 | +import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 31 | +import software.amazon.awssdk.core.interceptor.Context; |
| 32 | +import software.amazon.awssdk.core.interceptor.ExecutionAttributes; |
| 33 | +import software.amazon.awssdk.http.SdkHttpFullRequest; |
| 34 | +import software.amazon.awssdk.http.SdkHttpMethod; |
| 35 | +import software.amazon.awssdk.http.SdkHttpRequest; |
| 36 | +import software.amazon.awssdk.services.ec2.model.CopySnapshotRequest; |
| 37 | + |
| 38 | +@RunWith(MockitoJUnitRunner.class) |
| 39 | +public class GeneratePreSignUrlInterceptorTest { |
| 40 | + private static final GeneratePreSignUrlInterceptor INTERCEPTOR = new GeneratePreSignUrlInterceptor(); |
| 41 | + |
| 42 | + @Mock |
| 43 | + private Context.ModifyHttpRequest mockContext; |
| 44 | + |
| 45 | + @Test |
| 46 | + public void copySnapshotRequest_httpsProtocolAddedToEndpoint() { |
| 47 | + SdkHttpFullRequest request = SdkHttpFullRequest.builder() |
| 48 | + .uri(URI.create("https://ec2.us-west-2.amazonaws.com")) |
| 49 | + .method(SdkHttpMethod.POST) |
| 50 | + .build(); |
| 51 | + |
| 52 | + CopySnapshotRequest ec2Request = CopySnapshotRequest.builder() |
| 53 | + .sourceRegion("us-west-2") |
| 54 | + .destinationRegion("us-east-2") |
| 55 | + .build(); |
| 56 | + |
| 57 | + when(mockContext.httpRequest()).thenReturn(request); |
| 58 | + when(mockContext.request()).thenReturn(ec2Request); |
| 59 | + |
| 60 | + ExecutionAttributes attrs = new ExecutionAttributes(); |
| 61 | + attrs.putAttribute(AWS_CREDENTIALS, AwsBasicCredentials.create("foo", "bar")); |
| 62 | + |
| 63 | + SdkHttpRequest modifiedRequest = INTERCEPTOR.modifyHttpRequest(mockContext, attrs); |
| 64 | + |
| 65 | + String presignedUrl = modifiedRequest.rawQueryParameters().get("PresignedUrl").get(0); |
| 66 | + |
| 67 | + assertThat(presignedUrl).startsWith("https://"); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void copySnapshotRequest_generatesCorrectPresignedUrl() { |
| 72 | + // Expected URL was derived by first making a request to EC2 using |
| 73 | + // valid credentials and a KMS encrypted snapshot and verifying that |
| 74 | + // the snapshot was copied to the destination region, also encrypted. |
| 75 | + // Then the same code was used to make a second request, changing only |
| 76 | + // the credentials and snapshot ID. |
| 77 | + String expectedPresignedUrl = "https://ec2.us-west-2.amazonaws.com?Action=CopySnapshot" + |
| 78 | + "&Version=2016-11-15" + |
| 79 | + "&DestinationRegion=us-east-1" + |
| 80 | + "&SourceRegion=us-west-2" + |
| 81 | + "&SourceSnapshotId=SNAPSHOT_ID" + |
| 82 | + "&X-Amz-Algorithm=AWS4-HMAC-SHA256" + |
| 83 | + "&X-Amz-Date=20200107T205609Z" + |
| 84 | + "&X-Amz-SignedHeaders=host" + |
| 85 | + "&X-Amz-Expires=604800" + |
| 86 | + "&X-Amz-Credential=akid%2F20200107%2Fus-west-2%2Fec2%2Faws4_request" + |
| 87 | + "&X-Amz-Signature=c1f5e34834292a86ff2b46b5e97cebaf2967b09641b4e2e60a382a37d137a03b"; |
| 88 | + |
| 89 | + ZoneId utcZone = ZoneId.of("UTC").normalized(); |
| 90 | + |
| 91 | + // Same signing date as the one used for the request above |
| 92 | + Instant signingInstant = ZonedDateTime.of(2020, 1, 7, 20, 56, 9, 0, utcZone).toInstant(); |
| 93 | + Clock signingClock = Clock.fixed(signingInstant, utcZone); |
| 94 | + |
| 95 | + GeneratePreSignUrlInterceptor interceptor = new GeneratePreSignUrlInterceptor(signingClock); |
| 96 | + |
| 97 | + // These details don't really affect the test as they're not used in generating the signed URL |
| 98 | + SdkHttpFullRequest request = SdkHttpFullRequest.builder() |
| 99 | + .uri(URI.create("https://ec2.us-west-2.amazonaws.com")) |
| 100 | + .method(SdkHttpMethod.POST) |
| 101 | + .build(); |
| 102 | + |
| 103 | + CopySnapshotRequest ec2Request = CopySnapshotRequest.builder() |
| 104 | + .sourceRegion("us-west-2") |
| 105 | + .destinationRegion("us-east-1") |
| 106 | + .sourceSnapshotId("SNAPSHOT_ID") |
| 107 | + .build(); |
| 108 | + |
| 109 | + when(mockContext.httpRequest()).thenReturn(request); |
| 110 | + when(mockContext.request()).thenReturn(ec2Request); |
| 111 | + |
| 112 | + ExecutionAttributes attrs = new ExecutionAttributes(); |
| 113 | + attrs.putAttribute(AWS_CREDENTIALS, AwsBasicCredentials.create("akid", "skid")); |
| 114 | + |
| 115 | + SdkHttpRequest modifiedRequest = interceptor.modifyHttpRequest(mockContext, attrs); |
| 116 | + |
| 117 | + String generatedPresignedUrl = modifiedRequest.rawQueryParameters().get("PresignedUrl").get(0); |
| 118 | + |
| 119 | + assertThat(generatedPresignedUrl).isEqualTo(expectedPresignedUrl); |
| 120 | + } |
| 121 | +} |
0 commit comments