feat(cloudflared): add the cloudflared role #316

Merged
Defame1297 merged 10 commits from feat/cloudflared-role into main 2026-11-23 16:02:40 +00:00
Owner

Adds cloudflared, a Cloudflare Tunnel daemon role with a mint entry point. It is the fourth role to call _binary_update, after gitea, gitea_runner and traefik. The role is ported from the maintainer's local role, with the changes listed below. It is the wave 2 extraction, so it is removed from the README Roadmap.

What changed compared with the source role

  • Updates. The role still stages the download and checks its sha256. Stop, backup, swap, restart, version check and rollback now run in mortis.kamino._binary_update, included with apply tags, so --tags update reaches the download. The cloudflared-specific stale-sha guard and the 0.0.0 handling stay in the role.
  • tasks/validate.yml (tag always). It checks the version and sha format. Ingress hostnames must be lowercase, may start with *., and must not contain whitespace or a newline. The user, group and directories must not contain a newline or %, and the directories must be absolute paths. The tunnel id must be a UUID. The secrets and, when DNS is managed, the zone are checked before any change.
  • Exception to "a secret is a required input." The tunnel id, credentials, account id and API token are not required: true. When one is missing, the role's own assert tells the operator to run the mint entry point, and that is the only way the first run on a new host can work. The reason is written in both argument_specs and the README. mint keeps the generated secret in no_log facts only. Persisting it is the consumer's job, as in gitea's mint.
  • New input cloudflared_manage_dns (default true). With false, every DNS call is skipped.
  • DNS handling:
    • the overwrite guard queries one hostname at a time with the name urlencoded;
    • hostnames compare case-insensitively;
    • the prune pages through result_info.total_pages;
    • config.yml is checked with ingress validate before it is written.
  • Fixes to behaviour carried over from the source role:
    • A 404 or a deleted tunnel now reaches the re-mint message. Before, it failed inside a hidden no_log task. Any other status fails with its HTTP code.
    • A removal now ends the role, as in gitea_runner; before, the same run reinstalled the daemon.
    • The mint block runs with become: false and checks the account id, token and tunnel name before it creates anything.
  • Dropped: the legacy-name guards.
  • Renamed: internal names now use __cloudflared_*, and the handler is now Restart cloudflared.
  • Moved: the meta namespace is now mortis.
  • Docs: changelog fragment changelogs/fragments/cloudflared-role.yml (minor_changes), the README status line, the list of _binary_update callers, and the Roadmap.
  • galaxy.yml is not bumped. The release is a separate PR.

