use of software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider 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.credentials.StaticCredentialsProvider 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.credentials.StaticCredentialsProvider 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.auth.credentials.StaticCredentialsProvider in project aws-crt-java by awslabs.
the class SigningTest method testSigningBasicSigv4Test.
@Test
public void testSigningBasicSigv4Test() 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_HEADERS);
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);
CompletableFuture<HttpRequest> result = AwsSigner.signRequest(request, config);
HttpRequest signedRequest = result.get();
assertNotNull(signedRequest);
assertTrue(hasHeaderWithValue(signedRequest, "X-Amz-Date", "20150830T123600Z"));
// expected authorization value for post-vanilla-query test
assertTrue(hasHeaderWithValue(signedRequest, "Authorization", "AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=28038455d6de14eafc1f9222cf5aa6f1a96197d7deb8263271d420d138af7f11"));
}
}
}
use of software.amazon.awssdk.crt.auth.credentials.StaticCredentialsProvider in project aws-crt-java by awslabs.
the class SigningTest method testSigningFailureBadRequest.
@Test(expected = CrtRuntimeException.class)
public void testSigningFailureBadRequest() 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 = createUnsignableRequest("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.setSignedBodyValue(AwsSigningConfig.AwsSignedBodyValue.EMPTY_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_AUTH_SIGNING_ILLEGAL_REQUEST_HEADER"));
throw crt;
}
}
Aggregations