Search in sources :

Example 6 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-crt-java by awslabs.

the class SigningTest method testChunkedSigv4Signing.

@Test
public void testChunkedSigv4Signing() throws Exception {
    HttpRequest request = createChunkedTestRequest();
    CompletableFuture<HttpRequest> result = AwsSigner.signRequest(request, createChunkedRequestSigningConfig());
    HttpRequest signedRequest = result.get();
    assertNotNull(signedRequest);
    assertTrue(hasHeaderWithValue(signedRequest, "Authorization", EXPECTED_CHUNK_REQUEST_AUTHORIZATION_HEADER));
    /* If the authorization header is equal then certainly we can assume the signature value */
    byte[] signature = EXPECTED_REQUEST_SIGNATURE;
    HttpRequestBodyStream chunk1 = createChunk1Stream();
    CompletableFuture<byte[]> chunk1Result = AwsSigner.signChunk(chunk1, signature, createChunkSigningConfig());
    signature = chunk1Result.get();
    assertTrue(Arrays.equals(signature, EXPECTED_FIRST_CHUNK_SIGNATURE));
    HttpRequestBodyStream chunk2 = createChunk2Stream();
    CompletableFuture<byte[]> chunk2Result = AwsSigner.signChunk(chunk2, signature, createChunkSigningConfig());
    signature = chunk2Result.get();
    assertTrue(Arrays.equals(signature, EXPECTED_SECOND_CHUNK_SIGNATURE));
    CompletableFuture<byte[]> finalChunkResult = AwsSigner.signChunk(null, signature, createChunkSigningConfig());
    signature = finalChunkResult.get();
    assertTrue(Arrays.equals(signature, EXPECTED_FINAL_CHUNK_SIGNATURE));
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpRequestBodyStream(software.amazon.awssdk.crt.http.HttpRequestBodyStream) Test(org.junit.Test)

Example 7 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-crt-java by awslabs.

the class SigningTest method createChunkedTestRequest.

private HttpRequest createChunkedTestRequest() throws Exception {
    URI uri = new URI("https://s3.amazonaws.com/examplebucket/chunkObject.txt");
    HttpHeader[] requestHeaders = new HttpHeader[] { new HttpHeader("Host", uri.getHost()), new HttpHeader("x-amz-storage-class", "REDUCED_REDUNDANCY"), new HttpHeader("Content-Encoding", "aws-chunked"), new HttpHeader("x-amz-decoded-content-length", "66560"), new HttpHeader("Content-Length", "66824") };
    return new HttpRequest("PUT", uri.getPath(), requestHeaders, null);
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) URI(java.net.URI)

Example 8 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-crt-java by awslabs.

the class SigningTest method testTrailingHeadersSigv4aSigning.

@Test
public void testTrailingHeadersSigv4aSigning() throws Exception {
    HttpRequest request = createChunkedTrailerTestRequest();
    AwsSigningConfig chunkedRequestSigningConfig = createChunkedRequestSigningConfig();
    chunkedRequestSigningConfig.setAlgorithm(AwsSigningConfig.AwsSigningAlgorithm.SIGV4_ASYMMETRIC);
    chunkedRequestSigningConfig.setSignedBodyValue(AwsSigningConfig.AwsSignedBodyValue.STREAMING_AWS4_ECDSA_P256_SHA256_PAYLOAD_TRAILER);
    CompletableFuture<AwsSigningResult> result = AwsSigner.sign(request, chunkedRequestSigningConfig);
    HttpRequest signedRequest = result.get().getSignedRequest();
    assertNotNull(signedRequest);
    byte[] requestSignature = result.get().getSignature();
    assertTrue(AwsSigningUtils.verifySigv4aEcdsaSignature(request, CHUNKED_TRAILER_SIGV4A_CANONICAL_REQUEST, chunkedRequestSigningConfig, requestSignature, CHUNKED_SIGV4A_TEST_ECC_PUB_X, CHUNKED_SIGV4A_TEST_ECC_PUB_Y));
    HttpRequestBodyStream chunk1 = createChunk1Stream();
    AwsSigningConfig chunkSigningConfig = createChunkSigningConfig();
    chunkSigningConfig.setAlgorithm(AwsSigningConfig.AwsSigningAlgorithm.SIGV4_ASYMMETRIC);
    CompletableFuture<AwsSigningResult> chunk1Result = AwsSigner.sign(chunk1, requestSignature, chunkSigningConfig);
    byte[] chunk1StringToSign = buildChunkStringToSign(requestSignature, CHUNK1_STS_POST_SIGNATURE);
    assertTrue(AwsSigningUtils.verifyRawSha256EcdsaSignature(chunk1StringToSign, chunk1Result.get().getSignature(), CHUNKED_SIGV4A_TEST_ECC_PUB_X, CHUNKED_SIGV4A_TEST_ECC_PUB_Y));
    HttpRequestBodyStream chunk2 = createChunk2Stream();
    CompletableFuture<AwsSigningResult> chunk2Result = AwsSigner.sign(chunk2, chunk1Result.get().getSignature(), chunkSigningConfig);
    byte[] chunk2StringToSign = buildChunkStringToSign(chunk1Result.get().getSignature(), CHUNK2_STS_POST_SIGNATURE);
    assertTrue(AwsSigningUtils.verifyRawSha256EcdsaSignature(chunk2StringToSign, chunk2Result.get().getSignature(), CHUNKED_SIGV4A_TEST_ECC_PUB_X, CHUNKED_SIGV4A_TEST_ECC_PUB_Y));
    CompletableFuture<AwsSigningResult> chunk3Result = AwsSigner.sign((HttpRequestBodyStream) null, chunk2Result.get().getSignature(), chunkSigningConfig);
    byte[] chunk3StringToSign = buildChunkStringToSign(chunk2Result.get().getSignature(), CHUNK3_STS_POST_SIGNATURE);
    assertTrue(AwsSigningUtils.verifyRawSha256EcdsaSignature(chunk3StringToSign, chunk3Result.get().getSignature(), CHUNKED_SIGV4A_TEST_ECC_PUB_X, CHUNKED_SIGV4A_TEST_ECC_PUB_Y));
    List<HttpHeader> trailingHeaders = createTrailingHeaders();
    AwsSigningConfig trailingHeadersSigningConfig = createTrailingHeadersSigningConfig();
    trailingHeadersSigningConfig.setAlgorithm(AwsSigningConfig.AwsSigningAlgorithm.SIGV4_ASYMMETRIC);
    CompletableFuture<AwsSigningResult> trailingHeadersResult = AwsSigner.sign(trailingHeaders, chunk3Result.get().getSignature(), trailingHeadersSigningConfig);
    byte[] trailingHeadersStringToSign = buildTrailingHeadersStringToSign(chunk3Result.get().getSignature(), TRAILING_HEADERS_STS_POST_SIGNATURE);
    assertTrue(AwsSigningUtils.verifyRawSha256EcdsaSignature(trailingHeadersStringToSign, trailingHeadersResult.get().getSignature(), CHUNKED_SIGV4A_TEST_ECC_PUB_X, CHUNKED_SIGV4A_TEST_ECC_PUB_Y));
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) HttpRequestBodyStream(software.amazon.awssdk.crt.http.HttpRequestBodyStream) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) AwsSigningConfig(software.amazon.awssdk.crt.auth.signing.AwsSigningConfig) AwsSigningResult(software.amazon.awssdk.crt.auth.signing.AwsSigningResult) Test(org.junit.Test)

