Skip to content

feat(decdn_gateway): deploy the public gateway behind nginx with a wildcard certificate - #187

Merged
thiras merged 2 commits into
mainfrom
feat/decdn-gateway-role
Oct 30, 2026
Merged

thiras merged 2 commits into
mainfrom
feat/decdn-gateway-role

Conversation

@thiras

@thiras thiras commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

Summary

First of three PRs for #176. This one adds a reusable deploy unit for decdn gateway --public-url: a new decdn_gateway role in decdn.publisher, the decdn_gateway.yml playbook (decdn_gateway_hosts, make deploy-gateway), and everything around them.

The role

  • Binary. Installs the decdn CLI from the signed decdn release (resolver and KEYS are byte copies) at /usr/local/lib/decdn-gateway/decdn. It never touches /usr/local/bin/decdn, which belongs to a co-located node's decdn_node role.
  • Unit. Hardened and on loopback (127.0.0.1:8484, metrics on 127.0.0.1:9094), with KillSignal=SIGINT: the gateway settles its in-flight pulls only on SIGINT.
  • Config. Chain settings and the RPC URL go in a 0600 gateway.toml, not on the command line. --keystore is always passed, because the CLI's default lookup (~/.decdn/client/keystore.json) is not where key-gen writes. The wallet can optionally be minted on the host.
  • Restarts happen only when a hashed input changes. A restart resets the day's allowance and drops the pulls in flight.

The proxy: distro nginx and lego (not Caddy)

  • nginx is apt-signed and patched by unattended-upgrades, and its limit_req limits PUT /_pointer per client address with no plugin. Stock Caddy can do neither the rate limit nor DNS-01 without a custom build.
  • The certificate is a wildcard for <domain> and *.<domain>. lego obtains it over ACME DNS-01 through any lego DNS provider; the token lives in a root-only env file on the host. lego is pinned by version and per-arch sha256, as the iroh roles' releases are. A daily timer renews the certificate and reloads nginx.
  • Host is passed through and nothing is added: the gateway sets its own CORS headers and reads no X-Forwarded-*.
  • A self-signed mode covers staging and tests.

Couplings

  • Firewall: tcp/80 + tcp/443 for the group, in all.yml, pinned in tests/firewall-holes.
  • Placement guard: refuses a host that is also an onramp, relay or DNS server host.
  • Backup and decommission: backup covers the wallet and the namespace pointers; decommission plays added, and every role's confirmation prompt names the gateway.
  • Fourth release-line pin: decdn_gateway_version, kept in step by test-scripts, bump_decdn.py, the drift job and AGENTS.md.
  • Fourth network mirror: generated at v0.3.0.
  • cloud-init: FORBIDDEN_VARS lists the new keyring and secret paths. There is no cloud-init group, same as the relay and DNS server.
  • Inventory templates in both trees.
  • Docs: publishers.md, requirements.md, lifecycle.md, the READMEs, AGENTS.md and SECURITY.md.

Known limitation. No wildcard can cover the namespace-hint host the gateway's ?ns= redirect produces, <ns>.<hash32>.<domain>, because it is two labels deep. Filed upstream as decdn/decdn#2784.

Not in this PR. Alloy scrape, dashboard and alerts come in PR 2. The Compose gateway profile comes in PR 3. Our own decdn.link deployment is decdn/fleet#47, after a decdn.publisher release.

Test plan

  • make molecule SCENARIOS='decdn-gateway decdn-gateway-lifecycle validation-decdn-gateway', all green:
    • decdn-gateway (Debian 12 + Ubuntu 24.04, real nginx): converge and idempotence; restart triggers, checked against a SIGINT marker; the fatal readiness gate. Verify covers Host passthrough, one CORS header and no added headers, the 301, refusal of unknown names on both ports, and 429 on a PUT /_pointer burst.
    • decdn-gateway-lifecycle: lego v5.5.2 really downloaded and verified, its units rendered, and the first issuance against a dead ACME server is fatal. Then the timer stops when TLS goes back to self-signed, the backup decrypts to the wallet and pointers, and decommission runs (a refusal, the real thing, then a no-op).
    • validation-decdn-gateway: 37 broken-input cases, each rejected by its own assert.
  • Real decdn v0.3.0 (scripts/fetch-decdn-cli.sh) with the role's exact flags and gateway.toml:
    • routing: GET / → 200, unknown Host → 403, PUT /_pointer on a site host → 404;
    • all nine decdn_gateway_* series on the metrics listener;
    • exit 0 on SIGINT.
  • make lint, make lint-ansible (production profile), make test-scripts, make lint-cloud-init, make security (no HIGH), make galaxy-check-publisher
  • scripts/sync-network-profiles.py ../decdn --check and scripts/tests/test_bump_decdn.py
  • CI

