use of software.amazon.awssdk.auth.signer.params.Aws4SignerParams in project hibernate-search by hibernate.
the class AwsSigningRequestInterceptor method sign.
private void sign(HttpRequest request, HttpContext context, HttpEntityContentStreamProvider contentStreamProvider) {
SdkHttpFullRequest awsRequest = toAwsRequest(request, context, contentStreamProvider);
if (log.isTraceEnabled()) {
log.tracef("HTTP request (before signing): %s", request);
log.tracef("AWS request (before signing): %s", awsRequest);
}
AwsCredentials credentials = credentialsProvider.resolveCredentials();
log.tracef("AWS credentials: %s", credentials);
Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingRegion(region).signingName(ELASTICSEARCH_SERVICE_NAME).build();
awsRequest = signer.sign(awsRequest, signerParams);
// We don't expect signing to affect anything else (path, query, content, ...).
for (Map.Entry<String, List<String>> header : awsRequest.headers().entrySet()) {
String name = header.getKey();
boolean first = true;
for (String value : header.getValue()) {
if (first) {
request.setHeader(name, value);
first = false;
} else {
request.addHeader(name, value);
}
}
}
if (log.isTraceEnabled()) {
log.tracef("AWS request (after signing): %s", awsRequest);
log.tracef("HTTP request (after signing): %s", request);
}
}
use of software.amazon.awssdk.auth.signer.params.Aws4SignerParams in project aws-sdk-java-v2 by aws.
the class NonStreamingAsyncBodyAws4SignerTest method test_sign_computesCorrectSignatureWithChecksum.
@Test
void test_sign_computesCorrectSignatureWithChecksum() {
Aws4Signer aws4Signer = Aws4Signer.create();
AsyncAws4Signer asyncAws4Signer = AsyncAws4Signer.create();
byte[] content = "abc".getBytes(StandardCharsets.UTF_8);
ContentStreamProvider syncBody = () -> new ByteArrayInputStream(content);
AsyncRequestBody asyncBody = AsyncRequestBody.fromBytes(content);
SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder().protocol("https").host("my-cool-aws-service.us-west-2.amazonaws.com").method(SdkHttpMethod.GET).putHeader("header1", "headerval1").contentStreamProvider(syncBody).build();
AwsCredentials credentials = AwsBasicCredentials.create("akid", "skid");
Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingClockOverride(Clock.fixed(Instant.EPOCH, ZoneId.of("UTC"))).signingName("my-cool-aws-service").signingRegion(Region.US_WEST_2).checksumParams(SignerChecksumParams.builder().algorithm(Algorithm.SHA256).checksumHeaderName("x-amzn-header").isStreamingRequest(true).build()).build();
List<String> syncSignature = aws4Signer.sign(httpRequest, signerParams).headers().get("Authorization");
final SdkHttpFullRequest sdkHttpFullRequest = asyncAws4Signer.signWithBody(httpRequest, asyncBody, signerParams).join();
List<String> asyncSignature = sdkHttpFullRequest.headers().get("Authorization");
assertThat(asyncSignature).isEqualTo(syncSignature);
final List<String> stringList = sdkHttpFullRequest.headers().get("x-amzn-header");
assertThat(stringList.stream().findFirst()).hasValue("ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0=");
assertThat(sdkHttpFullRequest.firstMatchingHeader(("x-amzn-trailer"))).isNotPresent();
}
use of software.amazon.awssdk.auth.signer.params.Aws4SignerParams in project aws-sdk-java-v2 by aws.
the class NonStreamingAsyncBodyAws4SignerTest method test_sign_publisherThrows_exceptionPropagated.
@Test
void test_sign_publisherThrows_exceptionPropagated() {
AsyncAws4Signer asyncAws4Signer = AsyncAws4Signer.create();
RuntimeException error = new RuntimeException("error");
Flowable<ByteBuffer> errorPublisher = Flowable.error(error);
AsyncRequestBody asyncBody = new AsyncRequestBody() {
@Override
public Optional<Long> contentLength() {
return Optional.of(42L);
}
@Override
public void subscribe(Subscriber<? super ByteBuffer> subscriber) {
errorPublisher.subscribe(subscriber);
}
};
SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder().protocol("https").host("my-cool-aws-service.us-west-2.amazonaws.com").method(SdkHttpMethod.GET).putHeader("header1", "headerval1").build();
AwsCredentials credentials = AwsBasicCredentials.create("akid", "skid");
Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingClockOverride(Clock.fixed(Instant.EPOCH, ZoneId.of("UTC"))).signingName("my-cool-aws-service").signingRegion(Region.US_WEST_2).build();
assertThatThrownBy(asyncAws4Signer.signWithBody(httpRequest, asyncBody, signerParams)::join).hasCause(error);
}
use of software.amazon.awssdk.auth.signer.params.Aws4SignerParams in project aws-sdk-java-v2 by aws.
the class NonStreamingAsyncBodyAws4SignerTest method test_sign_computesCorrectSignature.
@Test
void test_sign_computesCorrectSignature() {
Aws4Signer aws4Signer = Aws4Signer.create();
AsyncAws4Signer asyncAws4Signer = AsyncAws4Signer.create();
byte[] content = "Hello AWS!".getBytes(StandardCharsets.UTF_8);
ContentStreamProvider syncBody = () -> new ByteArrayInputStream(content);
AsyncRequestBody asyncBody = AsyncRequestBody.fromBytes(content);
SdkHttpFullRequest httpRequest = SdkHttpFullRequest.builder().protocol("https").host("my-cool-aws-service.us-west-2.amazonaws.com").method(SdkHttpMethod.GET).putHeader("header1", "headerval1").contentStreamProvider(syncBody).build();
AwsCredentials credentials = AwsBasicCredentials.create("akid", "skid");
Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingClockOverride(Clock.fixed(Instant.EPOCH, ZoneId.of("UTC"))).signingName("my-cool-aws-service").signingRegion(Region.US_WEST_2).build();
List<String> syncSignature = aws4Signer.sign(httpRequest, signerParams).headers().get("Authorization");
List<String> asyncSignature = asyncAws4Signer.signWithBody(httpRequest, asyncBody, signerParams).join().headers().get("Authorization");
assertThat(asyncSignature).isEqualTo(syncSignature);
}
use of software.amazon.awssdk.auth.signer.params.Aws4SignerParams in project aws-sdk-java-v2 by aws.
the class SignerTestUtils method signAsyncRequest.
public static AsyncRequestBody signAsyncRequest(BaseAsyncAws4Signer signer, SdkHttpFullRequest request, AsyncRequestBody asyncRequestBody, AwsCredentials credentials, String signingName, Clock signingDateOverride, String region) {
Aws4SignerParams signerParams = Aws4SignerParams.builder().awsCredentials(credentials).signingName(signingName).signingClockOverride(signingDateOverride).signingRegion(Region.of(region)).build();
final Aws4SignerRequestParams requestParams = new Aws4SignerRequestParams(signerParams);
return signer.signAsync(request, asyncRequestBody, requestParams, signerParams);
}
Aggregations