Example 9 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-crt-java by awslabs.

the class SigningTest method testSigningFailureBodyStreamException.

@Test(expected = CrtRuntimeException.class)
public void testSigningFailureBodyStreamException() throws Exception {
    try (StaticCredentialsProvider provider = new StaticCredentialsProvider.StaticCredentialsProviderBuilder().withAccessKeyId(TEST_ACCESS_KEY_ID).withSecretAccessKey(TEST_SECRET_ACCESS_KEY).build()) {
        // request is missing Host header
        HttpRequest request = createBadBodyStreamRequest("POST", "/bad");
        try (AwsSigningConfig config = new AwsSigningConfig()) {
            config.setAlgorithm(AwsSigningConfig.AwsSigningAlgorithm.SIGV4);
            config.setSignatureType(AwsSigningConfig.AwsSignatureType.HTTP_REQUEST_VIA_HEADERS);
            config.setRegion("us-east-1");
            config.setService("service");
            config.setTime(System.currentTimeMillis());
            config.setCredentialsProvider(provider);
            config.setUseDoubleUriEncode(true);
            config.setShouldNormalizeUriPath(true);
            config.setSignedBodyHeader(AwsSigningConfig.AwsSignedBodyHeaderType.X_AMZ_CONTENT_SHA256);
            CompletableFuture<HttpRequest> result = AwsSigner.signRequest(request, config);
            result.get();
        }
    } catch (Exception e) {
        Throwable cause = e.getCause();
        assertTrue(cause != null);
        assertTrue(cause.getClass() == CrtRuntimeException.class);
        CrtRuntimeException crt = (CrtRuntimeException) cause;
        assertTrue(crt.errorName.equals("AWS_ERROR_HTTP_CALLBACK_FAILURE"));
        throw crt;
    }
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) StaticCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider) AwsSigningConfig(software.amazon.awssdk.crt.auth.signing.AwsSigningConfig) Test(org.junit.Test)

