How to use AWSRequestSigningApacheInterceptor with AWS SDK2

扶醉桌前 提交于 2019-12-11 03:09:20

问题


I am trying to use REST calls to Neptune SPARQL on existing Java code which already uses Apache HTTP clients. I'd like to not mix and match AWS SDK1 and SDK2 (which I use for the S3 portion of loading owl to Neptune).

I see these solutions:

  • AWSRequestSigningApacheInterceptor that works with SDK1, but can't find the equivalent in SDK2.

  • aws-request-signing-apache-interceptor on github for building an adaptor class so it can be used in SDK 2 with mix-and-match SDK 1 & 2

  • javaquery/Examples where Vicky Thakor has gone even more generic and just implemented the V4 signing for any Java REST implementation

But none of these is what I expected: an AWS or Apache implmentation of an Apache Interceptor for AWS SDK 2.

Is there such a thing? or is one of the above solutions the best available at the moment?


回答1:


So, I settled on the second option with an important caveat: it does not handle AWS_SESSION_TOKEN. This is a simple fix. I've posted it along with the original answer at http://github.com/awslabs/aws-request-signing-apache-interceptor/




回答2:


Here is some minimal code to make a few different authenticated REST requests to the ElasticSearch API (not Neptune SPARQL, but it's all REST).

pom.xml:

  <dependencies>
    <dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>s3</artifactId>
      <!-- version number is not needed due to the BOM below -->
    </dependency>

    <!-- below is needed for this issue: https://github.com/aws/aws-sdk-java-v2/issues/652 -->
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpcore</artifactId>
      <version>4.4.11</version>
    </dependency>

    <dependency>
      <groupId>software.amazon.awssdk</groupId>
      <artifactId>apache-client</artifactId>
      <!-- version number is not needed due to the BOM below -->
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>bom</artifactId>
        <version>2.7.36</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

And here's the java:

import org.json.JSONObject;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.auth.signer.Aws4Signer;
import software.amazon.awssdk.auth.signer.params.Aws4SignerParams;
import software.amazon.awssdk.http.*;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.utils.StringInputStream;
import java.io.*;

public class ElasticSearch implements Closeable {
    private static final String HOST = "my-elasticsearch-3490jvoi2je3o.us-east-2.es.amazonaws.com";

    private Aws4SignerParams params = Aws4SignerParams.builder()
            .awsCredentials(DefaultCredentialsProvider.create().resolveCredentials())
            .signingName("es")  // "es" stands for elastic search.  Change this to match your service!
            .signingRegion(Region.US_EAST_2)
            .build();

    private Aws4Signer signer = Aws4Signer.create();

    SdkHttpClient httpClient = ApacheHttpClient.builder().build();

    /** @param path should not have a leading "/" */
    private HttpExecuteResponse restRequest(SdkHttpMethod method, String path) throws IOException {
        return restRequest(method, path, null);
    }


    private HttpExecuteResponse restRequest(SdkHttpMethod method, String path, JSONObject body)
            throws IOException {
        SdkHttpFullRequest.Builder b = SdkHttpFullRequest.builder()
                .encodedPath(path)
                .host(HOST)
                .method(method)
                .protocol("https");
        if (body != null) {
            b.putHeader("Content-Type", "application/json; charset=utf-8");
            b.contentStreamProvider(() -> new StringInputStream(body.toString()));
        }
        SdkHttpFullRequest request = b.build();

        // now sign it
        SdkHttpFullRequest signedRequest = signer.sign(request, params);
        HttpExecuteRequest.Builder rb = HttpExecuteRequest.builder().request(signedRequest);
        // !!!: line below is necessary even though the contentStreamProvider is in the request.
        // Otherwise the body will be missing from the request and auth signature will fail.
        request.contentStreamProvider().ifPresent(c -> rb.contentStreamProvider(c));
        return httpClient.prepareRequest(rb.build()).call();
    }

    public void search(String indexName, String searchString) throws IOException {
        HttpExecuteResponse result = restRequest(SdkHttpMethod.GET, indexName+"/_search",
                new JSONObject().put("query",
                        new JSONObject().put("match",
                                new JSONObject().put("txt",
                                        new JSONObject().put("query", searchString)))));
        System.out.println("Search results:");
        System.out.println(new JSONObject(result.responseBody()));
    }

    /** @return success status */
    public boolean createIndex(String indexName) throws IOException {
        if (indexName.contains("/")) {
            throw new RuntimeException("indexName cannot contain '/' character");
        }
        HttpExecuteResponse r = restRequest(SdkHttpMethod.PUT, indexName);
        System.out.println("PUT /"+indexName + " response code: " + r.httpResponse().statusCode());
        printInputStream(r.responseBody().get());
        return r.httpResponse().isSuccessful();
    }

    private void printInputStream(InputStream is) {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
            String readLine;
            while (((readLine = br.readLine()) != null)) System.out.println(readLine);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public boolean postDoc(String indexName, String docId, JSONObject docBody) throws IOException {
        HttpExecuteResponse response = restRequest(
                SdkHttpMethod.PUT,
                String.format("%s/_doc/%s", indexName, docId),
                docBody
        );
        System.out.println("Index operation response:");
        printInputStream(response.responseBody().get());
        return response.httpResponse().isSuccessful();
    }

    @Override
    public void close() throws IOException {
        httpClient.close();
    }
}



来源:https://stackoverflow.com/questions/56224130/how-to-use-awsrequestsigningapacheinterceptor-with-aws-sdk2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!