Appearance
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.
| Stack | What it holds |
|---|---|
cognito | The user pool and app client |
fleet-provisioning | The provisioning template, its pre-provisioning hook, and the policy every controller gets |
authenticator | The IoT custom authorizer ControlFreakAuth for app and console connections |
fleet-list | The 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.
| Script | What it does |
|---|---|
01-provision-certs.sh | Installs the claim certificate from the image, unless the controller already has its own |
02-aws-iot-setup.sh | Installs aws-iot-device-client as a systemd service and configures it for fleet provisioning under a new thing name |
03-mosquitto-bridge.sh | Waits 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.sh | Reports 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.
- The pre-provisioning hook,
fleet-provisioning/index.py, rejects any thing name that does not start withcontrolfreak-. - IoT creates the thing, issues the controller its own certificate, and attaches the controller policy.
- 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}:
| Action | Resource |
|---|---|
iot:Connect | client/<thing> |
iot:Publish, iot:Receive | topic/devices/<thing>/controller/* |
iot:Subscribe | topicfilter/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.
| Variable | Value |
|---|---|
AWS_IOT_ENDPOINT | The account's IoT data endpoint |
AWS_IOT_FP_TEMPLATE_NAME | controlfreak-fp-template |
AWS_IOT_THING_PREFIX | controlfreak |
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.
- The user holds the two left buttons until the display shows PAIRING.
controlfreak-displayopens a 120 second window and publishes{ "status": "pairing", "expires_at": <unix time> }oncontroller/pairing/status. - The bridge carries it to
devices/<thing>/controller/pairing/status. The IoT rulecontrolfreak_pairing_windowwrites it intocontrolfreak-pairing-windows, keyed by thing name, with a TTL. - The app calls
POST /pairingson the fleet API with the thing name. - The fleet Lambda checks the window is open, then writes a row to
controlfreak-pairings. Without an open window it answers403.
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.
| Route | Who | What it does |
|---|---|---|
GET /fleet/pairings | Any signed in user | The caller's own controllers |
POST /fleet/pairings | Any signed in user | Claim a controller in pairing mode |
DELETE /fleet/pairings | The owner, or an admin | Remove one grant |
GET /fleet/things | Admin | Every controller in the registry |
PATCH /fleet/things | Admin, or a paired user | Set 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.
| User | Policy |
|---|---|
| No controllers | Connect only |
| Paired with some controllers | Connect, plus publish, subscribe and receive on devices/<thing>/* for each |
Admin, custom:admin of true or 1 | Connect, 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 Pi | What it is |
|---|---|
/home/pi/configs/conf.d/bridge.conf | The generated bridge config |
/home/pi/configs/certs/ | The certificates mosquitto uses for the bridge |
/etc/systemd/system/aws-iot-device-client.service | The 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 -fDeploying the stacks
bash
cd infrastructure/cognito
aws cloudformation deploy --template-file template.yaml --stack-name controlfreak-cognito
cd infrastructure/<stack>
sam build && sam deployDeploy 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.