【问题标题】:Set SQS policy document with AWS CLI使用 AWS CLI 设置 SQS 策略文档
【发布时间】:2021-08-10 15:19:00
【问题描述】:

我正在尝试设置 SQS 队列的策略以允许 SNS 主题发送消息。

我已尝试关注thisthisthis

我正在使用这个命令

policy=$(getPolicy $queueArn $topicArn)
echo  $policy
aws sqs set-queue-attributes --queue-url $queueUrl --attributes "{\"Policy\":${policy}}"

getPolicy 函数构建策略文档

function getPolicy() {
    queueArn=$1
    topicArn=$2

    policy="{\"Version\":\"2012-10-17\",\"Id\":\"policy12345\",\"Statement\":[{\"Sid\":\"stmt12345\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::${account}:root\"},\"Action\":\"SQS:*\",\"Resource\":\"${queueArn}\"},{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"sns.amazonaws.com\"},\"Action\":\"sqs:SendMessage\",\"Resource\":\"${queueArn}\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"${topicArn}\"}}}]}"
    echo $policy
}

我的参数验证失败。参数 Attributes.Policy 的类型无效。

正在生成的政策已转义,似乎是有效的政策声明。

这是将 json 作为参数传递给 --attributes 参数。

如果我将其切换为使用简写语法 -

aws sqs set-queue-attributes --queue-url $queueUrl --attributes Policy=${policy}

我得到一个不同的错误:

【问题讨论】:

  • 您能否将政策单引号括起来,这样可以避免转义双引号?我知道在我的带有 zsh 的 Mac 上,它更喜欢用单引号括起来的参数。如果做不到这一点,您能否将echo 放在set-queue-attributes 行的前面并向我们展示发送到AWS 的命令的全文,以便我们尝试重现您的情况?
  • 我明白了。你在单引号和双引号中发现了。我已经在双引号内转义了 json,所以我的变量会被扩展。结果 bash 也转义了双引号内的项目,但单引号保留了字符串。我会发布答案解释它。

标签: amazon-web-services aws-cli amazon-sqs


【解决方案1】:

所以@John Rotenstein 发现了这个问题。

我将为遇到此问题的其他人发布此信息。

根本问题是 bash 脚本中的单引号 ' 与双引号 " - 或者更具体地说是我在使用它们时的天真。

我正在尝试为我的 SQS 队列动态生成一个 json 策略文档,以允许将各种 SNS 主题发布到它们。 CLI 示例使用 json 文件 - 但这不起作用,因为我想在我的脚本执行时指定 SNS / SQS 资源的 ARN。

Attributes 参数想要一个带有 json 对象的字符串;属性需要转义json。

{
  "DelaySeconds": "10",
  "MaximumMessageSize": "131072",
  "MessageRetentionPeriod": "259200",
  "ReceiveMessageWaitTimeSeconds": "20",
  "RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:us-east-1:80398EXAMPLE:MyDeadLetterQueue\",\"maxReceiveCount\":\"1000\"}",
  "VisibilityTimeout": "60"
}

我的问题是我有一个 bash 函数返回用双引号括起来的“转义”json。我需要它来扩展我的变量。

function getPolicy() {
    queueArn=$1
    topicArn=$2

    policy="{\"Version\":\"2012-10-17\",\"Id\":\"policy12345\",\"Statement\":[{\"Sid\":\"stmt12345\",\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::${account}:root\"},\"Action\":\"SQS:*\",\"Resource\":\"${queueArn}\"},{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"sns.amazonaws.com\"},\"Action\":\"sqs:SendMessage\",\"Resource\":\"${queueArn}\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"${topicArn}\"}}}]}"
    echo $policy
}

问题是双引号转义某些字符,如 Difference between single and double quotes in Bash 中所述。

所以我的转义 json 在被发送之前被转回了一个正确的 json。

解决方案是在我的函数中使用单引号。

function buildPolicy() {
    queueArn=$1
    topicArn=$2

    policy='{\"Version\":\"2012-10-17\",\"Id\":\"Policy1564523767951\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::'$account':root\"},\"Action\":\"SQS:*\",\"Resource\":\"\"},{\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"sns.amazonaws.com\"},\"Action\":\"sqs:SendMessage\",\"Resource\":\"'$queueArn'\",\"Condition\":{\"ArnEquals\":{\"aws:SourceArn\":\"'$topicArn'\"}}}]}'
    echo $policy
}

您必须停止和启动字符串以允许您的变量扩展。

然后设置属性:

function setPolicy() {
    url=$1
    topic=$2

    arn=$(getQueueArn $url)
    policy=$(buildPolicy $arn $topic)
    aws sqs set-queue-attributes --queue-url $url --attributes "{ \"Policy\" : \"${policy}\" }"
    echo "${arn} policy set"
}

您可以在此处使用双引号来扩展 $policy 变量 - 但这将被转义为 json。

这是一个微不足道的错误,但老实说,我在这上面花了太长时间,希望其他人能从中受益。

【讨论】:

    猜你喜欢
    • 2019-12-07
    • 2023-03-14
    • 2023-04-07
    • 2014-05-02
    • 2018-01-24
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 2018-12-08
    相关资源
    最近更新 更多