【问题标题】:Cognito + IoT Integration - How to restrict a client so that it can subscribe to MQTT topic with name as Cognito user id only?Cognito + IoT 集成 - 如何限制客户端,使其只能订阅名称为 Cognito 用户 ID 的 MQTT 主题?
【发布时间】:2020-10-08 18:12:16
【问题描述】:

我正在寻找一种方法来向 AWS IoT 服务验证 Cognito 用户,以便用户可以订阅 MQTT 主题名称作为唯一的用户 ID(认知池的身份池)。我知道使用2 step process 可以实现。

我不知道的是一个特定的策略,我们需要将它附加到 Cognito 身份(又名用户),该策略必须限制用户以 MQTT 订阅他/她的用户 ID话题。这意味着该应用无法订阅任何其他非预期主题。

此外,策略需要是动态的(可能使用 ${cognito-identity.amazonaws.com:sub} 和条件)以简化开发

值得注意的是,用户可以同时登录多个移动应用实例(Android 和 iOS),如果用户同时登录 Android 和 iOS,那么两个应用实例应该能够订阅同一个主题(因为用户 id对于同一个用户将保持不变)。

【问题讨论】:

    标签: amazon-web-services authentication amazon-cognito aws-iot


    【解决方案1】:

    终于解决了。分享解决方案,如果它可以帮助别人。步骤如下:

    1. 我们需要将 IAM 策略附加到经过身份验证的用户的 Cognito 角色。附加的策略必须允许操作——iot:Subscribe、iot:Connect、iot:Receive。 最终策略应如下所示。

      {
       "Version": "2012-10-17",
        "Statement": [
          {
          "Sid": "Stmt1232909773123",
           "Action": [
           "iot:Connect",
           "iot:Receive",
           "iot:Subscribe"
            ],
          "Effect": "Allow",
          "Resource": "*"
        }
      ]
      }
      
    2. 需要附加到身份 id(cognito)的 IoT 策略应如下所示。

      {
       "Version": "2012-10-17",
       "Statement": [
        {
        "Effect": "Allow",
        "Action": "iot:Connect",
        "Resource": "arn:aws:iot:us-east-1:<account-number>:client/*"
        },
        {
        "Effect": "Allow",
        "Action": [
          "iot:Receive"
        ],
        "Resource": [
          "arn:aws:iot:us-east-1:<account-number>:topic/${cognito-identity.amazonaws.com:sub}"
        ]
       },
      {
        "Effect": "Allow",
        "Action": [
          "iot:Subscribe"
        ],
        "Resource": [
          "arn:aws:iot:us-east-1:<account-number>:topicfilter/${cognito-identity.amazonaws.com:sub}"
        ]
      }
      ] 
      }
      

    注意:对于 #2,您需要将您的 AWS 帐号放入政策中

    【讨论】:

      【解决方案2】:

      在这个特定问题上花费了大量时间。 MQTT IoT 策略中围绕变量替换的文档和错误处理不是很好。以下内容也对我有用。如果您使用 Cognito 联合身份,请确保将 IoT 策略添加到经过身份验证的用户角色。最后说明:${cognito-identity.amazonaws.com:sub} 解析为来自 Cognito 开发人员身份的 identityId,而不是用户池或 JWT。无论如何,希望这对其他人有所帮助。

      Cognito 身份验证角色策略 注意:这太宽松了 - 根据需要进行优化。

      {
          "Version": "2012-10-17",
          "Statement": [
              {
                  "Action": [
                      "iot:*"
                  ],
                  "Effect": "Allow",
                  "Resource": "*"
              }
          ]
      }
      

      MQTT 物联网政策:

      {
        "Version": "2012-10-17",
        "Statement": [
          {
            "Effect": "Allow",
            "Action": "iot:Connect",
            "Resource": "arn:aws:iot:us-east-1:<account-id>:client/*"
          },
          {
            "Effect": "Allow",
            "Action": [
              "iot:Publish",
              "iot:Receive"
            ],
            "Resource": [
              "arn:aws:iot:us-east-1:<account-id>:topic/${cognito-identity.amazonaws.com:sub}/*"
            ]
          },
          {
            "Effect": "Allow",
            "Action": [
              "iot:Subscribe"
            ],
            "Resource": [
              "arn:aws:iot:us-east-1:<account-id>:topicfilter/${cognito-identity.amazonaws.com:sub}/*"
            ]
          }
        ]
      }
      

      【讨论】:

        猜你喜欢
        • 2018-05-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-21
        • 1970-01-01
        • 2020-07-21
        • 2018-05-01
        • 1970-01-01
        相关资源
        最近更新 更多