Refs #176

🤖 Generated with Claude Code

…ldcard certificate

A new decdn.publisher role and playbook (decdn_gateway_hosts, make deploy-gateway)
for `decdn gateway --public-url` on a publisher's own domain:

- The decdn CLI from the signed release at its own path (never a co-located node's
  /usr/local/bin/decdn); a hardened unit on loopback with KillSignal=SIGINT; the
  chain settings and RPC URL in a 0600 gateway.toml; an opt-in wallet; restarts
  only when a hashed input changed.
- Distro nginx in front: a wildcard certificate for <domain> and *.<domain> from
  lego (sha256-pinned) over ACME DNS-01 through any lego DNS provider, renewed by a
  timer; Host passed through with nothing added; a per-client limit_req on
  PUT /_pointer. A self-signed mode for staging and tests.
- tcp/80 + tcp/443 for the group, a placement guard against the onramp, relay and
  DNS server, backup (wallet + pointers) and decommission, a fourth release-line
  pin and network mirror, inventory templates in both trees, docs.
- molecule: decdn-gateway (Debian 12, Ubuntu 24.04), decdn-gateway-lifecycle (lego
  against a dead ACME server, backup, decommission) and validation-decdn-gateway.

The namespace-hint host <ns>.<hash32>.<domain> has no certificate (no wildcard
covers two labels): decdn/decdn#2784. Alloy, dashboards and alerts follow in a
second PR, the Compose profile in a third.

Refs #176

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 30, 2026 22:49
The role claimed to check the operator's credentials file was root-only but
only checked it existed; it now enforces root 0600, as the onramp does for its
Turnstile secret, and the lifecycle scenario starts it at 0644 to prove it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unresolved nginx and TLS issues can break valid zero-burst deployments, retain staging certificates in production, and skip retries after failed domain-change issuance.

4 open issues
What changed in this PR

Adds an Ansible deployment path for deCDN’s public gateway as part of decdn.publisher, addressing the first portion of #176. The gateway runs on loopback behind nginx with wildcard TLS; monitoring integration and Compose support remain follow-up work.

Changes:

  • Adds signed CLI installation, hardened gateway and certificate services, nginx routing, and per-client pointer rate limits.
  • Integrates gateway deployment, firewall rules, backups, decommissioning, collection packaging, and release synchronization.
  • Adds Molecule scenarios, validation fixtures, inventory examples, and operator documentation.
