IoT Core 사용자 인증

0

Device -> FreeRTOS esp32

Device에는 도메인에 대한 검증을 시도합니다. 때문에 도메인 구성을 활성화하였습니다. Device는 mqtt로 username과 password, clientId를 전송합니다 도메인 구성에 권한 부여자를 추가하였습니다.


export const handler = async (event, context) => {
    const uname = event.protocolData.mqtt.username;
    const pwd = event.protocolData.mqtt.password;
    const clientId = event.protocolData.mqtt.client_id;
    const buff = Buffer.from(pwd, 'base64');
    const passwd = buff.toString('ascii');

    switch (passwd) {
        case 'test':
            return generateAuthResponse(clientId, passwd, 'Allow');
        default:
            return generateAuthResponse(clientId, passwd, 'Deny');
    }
};

// Helper function to generate the authorization response.
const generateAuthResponse = (clientId, token, effect) => {
    const authResponse = {};
    authResponse.isAuthenticated = true;
    authResponse.principalId = 'user11111';

    const policyDocument = {};
    policyDocument.Version = '2012-10-17';
    policyDocument.Statement = [];
    
    const fullResource = {
        Action: ['iot:*'],
        Effect: effect,
        Resource: 'arn:aws:iot:REGION:************:*'
    };
    const statement = {};
    statement.Action = 'iot:*';
    statement.Effect = effect;
    statement.Resource = 'arn:aws:iot:REGION:************:*';

    policyDocument.Statement[0] = statement;


    authResponse.policyDocuments = [policyDocument];
    authResponse.disconnectAfterInSeconds = 3600;
    authResponse.refreshAfterInSeconds = 300;

    return authResponse;
};
  • 테스트 상황에는 전부 Allow
  • Node.js 18.x

lambda 함수에 권한을 주가하였습니다.

{
  "ArnLike": {
    "AWS:SourceArn": "arn:aws:iot:REGION:************:authorizer/authfunction"
  }
}

아래와 같이 테스트 하였을 때는 정상적으로 작동합니다

aws iot test-invoke-authorizer --authorizer-name NAME_OF_AUTHORIZER  \
--mqtt-context '{"username": "USER_NAME", "password": "dGVzdA==", "clientId":"CLIENT_NAME"}'
{
    "isAuthenticated": true,
    "principalId": "user",
    "policyDocuments": [
        "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Action\":\"iot:*\",\"Effect\":\"Allow\",\"Resource\":\"arn:aws:iot:REGION:************:*\"}]}"
    ],
    "refreshAfterInSeconds": 300,
    "disconnectAfterInSeconds": 3600
}

디바이스에서는 아래와 같이 mqtt 연결을 시도합니다.

	    const esp_mqtt_client_config_t mqtt_cfg = {
	        .uri = domain , // mqtts://domain.com
	        .event_handle = mqtt_event_handler,
	        .client_id = device_id,
	        .username = "test",
	        .password = "1234",
	        .port = 443,
	        .reconnect_timeout_ms = MQTT_RECON_DEFAULT_MS,
	        .cert_pem = (const char *)ca_pem // 도메인 인증
	    };

도메인:443 으로 연결을 시도하였습니다. 하지만 cloud watch에는 어떠한 로그도 나오지 않습니다. (Iot, lambda)

디바이스의 에러 로그는 이렇습니다.

E (26635) MQTT_CLIENT: esp_mqtt_connect: mqtt_message_receive() returned 0
E (26635) MQTT_CLIENT: MQTT connect failed

커스텀 도메인을 구성하지 않고 IoT Endpoint로 연결 시도 시 에러 로그는 이렇습니다.

E (22425) esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x2700
I (22425) esp-tls-mbedtls: Failed to verify peer certificate!
I (22425) esp-tls-mbedtls: verification info:   ! The certificate is not correctly signed by the trusted CA

E (22435) esp-tls: Failed to open new connection
I (22445) APP_MAIN: The current date/time in South Korea is: ======> Fri May 10 17:24:11 2024
E (22445) TRANS_SSL: Failed to open a new connection
E (22465) MQTT_CLIENT: Error transport connect
MQTT_EVENT_ERROR
Last error code reported from esp-tls: 0x8010
Last tls stack error number: 0x2700

우리는 무조건 mqtt통신을 해야합니다 또한 username과 password로 인증을 하여야 하며 IoT Core의 X509 인증서 사용이 불가능합니다. 또한 커스텀 도메인 구성을 해야합니다. 이 문제를 해결하고 디바이스를 IoT Core에 연결할 수 있게 도와주세요.

clyzen
질문됨 15일 전174회 조회
답변 없음

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