Search in sources :

Example 61 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.

the class GeneratePreSignUrlInterceptorTest method copySnapshotRequest_generatesCorrectPresignedUrl.

@Test
public void copySnapshotRequest_generatesCorrectPresignedUrl() {
    // Expected URL was derived by first making a request to EC2 using
    // valid credentials and a KMS encrypted snapshot and verifying that
    // the snapshot was copied to the destination region, also encrypted.
    // Then the same code was used to make a second request, changing only
    // the credentials and snapshot ID.
    String expectedPresignedUrl = "https://ec2.us-west-2.amazonaws.com?Action=CopySnapshot" + "&Version=2016-11-15" + "&DestinationRegion=us-east-1" + "&SourceRegion=us-west-2" + "&SourceSnapshotId=SNAPSHOT_ID" + "&X-Amz-Algorithm=AWS4-HMAC-SHA256" + "&X-Amz-Date=20200107T205609Z" + "&X-Amz-SignedHeaders=host" + "&X-Amz-Expires=604800" + "&X-Amz-Credential=akid%2F20200107%2Fus-west-2%2Fec2%2Faws4_request" + "&X-Amz-Signature=c1f5e34834292a86ff2b46b5e97cebaf2967b09641b4e2e60a382a37d137a03b";
    ZoneId utcZone = ZoneId.of("UTC").normalized();
    // Same signing date as the one used for the request above
    Instant signingInstant = ZonedDateTime.of(2020, 1, 7, 20, 56, 9, 0, utcZone).toInstant();
    Clock signingClock = Clock.fixed(signingInstant, utcZone);
    GeneratePreSignUrlInterceptor interceptor = new GeneratePreSignUrlInterceptor(signingClock);
    // These details don't really affect the test as they're not used in generating the signed URL
    SdkHttpFullRequest request = SdkHttpFullRequest.builder().uri(URI.create("https://ec2.us-west-2.amazonaws.com")).method(SdkHttpMethod.POST).build();
    CopySnapshotRequest ec2Request = CopySnapshotRequest.builder().sourceRegion("us-west-2").destinationRegion("us-east-1").sourceSnapshotId("SNAPSHOT_ID").build();
    when(mockContext.httpRequest()).thenReturn(request);
    when(mockContext.request()).thenReturn(ec2Request);
    ExecutionAttributes attrs = new ExecutionAttributes();
    attrs.putAttribute(AWS_CREDENTIALS, AwsBasicCredentials.create("akid", "skid"));
    SdkHttpRequest modifiedRequest = interceptor.modifyHttpRequest(mockContext, attrs);
    String generatedPresignedUrl = modifiedRequest.rawQueryParameters().get("PresignedUrl").get(0);
    assertThat(generatedPresignedUrl).isEqualTo(expectedPresignedUrl);
}
Also used : CopySnapshotRequest(software.amazon.awssdk.services.ec2.model.CopySnapshotRequest) SdkHttpRequest(software.amazon.awssdk.http.SdkHttpRequest) ZoneId(java.time.ZoneId) SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ExecutionAttributes(software.amazon.awssdk.core.interceptor.ExecutionAttributes) Instant(java.time.Instant) Clock(java.time.Clock) Test(org.junit.Test)

Example 62 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.

the class PutItemRequestMarshallerTest method onlyRemainingByteBufferDataIsMarshalled.

/**
 *l
 * Regression test for TT0075355961
 */
@Test
public void onlyRemainingByteBufferDataIsMarshalled() throws IOException {
    final ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[] { 0, 0, 0, 1, 1, 1 });
    // Consume some of the byte buffer
    byteBuffer.position(3);
    SdkHttpFullRequest marshalled = marshaller.marshall(PutItemRequest.builder().item(ImmutableMap.of("binaryProp", AttributeValue.builder().b(SdkBytes.fromByteBuffer(byteBuffer)).build())).build());
    JsonNode marshalledContent = MAPPER.readTree(marshalled.contentStreamProvider().get().newStream());
    String base64Binary = marshalledContent.get("Item").get("binaryProp").get("B").asText();
    // Only the remaining data in the byte buffer should have been read and marshalled.
    assertEquals(BinaryUtils.toBase64(new byte[] { 1, 1, 1 }), base64Binary);
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 63 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.

the class Aws4SignerTest method signing_with_Crc32Checksum_with__trailer_header_already_present.