Molecule (molecule/default)

  • A file:// download and a local Cloudflare API stub (files/cloudflare_api_stub.py, bound to 127.0.0.1 on a random port, exits after 1500 s).
  • Covers install, configure, the update through _binary_update, the lookup failures (404, deleted, other status), the input refusals, and mint (confirm guard, CI refusal, generation in memory).
  • The cleanup stops the stub only if the pid still belongs to the stub.
  • No check step, as in gitea_runner (#134).

Scrub check

I grepped the diff and the commit messages for the #293 terms: domain, hosts and groups, private IPs, issue references to the other repo, its paths, and tunnel words. The remaining hits are intentional: mortis.kamino.* names, namespace/author in meta, the scenario's group_vars key and its own mint.yml. Test data uses only example.test/example.com, 192.0.2.10 and dummy UUIDs.

Verified (locally)

  • All commit and pre-push hooks pass, including ansible-lint (production profile), yamllint, ruff, REUSE, the changelog lint and gitleaks.
  • ansible-test sanity: 0 errors, pylint and pep8 included on the stub.
  • tests/scripts: 243 tests pass.
  • --syntax-check passes on the 5 scenario playbooks.
  • --check against localhost with the stub: every validation refusal fires with its message; the 404, deleted and 403 lookups fire too. The DNS guard fires and the token does not appear in its output. --tags update reaches the staged download and _binary_update. The mint guards hold and nothing is POSTed in check mode.
  • Two independent reviews, one against the source role and one against roles/AGENTS.md, found nothing blocking. For the existing consumer, the rendered config.yml, the credentials file and the unit file are byte-identical to the source role, so the first run does not restart the tunnel.

Not verified

  • The first Molecule run. There is no local container runtime, so this PR's CI is the first run and a fix commit is likely.
  • The cloudflare_dns create, prune and delete calls: the module hard-codes the API URL, so they cannot reach the stub. That also leaves out prune paging past page 1 and the case-insensitive allowlist.
  • The mint child playbook needs openssl on the molecule runner.

For consumers upgrading

  • List mortis.kamino.cloudflared in services and in the mint play's include_role.
  • Old pre-rename variable names are now silently ignored.
  • A removal no longer reinstalls the daemon.
  • The checks in validate.yml run under every tag, --tags update included.

Follow-ups (left out on purpose: each one changes rendered files and restarts the tunnel)

  • Add --no-autoupdate, hardening directives and network-online.target to the unit.
  • Make root:cloudflared own the config files, with 0640 files and a 0750 directory.
  • Quote wildcard hostnames with to_json in config.yml.
  • Page the per-hostname DNS lookup. Today it reads one page of 100 records per hostname.

Assisted-by: Claude Code:claude-opus-5-5

Adds `cloudflared`, a Cloudflare Tunnel daemon role with a `mint` entry point. It is the fourth role to call `_binary_update`, after `gitea`, `gitea_runner` and `traefik`. The role is ported from the maintainer's local role, with the changes listed below. It is the wave 2 extraction, so it is removed from the README Roadmap. ## What changed compared with the source role - **Updates.** The role still stages the download and checks its sha256. Stop, backup, swap, restart, version check and rollback now run in `mortis.kamino._binary_update`, included with `apply` tags, so `--tags update` reaches the download. The cloudflared-specific stale-sha guard and the `0.0.0` handling stay in the role. - **`tasks/validate.yml`** (tag `always`). It checks the version and sha format. Ingress hostnames must be lowercase, may start with `*.`, and must not contain whitespace or a newline. The user, group and directories must not contain a newline or `%`, and the directories must be absolute paths. The tunnel id must be a UUID. The secrets and, when DNS is managed, the zone are checked before any change. - **Exception to "a secret is a required input."** The tunnel id, credentials, account id and API token are not `required: true`. When one is missing, the role's own assert tells the operator to run the `mint` entry point, and that is the only way the first run on a new host can work. The reason is written in both `argument_specs` and the README. `mint` keeps the generated secret in no_log facts only. Persisting it is the consumer's job, as in `gitea`'s mint. - **New input `cloudflared_manage_dns`** (default `true`). With `false`, every DNS call is skipped. - **DNS handling:** - the overwrite guard queries one hostname at a time with the name urlencoded; - hostnames compare case-insensitively; - the prune pages through `result_info.total_pages`; - `config.yml` is checked with `ingress validate` before it is written. - **Fixes to behaviour carried over from the source role:** - A 404 or a deleted tunnel now reaches the re-mint message. Before, it failed inside a hidden `no_log` task. Any other status fails with its HTTP code. - A removal now ends the role, as in `gitea_runner`; before, the same run reinstalled the daemon. - The mint block runs with `become: false` and checks the account id, token and tunnel name before it creates anything. - **Dropped:** the legacy-name guards. - **Renamed:** internal names now use `__cloudflared_*`, and the handler is now `Restart cloudflared`. - **Moved:** the meta namespace is now `mortis`. - **Docs:** changelog fragment `changelogs/fragments/cloudflared-role.yml` (minor_changes), the README status line, the list of `_binary_update` callers, and the Roadmap. - `galaxy.yml` is not bumped. The release is a separate PR. ## Molecule (`molecule/default`) - A `file://` download and a local Cloudflare API stub (`files/cloudflare_api_stub.py`, bound to 127.0.0.1 on a random port, exits after 1500 s). - Covers install, configure, the update through `_binary_update`, the lookup failures (404, deleted, other status), the input refusals, and `mint` (confirm guard, CI refusal, generation in memory). - The cleanup stops the stub only if the pid still belongs to the stub. - No `check` step, as in `gitea_runner` (#134). ## Scrub check I grepped the diff and the commit messages for the #293 terms: domain, hosts and groups, private IPs, issue references to the other repo, its paths, and tunnel words. The remaining hits are intentional: `mortis.kamino.*` names, `namespace`/`author` in meta, the scenario's `group_vars` key and its own `mint.yml`. Test data uses only `example.test`/`example.com`, 192.0.2.10 and dummy UUIDs. ## Verified (locally) - All commit and pre-push hooks pass, including ansible-lint (production profile), yamllint, ruff, REUSE, the changelog lint and gitleaks. - `ansible-test sanity`: 0 errors, pylint and pep8 included on the stub. - `tests/scripts`: 243 tests pass. - `--syntax-check` passes on the 5 scenario playbooks. - `--check` against localhost with the stub: every validation refusal fires with its message; the 404, deleted and 403 lookups fire too. The DNS guard fires and the token does not appear in its output. `--tags update` reaches the staged download and `_binary_update`. The mint guards hold and nothing is POSTed in check mode. - Two independent reviews, one against the source role and one against `roles/AGENTS.md`, found nothing blocking. For the existing consumer, the rendered `config.yml`, the credentials file and the unit file are byte-identical to the source role, so the first run does not restart the tunnel. ## Not verified - The first Molecule run. There is no local container runtime, so this PR's CI is the first run and a fix commit is likely. - The `cloudflare_dns` create, prune and delete calls: the module hard-codes the API URL, so they cannot reach the stub. That also leaves out prune paging past page 1 and the case-insensitive allowlist. - The mint child playbook needs `openssl` on the molecule runner. ## For consumers upgrading - List `mortis.kamino.cloudflared` in `services` and in the mint play's `include_role`. - Old pre-rename variable names are now silently ignored. - A removal no longer reinstalls the daemon. - The checks in `validate.yml` run under every tag, `--tags update` included. ## Follow-ups (left out on purpose: each one changes rendered files and restarts the tunnel) - Add `--no-autoupdate`, hardening directives and `network-online.target` to the unit. - Make `root:cloudflared` own the config files, with 0640 files and a 0750 directory. - Quote wildcard hostnames with `to_json` in `config.yml`. - Page the per-hostname DNS lookup. Today it reads one page of 100 records per hostname. Assisted-by: Claude Code:claude-opus-5-5
Claude added 8 commits 2026-11-23 15:04:07 +00:00
Installs cloudflared as a systemd service for a locally managed
Cloudflare Tunnel: checks that the tunnel exists, manages its proxied
CNAMEs, renders config.yml and the credentials file, and updates the
binary through the internal _binary_update role. The opt-in mint entry
point creates the tunnel and registers its id and credentials as no_log
facts for the calling play to persist.

Validation runs first under always and asserts the tunnel secrets with
the mint instructions; the secrets stay optional in the argument spec on
purpose. cloudflared_manage_dns lets the records be managed elsewhere.
A removal now ends the role.

Assisted-by: Claude Code:claude-opus-5-5
Runs without Cloudflare and without a real cloudflared: a stub of the
Cloudflare API on the controller's loopback answers the tunnel, zone and
DNS record calls through cloudflared_api_base_url, a stub binary stands
in for the daemon, and the releases come from file:// URLs.

Covers the converge with an update through _binary_update and its
idempotence, the rendered files and the running unit, the input asserts,
the deleted-tunnel and DNS guards, a stale hash and a failed checksum,
the mint entry point (as child processes outside CI) and the remove
path. The cloudflare_dns calls cannot reach the stub and are not run.

Assisted-by: Claude Code:claude-opus-5-5
The role is in the Collection now, so it leaves the wave 2 Roadmap
line, and the status line names it as an unreleased caller of the
internal _binary_update role.

Assisted-by: Claude Code:claude-opus-5-5
A lookup that still returned 404 after its retries failed the lookup
task itself, whose output no_log hides, so the message with the re-mint
steps never ran. The lookup now ignores its own failure; the next task
fails with the re-mint steps for a 404 or a deleted tunnel, and a new
one fails with the HTTP status for any other lookup error.

Assisted-by: Claude Code:claude-opus-5-5
Assisted-by: Claude Code:claude-opus-5-5
Ingress hostnames must be lowercase, with an optional leading *. and no
whitespace: they land unquoted in config.yml. The service account names
and directories must hold no newline or %, which the unit would read as
directives, and the directories must be absolute. A set tunnel id must
be a lowercase UUID, the shape the stale-credentials search relies on.
Each refusal has a negative case in the scenario.

Assisted-by: Claude Code:claude-opus-5-5
The overwrite guard listed the zone's first 100 records only, so a
record further on went unseen. It now queries each ingress hostname by
name and compares names case-insensitively, and the prune listing pages
through result_info.total_pages. Query values are urlencoded. The
template task validates config.yml with cloudflared before it replaces
the file; the rendered content is unchanged. The API stub filters by
name and pages to match.

Assisted-by: Claude Code:claude-opus-5-5
test(cloudflared): stop the API stub only while its pid is still the stub
Build / build (pull_request) Successful in 17s
Lint / lint (pull_request) Successful in 2m11s
Molecule / molecule (pull_request) Failing after 1s
92534a486e
Cleanup now checks /proc/<pid>/cmdline before it signals the pid, so a
reused pid is never killed, and removes the pid file afterwards.

Assisted-by: Claude Code:claude-opus-5-5
Defame1297 approved these changes 2026-11-23 15:06:14 +00:00
Dismissed
Defame1297 scheduled this pull request to auto merge when all checks succeed 2026-11-23 15:06:20 +00:00
Defame1297 added 1 commit 2026-11-23 15:13:26 +00:00
fix(cloudflared): escape the stub's shell variables in molecule.yml
Build / build (pull_request) Successful in 19s
Lint / lint (pull_request) Successful in 1m23s
Molecule / molecule (pull_request) Failing after 2m21s
701226f499
molecule interpolates environment variables in molecule.yml, so `$1`
and `$*` in the embedded stub script failed the config parse ("Invalid
placeholder") before any step ran. `$$` renders as a literal `$`.

Assisted-by: Claude Code:claude-opus-5-5
Defame1297 dismissed Defame1297's review 2026-11-23 15:13:26 +00:00
Reason:

New commits pushed, approval review dismissed automatically according to repository settings

Defame1297 added 1 commit 2026-11-23 15:58:21 +00:00
fix(cloudflared): quote the mint POST status assertion in verify.yml
Build / build (pull_request) Successful in 17s
Lint / lint (pull_request) Successful in 1m39s
Molecule / molecule (pull_request) Successful in 2m20s
42c391ccbe
The plain scalar `cloudflared_test_posts[0] is search('"status": 200')`
holds ': ', so YAML parsed it as a one-key mapping and ansible-core
refused it ("Conditional expressions must be strings"). A folded block
scalar keeps it a string.

Assisted-by: Claude Code:claude-opus-5-5
Defame1297 merged commit db237569b2 into main 2026-11-23 16:02:40 +00:00
Defame1297 deleted branch feat/cloudflared-role 2026-11-23 16:02:41 +00:00
Defame1297 referenced this issue from a commit 2026-11-23 16:05:55 +00:00
Sign in to join this conversation.
No Reviewers
2 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Kamino#316