Search in sources :

Example 11 with HttpHeader

use of software.amazon.awssdk.crt.http.HttpHeader 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 12 with HttpHeader

use of software.amazon.awssdk.crt.http.HttpHeader 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 13 with HttpHeader

use of software.amazon.awssdk.crt.http.HttpHeader 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)

Example 14 with HttpHeader

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

the class S3ClientTest method testS3OverrideRequestCredentials.

@Test
public void testS3OverrideRequestCredentials() {
    skipIfNetworkUnavailable();
    Log.initLoggingToFile(Log.LogLevel.Error, "log.txt");
    Assume.assumeTrue(hasAwsCredentials());
    S3ClientOptions clientOptions = new S3ClientOptions().withEndpoint(ENDPOINT).withRegion(REGION);
    boolean expectedException = false;
    byte[] madeUpCredentials = "I am a madeup credentials".getBytes();
    StaticCredentialsProvider.StaticCredentialsProviderBuilder builder = new StaticCredentialsProvider.StaticCredentialsProviderBuilder().withAccessKeyId(madeUpCredentials).withSecretAccessKey(madeUpCredentials);
    try (S3Client client = createS3Client(clientOptions);
        CredentialsProvider emptyCredentialsProvider = builder.build()) {
        CompletableFuture<Integer> onFinishedFuture = new CompletableFuture<>();
        S3MetaRequestResponseHandler responseHandler = new S3MetaRequestResponseHandler() {

            @Override
            public void onFinished(int errorCode, int responseStatus, byte[] errorPayload) {
                Log.log(Log.LogLevel.Info, Log.LogSubject.JavaCrtS3, "Meta request finished with error code " + errorCode);
                if (errorCode != 0) {
                    onFinishedFuture.completeExceptionally(new CrtS3RuntimeException(errorCode, responseStatus, errorPayload));
                    return;
                }
                onFinishedFuture.complete(Integer.valueOf(errorCode));
            }
        };
        HttpHeader[] headers = { new HttpHeader("Host", ENDPOINT) };
        HttpRequest httpRequest = new HttpRequest("GET", "/get_object_test_1MB.txt", headers, null);
        S3MetaRequestOptions metaRequestOptions = new S3MetaRequestOptions().withMetaRequestType(MetaRequestType.GET_OBJECT).withHttpRequest(httpRequest).withResponseHandler(responseHandler).withCredentialsProvider(emptyCredentialsProvider);
        try (S3MetaRequest metaRequest = client.makeMetaRequest(metaRequestOptions)) {
            Assert.assertEquals(Integer.valueOf(0), onFinishedFuture.get());
        }
    } catch (InterruptedException | ExecutionException ex) {
        expectedException = true;
        /*
             * Maybe better to have a cause of the max retries exceed to be more informative
             */
        if (!(ex.getCause() instanceof CrtS3RuntimeException)) {
            Assert.fail(ex.getMessage());
        }
    }
    Assert.assertTrue(expectedException);
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) StaticCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider) CredentialsProvider(software.amazon.awssdk.crt.auth.credentials.CredentialsProvider) StaticCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider) DefaultChainCredentialsProvider(software.amazon.awssdk.crt.auth.credentials.DefaultChainCredentialsProvider) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) Test(org.junit.Test)

Example 15 with HttpHeader

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

the class S3ClientTest method testS3Get.

@Test
public void testS3Get() {
    skipIfNetworkUnavailable();
    Assume.assumeTrue(hasAwsCredentials());
    S3ClientOptions clientOptions = new S3ClientOptions().withEndpoint(ENDPOINT).withRegion(REGION);
    try (S3Client client = createS3Client(clientOptions)) {
        CompletableFuture<Integer> onFinishedFuture = new CompletableFuture<>();
        S3MetaRequestResponseHandler responseHandler = new S3MetaRequestResponseHandler() {

            @Override
            public int onResponseBody(ByteBuffer bodyBytesIn, long objectRangeStart, long objectRangeEnd) {
                byte[] bytes = new byte[bodyBytesIn.remaining()];
                bodyBytesIn.get(bytes);
                Log.log(Log.LogLevel.Info, Log.LogSubject.JavaCrtS3, "Body Response: " + Arrays.toString(bytes));
                return 0;
            }

            @Override
            public void onFinished(int errorCode, int responseStatus, byte[] errorPayload) {
                Log.log(Log.LogLevel.Info, Log.LogSubject.JavaCrtS3, "Meta request finished with error code " + errorCode);
                if (errorCode != 0) {
                    onFinishedFuture.completeExceptionally(new CrtS3RuntimeException(errorCode, responseStatus, errorPayload));
                    return;
                }
                onFinishedFuture.complete(Integer.valueOf(errorCode));
            }
        };
        HttpHeader[] headers = { new HttpHeader("Host", ENDPOINT) };
        HttpRequest httpRequest = new HttpRequest("GET", "/get_object_test_1MB.txt", headers, null);
        S3MetaRequestOptions metaRequestOptions = new S3MetaRequestOptions().withMetaRequestType(MetaRequestType.GET_OBJECT).withHttpRequest(httpRequest).withResponseHandler(responseHandler);
        try (S3MetaRequest metaRequest = client.makeMetaRequest(metaRequestOptions)) {
            Assert.assertEquals(Integer.valueOf(0), onFinishedFuture.get());
        }
    } catch (InterruptedException | ExecutionException ex) {
        Assert.fail(ex.getMessage());
    }
}
Also used : HttpRequest(software.amazon.awssdk.crt.http.HttpRequest) ByteBuffer(java.nio.ByteBuffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpHeader(software.amazon.awssdk.crt.http.HttpHeader) Test(org.junit.Test)

Aggregations

HttpHeader (software.amazon.awssdk.crt.http.HttpHeader)35 HttpRequest (software.amazon.awssdk.crt.http.HttpRequest)25 URI (java.net.URI)12 ByteBuffer (java.nio.ByteBuffer)11 Test (org.junit.Test)11 HttpRequestBodyStream (software.amazon.awssdk.crt.http.HttpRequestBodyStream)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 ArrayList (java.util.ArrayList)7 List (java.util.List)7 CompletableFuture (java.util.concurrent.CompletableFuture)6 SdkInternalApi (software.amazon.awssdk.annotations.SdkInternalApi)5 String (java.lang.String)4 Optional (java.util.Optional)4 AwsSigningResult (software.amazon.awssdk.crt.auth.signing.AwsSigningResult)4 SdkHttpFullRequest (software.amazon.awssdk.http.SdkHttpFullRequest)4 SdkHttpRequest (software.amazon.awssdk.http.SdkHttpRequest)4 CollectionUtils.isNullOrEmpty (software.amazon.awssdk.utils.CollectionUtils.isNullOrEmpty)4 SdkHttpUtils (software.amazon.awssdk.utils.http.SdkHttpUtils)4 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3