@Test
public void signing_with_Crc32Checksum_with__trailer_header_already_present() throws Exception {
    String expectedAuthorization = AWS_4_HMAC_SHA_256_AUTHORIZATION + SIGNER_HEADER_WITH_CHECKSUMS_IN_TRAILER + "Signature=3436c4bc175d31e87a591802e64756cebf2d1c6c2054d26ca3dc91bdd3de303e";
    final SignerChecksumParams signerChecksumParams = SignerChecksumParams.builder().algorithm(Algorithm.CRC32).checksumHeaderName("x-amzn-header-crc").isStreamingRequest(false).build();
    SdkHttpFullRequest signed = SignerTestUtils.signRequest(signer, request.contentStreamProvider(() -> new ByteArrayInputStream(("{\"TableName" + "\": " + "\"foo\"}").getBytes(StandardCharsets.UTF_8))).appendHeader("x-amz-trailer", "x-amzn-header-crc").build(), credentials, "demo", signingOverrideClock, "us-east-1", signerChecksumParams);
    assertThat(signed.firstMatchingHeader("x-amzn-header-crc")).isNotPresent();
    assertThat(signed.firstMatchingHeader("x-amz-trailer")).contains("x-amzn-header-crc");
    assertThat(signed.firstMatchingHeader(SignerConstant.X_AMZ_CONTENT_SHA256)).isNotPresent();
    assertThat(signed.firstMatchingHeader("Authorization")).hasValue(expectedAuthorization);
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) SignerChecksumParams(software.amazon.awssdk.auth.signer.params.SignerChecksumParams) Test(org.junit.Test)

Example 64 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.

the class Aws4SignerTest method signing_with_NoHttpChecksum_As_No_impact_on_Signature.

@Test
public void signing_with_NoHttpChecksum_As_No_impact_on_Signature() throws Exception {
    // Note here x_amz_sha25_header is not present in SignedHeaders
    String expectedAuthorization = AWS_4_HMAC_SHA_256_AUTHORIZATION + "SignedHeaders=host;x-amz-archive-description;x-amz-date, " + "Signature=77fe7c02927966018667f21d1dc3dfad9057e58401cbb9ed64f1b7868288e35a";
    SdkHttpFullRequest signed = SignerTestUtils.signRequest(signer, request.contentStreamProvider(() -> new ByteArrayInputStream("{\"TableName\": \"foo\"}".getBytes(StandardCharsets.UTF_8))).build(), credentials, "demo", signingOverrideClock, "us-east-1", null);
    assertThat(signed.firstMatchingHeader("Authorization")).hasValue(expectedAuthorization);
    assertThat(signed.firstMatchingHeader("x-amzn-header-crc")).isNotPresent();
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 65 with SdkHttpFullRequest

use of software.amazon.awssdk.http.SdkHttpFullRequest in project aws-sdk-java-v2 by aws.

the class Aws4SignerTest method canonicalizedHeaderString_multiValueHeaders_areCommaSeparated.

/**
 * Multi-value headers should be comma separated.
 */
@Test
public void canonicalizedHeaderString_multiValueHeaders_areCommaSeparated() throws Exception {
    AwsBasicCredentials credentials = AwsBasicCredentials.create("akid", "skid");
    SdkHttpFullRequest.Builder request = generateBasicRequest();
    request.appendHeader("foo", "bar");
    request.appendHeader("foo", "baz");
    SdkHttpFullRequest actual = SignerTestUtils.signRequest(signer, request.build(), credentials, "demo", signingOverrideClock, "us-east-1");
    // We cannot easily test the canonical header string value, but the below signature asserts that it contains:
    // foo:bar,baz
    assertThat(actual.firstMatchingHeader("Authorization")).hasValue("AWS4-HMAC-SHA256 Credential=akid/19810216/us-east-1/demo/aws4_request, " + "SignedHeaders=foo;host;x-amz-archive-description;x-amz-date, " + "Signature=1253bc1751048ea299e688cbe07a2224292e5cc606a079cb40459ad987793c19");
}
Also used : SdkHttpFullRequest(software.amazon.awssdk.http.SdkHttpFullRequest) AwsBasicCredentials(software.amazon.awssdk.auth.credentials.AwsBasicCredentials) Test(org.junit.Test)

Aggregations

SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)178 Test (org.junit.Test)70 Test (org.junit.jupiter.api.Test)50 ExecutionAttributes (software.amazon.awssdk.core.interceptor.ExecutionAttributes)43 URI (java.net.URI)25 SigningTestCase (software.amazon.awssdk.authcrt.signer.SigningTestCase)23 ByteArrayInputStream (java.io.ByteArrayInputStream)18 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)16 List (java.util.List)13 AwsCredentials (software.amazon.awssdk.auth.credentials.AwsCredentials)13 RequestExecutionContext (software.amazon.awssdk.core.internal.http.RequestExecutionContext)12 ByteBuffer (java.nio.ByteBuffer)11 AsyncRequestBody (software.amazon.awssdk.core.async.AsyncRequestBody)11 Map (java.util.Map)10 ContentStreamProvider (software.amazon.awssdk.http.ContentStreamProvider)10 Instant (java.time.Instant)9 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)9 Aws4SignerParams (software.amazon.awssdk.auth.signer.params.Aws4SignerParams)9 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)8