File Description
tests/​scripts-test.sh Extends deployment, packaging, pin, and placement checks.
SECURITY.md Documents gateway release-signature verification.
scripts/​tests/​test_bump_decdn.py Covers gateway release-pin updates.
scripts/​sync-network-profiles.py Generates the gateway network mirror.
scripts/​open-decdn-bump.sh Synchronizes gateway release-signing keys.
scripts/​bump_decdn.py Updates gateway pins and inventory examples.
examples/​collection-fleet/​README.md Documents gateway collection commands.
examples/​collection-fleet/​inventory/​hosts-publisher.yml.example Adds an optional gateway host example.
examples/​collection-fleet/​inventory/​group_vars/​decdn_gateway_hosts.yml Provides gateway configuration examples.
docs/​requirements.md Lists gateway networking requirements.
docs/​publishers.md Adds gateway setup and operating guidance.
docs/​lifecycle.md Documents gateway backup, migration, and decommissioning.
CONTRIBUTING.md Lists the generated gateway network mirror.
cloud-init/​tests/​lint.py Rejects gateway-sensitive configuration paths.
ansible/​tests/​playbook-guards/​inventory.yml Adds gateway placement test cases.
ansible/​tests/​firewall-holes/​inventory.yml Adds standalone and co-located gateway fixtures.
ansible/​tests/​firewall-holes/​expected.json Defines expected gateway firewall openings.
ansible/​tests/​firewall-holes/​check.sh Updates comments describing firewall groups.
ansible/​roles/​sponsord/​tasks/​resolve-release.yml Updates resolver-copy documentation only.
ansible/​roles/​sponsord/​tasks/​decommission.yml Includes gateways in confirmation prompts.
ansible/​roles/​sponsord_onramp/​tasks/​resolve-release.yml Updates resolver-copy documentation only.
ansible/​roles/​sponsord_onramp/​tasks/​decommission.yml Includes gateways in confirmation prompts.
ansible/​roles/​iroh_relay/​tasks/​decommission.yml Includes gateways in confirmation prompts.
ansible/​roles/​iroh_dns_server/​tasks/​decommission.yml Includes gateways in confirmation prompts.
ansible/​roles/​decdn_node/​tasks/​resolve-release.yml Updates resolver-copy documentation only.
ansible/​roles/​decdn_node/​tasks/​decommission.yml Includes gateways in confirmation prompts.
ansible/​roles/​decdn_node/​README.md Extends the release-bump checklist.
ansible/​roles/​decdn_gateway/​vars/​main/​paths.yml Defines nginx, certificate, and install-stamp paths.
ansible/​roles/​decdn_gateway/​vars/​main/​networks.yml Supplies generated gateway chain addresses.
ansible/​roles/​decdn_gateway/​vars/​main/​arch.yml Maps supported CLI and lego architectures.
ansible/​roles/​decdn_gateway/​templates/​gateway.toml.j2 Renders private blockchain configuration.
ansible/​roles/​decdn_gateway/​templates/​decdn-gateway.service.j2 Defines the hardened gateway service and SIGINT shutdown.
ansible/​roles/​decdn_gateway/​templates/​decdn-gateway.nginx.j2 Configures TLS proxying, routing, and rate limits.
ansible/​roles/​decdn_gateway/​templates/​decdn-gateway-certs.timer.j2 Schedules certificate renewal checks.
ansible/​roles/​decdn_gateway/​templates/​decdn-gateway-certs.service.j2 Runs lego DNS-01 issuance and renewal.
ansible/​roles/​decdn_gateway/​tasks/​tls.yml Installs lego and provisions certificates.
ansible/​roles/​decdn_gateway/​tasks/​resolve-release.yml Resolves release lines to exact versions.
ansible/​roles/​decdn_gateway/​tasks/​nginx.yml Deploys nginx configuration and probes HTTPS readiness.
ansible/​roles/​decdn_gateway/​tasks/​nginx-install.yml Installs distro nginx and validation tooling.
ansible/​roles/​decdn_gateway/​tasks/​install.yml Installs and verifies the dedicated gateway CLI.
ansible/​roles/​decdn_gateway/​tasks/​decommission.yml Removes services while preserving wallet and state.
ansible/​roles/​decdn_gateway/​tasks/​backup.yml Encrypts wallet and namespace-pointer backups.
ansible/​roles/​decdn_gateway/​README.md Documents setup, variables, limitations, and operations.
ansible/​roles/​decdn_gateway/​meta/​main.yml Declares role metadata and supported platforms.
ansible/​roles/​decdn_gateway/​handlers/​main.yml Defines gateway restart and nginx reload handlers.
ansible/​roles/​decdn_gateway/​files/​nginx-validate-site Validates candidate nginx site configurations.
ansible/​roles/​decdn_gateway/​files/​decdn-release-KEYS.asc Vendors trusted release-signing public keys.
ansible/​roles/​decdn_gateway/​defaults/​main.yml Defines deployment and lifecycle defaults.
ansible/​README.md Adds gateway architecture and command documentation.
ansible/​playbooks/​publisher.yml Includes gateway deployment in publisher runs.
ansible/​playbooks/​group_vars/​decdn_gateway_hosts.yml Applies shared firewall rules to gateway hosts.
ansible/​playbooks/​group_vars/​all.yml Adds gateway TCP ports 80 and 443.
ansible/​playbooks/​decommission.yml Includes gateways in fleet decommissioning.
ansible/​playbooks/​decdn_gateway.yml Adds placement guards and gateway provisioning.
ansible/​playbooks/​backup.yml Includes gateway wallet and pointer backups.
ansible/​molecule/​validation-decdn-gateway/​molecule.yml Defines the invalid-input validation scenario.
ansible/​molecule/​decdn-gateway/​side_effect.yml Checks restart triggers, SIGINT, and readiness failures.
ansible/​molecule/​decdn-gateway/​molecule.yml Defines gateway deployment and idempotence coverage.
ansible/​molecule/​decdn-gateway/​files/​decdn-gateway-stub Emulates CLI and gateway behavior for scenarios.
ansible/​molecule/​decdn-gateway/​converge.yml Exercises the real gateway playbook.
ansible/​molecule/​decdn-gateway-lifecycle/​verify.yml Checks decommissioned services and preserved state.
ansible/​molecule/​decdn-gateway-lifecycle/​side_effect.yml Exercises issuance failure, backup, and decommissioning.
ansible/​molecule/​decdn-gateway-lifecycle/​molecule.yml Defines the separate lifecycle scenario.
ansible/​Makefile Adds guarded gateway check and deploy targets.
ansible/​inventory/​hosts-publisher.yml.example Adds an optional gateway host example.
ansible/​inventory/​group_vars/​decdn_gateway_hosts.yml Provides gateway inventory settings.
ansible/​galaxy/​publisher/​roles.txt Packages the gateway role.
ansible/​galaxy/​publisher/​README.md Documents gateway collection support.
ansible/​galaxy/​publisher/​playbooks.txt Packages the gateway playbook.
ansible/​galaxy/​publisher/​galaxy.yml Updates collection descriptions and dependency comments.
ansible/​galaxy/​node/​playbooks/​decommission.yml Updates the co-located-service warning comment.
AGENTS.md Documents gateway conventions and cross-component dependencies.
.github/​workflows/​upstream-drift.yml Includes the gateway in release-pin drift checks.

