Skip to content

Commit

Permalink
feat(wirepas): re-add gateway resources
Browse files Browse the repository at this point in the history
  • Loading branch information
coderbyheart committed Jan 29, 2024
1 parent 0d82819 commit fc876c0
Show file tree
Hide file tree
Showing 35 changed files with 16,102 additions and 16,705 deletions.
6 changes: 6 additions & 0 deletions .envrc.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ export AWS_REGION=... # AWS region to use, e.g. eu-west-1
export AWS_DEFAULT_REGION=$AWS_REGION
export AWS_ACCESS_KEY_ID= ... # Your AWS Access Key ID
export AWS_SECRET_ACCESS_KEY=... # Your AWS Secret Acces Key

# Gateway settings
export GATEWAY_MQTT_ENDPOINT=... # e.g. mqtts://<user>:<password>@wirepas.com:8883
export GATEWAY_REGION=... # AWS region to use, e.g. eu-west-1
export GATEWAY_AWS_ACCESS_KEY_ID=... # Value of thingy-rocks-backend.wirepasGatewayUserAccessKeyId
export GATEWAY_AWS_SECRET_ACCESS_KEY=... # Value of thingy-rocks-backend.wirepasGatewayUserSecretAccessKey
2 changes: 1 addition & 1 deletion .github/workflows/code-style.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:

- uses: actions/setup-node@v3
with:
node-version: "18.x"
node-version: "20.x"

- name: Keep npm cache around to speed up installs
uses: actions/cache@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-and-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

- uses: actions/setup-node@v3
with:
node-version: "18.x"
node-version: "20.x"

