Skip to content
GOcontrollPublic

About

Deployment agent for Linux based Moduline controllers

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

go-deploy

The update agent of a GOcontroll Moduline Linux controller (Moduline L4, M1, HMI1; Debian bullseye, bookworm or trixie on arm64). One Python 3 script (go-deploy, Python 3.9 or newer, python3-paho-mqtt), run by systemd as go-deploy.service, configured by /etc/go-deploy/go-deploy.conf.

File What
go-deploy the agent, installed as /usr/local/bin/go-deploy
go-deploy.service the systemd unit (root, hardened, see below)
go-deploy.conf.example an example config (legacy mode)
gocontroll-mqtt-bridge.py the bridge between the Moduline dashboard and EMQX; runs on the VPS, not on a controller
.github/workflows/build-package.yml builds go-deploy_<version>_all.deb on a v* tag and publishes it to the public apt repository
tests/ unit tests (no paho, no root, no controller needed)

Two modes

The config decides:

  • legacy: no [apt] section. The Moduline dashboard: a tarball, a version manifest and post-install.sh, commanded over the dashboard's EMQX broker. The protocol is the one of go-deploy 1.4 (see below).
  • fleet: an [apt] section. The fleet of a GOcontroll Suite project: TLS to the project's own broker, and the application only as the Debian package gocontroll-application from the project's signed apt repository.

Legacy mode

Config as in go-deploy.conf.example. MQTT client id go-deploy-<serial>, topics under <topic_prefix>/<serial>/ (default prefix gocontroll):

Topic Direction Payload
cmd/update in {download_url, version, app_id, sha256, files: [{source, destination, mode, owner, group, sha256}]}
status/update out, QoS 1, retained {"status": "running" | "success" | "failed", "version", "app_id", "agent_version"[, "error"]}
cmd/tunnel in {"duration": <seconds>} (0 stops cloudflared)
status/tunnel out, QoS 1 {"status": "active", "duration"}, {"status": "stopped"}, {"status": "error", "error"}

running is published on every connect (from /etc/go-deploy/current-app.json). An update downloads the tarball, checks its SHA-256, extracts it, places the files of the manifest and runs files/post-install.sh. Nothing is published on status/online in this mode (the dashboard's ACL does not allow it).

What changed in 1.5.0 (hardening, the protocol is the same):

  • sha256 is required; an update without it fails (status: failed).
  • download_url must be https:// on a host of [agent] download_hosts (comma separated, default updates.gocontroll.tech, where the dashboard serves its download links); a redirect is only followed to such a URL. TLS 1.2 or newer, certificate and host name verified. At most 512 MB.
  • Extraction refuses the whole tarball when one member has an absolute name, a .., is a link, a device or a fifo: tarfile's filter="data" where Python has it, the same rules by hand on older Pythons (bullseye's 3.9). Setuid bits and the archive's owners are dropped.
  • files[].destination must be absolute and normalised, files[].source a path inside the tarball; both are checked before anything is downloaded. Files are written through a temp file with an unpredictable name in the destination's directory, fsync and rename.
  • The update runs in its own thread, so a long download no longer stops the MQTT keep-alive; a second command meanwhile is ignored, as before. A malformed command or tunnel payload is logged and ignored instead of stopping paho's network thread. A tunnel lasts at most 24 h.

Fleet mode

go-upload-server (1.3.0, POST /fleet) writes the config, the apt source, its key and its pin when the Suite adds the controller to a project's fleet (the repository is public since Suite 0.74.0: the Suite gives no apt login), then restarts go-deploy:

[mqtt]
broker = gs-qne46v94.mqtt.gocontroll.tech
port = 8884
tls = yes
username = D1AE-B001-B001-A001
password = <32 hex>

[agent]
serial_number = D1AE-B001-B001-A001
app_id = GS-QNE46V94
topic_prefix = gocontroll

[apt]
package = gocontroll-application
list = /etc/apt/sources.list.d/gocontroll-fleet.list

Fleet mode refuses to start without tls = yes, a password, a DNS name as broker, or a valid serial and App ID.

