AWS IoT - AMQJS0008I Socket closed - AUTHORIZATION_FAILURE

こ雲淡風輕ζ 提交于 2020-05-01 09:46:27

问题


I am trying to configure AWS IoT to use with AWS Amplify. I always see error as "AMQJS0008I Socket closed.” and CloudWatch says “ AUTHORIZATION_FAILURE”. This is what I configured

  1. I AM policy for authenticated Cognito Identity Pool
    {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "VisualEditor0",
                "Effect": "Allow",
                "Action": [
                    "cognito-identity:*",
                    "mobileanalytics:PutEvents",
                    "cognito-sync:*",
                    "iot:Connect",
                    "iot:Publish",
                    "iot:Subscribe",
                    "iot:Receive",
                    "iot:GetThingShadow",
                    "iot:UpdateThingShadow",
                    "iot:DeleteThingShadow",
                    "iot:AttachPolicy",
                    "iot:AttachPrincipalPolicy"
                ],
                "Resource": "*"
            }
        ]
    }
  1. IoT policy for Cognito Identity
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:Connect",
      "Resource": "arn:aws:iot:ap-south-1:XXXXXXX:client/${iot:ClientId}"
    },
    {
      "Effect": "Allow",
      "Action": [
        "iot:Publish",
        "iot:Subscribe",
        "iot:Receive"
      ],
      "Resource": "arn:aws:iot:ap-south-1:XXXXXXX:topic/*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "iot:UpdateThingShadow",
        "iot:GetThingShadow",
        "iot:DeleteThingShadow"
      ],
      "Resource": "arn:aws:iot:ap-south-1:XXXXXXX:thing/*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "iot:AttachPrincipalPolicy”,
        "iot:AttachPolicy"
      ],
      "Resource": [
        "*"
      ]
    }
  ]
}
  1. Attached individual cognito identity using AWS CLI
aws iot attach-policy --policy-name "hub-iot-policy" --target "ap-south-1:XXXX-USER_COGNITO_IDENTITY
  1. Connecting and subscribing using AWS Amplify using packages
    “@aws-amplify/api": "^3.1.7",
    "@aws-amplify/auth": "^3.2.4",
    "@aws-amplify/core": "^3.2.4",
    "@aws-amplify/pubsub": "^3.0.8”,

code is

PubSub.addPluggable(new AWSIoTProvider({
      aws_pubsub_region: config.pubsub.REGION,
      aws_pubsub_endpoint: `wss://${config.pubsub.MQTT_ID}.iot.${config.pubsub.REGION}.amazonaws.com/mqtt`,
    }));
PubSub.subscribe('hub31-iot-thing').subscribe({
      next: data => console.log('Message received', data),
      error: error => console.error(error),
      close: () => console.log('Done'),
    });
  }
  1. JS Console throws error as

{provider: AWSIoTProvider, error: {…}}error: {invocationContext: undefined, errorCode: 8, errorMessage: "AMQJS0008I Socket closed."}provider: AWSIoTProvider {_config: {…}, _clientsQueue: ClientsQueue, _topicObservers: Map(1), _clientIdObservers: Map(1)}

  1. Cloudwatch gives error as AUTHORIZATION_FAILURE
{
    "timestamp": "2020-04-21 00:13:24.953",
    "logLevel": "ERROR",
    "traceId": “308de5a7-XXXX-d2d5-XXXX-7e24b6d6e0e6",
    "accountId": “XXXXXXXX",
    "status": "Failure",
    "eventType": "Connect",
    "protocol": "MQTT",
    "clientId": “f5e1abef-XXXX-44af-XXXX-4a327b45481c",
    "principalId": “XXXXX:CognitoIdentityCredentials",
    "sourceIp": “XXXX",
    "sourcePort": 59101,
    "reason": "AUTHORIZATION_FAILURE",
    "details": "Authorization Failure"
}

回答1:


Got the same error, and this is how I solved it.

1. Cognito policy as

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "iot:Receive",
                "cognito-identity:*",
                "iot:Subscribe",
                "iot:AttachPolicy",
                "iot:AttachPrincipalPolicy",
                "iot:Connect",
                "mobileanalytics:PutEvents",
                "iot:GetThingShadow",
                "iot:DeleteThingShadow",
                "iot:UpdateThingShadow",
                "iot:Publish",
                "cognito-sync:*"
            ],
            "Resource": "*"
        }
    ]
}

Also note that AttachPrincipalPolicy is deprecated, but for safer side I included it

2. IoT Policy as

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

3. Attach IoT policy to individual cognito Identity through lambda or AWS CLI. Using CLI this command looks like

aws iot attach-policy --policy-name "iot-policy" --target "ap-south-1:XXXX-USER-COGNITO-IDENTITY”

Again note AttachPrincipalPolicy is deprecated, use AttachPolicy

Using lambda:

export const main = async (event, context, callback) => {
    const principal = event.requestContext.identity.cognitoIdentityId;
    const policyName = 'iot-policy';

    const iot = new AWS.Iot();
    await iot.attachPrincipalPolicy({ principal, policyName }).promise();
    callback(null, "success");
};

4. Test If your frontend is configured properly, you should be able to solve errorCode: 8, errorMessage: AMQJS0008I Socket closed error.

5. Fine Tune Now Fine tune iot-policy according to your requirements and check immediately if the changes are working



来源:https://stackoverflow.com/questions/61335975/aws-iot-amqjs0008i-socket-closed-authorization-failure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!