Skip to content

AWS and web enablement ​

A controller works on the LAN out of the box. Web enabling it gives it an AWS IoT identity and a bridge to AWS IoT Core, so the app can reach it from anywhere. Pairing then decides which accounts may.

Every AWS resource lives in infrastructure/ as four independent stacks in us-east-2. Changes go through the templates, never the console, or the next deploy reverts them.

StackWhat it holds
cognitoThe user pool and app client
fleet-provisioningThe provisioning template, its pre-provisioning hook, and the policy every controller gets
authenticatorThe IoT custom authorizer ControlFreakAuth for app and console connections
fleet-listThe fleet API, the pairing tables, and the IoT rule that records pairing windows

Web enabling a controller ​

What starts it ​

The app calls POST /controller-config/install-aws on the controller, over the LAN, with the installer image to run. controlfreak-api fills in everything else from the aws_installer entry in config.prod.json: mounts, privileges, host PID namespace and environment. It then sets aws_installed to pending, pulls the image, and runs it to completion.

A controller already installed returns its thing name without running anything. ?force=true runs the installer again, which is the supported repair for a controller that provisioned but never bridged.

What the installer does ​

aws-installer/entrypoint.sh runs each script in aws-installer/scripts/ in order. Any failure reports aws_installed: failed and stops.

ScriptWhat it does
01-provision-certs.shInstalls the claim certificate from the image, unless the controller already has its own
02-aws-iot-setup.shInstalls aws-iot-device-client as a systemd service and configures it for fleet provisioning under a new thing name
03-mosquitto-bridge.shWaits for provisioning, copies the controller's certificate to mosquitto, writes conf.d/bridge.conf, stops the device client, tests the config and restarts mosquitto
04-register-thing.shReports the thing name and aws_installed: installed to POST /controller-config/aws-status

When the installer exits successfully, controlfreak-api starts api-bridge.

Thing names ​

A thing name is controlfreak- and 12 random hex characters, generated on the controller the first time it provisions. It is the controller's identity in AWS and is never renamed. It is also the MQTT client id the bridge uses.

Fleet provisioning ​

The device client connects with the claim certificate, a single shared certificate baked into every aws-installer image, and asks the controlfreak-fp-template provisioning template to register it.

  1. The pre-provisioning hook, fleet-provisioning/index.py, rejects any thing name that does not start with controlfreak-.
  2. IoT creates the thing, issues the controller its own certificate, and attaches the controller policy.
  3. The device client writes the certificate to the controller.

From then on the controller authenticates with its own certificate, and the claim certificate is not used again.

The controller policy limits each controller to its own topics, using ${iot:Connection.Thing.ThingName}:

ActionResource
iot:Connectclient/<thing>
iot:Publish, iot:Receivetopic/devices/<thing>/controller/*
iot:Subscribetopicfilter/devices/<thing>/controller/*

The client id has to be exactly the thing name, or the policy variable does not resolve and the bridge is dropped right after it connects.

The claim certificate ​

The claim certificate is made by hand, once, because CloudFormation cannot create a certificate and return its private key. CI writes it into certs/ from the AWS_IOT_CLAIM_CERT_B64 and AWS_IOT_CLAIM_KEY_B64 secrets when it builds the aws-installer image.

  • Never delete it from the account. It is attached to no thing, so it looks like an orphan, and without it no new controller can provision.
  • Replacing it means rebuilding the installer image.
  • Never commit it. certs/*.pem* is gitignored.

Values baked into the installer ​

aws-installer carries these as build arguments, set by CI from the GitHub repository or environment variables.

VariableValue
AWS_IOT_ENDPOINTThe account's IoT data endpoint
AWS_IOT_FP_TEMPLATE_NAMEcontrolfreak-fp-template
AWS_IOT_THING_PREFIXcontrolfreak

The template name and thing prefix have to match the fleet-provisioning stack. Changing either there breaks every installer image already built.

Pairing ​

Pairing grants an account access to a controller. A controller can be paired with many accounts, one per member of a household.

  1. The user holds the two left buttons until the display shows PAIRING. controlfreak-display opens a 120 second window and publishes { "status": "pairing", "expires_at": <unix time> } on controller/pairing/status.
  2. The bridge carries it to devices/<thing>/controller/pairing/status. The IoT rule controlfreak_pairing_window writes it into controlfreak-pairing-windows, keyed by thing name, with a TTL.
  3. The app calls POST /pairings on the fleet API with the thing name.
  4. The fleet Lambda checks the window is open, then writes a row to controlfreak-pairings. Without an open window it answers 403.

The window message rides the controller's own certificate authenticated connection, so no client can forge one. Being at the controller is what allows a claim.

The window closes on a second press, when it expires, or when the display driver restarts. The fleet Lambda also refuses any window longer than 15 minutes.

The fleet API ​

API Gateway HTTP API with a Cognito JWT authorizer, in front of one Lambda. Every route takes Authorization: Bearer <idToken>. Invalid tokens are rejected at the gateway before the Lambda runs.

RouteWhoWhat it does
GET /fleet/pairingsAny signed in userThe caller's own controllers
POST /fleet/pairingsAny signed in userClaim a controller in pairing mode
DELETE /fleet/pairingsThe owner, or an adminRemove one grant
GET /fleet/thingsAdminEvery controller in the registry
PATCH /fleet/thingsAdmin, or a paired userSet a controller's display name

The app sets EXPO_PUBLIC_FLEET_URL to the base URL ending in /fleet. The display name is stored on the thing's name attribute and on the controller.

App and console connections ​

Clients connect to IoT Core over MQTT on WebSocket, through the custom authorizer ControlFreakAuth. The app passes the Cognito idToken in the connection URL as ?x-amz-customauthorizer-name=ControlFreakAuth&token=<idToken>.

On every connect the authorizer Lambda verifies the token, looks up the account's controllers in controlfreak-pairings, and returns a policy for the connection.

UserPolicy
No controllersConnect only
Paired with some controllersConnect, plus publish, subscribe and receive on devices/<thing>/* for each
Admin, custom:admin of true or 1Connect, plus the whole fleet on devices/*

The Cognito token says who a user is. The pairing table says what they may reach. The authorizer only ever reads the table, and caches a successful lookup for 10 seconds. When the table cannot be reached and nothing is cached, it grants connect only.

Local controller files ​

Path on the PiWhat it is
/home/pi/configs/conf.d/bridge.confThe generated bridge config
/home/pi/configs/certs/The certificates mosquitto uses for the bridge
/etc/systemd/system/aws-iot-device-client.serviceThe device client unit, disabled once provisioning is done

Check the device client on a controller that is still provisioning:

bash
journalctl -u aws-iot-device-client -f

Deploying the stacks ​

bash
cd infrastructure/cognito
aws cloudformation deploy --template-file template.yaml --stack-name controlfreak-cognito

cd infrastructure/<stack>
sam build && sam deploy

Deploy in order: cognito, fleet-provisioning, authenticator, fleet-list. Each stack's README covers its parameters and outputs.

Provisioning only runs when a controller is first web enabled, so a broken fleet-provisioning deploy stays invisible until then. After any change to that stack, web enable a test controller end to end.

Resetting a controller ​

To test web enablement again on a dev Pi, see Onboard development. The reset script leaves the thing and its certificate in AWS, and leaves the thing name in the controller database.