From be47a12791bb8b1cae4535d1ae43f7d841d8126b Mon Sep 17 00:00:00 2001 From: Markus Tacker Date: Mon, 6 Nov 2023 18:05:12 +0100 Subject: [PATCH] fix(lwm2m): send with alias, update message format --- cdk/resources/WebsocketAPI.ts | 14 +++++--------- lambda/onMessage.ts | 13 +++++++++---- lambda/withDeviceAlias.ts | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/cdk/resources/WebsocketAPI.ts b/cdk/resources/WebsocketAPI.ts index dcb8180..43b0ad5 100644 --- a/cdk/resources/WebsocketAPI.ts +++ b/cdk/resources/WebsocketAPI.ts @@ -140,20 +140,16 @@ export class WebsocketAPI extends Construct { }:topic/*/nrplus-ctrl`, ], }), - new IAM.PolicyStatement({ - actions: ['iot:DescribeThing'], - resources: [ - `arn:aws:iot:${Stack.of(parent).region}:${ - Stack.of(parent).account - }:thing/nrplus-gw-*`, - ], - }), new IAM.PolicyStatement({ actions: ['execute-api:ManageConnections'], resources: [this.websocketAPIArn], }), new IAM.PolicyStatement({ - actions: ['iot:GetThingShadow', 'iot:ListThings'], + actions: [ + 'iot:GetThingShadow', + 'iot:ListThings', + 'iot:DescribeThing', + ], resources: ['*'], }), ], diff --git a/lambda/onMessage.ts b/lambda/onMessage.ts index f7b9af5..fbbda8a 100644 --- a/lambda/onMessage.ts +++ b/lambda/onMessage.ts @@ -20,6 +20,7 @@ import { ApiGatewayManagementApi } from '@aws-sdk/client-apigatewaymanagementapi import { sendEvent } from './notifyClients.js' import type { LwM2MObject } from '@hello.nrfcloud.com/proto-lwm2m' import { shadowToObjects } from '../lwm2m/shadowToObjects.js' +import { getDeviceInfo } from './withDeviceAlias.js' const { TableName, websocketManagementAPIURL } = fromEnv({ TableName: 'CONNECTIONS_TABLE_NAME', @@ -47,6 +48,8 @@ const apiGwManagementClient = new ApiGatewayManagementApi({ const send = sendEvent(apiGwManagementClient) const decoder = new TextDecoder() +const deviceInfo = getDeviceInfo(iot) + export const handler = async ( event: APIGatewayProxyWebsocketEventV2, ): Promise => { @@ -106,24 +109,26 @@ export const handler = async ( const shadows = ( await Promise.all<{ deviceId: string - shadow: LwM2MObject[] + alias?: string + objects: LwM2MObject[] }>( (things ?? []).map(async ({ thingName }) => iotData .send(new GetThingShadowCommand({ thingName, shadowName: 'lwm2m' })) .then(async ({ payload }) => ({ deviceId: thingName as string, - shadow: shadowToObjects( + alias: (await deviceInfo(thingName as string)).alias, + objects: shadowToObjects( JSON.parse(decoder.decode(payload)).state.reported, ), })) .catch(() => ({ deviceId: thingName as string, - shadow: [], + objects: [], })), ), ) - ).filter(({ shadow }) => shadow.length > 0) + ).filter(({ objects }) => objects.length > 0) await send( event.requestContext.connectionId, diff --git a/lambda/withDeviceAlias.ts b/lambda/withDeviceAlias.ts index 961cb08..a7cdd0f 100644 --- a/lambda/withDeviceAlias.ts +++ b/lambda/withDeviceAlias.ts @@ -25,7 +25,7 @@ export const withDeviceAlias = >( const deviceInfo: Record = {} -const getDeviceInfo = +export const getDeviceInfo = (iot: IoTClient) => async (deviceId: string): Promise<{ alias?: string; location?: string }> => { const info =