Review effort: Balanced

failed_when: false
when:
- not ansible_check_mode
- not decdn_gateway_cert_stat.stat.exists or decdn_gateway_certs_units.changed
Comment on lines +26 to +27
--cert.name {{ _decdn_gateway_lego_cert_name }} \
--path {{ decdn_gateway_acme_dir }} \
proxy_send_timeout 300s;

location = /_pointer {
limit_req zone=decdn_gateway_pointer burst={{ decdn_gateway_pointer_burst }} nodelay;
Comment thread docs/lifecycle.md
Comment on lines +395 to +400
- **Restore and migration:** on the new host, restore the archive with `tar -C /`
before the first deploy, so the role finds the wallet and does not ask for (or
mint) another. Then deploy with the same `decdn_gateway_domain` and move `<domain>`
and `*.<domain>` to the new host. The certificate does not move: lego issues one on
the new host over DNS-01, which needs no DNS change first. Without the pointers,
each `<ns>.<domain>` answers 404 until its owner runs `decdn publish point` again.
@thiras
thiras merged commit 347f898 into main Oct 30, 2026
58 checks passed
@thiras
thiras deleted the feat/decdn-gateway-role branch October 30, 2026 23:31
@thiras
thiras requested a balanced review from Copilot October 30, 2026 23:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

thiras added a commit that referenced this pull request Oct 31, 2026
…ange, allow a zero burst, re-own restored state (#189)

* fix(decdn_gateway): keep a failed reissue pending, reissue on a CA change, allow a zero burst, re-own restored state

Copilot's review of #187:
- A certificate is now recorded as issued for "<server> <domain>" only after a run
  succeeds; while the record differs, force.env (LEGO_RENEW_FORCE, read by the
  certificate unit) forces a reissue, on every converge and timer run, until one
  succeeds. A failed domain change used to drop out once the unit file stopped
  changing, and a move off letsencrypt-staging kept the staging certificate until
  it was due. A certificate from before the record is adopted when its names match.
- decdn_gateway_pointer_burst: 0 rendered burst=0, which nginx rejects; the
  parameter is now left out.
- The role re-owns decdn_gateway_data_dir for the gateway's account, so a restore
  made as root before the first deploy (docs/lifecycle.md) works.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(decdn_gateway): re-own restored state without following links

The recursive file task ran as root and followed symlinks in a directory the
gateway's account can write, so a planted link could hand root's files to it.
The data dir must be a real directory, and chown -R -P -h runs only when find
reports something the account does not own. The lifecycle scenario plants a
link to a root-only canary and checks it stays root's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(decdn_gateway): clear a pending reissue after a timer run that succeeds

The record and force.env were cleaned up only by the role's tasks, so after a
failed converge every later timer run kept forcing a reissue (Copilot review on
#189). The certificate unit now records the issue and removes force.env in an
ExecStartPost=, which systemd runs only after lego succeeds; the lifecycle
scenario checks it with a lego that succeeds.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(decdn_gateway): count a reissue as done only when lego issued, and skip ARI's replaces

Copilot review on #189: ExecStartPost recorded the new CA after any run lego
exited 0 from, including one that renewed nothing (a timer run before the role
forced it), and a forced renewal still sent ARI's replaces naming the old CA's
certificate. lego's deploy hook, which runs only after an issuance, now records
it, removes force.env and reloads nginx (files/certs-deployed); the role fails a
pending reissue the hook did not record; force.env also sets LEGO_ARI_DISABLE.
The lifecycle scenario runs a lego that renews nothing (nothing acknowledged)
and one that issues (recorded, force cleared).

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(decdn_gateway): anchor the data dir under the home, and clear only the force a run belongs to

Copilot review on #189 (previously missed):
- decdn_gateway_data_dir must be one plain directory directly under the home,
  with no trailing slash: a symlinked ancestor, or a link a trailing slash
  resolves, passed the lstat guard and took the recursive chown outside it.
  validation-decdn-gateway rejects both shapes.
- force.env names the server and domain it forces, and the deploy hook removes it
  only when they are its own: a run already under way for another CA no longer
  clears a newer pending reissue. The lifecycle scenario runs another CA's hook
  against a pending force.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(decdn_gateway): drop a stale force once the certificate is the wanted one

After a failed switch from one CA to another, reverting to the first left the
other's force.env in place: every timer run reissued from the first CA, whose
deploy hook never clears a force naming another (Copilot review on #189). The
role now removes force.env whenever no reissue is needed, before the service can
run; the lifecycle scenario reverts with another CA's force left behind.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016nMiELpJumeQUrXH5BrAE8

* fix(decdn_gateway): pause the renewal timer while a reissue is forced

A renewal already under way for the previous server or domain could finish
between the role writing the newer force.env and that run's hook checking it,
and clear the newer force (Copilot review on #189, flagged for human
assessment). While a reissue is pending the role now stops the timer, waits for
any certificate run in flight, writes the force and runs the issuance, and
starts the timer again in an always: whatever the outcome. The lifecycle
scenario checks the timer is running after each failed issuance.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016nMiELpJumeQUrXH5BrAE8

* fix(decdn_gateway): drain the renewal timer before reading the certificate state

Copilot review on #189: the timer was paused only once a reissue was already
decided, so on a revert a renewal in flight for the unwanted CA could still
replace the certificate after the decision, and lego's own random sleep (up to
minutes) could outlast the five-minute wait. The role now stops the timer and
waits (up to 15 minutes, for DNS-01 propagation) for any run in flight before it
writes the units or reads the record, on every acme converge, and starts it
again in an always:. The unit passes --no-random-sleep: the timer already
randomizes. The lifecycle scenario starts a 90-second renewal and checks the
converge waited it out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016nMiELpJumeQUrXH5BrAE8

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants