これと同様の質問が以前にあり、私が見つけた答えは問題を解決するのに役立たなかったが、私はたくさん検索した。
IoTデバイス用にAWSのMQTTブローカーに接続しようとしていますが、AWSAmplifyのPubSubライブラリを介してMQTTover WebSocketsを使用しています。認証もAmplifyを介して処理されます(はい、ユーザーは正常に認証されます)。接続しようとすると、すぐに閉じてエラーメッセージが表示されます。
errorCode:8、errorMessage:AMQJS0008Iソケットがクローズされました。
IDプールのIAMロールを設定し、認証されたユーザーにIoTポリシーを添付しました(Amplifyから取得したID IDを使用)。
ポリシーの添付に使用されるコマンド:
aws iot attach-principal-policy --policy-name "Air-RME-Users" --principal "ap-northeast-1:4f76a019-9f84-44f0-a23d-48357210016c"
どんな助けでもありがたいです、ありがとう!
CloudWatch
IoTのログ:
{
"timestamp": "2018-07-05 04:00:42.998",
"logLevel": "ERROR",
"traceId": "<removed>",
"accountId": "<removed>",
"status": "Failure",
"eventType": "Connect",
"protocol": "MQTT",
"clientId": "4e900ea4-8f05-4022-98a8-c1b26280b2a2",
"principalId": "<removed>:CognitoIdentityCredentials",
"sourceIp": "<removed>",
"sourcePort": 54641,
"reason": "AUTHORIZATION_FAILURE"
}
Cognito Identityプールポリシー:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect",
"iot:Publish",
"iot:Subscribe",
"iot:Receive",
"iot:GetThingShadow",
"iot:UpdateThingShadow",
"iot:DeleteThingShadow"
],
"Resource": [
"*"
]
}
]
}
IoTポリシー:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "iot:*",
"Resource": "arn:aws:iot:ap-northeast-1:<removed>:topic/*"
}
]
}
テストコード:
import Amplify from 'aws-amplify';
import {AWSIoTProvider} from 'aws-amplify/lib/PubSub/Providers';
require('babel-polyfill');
global.app = function () {
let user = null;
const onBtn = document.getElementById("on");
const offBtn = document.getElementById("off");
Amplify.configure({
Auth: {
// REQUIRED only for Federated Authentication - Amazon Cognito Identity Pool ID
identityPoolId: 'ap-northeast-1:<removed>',
// REQUIRED - Amazon Cognito Region
region: 'ap-northeast-1',
// OPTIONAL - Amazon Cognito User Pool ID
userPoolId: 'ap-northeast-1_<removed>',
// OPTIONAL - Amazon Cognito Web Client ID (26-char alphanumeric string)
userPoolWebClientId: '<removed>',
// OPTIONAL - Enforce user authentication prior to accessing AWS resources or not
mandatorySignIn: true,
}
});
Amplify.addPluggable(new AWSIoTProvider({
aws_pubsub_region: 'ap-northeast-1',
aws_pubsub_endpoint: 'wss://<removed>.iot.ap-northeast-1.amazonaws.com/mqtt',
}));
process();
async function process() {
await Amplify.Auth.signIn("[email protected]", "Test1234567")
.then(user => {
console.log("ログインできました。")
onBtn.style.visibility = "visible";
offBtn.style.visibility = "visible";
console.log(user);
if (user.challengeName === "NEW_PASSWORD_REQUIRED") {
const currentPassword = "Test123456";
const newPassword = "Test1234567"
user.completeNewPasswordChallenge(newPassword)
.then(() => {
// winning
}).catch(error => {
console.log(error);
});
}
})
.catch(err => {
alert("ログインできませんでした。");
console.log(err);
});
Amplify.Auth.currentCredentials().then((info) => {
const cognitoIdentityId = info._identityId;
console.log(cognitoIdentityId);
});
Amplify.PubSub.subscribe('$aws/things/Air-RME-test/shadow/get/accepted').subscribe({
next: data => console.log('Message received', data),
error: error => console.error(error),
close: () => console.log('Done'),
});
Amplify.PubSub.publish('$aws/things/Air-RME-test/shadow/get', '');
}
};
編集:
ポリシーを添付する前に、認証されたコグニトIDロールに次のポリシーを追加すると、問題が解決しました。使用していたarnにも問題があったので、当面は*ワイルドカードを使用します。動作するようになったら、再度更新します。
ポリシー:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:AttachPrincipalPolicy"
],
"Resource": [
"*"
]
}
]
}
したがって、1つのポリシーが欠落しています。そのポリシーは、各ユーザーに添付する必要がありますiot:AttachPrincipalPolicy
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:AttachPrincipalPolicy"
],
"Resource": [
"*"
]
}
] }
現在、CLIを使用する方法とAWSLambda関数を使用する方法の2つがあります。
'use strict';
console.log('Loading function');
var AWS = require('aws-sdk');
exports.handler = (event, context, callback) => {
console.log('Received event:', JSON.stringify(event, null, 2))
event.Records.forEach((record) => {
console.log(record.eventName);
if (record.eventName == "INSERT") {
console.log('DynamoDB Record:', JSON.stringify(record));
console.log('DynamoDB Record:', record.dynamodb['Keys']['UserId']['S']);
var user = record.dynamodb['Keys']['UserId']['S'];
const iotMgmt = new AWS.Iot();
return new Promise(function(resolve, reject) {
let params = {
policyName: "basic",
principal: user
};
iotMgmt.attachPrincipalPolicy(params, (err, res) => {
console.log("Attaching IoT policy to " + user);
if (err) {
console.error(err);
reject(err);
}
else {
resolve();
}
});
});
}
});
};
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加