发布到IoT Core MQTT主题时的授权失败。

0

【以下的问题经过翻译处理】 你好,

我目前遇到一些设置IoT Core MQTT broker的问题。我能够使用终端和mosquitto连接到我的broker,但当我尝试发布���何主题的消息时,mosquitto客户端会断开连接并重新连接而无法发布。

我通过$aws/events/presence/# topic和调试模式下的mosquitto客户端验证了这种连接/断开行为,我可以提供样本输出:

Client william_terminal 发送 CONNECT
Client william_terminal 收到 CONNACK (0)
HELLO
Client william_terminal 发送 PUBLISH (d0、q0、r0、m1、'test'、... (5 bytes))
Client william_terminal 发送 CONNECT
Client william_terminal 收到 CONNACK (0)

使用设置为调试级别的AWSIotLogs,我发现这种异常行为是由于发布期间发生的授权问题引起的。以下是流的连续采样日志:

{
    "timestamp": "2022-09-29 15:16:55.406",
    "logLevel": "INFO",
    "traceId": "5697ba84-38f7-eefc-08e9-b6dd00096727",
    "accountId": "673559919736",
    "status": "Success",
    "eventType": "Connect",
    "protocol": "MQTT",
    "clientId": "$GEN/af403525-5e3b-4f81-9888-a31f16e300f0",
    "principalId": "49964471e92f354742f5394e648c97d9ac3aa940081cccf0962918bf97fcdf09",
    "sourceIp": "10.240.100.18",
    "sourcePort": 46898
}
{
    "timestamp": "2022-09-29 15:16:59.554",
    "logLevel": "ERROR",
    "traceId": "067b15e5-9bcb-5c6d-2061-9bbefbccb3d0",
        "accountId": "673559919736",
    "status": "Failure",
    "eventType": "Publish-In",
    "protocol": "MQTT",
    "topicName": "sim/2",
    "clientId": "$GEN/af403525-5e3b-4f81-9888-a31f16e300f0",
    "principalId": "49964471e92f354742f5394e648c97d9ac3aa940081cccf0962918bf97fcdf09",
    "sourceIp": "10.240.100.18",
    "sourcePort": 46898,
    "reason": "AUTHORIZATION_FAILURE",
    "details": "Authorization Failure"
}

我使用来进行身份验证的证书附带了以下策略:

{
  "Statement": [
    {
      "Action": [
        "iot:Connect"
      ],
      "Condition": {
        "Bool": {
          "iot:Connection.Thing.IsAttached": [
            "true"
          ]
        }
      },
      "Effect": "Allow",
      "Resource": "*"
    },
    {
      "Action": [
        "iot:Publish"
      ],
      "Effect": "Allow",
      "Resource": "*"
    },
    {
      "Action": [
        "iot:Subscribe"
      ],
      "Effect": "Allow",
      "Resource": "*"
    },
    {
      "Action": [
        "iot:Receive"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ],
  "Version": "2012-10-17"
}

这个策略中唯一限制的部分是连接操作,我不知道为什么会出现发布授权失败的情况。

profile picture
专家
已提问 5 个月前14 查看次数
1 回答
0

【以下的回答经过翻译处理】 在AWS IoT中,IoT策略附加在设备证书上,可以选择从附加到Thing Group的IoT策略继承。要验证有效应用的IoT策略,最好从设备证书开始检查:

指定要测试的设备证书文件名,确认AWS区域和AWS账户

export DEVICE_CRT=dd001.crt
export ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output text)
export AWS_REGION=eu-west-1

使用设备证书文件名计算sha256指纹

export CERTIFICATE_ID=$(openssl x509 -noout -fingerprint -sha256 -inform pem -in $DEVICE_CRT | cut -c 20- | tr -d : | tr A-F a-f)
export PRINCIPAL_ID=arn:aws:iot:${AWS_REGION}:${ACCOUNT_ID}:cert/${CERTIFICATE_ID}

检查证书是否在AWS IoT中注册

aws iot describe-certificate --certificate-id $CERTIFICATE_ID

查找在AWS IoT中附加到此证书的Thing。如果您有多个Thing附加到您的证书,下面的命令将获取附加到证书的第一个Thing。

export THING_NAME=$(aws iot list-principal-things --principal $PRINCIPAL_ID --query 'things[0]' --output text)

检索此Thing的有效IoT策略,以获得附加到证书和从Thing Groups继承的IoT策略。

aws iot get-effective-policies --thing-name $THING_NAME --principal $PRINCIPAL_ID 

{
  "effectivePolicies": [
    {
      "policyName": "E81FC...",
      "policyArn": "arn:aws:iot:eu-west-1:299807768844:policy/E81FCE....",
      "policyDocument": "{ \"Version\": \"2012-10-17\", \"Statement\": [{ \"Effect\": \"Allow\", \"Action\":[\"iot:*\"], \"Resource\": [\"*\"] }] }"
    }
  ]
}

验证您没有从Thing Groups继承拒绝发布的策略。

profile picture
专家
已回答 5 个月前

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则