即使设置了策略,也拒绝访问 SQS 队列

Access to SQS Queue is denied even when policies are set

我们有一个 reader 从 SQS 队列读取消息。在本地它可以工作,但是当我们部署到 ECS 时,我们收到一个异常:

Access to the resource [queue-url] is denied.

我们可以在本地访问队列,即当我们通过 CredentialProfileStoreChain 使用配置文件初始化客户端时。我们还对所有策略和权限进行了双重和三重检查 - 所有资源都具有彼此所需的访问权限(即 ECS -> read/write/delete SQS)。

我认为我没有正确初始化 AmazonSQSClient,但我已遵循所有文档并尝试了各种选项。

代码如下:

我的 SqsClientFactory(这是一个 AmazonSQSClient):

public static AmazonSQSClient CreateClient(AppSettings appSettings)
{
    var sqsConfig = new AmazonSQSConfig()
    {
        RegionEndpoint = RegionEndpoint.GetBySystemName(appSettings.Region)
    };          

    //For testing on the local machine - this works
    if(!String.IsNullOrEmpty(appSettings.AwsProfile))
    {

        var credentialProfileStoreChain = new CredentialProfileStoreChain();
        AWSCredentials credentials;
        credentialProfileStoreChain.TryGetAWSCredentials(appSettings.Aws.Profile, out credentials);

        return new AmazonSQSClient(credentials, sqsConfig);
    }           
    
    //For deployed versions
    return new AmazonSQSClient(RegionEndpoint.GetBySystemName(appSettings.Region));
    //Also tried:
    //return new AmazonSQSClient(sqsConfig));
    //return new AmazonSQSClient();

}

我是否遗漏了有关正确初始化 AmazongSQSClient 的内容?

确保以下两种策略都允许访问 SQS 队列

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "Service": "ecs-task.amazonaws.com"
      },
      "Action": [
                "sqs:DeleteMessage",
                "sqs:ReceiveMessage",
                "sqs:SendMessage"
      ],
      "Resource": arn:aws:sqs:<region>:<account>:<queue name>",
    }
  ]
}
  • 基于身份的策略:访问队列的身份的策略应该有权调用队列上的操作。在这种情况下,TaskRole
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "sqs:DeleteMessage",
                "sqs:ReceiveMessage",
                "sqs:SendMessage"
            ],
            "Resource": "arn:aws:sqs:<region>:<account>:<queue name>"
        }
    ]
}

确保访问不受其中任何一个限制。

https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-using-identity-based-policies.html

找到了解决方案,当然它有些愚蠢得令人吃惊。有人签入了配置文件值不为空的 appsettings.json 文件,即“配置文件:#aws_profile#”。在我们选择参数存储之前,在管道执行期间注入这些设置时遗留下来的。

也就是说这部分:

if(!String.IsNullOrEmpty(appSettings.AwsProfile))

是真的,但从未达到正确的代码。