use of software.amazon.awssdk.crt.auth.signing.AwsSigningConfig in project aws-crt-java by awslabs.
the class SigningTest method testSigningSuccess.
@Test
public void testSigningSuccess() throws Exception {
try (StaticCredentialsProvider provider = new StaticCredentialsProvider.StaticCredentialsProviderBuilder().withAccessKeyId(TEST_ACCESS_KEY_ID).withSecretAccessKey(TEST_SECRET_ACCESS_KEY).build()) {
HttpRequest request = createSimpleRequest("https://www.example.com", "POST", "/derp", "<body>Hello</body>");
Predicate<String> filterParam = param -> !param.equals("bad-param");
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.setShouldSignHeader(filterParam);
config.setUseDoubleUriEncode(true);
config.setShouldNormalizeUriPath(true);
config.setSignedBodyValue(AwsSigningConfig.AwsSignedBodyValue.EMPTY_SHA256);
CompletableFuture<HttpRequest> result = AwsSigner.signRequest(request, config);
HttpRequest signedRequest = result.get();
assertNotNull(signedRequest);
assertTrue(hasHeader(signedRequest, "X-Amz-Date"));
assertTrue(hasHeader(signedRequest, "Authorization"));
}
}
}
use of software.amazon.awssdk.crt.auth.signing.AwsSigningConfig in project aws-crt-java by awslabs.
the class SigningTest method testQuerySigningSuccess.
@Test
public void testQuerySigningSuccess() throws Exception {
try (StaticCredentialsProvider provider = new StaticCredentialsProvider.StaticCredentialsProviderBuilder().withAccessKeyId(TEST_ACCESS_KEY_ID).withSecretAccessKey(TEST_SECRET_ACCESS_KEY).build()) {
HttpRequest request = createSigv4TestSuiteRequest();
try (AwsSigningConfig config = new AwsSigningConfig()) {
config.setAlgorithm(AwsSigningConfig.AwsSigningAlgorithm.SIGV4);
config.setSignatureType(AwsSigningConfig.AwsSignatureType.HTTP_REQUEST_VIA_QUERY_PARAMS);
config.setRegion("us-east-1");
config.setService("service");
config.setTime(DATE_FORMAT.parse("2015-08-30T12:36:00Z").getTime());
config.setCredentialsProvider(provider);
config.setUseDoubleUriEncode(true);
config.setShouldNormalizeUriPath(true);
config.setSignedBodyValue(AwsSigningConfig.AwsSignedBodyValue.EMPTY_SHA256);
config.setExpirationInSeconds(60);
CompletableFuture<HttpRequest> result = AwsSigner.signRequest(request, config);
HttpRequest signedRequest = result.get();
assertNotNull(signedRequest);
String path = signedRequest.getEncodedPath();
assertTrue(path.contains("X-Amz-Signature="));
assertTrue(path.contains("X-Amz-SignedHeaders=host"));
assertTrue(path.contains("X-Amz-Credential=AKIDEXAMPLE%2F20150830%2F"));
assertTrue(path.contains("X-Amz-Algorithm=AWS4-HMAC-SHA256"));
assertTrue(path.contains("X-Amz-Expires=60"));
}
}
}
use of software.amazon.awssdk.crt.auth.signing.AwsSigningConfig in project aws-crt-java by awslabs.
the class SigningTest method createChunkedRequestSigningConfig.
private AwsSigningConfig createChunkedRequestSigningConfig() throws Exception {
AwsSigningConfig config = new AwsSigningConfig();
config.setAlgorithm(AwsSigningConfig.AwsSigningAlgorithm.SIGV4);
config.setSignatureType(AwsSigningConfig.AwsSignatureType.HTTP_REQUEST_VIA_HEADERS);
config.setRegion(CHUNKED_TEST_REGION);
config.setService(CHUNKED_TEST_SERVICE);
config.setTime(DATE_FORMAT.parse(CHUNKED_TEST_SIGNING_TIME).getTime());
config.setUseDoubleUriEncode(false);
config.setShouldNormalizeUriPath(true);
config.setSignedBodyHeader(AwsSigningConfig.AwsSignedBodyHeaderType.X_AMZ_CONTENT_SHA256);
config.setSignedBodyValue(AwsSigningConfig.AwsSignedBodyValue.STREAMING_AWS4_HMAC_SHA256_PAYLOAD);
config.setCredentials(createChunkedTestCredentials());
return config;
}
use of software.amazon.awssdk.crt.auth.signing.AwsSigningConfig in project aws-crt-java by awslabs.
the class SigningTest method testTrailingHeadersSigv4Signing.
@Test
public void testTrailingHeadersSigv4Signing() 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<AwsSigningResult> chunk1Result = AwsSigner.sign(chunk1, signature, createChunkSigningConfig());
signature = chunk1Result.get().getSignature();
assertTrue(Arrays.equals(signature, EXPECTED_FIRST_CHUNK_SIGNATURE));
HttpRequestBodyStream chunk2 = createChunk2Stream();
CompletableFuture<AwsSigningResult> chunk2Result = AwsSigner.sign(chunk2, signature, createChunkSigningConfig());
signature = chunk2Result.get().getSignature();
assertTrue(Arrays.equals(signature, EXPECTED_SECOND_CHUNK_SIGNATURE));
CompletableFuture<AwsSigningResult> finalChunkResult = AwsSigner.sign((HttpRequestBodyStream) null, signature, createChunkSigningConfig());
signature = finalChunkResult.get().getSignature();
assertTrue(Arrays.equals(signature, EXPECTED_FINAL_CHUNK_SIGNATURE));
List<HttpHeader> trailingHeaders = createTrailingHeaders();
AwsSigningConfig trailingHeadersSigningConfig = createTrailingHeadersSigningConfig();
CompletableFuture<AwsSigningResult> trailingHeadersResult = AwsSigner.sign(trailingHeaders, signature, trailingHeadersSigningConfig);
signature = trailingHeadersResult.get().getSignature();
assertTrue(Arrays.equals(signature, EXPECTED_TRAILING_HEADERS_SIGNATURE));
}
use of software.amazon.awssdk.crt.auth.signing.AwsSigningConfig in project aws-crt-java by awslabs.
the class SigningTest method testSigningException.
@Test
public void testSigningException() throws Exception {
DelegateCredentialsHandler credentialsHandler = new DelegateCredentialsHandler() {
@Override
public Credentials getCredentials() {
throw new RuntimeException("failed");
}
};
boolean failed = false;
try (DelegateCredentialsProvider provider = new DelegateCredentialsProvider.DelegateCredentialsProviderBuilder().withHandler(credentialsHandler).build()) {
HttpRequest request = createSimpleRequest("https://www.example.com", "POST", "/derp", "<body>Hello</body>");
Predicate<String> filterParam = param -> !param.equals("bad-param");
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.setShouldSignHeader(filterParam);
config.setUseDoubleUriEncode(true);
config.setShouldNormalizeUriPath(true);
config.setSignedBodyValue(AwsSigningConfig.AwsSignedBodyValue.EMPTY_SHA256);
CompletableFuture<HttpRequest> result = AwsSigner.signRequest(request, config);
HttpRequest signedRequest = result.get();
assertNotNull(signedRequest);
assertTrue(hasHeader(signedRequest, "X-Amz-Date"));
assertTrue(hasHeader(signedRequest, "Authorization"));
}
} catch (Exception ex) {
failed = true;
}
assertTrue(failed);
}
Aggregations