【问题标题】:Signing AWS HTTP requests with Apache HttpComponents Client使用 Apache HttpComponents 客户端签署 AWS HTTP 请求
【发布时间】:2015-11-17 14:59:58
【问题描述】:

我正在尝试向受 IAM 访问策略保护的 AWS Elasticsearch 域发出 HTTP 请求。我需要sign 这些请求才能获得 AWS 的授权。 我正在使用Jest,而后者又使用Apache HttpComponents Client

这似乎是一个常见的用例,但我找不到我应该怎么做才能让 Jest 签署所有请求。

【问题讨论】:

    标签: java amazon-web-services elasticsearch apache-httpcomponents elasticsearch-jest


    【解决方案1】:

    我想我找到了! :)

    这个项目似乎完全符合我的要求:aws-signing-request-interceptor,描述为 “为 AWS 签名请求的 Apache 客户端的请求拦截器。最初创建是为了使用 Jest 客户端支持 AWS 的 Elasticsearch 服务。”。

    编辑:我forked the project 以满足我的需求(Java 7,临时 STS 凭据),它运行良好。

    这是一个使用示例(这里没有 STS 临时凭证):

    String region = "us-east-1";
    String service = "es";
    String url = "???"; // put the AWS ElasticSearch endpoint here
    
    DefaultAWSCredentialsProviderChain awsCredentialsProvider = new DefaultAWSCredentialsProviderChain();
    final AWSSigner awsSigner = new AWSSigner(awsCredentialsProvider, region, service, () -> new LocalDateTime(DateTimeZone.UTC));
    
    JestClientFactory factory = new JestClientFactory() {
        @Override
        protected HttpClientBuilder configureHttpClient(HttpClientBuilder builder) {
            builder.addInterceptorLast(new AWSSigningRequestInterceptor(awsSigner));
            return builder;
        }
    };
    factory.setHttpClientConfig(new HttpClientConfig.Builder(url)
            .multiThreaded(true)
            .build());
    JestClient client = factory.getObject();
    

    【讨论】:

    • 嗨,Eric,我正在关注您提供的 github 链接。我们正面临着被禁止的错误。另一个问题是在普通的 jest 客户端中,我们通过弹性搜索端点。但是这里我们只传递服务名称和区域。您能否提供示例代码或指导来解决我们的问题。
    • 嗨@MohanShanmugam。禁止错误可能是由于访问策略配置错误或凭据错误。我在答案中添加了一个使用示例。希望这会有所帮助。
    • 嗨,埃里克,感谢您的回复。我们使用 jestclient 创建索引,在此索引创建和删除索引中放置映射和插入记录工作正常。放置映射和插入记录显示禁止错误。当我浏览所有社区成员的讨论时,它看起来很奇怪。我们正在使用 jest 2.0.0 和 AWS Es 2.3。您是否知道为什么放置映射和插入记录不起作用。
    • 我们从 AWS lambda 运行这个程序。 lambda 角色拥有 Es 完全权限。我们在弹性搜索策略中使用了这个角色 ARN。
    • 在不了解详细信息的情况下很难为您解决此问题。您应该在 AWS 论坛中发布您的问题或联系支持人员。他们都有很大的帮助。
    【解决方案2】:

    这在异步请求的情况下不起作用。

    更新:

    忽略我之前的评论。在为异步请求添加拦截器后它也可以工作:

    final AWSSigningRequestInterceptor requestInterceptor = new AWSSigningRequestInterceptor(awsSigner);
                factory = new JestClientFactory() {
                    @Override
                    protected HttpClientBuilder configureHttpClient(HttpClientBuilder builder) {
                        builder.addInterceptorLast(requestInterceptor);
                        return builder;
                    }
                    @Override
                    protected HttpAsyncClientBuilder configureHttpClient(HttpAsyncClientBuilder builder) {
                        builder.addInterceptorLast(requestInterceptor);
                        return builder;
                    }
                };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-27
      • 2014-11-09
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      相关资源
      最近更新 更多