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
This commit is contained in:
Defame1297 committed 2026-11-27 22:09:56 +00:00
1 parent b3b62121a3
commit c7c38c6bc0
8 files changed
+78 -2

No files matched your search

@@ -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
@@ -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
+22
View File
@@ -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