SQS ExpiredToken:请求中包含的安全令牌已过期状态码:403

SQS ExpiredToken: The security token included in the request is expired status code: 403

我在 EC2 上有一个长 运行 工作进程 运行,它使用 SQS 队列中的项目。一段时间后(我估计是 8-12 小时),我开始收到过期的安全令牌错误。我希望 aws 库自动处理凭据的刷新,但事实并非如此。它无论如何都在客户端处理吗? 仅当我使用 DefaultCredentialsProviderChain 生成访问权限时才会发生这种情况。与 key 和 secret 一起使用时不会发生此错误。 堆栈跟踪如下:

com.amazonaws.AmazonServiceException: The security token included in the request is expired (Service: AmazonSQS; Status Code: 403; Error Code: ExpiredToken; Request ID: 6ff6e1a0-d668-5ac5-bcd7-ae30058f25c0)
    at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:1182)
    at com.amazonaws.http.AmazonHttpClient.executeOneRequest(AmazonHttpClient.java:770)
    at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:489)
    at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:310)
    at com.amazonaws.services.sqs.AmazonSQSClient.invoke(AmazonSQSClient.java:2419)
    at com.amazonaws.services.sqs.AmazonSQSClient.receiveMessage(AmazonSQSClient.java:1130)
    at com.amazonaws.services.sqs.AmazonSQSAsyncClient.call(AmazonSQSAsyncClient.java:1783)
    at com.amazonaws.services.sqs.AmazonSQSAsyncClient.call(AmazonSQSAsyncClient.java:1779)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我找到的解决方法是每次遇到令牌过期错误时更新 awsCredentials 并重置 sqs 客户端。

awsCredentials = (new DefaultAWSCredentialsProviderChain).getCredentials
sqs = SimpleSQSClient(awsCredentials, Regions.US_EAST_1)
queueSQS = sqs.simple(QueueName(queueName), true)

注意:我正在使用包装器kifi/franz

AWS SDK 确实能够循环从实例配置文件继承的临时凭证,但是通过在 SimpleSQSClient 的构造函数中传递一个显式的 AWSCredentials 对象,我相信你在否认它这样做的机会。

您没有明确声明您的应用程序正在继承实例角色,但您的 post 中有足够的证据可以推断是这种情况:

  • 您的应用程序 运行 在 EC2 上。
  • DefaultAWSCredentialsProviderChain 的行为是寻找 "Instance profile credentials delivered through the Amazon EC2 metadata service" 如果找不到其他凭据。
  • 您只有在未明确传递您自己已知的 access/secret 键时才会看到此行为。

文档中描述了自动凭据刷新的具体行为:

The automatic credentials refresh happens only when you use the default client constructor, which creates its own InstanceProfileCredentialsProvider as part of the default provider chain, or when you pass an InstanceProfileCredentialsProvider instance directly to the client constructor. If you use another method to obtain or pass instance profile credentials, you are responsible for checking for and refreshing expired credentials.

通过直接传递 AWSCredentials 而不是 AWSCredentialsProvider,您将负责检查和刷新过期的凭证。从好的方面来说,如果您想继续明确传递凭据,您的解决方法就可以了。

SimpleSQSClient 有一个构造函数,更适合您的用例:

new SimpleSQSClient(
    credentialProvider: com.amazonaws.auth.AWSCredentialsProvider,
    region: com.amazonaws.regions.Regions,
    buffered: Boolean
)

示例:

SimpleSQSClient sqs = SimpleSQSClient(new DefaultAWSCredentialsProviderChain(), Regions.US_EAST_1, false)

示例,明确使用 InstanceProfileCredentialsProvider:

SimpleSQSClient sqs = SimpleSQSClient(new InstanceProfileCredentialsProvider(), Regions.US_EAST_1, false)

进一步阅读: