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));
}
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);
}
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));
}
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;
}
}
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();
}
Aggregations