1

我想使用来自 Apache Beam 管道的短期凭证访问 AWS SQS。

在 AWS IAM 中,我创建了一个具有以下信任关系的角色:

{
  "Effect": "Allow",
  "Principal": {
    "AWS": "arn:aws:sts::xxxxxx:assumed-role/gcp_role/gcp-project-session-name",
    "Service": "sqs.amazonaws.com"
  },
  "Action": "sts:AssumeRole"
},

有了这个角色,我就可以从我的本地机器访问 SQS。我使用 AWS BasicSessionCredentials 如下:

  BasicSessionCredentials refreshedAWSCredentials = new BasicSessionCredentials(
            refreshedCredentials.getAccessKeyId(),
            refreshedCredentials.getSecretAccessKey(),
            refreshedCredentials.getSessionToken());

    AWSSecurityTokenService service = AWSSecurityTokenServiceClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(refreshedAWSCredentials))
            .withRegion(options.getAwsRegion()).build();

我将凭证对象添加到管道选项中:

     options.setAwsSessionToken(refreshedAWSCredentials.getSessionToken());
     options.setAwsCredentialsProvider(new AWSStaticCredentialsProvider(refreshedAWSCredentials));      
    return Pipeline.create(options); 

最后我总是遇到以下错误:

原因:org.apache.beam.sdk.util.UserCodeException:com.amazonaws.services.sqs.model.AmazonSQSException:请求中包含的安全令牌无效。(服务:AmazonSQS;状态代码:403;错误代码:InvalidClientTokenId;请求 ID:501e9869-ea58-5e80-9ec1-c1exxxx;代理:null

我假设 AWSStaticCredentialsProvider 不知道 AWS_SECRET_TOKEN。这就是我设置 STSAssumeRoleSessionCredentialsProvider 的原因,它应该与临时凭证一起使用

STSAssumeRoleSessionCredentialsProvider stsSessionProvider = new STSAssumeRoleSessionCredentialsProvider
            .Builder(awsRoleArn, awsRoleSession)
            .withStsClient(service)
            .build();

这是相关的管道代码

p.apply(SqsIO.read().withQueueUrl(options.getSourceQueueUrl())
            .withMaxNumRecords(options.getNumberOfRecords()))
            .apply(ParDo.of(new SqsMessageToJson()))
            .apply(TextIO.write()
                    .to(options.getDestinationBucketUrl() + "/purchase_intent/")
                    .withSuffix(".json"));

即使我使用了上述在本地也可以使用的提供程序,我也得到了上面显示的 sam 异常。所以,我想知道如何使用临时凭据设置 SqsIO。

4

0 回答 0