Connection. MQTT 3.1.1 over TLS (system CA store, certificate and host name verified, TLS 1.2 or newer, SNI), user name and client id the serial number. Last will gocontroll/<serial>/status/online = {"online":false} (QoS 1, retained); {"online":true} on every connect and {"online":false} on a clean stop. Subscribes to cmd/update, cmd/ping and cmd/tunnel (QoS 1).

Status gocontroll/<serial>/status/update, QoS 1, not retained (the Suite's fleet agent takes a retained status as a replay). On connect, after every change of the update state, on cmd/ping (with the ping's req copied in as "req") and every 15 minutes:

{ "platform": "linux", "app_id": "GS-QNE46V94", "version": "1.2.3", "local": false, "agent": "1.6.0",
  "controller": "Moduline L4", "hardware": "Moduline Mini V1.11", "os": "trixie", "ip": "192.168.1.13",
  "modules": [ { "slot": 1, "article": "20200206", "sw": "2.2.0" }, { "slot": 2, "article": null, "sw": null } ],
  "ota": { "target": "application", "state": "done", "version": "1.2.3", "req": "m-0123456789abcdef",
           "install": "now", "reason": null } }

A failed install (since 1.6.0) also has "detail" in ota: what installing said (see step 6 below).

  • version: the installed gocontroll-application (dpkg install ok installed, or hold ok installed), else null.
  • local: a file of files.list differs from the package's copy in /usr/share/gocontroll-application/files/ (the application was put there another way, e.g. Flash). Only a release of Suite contract 17 to 19 has files.list: since contract 20 (Suite 0.76.0) a release is an install script and its folder, which go-deploy cannot compare, so local is then false.
  • controller, hardware: /sys/firmware/devicetree/base/{platform,hardware}. os: VERSION_CODENAME. ip: the first global IPv4 address (ip -j addr), Ethernet, then Wi-Fi, then 4G, then the rest.
  • modules: from /lib/firmware/gocontroll/modules.json (go-modules); go-modules scan is never run.
  • ota: the last update this agent handled (/var/lib/go-deploy/ota.json), null before the first. States downloading, pending, flashing, done, failed, rejected; reasons busy, invalid, package, download, signature, install, downgrade. detail (since 1.6.0, only with failed/install): the end of what installing said, as plain text.

Update gocontroll/<serial>/cmd/update:

{ "apt": { "package": "gocontroll-application" }, "version": "1.2.3", "install": "now", "req": "m-0123456789abcdef" }
  1. Exactly this shape, version x.y.z (at most three digits each), install now or boot, req [A-Za-z0-9_-]{1,40}; anything else (also a download_url) is rejected/invalid. Another package: rejected/package. A retained cmd/update is ignored.
  2. One at a time: rejected/busy while an update runs (that rejection is answered in a status but not kept in ota.json, so the running update stays visible). A new command replaces a pending one.
  3. boot to an older version: rejected/downgrade. now may go back (--allow-downgrades).
  4. downloading: apt-get update of the fleet source only (-o Dir::Etc::sourcelist=<list> -o Dir::Etc::sourceparts=- -o APT::Get::List-Cleanup=0); output naming a signature (NO_PUBKEY, not signed, BADSIG, is not valid, …) is failed/signature, any other failure (also a W: that apt exits 0 on) failed/download. Then apt-get --download-only install gocontroll-application=<v> (failed/download).
  5. boot: /var/lib/go-deploy/pending.json = {version, req}, state pending. At the next start of go-deploy (once the broker is connected, or after 60 s without it) that version is installed from the apt cache (--no-download), then pending.json is removed.
  6. now: flashing, DEBIAN_FRONTEND=noninteractive apt-get -y -o Dpkg::Options::=--force-confdef -o Dpkg::Options::=--force-confold --no-remove [--allow-downgrades] install gocontroll-application=<v>. On failure: failed/install, and one attempt to put the previous version back the same way from the cache (--no-download --allow-downgrades), with the outcome in the journal. Since 1.6.0 the failure carries detail: the last 20 lines of apt's output without its progress (Reading package lists…, Unpacking …, …), at most 1000 characters and starting at a whole line, control characters as ?. That is the output of the release's install script (the postinst of a Suite release shows it, and keeps it in /var/log/gocontroll-application/install-<version>.log) and dpkg's error, so the fleet in the Suite says why. The same version as installed is installed with --reinstall (puts the package's files back after a Flash).
  7. done, and a status.

