An open API service indexing awesome lists of open source software.

https://github.com/acm19/aws-request-signing-apache-interceptor


https://github.com/acm19/aws-request-signing-apache-interceptor

aws java opensearch

Last synced: 5 months ago
JSON representation

Awesome Lists containing this project

README

          

# AWS Request Signing Interceptor

[![tests](https://github.com/acm19/aws-request-signing-apache-interceptor/actions/workflows/test.yml/badge.svg)](https://github.com/acm19/aws-request-signing-apache-interceptor/actions/workflows/test.yml)
[![Maven Central](https://img.shields.io/maven-central/v/io.github.acm19/aws-request-signing-apache-interceptor)](https://search.maven.org/artifact/io.github.acm19/aws-request-signing-apache-interceptor)

An AWS request signing interceptor for arbitrary HTTP requests. It supports both [Apache HTTP Client](https://search.maven.org/artifact/org.apache.httpcomponents/httpclient) and [Apache HTTP Client V5](https://search.maven.org/artifact/org.apache.httpcomponents.client5/httpclient5).

This library enables you to sign requests to any service that leverages SigV4, and thus access any AWS Service or APIG-backed service, including Amazon managed OpenSearch and OpenSearch Serverless.

This library is based on [AWS Interceptor](https://github.com/awslabs/aws-request-signing-apache-interceptor), but using AWS SDK 2.x.

## Usage

Add [io.github.acm19.aws-request-signing-apache-interceptor](https://repo1.maven.org/maven2/io/github/acm19/aws-request-signing-apache-interceptor/) as a dependency.

```xml

io.github.acm19
aws-request-signing-apache-interceptor
4.0.0

```

### Apache HTTP Client

To sign requests made with pre-5 versions of the clients the following interceptor should be used `io.github.acm19.aws.interceptor.http.AwsRequestSigningApacheInterceptor`.

```java
import java.io.IOException;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import io.github.acm19.aws.interceptor.http.AwsRequestSigningApacheInterceptor;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.utils.IoUtils;

final class Example {
public static void main(String[] args) throws IOException {
AwsRequestSigningApacheInterceptor interceptor = new AwsRequestSigningApacheInterceptor(
"service",
AwsV4HttpSigner.create(),
DefaultCredentialsProvider.builder().build(),
Region.US_WEST_2
);

try (CloseableHttpClient client = HttpClients.custom()
.addInterceptorLast(interceptor)
.build()) {
HttpGet httpGet = new HttpGet("https://...");
httpClient.execute(request, response -> {
System.out.println(response.getStatusLine());
System.out.println(IoUtils.toUtf8String(response.getEntity().getContent()));
});
}
}
}
```

### Apache HTTP Client V5

To sign requests made with version 5 of the client the following interceptor should be used `io.github.acm19.aws.interceptor.http.AwsRequestSigningApacheV5Interceptor`.

```java
import java.io.IOException;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import io.github.acm19.aws.interceptor.http.AwsRequestSigningApacheV5Interceptor;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.utils.IoUtils;

final class Example {
public static void main(String[] args) throws IOException {
AwsRequestSigningApacheV5Interceptor interceptor = new AwsRequestSigningApacheV5Interceptor(
"service",
AwsV4HttpSigner.create(),
DefaultCredentialsProvider.builder().build(),
Region.US_WEST_2
);

try (CloseableHttpClient client = HttpClients.custom()
.addExecInterceptorLast("aws-signing-interceptor", interceptor)
.build()) {
HttpGet httpGet = new HttpGet("https://...");
client.execute(httpGet, response -> {
System.out.println(response.getCode());
System.out.println(IoUtils.toUtf8String(response.getEntity().getContent()));
});
}
}
}
```

## Examples

### Apache HTTP Client

To run the [Amazon OpenSearch Sample](src/test/java/io/github/acm19/aws/interceptor/test/AmazonOpenSearchServiceSample.java) pass the values of `endpoint` and `region` into `exec.args`.

```
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export AWS_SESSION_TOKEN=

mvn test-compile exec:java -Dexec.classpathScope=test -Dexec.mainClass="io.github.acm19.aws.interceptor.test.AmazonOpenSearchServiceSample" -Dexec.args="--endpoint=https://...us-west-2.es.amazonaws.com --region=us-west-2 --service=es"
```

Alternatively use `make` as follows:

```
ENDPOINT= SERVICE=es REGION= SERVICE=es make run_sample
```

See [examples](src/test/java/io/github/acm19/aws/interceptor/test) for more valid requests.

### Apache HTTP Client V5

To run the [Amazon OpenSearch Sample](src/test/java/io/github/acm19/aws/interceptorv5/test/AmazonOpenSearchServiceSample.java) pass the values of `endpoint` and `region` into `exec.args`.

```
export AWS_ACCESS_KEY_ID=
export AWS_SECRET_ACCESS_KEY=
export AWS_SESSION_TOKEN=

mvn test-compile exec:java -Dexec.classpathScope=test -Dexec.mainClass="io.github.acm19.aws.interceptorv5.test.AmazonOpenSearchServiceSample" -Dexec.args="--endpoint=https://...us-west-2.aoss.amazonaws.com --region=us-west-2 --service=aoss"
```

Alternatively use `make` as follows:

```
ENDPOINT= REGION= SERVICE=aoss make run_v5_sample
```

See [examples](src/test/java/io/github/acm19/aws/interceptorv5/test) for more valid requests.

## Contributing

You're encouraged to contribute to this project. See [CONTRIBUTING](CONTRIBUTING.md) for details.

## Copyright

Copyright Amazon.com, Inc. or its affiliates, and Project Contributors.
See [NOTICE](NOTICE) for details.

## License

This library is licensed under the [Apache 2.0 License](LICENSE).