Example 10 with HttpRequest

use of software.amazon.awssdk.crt.http.HttpRequest in project aws-crt-java by awslabs.

the class ProxyTest method doHttpConnectionManagerProxyTest.

private void doHttpConnectionManagerProxyTest(HttpClientConnectionManager manager) {
    HttpRequest request = new HttpRequest("GET", "/");
    CompletableFuture requestCompleteFuture = new CompletableFuture();
    manager.acquireConnection().whenComplete((conn, throwable) -> {
        if (throwable != null) {
            requestCompleteFuture.completeExceptionally(throwable);
        }
        HttpStream stream = conn.makeRequest(request, new HttpStreamResponseHandler() {

            @Override
            public void onResponseHeaders(HttpStream stream, int responseStatusCode, int blockType, HttpHeader[] nextHeaders) {
                ;
            }

            @Override
            public void onResponseComplete(HttpStream stream, int errorCode) {
                // When this Request is complete, release the conn back to the pool
                manager.releaseConnection(conn);
                stream.close();
                if (errorCode != CRT.AWS_CRT_SUCCESS) {
                    requestCompleteFuture.completeExceptionally(new CrtRuntimeException(errorCode));
                } else {
                    requestCompleteFuture.complete(null);
                }
            }
        });
        if (stream != null) {
            stream.activate();
        }
    });
    requestCompleteFuture.join();
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) CompletableFuture(java.util.concurrent.CompletableFuture) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) HttpStreamResponseHandler(software.amazon.awssdk.crt.http.HttpStreamResponseHandler) CrtRuntimeException(software.amazon.awssdk.crt.CrtRuntimeException) HttpStream(software.amazon.awssdk.crt.http.HttpStream)

Aggregations

HttpRequest (software.amazon.awssdk.crt.http.HttpRequest)47 HttpHeader (software.amazon.awssdk.crt.http.HttpHeader)26 Test (org.junit.Test)22 URI (java.net.URI)15 ByteBuffer (java.nio.ByteBuffer)13 HttpRequestBodyStream (software.amazon.awssdk.crt.http.HttpRequestBodyStream)13 AwsSigningConfig (software.amazon.awssdk.crt.auth.signing.AwsSigningConfig)11 CompletableFuture (java.util.concurrent.CompletableFuture)8 StaticCredentialsProvider (software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider)8 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)8 List (java.util.List)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 ArrayList (java.util.ArrayList)6 AwsSigningResult (software.amazon.awssdk.crt.auth.signing.AwsSigningResult)6 AsyncExecuteRequest (software.amazon.awssdk.http.async.AsyncExecuteRequest)6 S3MetaRequestOptions (software.amazon.awssdk.crt.s3.S3MetaRequestOptions)5 Optional (java.util.Optional)4 S3MetaRequest (software.amazon.awssdk.crt.s3.S3MetaRequest)4 StandardCharsets (java.nio.charset.StandardCharsets)3 Arrays (java.util.Arrays)3