- name: Keep npm cache around to speed up installs
uses: actions/cache@v3
Expand Down
3 changes: 0 additions & 3 deletions .husky/commit-msg
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx commitlint --edit $1
3 changes: 0 additions & 3 deletions .husky/pre-commit
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
npx tsc
npm test
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,44 @@ Install the dependencies:
npm ci
```

### Configure

Set the Wirepas 5G Mesh Gateway endpoint:

```bash
aws ssm put-parameter --name thingy-rocks-backend-Wirepas5GMeshGatewayEndpoint --type String --value $GATEWAY_MQTT_ENDPOINT
```

### Deploy

```bash
npx cdk deploy
```

## Support for managing Wirepas 5G Mesh nodes

For interacting with these nodes,

1. Create a thing type `mesh-node` (they cannot be created using
CloudFormation).
1. Assign the thing type `mesh-node` to the thing which should act as a 5G Mesh
Node.

The thing type is check when an state update is received from the UI.

### Running the Wirepas 5G Mesh Gateway

Configure the gateway settings using the `.envrc` (see
[the example](./envrc.example)).

Run:

```bash
npx tsx wirepas-5g-mesh-gateway/gateway.ts
```

Run as a service using systemd:

```bash
systemd-run -E GATEWAY_MQTT_ENDPOINT=${GATEWAY_MQTT_ENDPOINT} -E GATEWAY_AWS_ACCESS_KEY_ID=${GATEWAY_AWS_ACCESS_KEY_ID} -E GATEWAY_REGION=${GATEWAY_REGION} -E GATEWAY_AWS_SECRET_ACCESS_KEY=${GATEWAY_AWS_SECRET_ACCESS_KEY} --working-directory ${PWD} npx tsx wirepas-5g-mesh-gateway/gateway.ts
```
9 changes: 9 additions & 0 deletions adr/004-static-protobuf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# ADR 004: Static protobuf parsers and generators

We generate static parsers and generators for protobuf files using
[`protobuf-ts`](https://github.com/timostamm/protobuf-ts/) to reduce the amount
of code needed to be bundled for lambda functions.

Initially [`protobufjs`](https://protobufjs.github.io/protobuf.js/)'
[pbsj](https://protobufjs.github.io/protobuf.js/#pbjs-for-javascript) was tried
but it did not generate working static files.
2 changes: 2 additions & 0 deletions cdk/backend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const packagesInLayer: string[] = [
'@nordicsemiconductor/timestream-helpers',
'@hello.nrfcloud.com/proto-lwm2m',
'jsonata',
'mqtt',
'@protobuf-ts/runtime',
]
const pack = async (
id: string,
Expand Down
7 changes: 6 additions & 1 deletion cdk/resources/WebsocketAPI.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
aws_apigatewayv2 as ApiGateway,
Duration,
aws_dynamodb as DynamoDB,
aws_iam as IAM,
aws_iot as IoT,
aws_lambda as Lambda,
aws_ssm as SSM,
Duration,
RemovalPolicy,
Stack,
aws_logs as Logs,
Expand Down Expand Up @@ -128,6 +129,10 @@ export class WebsocketAPI extends Construct {
VERSION: this.node.tryGetContext('version'),
CONNECTIONS_TABLE_NAME: this.connectionsTable.tableName,
WEBSOCKET_MANAGEMENT_API_URL: this.websocketManagementAPIURL,
GATEWAY_MQTT_ENDPOINT: SSM.StringParameter.valueForStringParameter(
this,
`${Stack.of(parent).stackName}-Wirepas5GMeshGatewayEndpoint`,
),
},
layers: [baseLayer],
logRetention: Logs.RetentionDays.ONE_WEEK,
Expand Down
31 changes: 31 additions & 0 deletions cdk/resources/Wirepas5GMeshGateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { Stack } from 'aws-cdk-lib'
import { aws_iam as IAM } from 'aws-cdk-lib'
import { Construct } from 'constructs'
import type { WebsocketAPI } from './WebsocketAPI'

export class Wirepas5GMeshGateway extends Construct {
public readonly accessKey
constructor(parent: Stack, { websocketAPI }: { websocketAPI: WebsocketAPI }) {
super(parent, 'Wirepas5GMeshGateway')

const gatewayUser = new IAM.User(this, 'gatewayUser')
gatewayUser.addToPolicy(
new IAM.PolicyStatement({
actions: ['iot:DescribeEndpoint'],
resources: [`*`],
}),
)
gatewayUser.addToPolicy(
new IAM.PolicyStatement({
actions: ['execute-api:ManageConnections'],
resources: [websocketAPI.websocketAPIArn],
}),
)
websocketAPI.connectionsTable.grantFullAccess(gatewayUser)

this.accessKey = new IAM.CfnAccessKey(this, 'accessKey', {
userName: gatewayUser.userName,
status: 'Active',
})
}
}
13 changes: 13 additions & 0 deletions cdk/stacks/BackendStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { ResolveCellLocation } from '../resources/ResolveCellLocation.js'
import { ResolveNetworkSurveyGeoLocation } from '../resources/ResolveNetworkSurveyGeoLocation.js'
import { UserAuthentication } from '../resources/UserAuthentication.js'
import { WebsocketAPI } from '../resources/WebsocketAPI.js'
import { Wirepas5GMeshGateway } from '../resources/Wirepas5GMeshGateway.js'
import { STACK_NAME } from './stackName.js'
import { NRPlusGateway } from '../resources/NRPlusGateway.js'
import { LwM2M } from '../resources/LwM2M.js'
Expand Down Expand Up @@ -102,6 +103,8 @@ export class BackendStack extends Stack {
baseLayer,
})

const wirepasGateway = new Wirepas5GMeshGateway(this, { websocketAPI: api })

// Outputs
new CfnOutput(this, 'WebSocketURI', {
exportName: `${this.stackName}:WebSocketURI`,
Expand All @@ -128,6 +131,16 @@ export class BackendStack extends Stack {
value: userAuthentication.identityPool.ref,
exportName: `${this.stackName}:identityPoolId`,
})

new CfnOutput(this, 'wirepasGatewayUserAccessKeyId', {
value: wirepasGateway.accessKey.ref,
exportName: `${this.stackName}:wirepasGatewayUserAccessKeyId`,
})

new CfnOutput(this, 'wirepasGatewayUserSecretAccessKey', {
value: wirepasGateway.accessKey.attrSecretAccessKey,
exportName: `${this.stackName}:wirepasGatewayUserSecretAccessKey`,
})
}
}

Expand Down
Loading

0 comments on commit fc876c0

Please sign in to comment.