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