Author SHA1 Message Date
Defame1297 067e0e4b3e docs(pihole): say how to clear app_password and what the upgrade run does
Build / build (pull_request) Successful in 34s
Lint / renovate-config (pull_request) Successful in 9s
Molecule / select (pull_request) Successful in 9s
Lint / lint (pull_request) Successful in 2m45s
Molecule / scenarios (pull_request) Successful in 3m24s
Molecule / Result (pull_request) Successful in 2s
The refusal message and the changelog fragment now tell the consumer to
remove app_password from pihole_overrides, the fragment says the first run
after upgrading re-renders pihole.toml and reconfigures Pi-hole, and the
role description lists the empty app_password among the conditions it
checks.

Refs: #383
Assisted-by: Claude Code
2026-12-06 22:09:22 +00:00
Defame1297 cb78edff6f fix(pihole)!: refuse a non-empty app password and render app_pwhash empty
Released pihole-FTL checks an app_pwhash that does not start with `$` as a
legacy double-SHA256 hash and, on a match, upgrades it to a BALLOON hash
written into the main webserver.api.pwhash. One login with the app
password the role rendered therefore replaced the web password. FTL
master fixes this, no release has it yet, and the role does not pin FTL.

validate.yml now refuses a non-empty config.webserver.api.app_password,
and the template always renders app_pwhash = "", which FTL treats as app
passwords disabled. The key stays in the defaults; only a value is
refused. Molecule verifies the rendered empty hash and the new refusal.

BREAKING CHANGE: a non-empty config.webserver.api.app_password now fails
the role, and app_pwhash always renders empty.

Refs: #383
Assisted-by: Claude Code
2026-12-06 21:35:12 +00:00
12 changed files with 52 additions and 67 deletions

No files matched your search

@@ -1,6 +0,0 @@
---
bugfixes:
- >-
ufw - A single quote in the ``comment`` of a ``ufw_rules`` entry is now refused before any rule is applied.
The ufw module cannot pass it to ufw, so the play failed while applying the rules, after earlier rules of the
list had been changed. The comment of an entry with ``delete: true`` is not checked, as it is never sent.
@@ -0,0 +1,9 @@
---
breaking_changes:
- >-
pihole - A non-empty ``config.webserver.api.app_password`` is now refused, and ``app_pwhash``
always renders empty, which disables app passwords. Released pihole-FTL upgrades a
legacy-hashed app password on login by writing the new hash into the main ``pwhash``, so one
login with the app password replaced the web password. Remove
``config.webserver.api.app_password`` from ``pihole_overrides``; the first run after upgrading
re-renders ``pihole.toml`` and reconfigures Pi-hole.
+2 -1
View File
@@ -58,13 +58,14 @@ The input asserts carry `always` (and `assert`), so they run whatever tags are s
## Tests
The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It replaces the upstream repository with a local stub whose installer only logs its arguments and creates the database file, so it tests the merge, the asserts, the user, the configuration directory and the rendered `pihole.toml` (owner, group and mode), the clone's checked-out version, the installer sequence (install once, then reconfigure from the handler) and idempotence, not Pi-hole itself: no pihole-FTL service runs, so that the service loaded the configuration is not verified. It runs `check` before the converge (a fresh host) and after it (a configured host). Negative tests in `verify.yml` cover a host without Ubuntu facts, a missing `pihole_web_password`, an unset interface, empty upstreams, a non-string password and the placeholder password.
The `default` molecule scenario runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). It replaces the upstream repository with a local stub whose installer only logs its arguments and creates the database file, so it tests the merge, the asserts, the user, the configuration directory and the rendered `pihole.toml` (owner, group and mode), the clone's checked-out version, the installer sequence (install once, then reconfigure from the handler) and idempotence, not Pi-hole itself: no pihole-FTL service runs, so that the service loaded the configuration is not verified. It runs `check` before the converge (a fresh host) and after it (a configured host). Negative tests in `verify.yml` cover a host without Ubuntu facts, a missing `pihole_web_password`, an unset interface, empty upstreams, a non-string password, the placeholder password and a non-empty app password.
## Notes
- Idempotent: yes.
- Merge model: the role sets an internal fact to `pihole_default | combine(pihole_overrides | default({}), recursive=True)`. The input is only read, never overwritten, so each run recomputes the merge. Dictionaries are not merged across variable sources, so define `pihole_overrides` in one place only.
- Secrets: the password is hashed twice with SHA-256 in the template (`pwhash`). The merge and both template tasks use `no_log`, and the templates `diff: false`, so a `pihole.toml` change shows no diff. `pihole_testing` masks the web password, app password and TOTP secret in its output. `totp_secret` and `app_password` default to empty strings.
- App passwords: `config.webserver.api.app_password` must stay empty, and the role fails otherwise; `app_pwhash` always renders empty, which disables app passwords. Released pihole-FTL upgrades a legacy-hashed (double SHA-256) app password on login by writing the new hash into the main `pwhash`, so one login with the app password would replace the web password.
- Install flow: the role creates the `pihole` system user (nologin), the configuration directory and, on first install, `pihole.toml`, because the unattended installer needs it first. It clones `repo.version` and runs the installer with `--unattended` only when `config.files.database` does not exist yet. A change to `pihole.toml` notifies the `reconfigure pihole` handler, which runs the installer with `--reconfigure --unattended`.
- Updates: the role does not upgrade an installed Pi-hole; changing `repo.version` only moves the clone.
- Check mode: `--check` is the supported preview; a play-level `check_mode: true` is not supported. On a host where the configuration directory does not exist yet, the `pihole.toml` template tasks are skipped under `--check`, because the directory they write into is not created.
+1 -1
View File
@@ -308,7 +308,7 @@ pihole_default:
password: "{{ pihole_web_password }}"
# Pi-hole 2FA TOTP secret.
totp_secret: ""
# Pi-hole application password (to circumvent 2FA for legacy 3rd-party services)
# Pi-hole application password: must stay empty, app_pwhash always renders empty (README Notes)
app_password: ""
# Should application password API sessions be allowed to modify config settings?
app_sudo: false
+7 -3
View File
@@ -13,8 +13,8 @@ argument_specs:
over C(pihole_default) in C(defaults/main.yml). The spec types it as a dictionary only, because sub-options
would reject every key it does not list. The merged result is an internal fact and never overwrites the
input. The role fails unless the merged C(config.dns.upstreams) is a non-empty list, C(config.dns.interface)
is a non-empty string and C(config.webserver.api.password) is a string other than
C(changetoyourownpassword).
is a non-empty string, C(config.webserver.api.password) is a string other than
C(changetoyourownpassword) and C(config.webserver.api.app_password) is empty.
- >-
C(pihole_default) (every setting rendered into C(pihole.toml)) and C(pihole_install_script) (the quoted
installer path under C(repo.path)) are role defaults deliberately not listed here, because argument
@@ -47,7 +47,11 @@ argument_specs:
C(config.dns.interface) must be set (string, for example C(eth0)): its default is an empty placeholder
and the role fails until it is set. C(config.dns.upstreams) (list of IP addresses or hostnames,
optionally with C(#port)) must stay non-empty. C(config.webserver.api.password) must stay a string.
C(config.webserver.api.totp_secret) and C(config.webserver.api.app_password) are also credentials.
C(config.webserver.api.totp_secret) is also a credential.
- >-
C(config.webserver.api.app_password) must stay empty and C(app_pwhash) always renders empty, which
disables app passwords: released pihole-FTL replaces the main web password on a login with a
legacy-hashed app password.
- >-
Other keys the tasks read are C(repo.remote), C(repo.version) (a tag, default C(v6.4)), C(repo.path)
(default C(~/.cache/pi-hole) of the connecting user), C(user), C(group) (both default C(pihole)) and
+8 -1
View File
@@ -56,7 +56,7 @@
database: passwd
key: pihole
- name: The config carries the overridden upstreams and a hashed password
- name: The config carries the overridden upstreams, a hashed password and no app password
ansible.builtin.assert:
that:
- "'9.9.9.9' in pihole_test_toml_content"
@@ -66,6 +66,7 @@
- "'\"eth0\"' not in pihole_test_toml_content"
- "'molecule-test-password' not in pihole_test_toml_content"
- pihole_test_toml_content is search('(?m)^\\s*pwhash = \"[0-9a-f]{64}\"')
- pihole_test_toml_content is search('(?m)^\\s*app_pwhash = \"\"$')
quiet: true
vars:
pihole_test_toml_content: "{{ pihole_test_toml.content | b64decode }}"
@@ -167,3 +168,9 @@
dns: {interface: molecule0}
webserver: {api: {password: changetoyourownpassword}}
expect: placeholder changetoyourownpassword
- name: app-password-set
overrides:
config:
dns: {interface: molecule0}
webserver: {api: {password: molecule-test-password, app_password: molecule-test-app-password}}
expect: app_password must stay empty
+12
View File
@@ -53,3 +53,15 @@
The web password is the placeholder changetoyourownpassword: set pihole_web_password, or
pihole_overrides.config.webserver.api.password, to a secret of your own.
quiet: true
# Released pihole-FTL upgrades a legacy-hashed app password on login by writing the new hash
# into the main pwhash, so the role renders app_pwhash empty, which disables app passwords.
- name: Check that no app password is set
ansible.builtin.assert:
that:
- (__pihole_candidate.config.webserver.api.app_password | default('', true)) == ''
fail_msg: >-
config.webserver.api.app_password must stay empty: released pihole-FTL replaces the main
web password when someone logs in with an app password the role has hashed. Remove
app_password from pihole_overrides.
quiet: true
+3 -3
View File
@@ -838,9 +838,9 @@
#
# Possible values are:
# <valid Pi-hole password hash>
# NOTE! I am assuming this should be double-hashed just like the regular password, but
# this assumption has not been tested!
app_pwhash = "{{ __pihole_effective.config.webserver.api.app_password | hash('sha256') | hash('sha256') }}"
# Always empty, which disables app passwords: the role refuses app_password, because
# released pihole-FTL rewrites the main pwhash on a login with a legacy-hashed app password.
app_pwhash = ""
# Should application password API sessions be allowed to modify config settings?
# Setting this to true allows third-party applications using the application password
+5 -5
View File
@@ -26,13 +26,13 @@ Main entry point:
| Variable | Default | Required | Description |
|---|---|---|---|
| `ufw_enabled` | `true` | no | Skip all UFW configuration when false (the package is still installed). |
| `ufw_manage_rules` | `true` | no | Skip the `ufw_rules` schema and comment checks, the lock-out guard and rule application when false. Before-rules validation and `before.rules`, defaults and enable still run (gated on `ufw_enabled`). |
| `ufw_manage_rules` | `true` | no | Skip the `ufw_rules` schema check, the lock-out guard and rule application when false. Before-rules validation and `before.rules`, defaults and enable still run (gated on `ufw_enabled`). |
| `ufw_defaults` | `incoming: deny`, `outgoing: allow`, `routed: deny` | no | Default policies. All three keys are read, so override all of them. |
| `ufw_logging` | `"on"` | no | UFW logging level. |
| `ufw_mask_nftables` | `true` | no | Mask `nftables.service` without stopping it, so a package upgrade cannot flush the rules ufw loaded (see Notes). Set false on a host that loads its own ruleset through that unit. Gated on `ufw_enabled`. |
| `ufw_require_ssh_allow` | `true` | no | Enable the SSH lock-out guard. |
| `ufw_ssh_port` | `ansible_port` or 22 | no | TCP port the guard looks for. |
| `ufw_rules` | `[]` | no | Base rule list: `rule` (allow, deny, reject, limit), `port`, `proto`, `from`, `to`, `interface`, `direction`, `comment`, `delete` (see Notes). A comment may not contain a single quote: the ufw module cannot pass one. |
| `ufw_rules` | `[]` | no | Base rule list: `rule` (allow, deny, reject, limit), `port`, `proto`, `from`, `to`, `interface`, `direction`, `comment`, `delete` (see Notes). |
| `ufw_before_rules` | `[]` | no | Entries added as ACCEPT lines to `before.rules`: `interface`, `proto`, `port`, `from`, `comment`. |
| `ufw_sysctl_overrides` | `[]` | no | `{name, value}` pairs written to `/etc/ufw/sysctl.conf`. |
@@ -141,7 +141,7 @@ The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_
- Prepare installs ufw and stores rules the role must handle: an untagged manual rule, a manual rule the base list deletes, and tagged rules of service owners that are stale, removed or orphaned, some of them looking like an SSH path (port 22 with a source, no port, a range, an application profile named like SSH).
- Converge applies the base `ufw_rules` (one entry with `delete: true`), then calls `service_rules` for several owners (purge on, purge off, `absent`), and `orphan_rules` with no list, an empty list, report only and purge.
- Verify first checks that each refusal stops the run with its message and changes nothing: default deny without an SSH rule (also when the SSH rule is only deleted, or added and deleted), a single quote in a base rule comment, a stray `*_ufw_rules` or `*_ufw_before_rules` variable, the invalid `service_rules` inputs (owner name, reserved `base`, a quote in a comment, a rule another owner holds, a rule that would retag the SSH rule, an unknown key, an interface without a direction, a purge with an empty list), and an invalid managed owner for `orphan_rules`.
- Verify first checks that each refusal stops the run with its message and changes nothing: default deny without an SSH rule (also when the SSH rule is only deleted, or added and deleted), a stray `*_ufw_rules` or `*_ufw_before_rules` variable, the invalid `service_rules` inputs (owner name, reserved `base`, a quote in a comment, a rule another owner holds, a rule that would retag the SSH rule, an unknown key, an interface without a direction, a purge with an empty list), and an invalid managed owner for `orphan_rules`.
- Verify then reads `ufw status verbose` and `ufw show added`: ufw is active with default deny incoming, the base rules are loaded, the deleted rule is gone, the manual and SSH rules are untouched, the service rules carry their owner tag, stale and orphan rules are deleted only where purging was on and only when they cannot carry SSH.
- Verify checks `before.rules`: the loopback accept, the INVALID-state drop behind its logging rule and in its place among the input rules.
- Verify runs `service_rules` under the `check_mode` keyword and checks that the rule list did not change.
@@ -155,9 +155,9 @@ Not covered: `ufw_mask_nftables: false` is never set, `ufw_before_rules` entries
- Idempotent: yes.
- Lock-out guard: `defaults.yml` sets the deny policy before `rules.yml` adds any rule. When `ufw_enabled`, `ufw_manage_rules` and `ufw_require_ssh_allow` are true and the incoming default is deny or reject, the play fails unless `ufw_rules` holds an allow or limit rule for `ufw_ssh_port` that is TCP (or has no `proto`) and has `direction: in` or no `direction`. `direction: incoming`, which the module accepts, is not counted, so write `in`. Any `from`, `to` or `interface` restriction still counts; the guard cannot know the control host's address. An entry with `delete: true` never counts.
- Replacing a base rule: list the new rule and the old one with `delete: true` (a boolean). Deletes are applied after every other rule, whatever their place in the list, so the replacement exists before the old rule goes, which matters when narrowing the SSH rule. The comment of a delete entry is neither sent nor checked, so the rule matches whatever its comment, and a delete of a rule that is already gone changes nothing. An entry that deletes a rule the same list adds (same `rule`, `port`, `proto`, `from`, `to`, `interface` and `direction`, where no `direction` is `in` and no `from` or `to` is `any`) fails the play. A delete without an address removes the IPv4 and the IPv6 rule; a replacement with an IPv4 `from` allows IPv4 only, so check that the hosts you connect from are covered before narrowing SSH. `delete` is for `ufw_rules` only; `service_rules` removes its own stale rules.
- Replacing a base rule: list the new rule and the old one with `delete: true` (a boolean). Deletes are applied after every other rule, whatever their place in the list, so the replacement exists before the old rule goes, which matters when narrowing the SSH rule. The comment of a delete entry is not sent, so the rule matches whatever its comment, and a delete of a rule that is already gone changes nothing. An entry that deletes a rule the same list adds (same `rule`, `port`, `proto`, `from`, `to`, `interface` and `direction`, where no `direction` is `in` and no `from` or `to` is `any`) fails the play. A delete without an address removes the IPv4 and the IPv6 rule; a replacement with an IPv4 `from` allows IPv4 only, so check that the hosts you connect from are covered before narrowing SSH. `delete` is for `ufw_rules` only; `service_rules` removes its own stale rules.
- `before.rules` is a template derived from UFW's own `before.rules` (GPL-3.0-only, see `NOTICE`) that fully replaces the stock file, including a loopback accept, conntrack (established accept and INVALID-state drop), ICMP and mDNS/UPnP entries. It is deployed whenever `ufw_enabled` is true, even with no before-rules.
- Rule schema is asserted before anything changes: `rule` must be allow, deny, reject or limit, `port` a positive integer, `proto` tcp or udp, each rule needs `port`, `from`, `to` or `interface`, and a `comment` must not contain a single quote.
- Rule schema is asserted before anything changes: `rule` must be allow, deny, reject or limit, `port` a positive integer, `proto` tcp or udp, and each rule needs `port`, `from`, `to` or `interface`.
- UFW is only re-enabled when `ufw status verbose` shows it inactive or the logging line differs, because the module rewrites rules and re-applies sysctl on every call.
- The `Reload UFW` handler runs `ufw reload`.
- nftables: when `nftables.service` is enabled (a preset can do that), the `nftables` package's postinst restarts it on upgrade (`dh_installsystemd --no-enable --no-start --restart-after-upgrade` emits `deb-systemd-invoke try-restart`), and both the restart and the unit's `ExecStop=/usr/sbin/nft flush ruleset` delete every rule ufw (and Docker) loaded, since the stock `/etc/nftables.conf` begins with `flush ruleset`. The role masks the unit and does not stop it, so nothing is flushed during the run. systemd refuses every job but a stop on a masked unit, so the postinst's restart fails before anything runs and the postinst ignores the failure (`|| true`); at boot the unit is not started either. The mask also applies when the unit or the package is absent, so a later install cannot enable it; the package's postinst removes only a mask its own helper created. The role never unmasks: with `ufw_mask_nftables: false` or `ufw_enabled: false` an existing mask stays. `--check` reports the mask without applying it. When `/etc/systemd/system/nftables.service` is a unit file of its own rather than a link, `systemctl mask` refuses with "already exists" and the play fails; set `ufw_mask_nftables: false` on such a host.
+5 -6
View File
@@ -22,7 +22,7 @@ argument_specs:
default: true
description:
- >-
When false the C(ufw_rules) schema and comment checks, the SSH lock-out guard and rule
When false the C(ufw_rules) schema check, the SSH lock-out guard and rule
application are skipped. Before-rules validation, C(before.rules), default policies and
enabling still run, gated on C(ufw_enabled).
ufw_defaults:
@@ -77,13 +77,12 @@ argument_specs:
- >-
Each item is a mapping with C(rule) (allow, deny, reject or limit) and at least one of C(port), C(from), C(to)
or C(interface). Optional keys are C(proto) (tcp or udp), C(direction), C(comment) and C(delete).
- A comment may not contain a single quote, because the ufw module cannot pass one.
- C(port) must be a positive integer value.
- >-
C(delete=true) (a boolean) removes the rule the other keys describe instead of adding it. Its comment is neither
sent nor checked, so the rule matches whatever its comment. Deletes are applied after every other rule of the
list, so a replacement exists before the old rule goes; a rule that is both added and deleted in the list fails
the play, and a delete entry never counts as the SSH allow rule of the lock-out guard.
C(delete=true) (a boolean) removes the rule the other keys describe instead of adding it. Its comment is not
sent, so the rule matches whatever its comment. Deletes are applied after every other rule of the list, so a replacement exists before the old
rule goes; a rule that is both added and deleted in the list fails the play, and a delete entry never counts as
the SSH allow rule of the lock-out guard.
ufw_before_rules:
type: raw
description:
-25
View File
@@ -7,12 +7,6 @@
- name: Refuse default-deny without an SSH allow rule
hosts: all
gather_facts: false
vars:
# A failed looped task reports "One or more items failed" in msg and the item
# messages in results; an unlooped one has the message in msg.
ufw_verify_failure: >-
{{ ansible_failed_result.msg | default('') }}
{{ ansible_failed_result.results | default([]) | map(attribute='msg', default='') | join(' ') }}
tasks:
- name: Run the role with only a non-SSH rule
block:
@@ -71,25 +65,6 @@
that: "'both added and deleted' in ansible_failed_result.msg | default('')"
quiet: true
# ufw's parser refuses a quote in a comment; the check must stop the play before any rule is applied.
- name: A single quote in a base rule comment
block:
- name: Include ufw
ansible.builtin.include_role:
name: mortis.kamino.ufw
vars:
ufw_rules:
- {rule: allow, port: 22, proto: tcp, comment: ssh}
- {rule: allow, port: 8080, proto: tcp, comment: "it's"}
- name: Unreachable when base rule comments are checked
ansible.builtin.fail:
msg: a quote in a base rule comment was accepted
rescue:
- name: The comment check stopped the play
ansible.builtin.assert:
that: "'single quote' in ufw_verify_failure"
quiet: true
# Stray variable guard: variables named like <role>_ufw_rules must fail the play
# before anything changes, naming the variable. The vars sit on the include only, so they
# do not reach the plays below (the base ufw_rules and ufw_before_rules never match).
-16
View File
@@ -16,22 +16,6 @@
- ufw_manage_rules
- ufw_aggregated_rules | length > 0
# The module wraps the comment in unescaped single quotes, which ufw's parser refuses, so the rules
# loop would fail part-way. Delete entries are skipped: rules.yml never sends their comment.
- name: Validate the ufw_rules comments
ansible.builtin.assert:
that: "\"'\" not in (ufw_rule.comment | default('', true) | string)"
fail_msg: >-
UFW rule comment contains a single quote, which the ufw module cannot pass to ufw: {{ ufw_rule }}
quiet: true
loop: "{{ ufw_aggregated_rules | default([]) }}"
loop_control:
loop_var: ufw_rule
when:
- ufw_manage_rules
- ufw_aggregated_rules | length > 0
- not ufw_rule.delete | default(false)
# A rule both added and deleted would be added, then deleted (deletes run last), and the
# lock-out guard below would still count the added one. Fields are normalised the way ufw
# stores them before comparing: no direction is in (incoming/outgoing are in/out), no from