From c7c38c6bc0c7846307f23538dadc624546e314c2 Mon Sep 17 00:00:00 2001 From: Defame1297 Date: Fri, 27 Nov 2026 22:09:56 +0000 Subject: [PATCH] feat(docker): pin the Docker apt key by checksum The key that signs the Docker repository was trusted as whatever the URL served. It is now checked against a pinned sha256, so a changed key stops the install until a person has verified it. Assisted-by: Claude Code --- .../fragments/448-docker-keyring-checksum.yml | 5 ++++ roles/docker/README.md | 6 +++-- roles/docker/defaults/main.yml | 4 +++ roles/docker/meta/argument_specs.yml | 7 +++++ roles/docker/molecule/default/converge.yml | 8 ++++++ .../default/tasks/refuse_checksum.yml | 27 +++++++++++++++++++ roles/docker/molecule/default/verify.yml | 22 +++++++++++++++ roles/docker/tasks/install.yml | 1 + 8 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/448-docker-keyring-checksum.yml create mode 100644 roles/docker/molecule/default/tasks/refuse_checksum.yml diff --git a/changelogs/fragments/448-docker-keyring-checksum.yml b/changelogs/fragments/448-docker-keyring-checksum.yml new file mode 100644 index 0000000..e8ca1b2 --- /dev/null +++ b/changelogs/fragments/448-docker-keyring-checksum.yml @@ -0,0 +1,5 @@ +--- +minor_changes: + - >- + docker - New input ``docker_keyring_checksum``: the Docker APT key is verified against a pinned + sha256, and a changed key fails the install until a person has checked it and updated the hash. diff --git a/roles/docker/README.md b/roles/docker/README.md index eee758c..ca7a400 100644 --- a/roles/docker/README.md +++ b/roles/docker/README.md @@ -27,6 +27,7 @@ The role targets Ubuntu hosts only: it installs from Docker's Ubuntu repository, | `docker_keyring_path` | `/etc/apt/keyrings/docker.asc` | no | Repository key location. | | `docker_apt_list_path` | `/etc/apt/sources.list.d/docker.list` | no | APT source file. | | `docker_gpg_url` | `https://download.docker.com/linux/ubuntu/gpg` | no | Key URL. | +| `docker_keyring_checksum` | sha256 of the current Docker key | no | Expected sha256 of the key at `docker_gpg_url`; a mismatch fails the download, so a changed key stops the install until this is updated. | | `docker_repo_base_url` | `https://download.docker.com/linux/ubuntu` | no | Repository base URL. | | `docker_apt_arch` | derived from the host | no | Architecture in the APT source. Empty derives it (`x86_64` gives `amd64`, `aarch64` gives `arm64`); another architecture fails until this is set. A value set here is always used; lowercase letters and digits only. | | `docker_start_missed` | `false` | no | Install a boot unit that starts the containers Docker skipped after a reboot (see [Boot and shutdown units](#boot-and-shutdown-units)). | @@ -86,17 +87,18 @@ With any of `docker_daemon_options`, `docker_live_restore`, `docker_log_driver`, ## Tests -`molecule/default` runs in CI only (`.gitea/workflows/molecule.yml`, no local runner), on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). A real Docker Engine cannot run there: `dockerd` needs the bridge, iptables rules and a delegated cgroup, and the container has no privileged mode. So the scenario builds a local APT repository, signs it with a throwaway key, and points the role at it with `docker_gpg_url` and `docker_repo_base_url`. The repository holds empty stand-ins for `docker-ce`, `docker-ce-cli` and `containerd.io`. The stand-in `docker-ce` ships a stub `docker.service` (`sleep infinity`) and creates the `docker` group, as the real package does. It runs `check` before the converge (on a fresh host) and after it (on the configured host). +`molecule/default` runs in CI only (`.gitea/workflows/molecule.yml`, no local runner), on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). A real Docker Engine cannot run there: `dockerd` needs the bridge, iptables rules and a delegated cgroup, and the container has no privileged mode. So the scenario builds a local APT repository, signs it with a throwaway key, and points the role at it with `docker_gpg_url`, `docker_keyring_checksum` (the throwaway key's hash) and `docker_repo_base_url`. The repository holds empty stand-ins for `docker-ce`, `docker-ce-cli` and `containerd.io`. The stand-in `docker-ce` ships a stub `docker.service` (`sleep infinity`) and creates the `docker` group, as the real package does. It runs `check` before the converge (on a fresh host) and after it (on the configured host). What it shows, with the role's tasks running unchanged: -- The key is fetched, stored at `docker_keyring_path` with mode `0644`, and apt really verifies the repository against it (an unsigned or wrongly signed repository would leave no install candidate). +- The key is fetched, checked against `docker_keyring_checksum`, stored at `docker_keyring_path` with mode `0644`, and apt really verifies the repository against it (an unsigned or wrongly signed repository would leave no install candidate). - The source line has the architecture, the `signed-by` keyring and `ansible_facts['distribution_release']`. - The packages are installed from that repository, the unit is enabled and active, and `docker_users` join `docker_group`. - `docker_remove` with `docker_remove_data` removes the source line, the keyring, the packages, the unit and `/var/lib/docker`. - Both units are installed, enabled, ordered against `docker.service` as stated, the shutdown unit is active, a unit is removed when its input is `false`, and `docker_remove` removes both. - With the daemon inputs set, `/etc/docker/daemon.json` is valid JSON with exactly the expected keys, mode `0644` and root-owned. A change with `docker_daemon_restart: false` leaves dockerd running (its start time is unchanged); with `true` it restarts it. A key set both in `docker_daemon_options` and by a dedicated input, and a bad log size, driver or file count, are refused before any change. - The argument check refuses a mapping for `docker_users`, a non-boolean `docker_remove` and a `docker_stop_at_shutdown_timeout` of 0. +- A key whose hash does not match `docker_keyring_checksum` fails the key download. - A second run changes nothing (molecule's idempotence check). What it does not show: what the units do at a real boot or shutdown (the container has no dockerd, no NFS shares and no reboot; the selection logic of `docker-start-missed` is unit-tested in `tests/scripts/test_docker_files.py`), that the upstream repository and key at `download.docker.com` are reachable and valid, that the real packages install, that `dockerd` starts, anything about container networking or iptables, the `docker_update` path (`tasks/update.yml`), and removal without `docker_remove_data` (the removal stops the unit first, so it can run only once per install). It needs the Ubuntu mirrors, for the `curl` the role installs. These gaps are a known residual risk of the role. diff --git a/roles/docker/defaults/main.yml b/roles/docker/defaults/main.yml index 7c85605..316001a 100644 --- a/roles/docker/defaults/main.yml +++ b/roles/docker/defaults/main.yml @@ -11,6 +11,10 @@ docker_packages: docker_keyring_path: /etc/apt/keyrings/docker.asc docker_apt_list_path: /etc/apt/sources.list.d/docker.list docker_gpg_url: https://download.docker.com/linux/ubuntu/gpg +# sha256 of Docker's package signing key (9DC858229FC7DD38854AE2D88D81803C0EBFCD88), as the ASCII-armored file +# docker_gpg_url serves. Static on purpose: a rotated or re-armored key fails the install until a person has checked the +# new one and updated this hash. +docker_keyring_checksum: "1500c1f56fa9e26b9b8f42452a553675796ade0807cdce11975eb98170b3a570" docker_repo_base_url: https://download.docker.com/linux/ubuntu # Architecture written into the APT source. Empty (the default) derives it from the host: x86_64 gives amd64, aarch64 gives arm64, any diff --git a/roles/docker/meta/argument_specs.yml b/roles/docker/meta/argument_specs.yml index 822b9e0..d367813 100644 --- a/roles/docker/meta/argument_specs.yml +++ b/roles/docker/meta/argument_specs.yml @@ -49,6 +49,13 @@ argument_specs: default: https://download.docker.com/linux/ubuntu/gpg description: - URL of the repository signing key. + docker_keyring_checksum: + type: str + default: 1500c1f56fa9e26b9b8f42452a553675796ade0807cdce11975eb98170b3a570 + description: + - >- + sha256 (hex) of the key at C(docker_gpg_url). The download fails on a mismatch, so a rotated or re-armored + upstream key stops the install until the new key is checked and the hash updated. docker_repo_base_url: type: str default: https://download.docker.com/linux/ubuntu diff --git a/roles/docker/molecule/default/converge.yml b/roles/docker/molecule/default/converge.yml index 652e5bd..2314ac2 100644 --- a/roles/docker/molecule/default/converge.yml +++ b/roles/docker/molecule/default/converge.yml @@ -7,6 +7,8 @@ hosts: all vars: docker_gpg_url: file:///srv/docker-stub-repo/docker.asc + # The stand-in key is generated per run, so its hash is read here rather than pinned. + docker_keyring_checksum: "{{ docker_stub_key.stat.checksum }}" docker_repo_base_url: file:///srv/docker-stub-repo docker_users: [moleculeuser] docker_start_missed: true @@ -18,5 +20,11 @@ docker_log_max_file: "3" docker_daemon_options: max-concurrent-downloads: 3 + pre_tasks: + - name: Stat the stand-in repository key + ansible.builtin.stat: + path: /srv/docker-stub-repo/docker.asc + checksum_algorithm: sha256 + register: docker_stub_key roles: - role: mortis.kamino.docker diff --git a/roles/docker/molecule/default/tasks/refuse_checksum.yml b/roles/docker/molecule/default/tasks/refuse_checksum.yml new file mode 100644 index 0000000..1bcff28 --- /dev/null +++ b/roles/docker/molecule/default/tasks/refuse_checksum.yml @@ -0,0 +1,27 @@ +--- +# The zeroed hash matches no file, so the key task must be the one that fails. +- name: Run the role with a checksum the stand-in key does not have + block: + - name: Include the role + ansible.builtin.include_role: + name: mortis.kamino.docker + vars: + docker_gpg_url: file:///srv/docker-stub-repo/docker.asc + docker_repo_base_url: file:///srv/docker-stub-repo + docker_keyring_path: /tmp/docker-refused-key.asc + docker_keyring_checksum: "{{ '0' * 64 }}" + - name: Fail when the wrong key was accepted + ansible.builtin.fail: + msg: MOLECULE-ACCEPTED + rescue: + - name: The key download stopped the play + ansible.builtin.assert: + that: + - ansible_failed_task.name == 'Install Docker APT GPG key' + - "'did not match' in ansible_failed_result.msg | default('')" + quiet: true + always: + - name: Remove the refused key path + ansible.builtin.file: + path: /tmp/docker-refused-key.asc + state: absent diff --git a/roles/docker/molecule/default/verify.yml b/roles/docker/molecule/default/verify.yml index 70bef8a..c9b8de1 100644 --- a/roles/docker/molecule/default/verify.yml +++ b/roles/docker/molecule/default/verify.yml @@ -302,12 +302,19 @@ hosts: all vars: docker_gpg_url: file:///srv/docker-stub-repo/docker.asc + docker_keyring_checksum: "{{ docker_stub_key.stat.checksum }}" docker_repo_base_url: file:///srv/docker-stub-repo docker_users: [moleculeuser] docker_start_missed: false docker_stop_at_shutdown: true docker_stop_at_shutdown_timeout: 20 tasks: + - name: Stat the stand-in repository key + ansible.builtin.stat: + path: /srv/docker-stub-repo/docker.asc + checksum_algorithm: sha256 + register: docker_stub_key + - name: Apply the role with the boot unit off ansible.builtin.include_role: name: mortis.kamino.docker @@ -368,6 +375,7 @@ hosts: all vars: docker_gpg_url: file:///srv/docker-stub-repo/docker.asc + docker_keyring_checksum: "{{ docker_stub_key.stat.checksum }}" docker_repo_base_url: file:///srv/docker-stub-repo docker_users: [moleculeuser] docker_start_missed: true @@ -378,6 +386,12 @@ docker_log_max_file: "3" docker_live_restore: true tasks: + - name: Stat the stand-in repository key + ansible.builtin.stat: + path: /srv/docker-stub-repo/docker.asc + checksum_algorithm: sha256 + register: docker_stub_key + - name: Read the start time of the service ansible.builtin.command: systemctl show -p ActiveEnterTimestampMonotonic docker.service # noqa: command-instead-of-module register: docker_verify_before @@ -420,6 +434,14 @@ - (docker_verify_daemon_after.content | b64decode | from_json)['max-concurrent-downloads'] == 6 quiet: true +# A key that does not match docker_keyring_checksum fails its download before the source line is touched. It goes to a separate path, so +# the installed key stays for the removal below. +- name: A key that does not match the pinned checksum is refused + hosts: all + tasks: + - name: Apply the role with a wrong checksum + ansible.builtin.include_tasks: tasks/refuse_checksum.yml + # docker_remove_data is set, so this also covers the destructive path: the packages are # purged and /var/lib/docker, seeded in prepare.yml, is deleted. The removal stops the unit # before the package takes it away, so it can only run once; a second run would fail on the diff --git a/roles/docker/tasks/install.yml b/roles/docker/tasks/install.yml index 22497a0..8bb8804 100644 --- a/roles/docker/tasks/install.yml +++ b/roles/docker/tasks/install.yml @@ -23,6 +23,7 @@ ansible.builtin.get_url: url: "{{ docker_gpg_url }}" dest: "{{ docker_keyring_path }}" + checksum: "sha256:{{ docker_keyring_checksum }}" mode: "0644" tags: [docker, install]