apt-get runs with LC_ALL=C (its output is read), waits up to 120 s for another apt or dpkg (DPkg::Lock::Timeout), and is retried a few times when the lists are locked. Nothing the command names is ever run; every program is started with an argument list, never through a shell.

Hardening (both modes)

  • Config file: never a password in a log line (configparser errors are reported without the line they quote). A config readable by group or others is made 0600 (logged once); go-deploy does not start when that fails, or when the file is owned by another user than root. The package's postinst also makes it 0600.
  • TLS: verified everywhere (MQTT in fleet mode, legacy downloads); tls_insecure_set is never used. Legacy MQTT stays as the dashboard's broker offers it (plain MQTT on EMQX), with a warning in the journal.
  • Processes: subprocess with argument lists only, no shell=True, os.system or os.popen (a test checks the source). post-install.sh comes only from a tarball whose SHA-256 the dashboard sent.
  • systemd (go-deploy.service): PrivateTmp, ProtectKernelTunables, ProtectKernelModules, ProtectControlGroups, LockPersonality, RestrictRealtime; StateDirectory=go-deploy (0700). go-deploy stays root (apt and dpkg need it). These limits also apply to post-install.sh, apt-get and the maintainer scripts it runs: they cannot load kernel modules, write /proc/sys or /sys, or use the system /tmp.
  • Input: every MQTT payload is size-limited and validated before use; callbacks never let an exception stop paho's network thread.

The MQTT bridge (VPS)

gocontroll-mqtt-bridge.py relays between the Moduline dashboard (a Cloudflare Worker) and EMQX: HTTP POST on 127.0.0.1:9190 (HMAC-SHA256 of the body in X-Bridge-Sig) → publish on EMQX, and gocontroll/+/status/update → the Worker's /api/mqtt-status. Since go-deploy 1.5.0:

  • No secrets in the file. The HMAC secret (the Worker's DOWNLOAD_SECRET, at least 32 characters) and the MQTT password come from the environment, BRIDGE_HMAC_SECRET and BRIDGE_MQTT_PASSWORD (for example EnvironmentFile=/etc/gocontroll-mqtt-bridge/env in its unit, a file of root with mode 0600), or from BRIDGE_SECRETS_FILE (default /etc/gocontroll-mqtt-bridge/secrets, KEY=value lines), which must be owned by root and not readable by group or others. Without both the bridge does not start. Optional: BRIDGE_MQTT_HOST (localhost), BRIDGE_MQTT_PORT (28741), BRIDGE_MQTT_USER (bridge), BRIDGE_HTTP_PORT (9190), BRIDGE_WORKER_WEBHOOK (https only).
  • The values that were in git must be rotated by a person: a new DOWNLOAD_SECRET in the Worker and the bridge together, and a new password of the EMQX user bridge. Until then the old ones are public to whoever can read this repository's history.
  • It publishes through its own paho connection instead of mosquitto_pub, so the password is no longer on a command line (ps, /proc/<pid>/cmdline), and the Worker gets an error (503/502/504) when the publish did not reach the broker (it got 200 before, whatever happened).
  • It publishes only gocontroll/<serial>/cmd/<name> (what the Worker sends), bodies up to 64 KB, QoS 0–2.
  • Test on the VPS after installing: the EMQX ACL of the user bridge must allow it to publish on gocontroll/+/cmd/# from its subscriber connection (client id go-bridge-sub).

Development

python -m unittest discover -s tests      # from the repository root
python -m py_compile go-deploy gocontroll-mqtt-bridge.py

The tests load the script with importlib.machinery.SourceFileLoader (it has no .py), stub paho, and point every path at a temporary directory; they run on Windows and Linux. The release workflow runs them too, and checks that the tag matches VERSION in the script and that the script has LF line endings (.gitattributes keeps every file LF: a CRLF after the shebang breaks it on the controller).

A v* tag builds the package and publishes it to the public apt repository (GOcontroll/go-apt): only tag when a release is intended.

About

Deployment agent for Linux based Moduline controllers

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages