test(openbao): add the default molecule scenario
Runs the role against a real OpenBao 2.7.3 on the noble and resolute containers, with check before and after converge and idempotence. Prepare builds a throwaway CA and a server certificate covering 127.0.0.1 with the openssl CLI, so no crypto collection is needed, and adds a drop-in that resets the namespace sandboxing and the service user, which the unprivileged container cannot provide. Verify asserts the effect on the host: the service is active with the unit's type, stop signal, MemorySwapMax and reload; the service user and the private data directory; the installed version; seal status over TLS on loopback reporting uninitialised and sealed; a reload that keeps the same process. Every input check has a negative case matched on its message, and the run ends with the switch to plain HTTP and back. Refs: #536 Assisted-by: Claude Code
This commit is contained in:
1 parent
eda69bcbf5
commit
421e30c5da
5 files changed
+421
No files matched your search
@@ -0,0 +1,8 @@
|
||||
---
|
||||
# Role variables come from the inventory group_vars in molecule.yml, so the idempotence step
|
||||
# reruns converge with identical configuration. Facts are off: the role gathers the one it reads.
|
||||
- name: Converge
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
roles:
|
||||
- role: mortis.kamino.openbao
|
||||
@@ -0,0 +1,71 @@
|
||||
---
|
||||
# Runs on the repository's own runner (label kamino-molecule), in the molecule-ubuntu2404
|
||||
# (MOLECULE_IMAGE) and molecule-ubuntu2604 (MOLECULE_IMAGE_2604) images, pinned in
|
||||
# .gitea/workflows/molecule.yml. Needs outbound network: prepare installs openssl with apt and the
|
||||
# converge downloads the pinned OpenBao release from GitHub. prepare.yml builds a throwaway CA and
|
||||
# server certificate. The container runs systemd as PID 1 with a writable cgroup; no privileged mode.
|
||||
driver:
|
||||
name: docker
|
||||
platforms:
|
||||
- name: ${KAMINO_RUN_ID:-local}-openbao-instance
|
||||
labels:
|
||||
net.rkdr.mortis.molecule-run: "${KAMINO_RUN_ID:-local}"
|
||||
image: ${MOLECULE_IMAGE}
|
||||
pre_build_image: true
|
||||
pull: true
|
||||
command: /sbin/init
|
||||
cgroupns_mode: host
|
||||
volumes:
|
||||
- /sys/fs/cgroup:/sys/fs/cgroup:rw
|
||||
tmpfs:
|
||||
- /run
|
||||
- /run/lock
|
||||
security_opts:
|
||||
- apparmor=unconfined
|
||||
- name: ${KAMINO_RUN_ID:-local}-openbao-2604-instance
|
||||
labels:
|
||||
net.rkdr.mortis.molecule-run: "${KAMINO_RUN_ID:-local}"
|
||||
image: ${MOLECULE_IMAGE_2604}
|
||||
pre_build_image: true
|
||||
pull: true
|
||||
command: /sbin/init
|
||||
cgroupns_mode: host
|
||||
volumes:
|
||||
- /sys/fs/cgroup:/sys/fs/cgroup:rw
|
||||
tmpfs:
|
||||
- /run
|
||||
- /run/lock
|
||||
security_opts:
|
||||
- apparmor=unconfined
|
||||
provisioner:
|
||||
name: ansible
|
||||
# One source for every play, so a verify play that runs the role again renders the same
|
||||
# configuration and the idempotence step reruns converge with identical inputs.
|
||||
inventory:
|
||||
group_vars:
|
||||
all:
|
||||
openbao_test_tls_dir: /etc/molecule-tls
|
||||
openbao_tls_cert_file: "{{ openbao_test_tls_dir }}/server.crt"
|
||||
openbao_tls_key_file: "{{ openbao_test_tls_dir }}/server.key"
|
||||
openbao_tls_ca_file: "{{ openbao_test_tls_dir }}/ca.crt"
|
||||
# The role's pinned version and checksums, for the failure cases that change other inputs.
|
||||
openbao_test_defaults: "{{ lookup('ansible.builtin.file', playbook_dir ~ '/../../defaults/main.yml') | from_yaml }}"
|
||||
verifier:
|
||||
name: ansible
|
||||
scenario:
|
||||
# molecule's default sequence minus both destroys: scripts/run_molecule_scenarios.sh removes the
|
||||
# container as soon as this scenario ends. After a local run, run `molecule destroy` yourself.
|
||||
# A check runs before the converge (fresh host) and after it (configured host).
|
||||
test_sequence:
|
||||
- dependency
|
||||
- cleanup
|
||||
- syntax
|
||||
- create
|
||||
- prepare
|
||||
- check
|
||||
- converge
|
||||
- check
|
||||
- idempotence
|
||||
- side_effect
|
||||
- verify
|
||||
- cleanup
|
||||
@@ -0,0 +1,112 @@
|
||||
---
|
||||
# systemd's sandboxing directives that need a mount or UTS namespace need CAP_SYS_ADMIN, which the
|
||||
# unprivileged test container lacks, so without this drop-in the unit fails with status
|
||||
# 226/NAMESPACE. The drop-in resets only those directives; the seccomp filters, Type, signals,
|
||||
# MemorySwapMax and ExecStart under test are the role's own. The namespace sandbox itself is
|
||||
# therefore not covered by this scenario.
|
||||
#
|
||||
# User and Group are reset too, as in the traefik scenario: the switch to the service user fails in
|
||||
# the unprivileged container, so the unit runs as root here. Root then needs the capabilities the
|
||||
# role's empty bounding set removes, to enter the 0700 data directory it does not own. The service
|
||||
# user, the directory ownership and the key's readability by that user are still asserted.
|
||||
- name: Prepare
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Install openssl
|
||||
ansible.builtin.apt:
|
||||
name: openssl
|
||||
update_cache: true
|
||||
|
||||
- name: Create the unit drop-in directory
|
||||
ansible.builtin.file:
|
||||
path: /etc/systemd/system/openbao.service.d
|
||||
state: directory
|
||||
mode: "0755"
|
||||
|
||||
- name: Disable the namespace-based sandboxing for the unprivileged container
|
||||
ansible.builtin.copy:
|
||||
dest: /etc/systemd/system/openbao.service.d/molecule.conf
|
||||
mode: "0644"
|
||||
content: |
|
||||
[Service]
|
||||
ProtectSystem=no
|
||||
ProtectHome=no
|
||||
PrivateTmp=no
|
||||
PrivateDevices=no
|
||||
ProtectKernelModules=no
|
||||
ProtectKernelTunables=no
|
||||
ProtectKernelLogs=no
|
||||
ProtectControlGroups=no
|
||||
ProtectClock=no
|
||||
ProtectHostname=no
|
||||
ReadWritePaths=
|
||||
User=
|
||||
Group=
|
||||
CapabilityBoundingSet=CAP_DAC_OVERRIDE CAP_DAC_READ_SEARCH CAP_FOWNER CAP_CHOWN
|
||||
|
||||
# A consumer deploys the key for the service group before the role runs, so the group
|
||||
# exists first here too; the role finds it and leaves it as it is.
|
||||
- name: Create the openbao group
|
||||
ansible.builtin.group:
|
||||
name: openbao
|
||||
system: true
|
||||
|
||||
- name: Create the TLS directory
|
||||
ansible.builtin.file:
|
||||
path: "{{ openbao_test_tls_dir }}"
|
||||
state: directory
|
||||
mode: "0755"
|
||||
|
||||
# A throwaway CA and a server certificate covering 127.0.0.1, with the openssl CLI so the
|
||||
# scenario needs no crypto collection. The extensions satisfy Python's strict verification.
|
||||
- name: Create the test CA
|
||||
ansible.builtin.command:
|
||||
argv: [openssl, req, -x509, -newkey, ec, -pkeyopt, "ec_paramgen_curve:prime256v1", -nodes,
|
||||
-keyout, ca.key, -out, ca.crt, -days, "3", -subj, "/CN=Molecule OpenBao test CA",
|
||||
-addext, "basicConstraints=critical,CA:TRUE", -addext, "keyUsage=critical,keyCertSign,cRLSign"]
|
||||
chdir: "{{ openbao_test_tls_dir }}"
|
||||
creates: ca.crt
|
||||
|
||||
- name: Write the server certificate extensions
|
||||
ansible.builtin.copy:
|
||||
dest: "{{ openbao_test_tls_dir }}/server.ext"
|
||||
mode: "0644"
|
||||
content: |
|
||||
basicConstraints=critical,CA:FALSE
|
||||
keyUsage=critical,digitalSignature
|
||||
extendedKeyUsage=serverAuth
|
||||
subjectAltName=IP:127.0.0.1,DNS:{{ inventory_hostname }}
|
||||
subjectKeyIdentifier=hash
|
||||
authorityKeyIdentifier=keyid
|
||||
|
||||
- name: Create the server key and request
|
||||
ansible.builtin.command:
|
||||
argv: [openssl, req, -new, -newkey, ec, -pkeyopt, "ec_paramgen_curve:prime256v1", -nodes,
|
||||
-keyout, server.key, -out, server.csr, -subj, "/CN={{ inventory_hostname }}"]
|
||||
chdir: "{{ openbao_test_tls_dir }}"
|
||||
creates: server.csr
|
||||
|
||||
- name: Sign the server certificate
|
||||
ansible.builtin.command:
|
||||
argv: [openssl, x509, -req, -in, server.csr, -CA, ca.crt, -CAkey, ca.key, -CAcreateserial,
|
||||
-out, server.crt, -days, "3", -extfile, server.ext]
|
||||
chdir: "{{ openbao_test_tls_dir }}"
|
||||
creates: server.crt
|
||||
|
||||
- name: Give the server key to the openbao group
|
||||
ansible.builtin.file:
|
||||
path: "{{ openbao_test_tls_dir }}/server.key"
|
||||
owner: root
|
||||
group: openbao
|
||||
mode: "0640"
|
||||
|
||||
# For the failure case of a key the service user cannot read.
|
||||
- name: Copy the server key to a root-only file
|
||||
ansible.builtin.copy:
|
||||
src: "{{ openbao_test_tls_dir }}/server.key"
|
||||
dest: "{{ openbao_test_tls_dir }}/root-only.key"
|
||||
remote_src: true
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0600"
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
# One failing run: runs the main entry point with the inputs of the case and asserts that the
|
||||
# expected message, not something else, stopped it. Input: openbao_test_case (name, vars,
|
||||
# expect). Task vars cannot come from a template as a whole, so each input a case may change is
|
||||
# passed with its scenario value as the default.
|
||||
- name: "Run the role, case {{ openbao_test_case.name }}"
|
||||
block:
|
||||
- name: "Include the role, case {{ openbao_test_case.name }}"
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.openbao
|
||||
vars:
|
||||
openbao_version: "{{ openbao_test_case.vars.openbao_version | default(openbao_test_defaults.openbao_version) }}"
|
||||
openbao_sha256: "{{ openbao_test_case.vars.openbao_sha256 | default(openbao_test_defaults.openbao_sha256) }}"
|
||||
openbao_bin_path: "{{ openbao_test_case.vars.openbao_bin_path | default('/usr/local/bin/bao') }}"
|
||||
openbao_listen_address: "{{ openbao_test_case.vars.openbao_listen_address | default('0.0.0.0') }}"
|
||||
openbao_port: "{{ openbao_test_case.vars.openbao_port | default(8200) }}"
|
||||
openbao_cluster_port: "{{ openbao_test_case.vars.openbao_cluster_port | default(8201) }}"
|
||||
openbao_node_id: "{{ openbao_test_case.vars.openbao_node_id | default(inventory_hostname) }}"
|
||||
openbao_cluster_addr: "{{ openbao_test_case.vars.openbao_cluster_addr | default('https://' ~ inventory_hostname ~ ':8201') }}"
|
||||
openbao_tls_cert_file: "{{ openbao_test_case.vars.openbao_tls_cert_file | default(openbao_test_tls_dir ~ '/server.crt') }}"
|
||||
openbao_tls_key_file: "{{ openbao_test_case.vars.openbao_tls_key_file | default(openbao_test_tls_dir ~ '/server.key') }}"
|
||||
openbao_tls_ca_file: "{{ openbao_test_case.vars.openbao_tls_ca_file | default(openbao_test_tls_dir ~ '/ca.crt') }}"
|
||||
openbao_tls_min_version: "{{ openbao_test_case.vars.openbao_tls_min_version | default('tls12') }}"
|
||||
|
||||
# Only reached when the include returned. The message must not contain the expected
|
||||
# text, so the rescue below cannot mistake it for the failure under test.
|
||||
- name: "Fail because the include returned, case {{ openbao_test_case.name }}"
|
||||
ansible.builtin.fail:
|
||||
msg: The role accepted the input
|
||||
rescue:
|
||||
- name: "The expected failure stopped the run, case {{ openbao_test_case.name }}"
|
||||
ansible.builtin.assert:
|
||||
that: openbao_test_case.expect in (ansible_failed_result.msg | default(''))
|
||||
fail_msg: "Expected '{{ openbao_test_case.expect }}', got: {{ ansible_failed_result.msg | default('') }}"
|
||||
quiet: true
|
||||
@@ -0,0 +1,195 @@
|
||||
---
|
||||
- name: Verify the converged server
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Ask systemd about the service
|
||||
ansible.builtin.systemd_service:
|
||||
name: openbao
|
||||
register: openbao_test_service
|
||||
|
||||
- name: The service runs with the role's unit settings
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- openbao_test_service.status.ActiveState == 'active'
|
||||
- openbao_test_service.status.UnitFileState == 'enabled'
|
||||
- openbao_test_service.status.Type == 'notify'
|
||||
- openbao_test_service.status.KillSignal == '2'
|
||||
- openbao_test_service.status.MemorySwapMax == '0'
|
||||
- openbao_test_service.status.NoNewPrivileges == 'yes'
|
||||
- "'HUP' in openbao_test_service.status.ExecReload"
|
||||
quiet: true
|
||||
|
||||
- name: Read the service account
|
||||
ansible.builtin.getent:
|
||||
database: passwd
|
||||
key: openbao
|
||||
|
||||
- name: Stat the data directory and the configuration
|
||||
ansible.builtin.stat:
|
||||
path: "{{ openbao_test_path }}"
|
||||
loop: [/var/lib/openbao, /etc/openbao/openbao.hcl]
|
||||
loop_control:
|
||||
loop_var: openbao_test_path
|
||||
register: openbao_test_paths
|
||||
|
||||
- name: The service user has no shell and owns the private data directory
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- ansible_facts.getent_passwd.openbao[5] == '/usr/sbin/nologin'
|
||||
- openbao_test_paths.results[0].stat.pw_name == 'openbao'
|
||||
- openbao_test_paths.results[0].stat.mode == '0700'
|
||||
- openbao_test_paths.results[1].stat.gr_name == 'openbao'
|
||||
- openbao_test_paths.results[1].stat.mode == '0640'
|
||||
quiet: true
|
||||
|
||||
- name: Ask the installed binary for its version
|
||||
ansible.builtin.command:
|
||||
argv: [/usr/local/bin/bao, version]
|
||||
register: openbao_test_version
|
||||
changed_when: false
|
||||
|
||||
# Over TLS on loopback, validated against the test CA: the listener serves the supplied
|
||||
# certificate, and the server is left uninitialised and sealed on raft storage.
|
||||
- name: Read the seal status over TLS
|
||||
ansible.builtin.uri:
|
||||
url: https://127.0.0.1:8200/v1/sys/seal-status
|
||||
ca_path: "{{ openbao_tls_ca_file }}"
|
||||
validate_certs: true
|
||||
register: openbao_test_seal
|
||||
|
||||
- name: The pinned server answers uninitialised and sealed on raft
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- ('OpenBao v' ~ openbao_test_defaults.openbao_version ~ ' ') in openbao_test_version.stdout
|
||||
- openbao_test_seal.json.version == openbao_test_defaults.openbao_version
|
||||
- openbao_test_seal.json.storage_type == 'raft'
|
||||
- not openbao_test_seal.json.initialized
|
||||
- openbao_test_seal.json.sealed
|
||||
quiet: true
|
||||
|
||||
- name: Reload the service
|
||||
ansible.builtin.systemd_service:
|
||||
name: openbao
|
||||
state: reloaded
|
||||
|
||||
- name: Ask systemd about the service after the reload
|
||||
ansible.builtin.systemd_service:
|
||||
name: openbao
|
||||
register: openbao_test_reloaded
|
||||
|
||||
- name: The reload signalled the running server instead of restarting it
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- openbao_test_reloaded.status.ActiveState == 'active'
|
||||
- openbao_test_reloaded.status.MainPID == openbao_test_service.status.MainPID
|
||||
quiet: true
|
||||
|
||||
- name: Verify the failure paths
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Checksum the configuration before the failure cases
|
||||
ansible.builtin.stat:
|
||||
path: /etc/openbao/openbao.hcl
|
||||
checksum_algorithm: sha256
|
||||
register: openbao_test_config_before
|
||||
|
||||
- name: Run each case
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
loop:
|
||||
- name: tls-cert-missing
|
||||
vars: {openbao_tls_cert_file: ""}
|
||||
expect: must be absolute paths of files on the host
|
||||
- name: tls-ca-missing
|
||||
vars: {openbao_tls_ca_file: ""}
|
||||
expect: must be absolute paths of files on the host
|
||||
- name: tls-key-absent
|
||||
vars: {openbao_tls_key_file: /etc/molecule-tls/absent.key}
|
||||
expect: /etc/molecule-tls/absent.key do not exist
|
||||
- name: tls-key-unreadable
|
||||
vars: {openbao_tls_key_file: /etc/molecule-tls/root-only.key}
|
||||
expect: The user openbao cannot read /etc/molecule-tls/root-only.key
|
||||
- name: tls-min-version-unknown
|
||||
vars: {openbao_tls_min_version: tls10}
|
||||
expect: openbao_tls_min_version must be one of
|
||||
- name: version-with-v
|
||||
vars: {openbao_version: v2.7.3}
|
||||
expect: openbao_version must be a release version
|
||||
- name: sha256-not-hex
|
||||
vars: {openbao_sha256: {amd64: not-a-hash, arm64: not-a-hash}}
|
||||
expect: openbao_sha256 must map
|
||||
- name: bin-path-not-bao
|
||||
vars: {openbao_bin_path: /usr/local/bin/openbao}
|
||||
expect: the file name of openbao_bin_path must be bao
|
||||
- name: listen-address-hostname
|
||||
vars: {openbao_listen_address: localhost}
|
||||
expect: openbao_listen_address must be an IPv4 or IPv6 address
|
||||
- name: ports-equal
|
||||
vars: {openbao_cluster_port: 8200}
|
||||
expect: must be two different ports
|
||||
- name: node-id-with-space
|
||||
vars: {openbao_node_id: "node one"}
|
||||
expect: openbao_node_id must be letters
|
||||
- name: cluster-addr-plain-http
|
||||
vars: {openbao_cluster_addr: "http://127.0.0.1:8201"}
|
||||
expect: openbao_cluster_addr an https:// URL
|
||||
loop_control:
|
||||
loop_var: openbao_test_case
|
||||
label: "{{ openbao_test_case.name }}"
|
||||
|
||||
- name: Checksum the configuration after the failure cases
|
||||
ansible.builtin.stat:
|
||||
path: /etc/openbao/openbao.hcl
|
||||
checksum_algorithm: sha256
|
||||
register: openbao_test_config_after
|
||||
|
||||
- name: Ask systemd about the service after the failure cases
|
||||
ansible.builtin.systemd_service:
|
||||
name: openbao
|
||||
register: openbao_test_after_failures
|
||||
|
||||
- name: The failures changed nothing and the server still runs
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- openbao_test_config_after.stat.checksum == openbao_test_config_before.stat.checksum
|
||||
- openbao_test_after_failures.status.ActiveState == 'active'
|
||||
- openbao_test_after_failures.status.MainPID == openbao_test_reloaded.status.MainPID
|
||||
quiet: true
|
||||
|
||||
# Runs last: it rewrites the configuration and restarts the server, then the TLS run restores it.
|
||||
- name: Verify the TLS switch
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Run the role with TLS disabled and no TLS input
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.openbao
|
||||
vars:
|
||||
openbao_tls_disable: true
|
||||
openbao_tls_cert_file: ""
|
||||
openbao_tls_key_file: ""
|
||||
openbao_tls_ca_file: ""
|
||||
|
||||
- name: Read the seal status over plain HTTP
|
||||
ansible.builtin.uri:
|
||||
url: http://127.0.0.1:8200/v1/sys/seal-status
|
||||
register: openbao_test_plain
|
||||
|
||||
- name: Run the role with the scenario's TLS inputs again
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.openbao
|
||||
|
||||
- name: Read the seal status over TLS again
|
||||
ansible.builtin.uri:
|
||||
url: https://127.0.0.1:8200/v1/sys/seal-status
|
||||
ca_path: "{{ openbao_tls_ca_file }}"
|
||||
validate_certs: true
|
||||
register: openbao_test_tls_again
|
||||
|
||||
- name: The switch served plain HTTP and TLS came back
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- openbao_test_plain.json.sealed
|
||||
- openbao_test_tls_again.json.sealed
|
||||
quiet: true
|
||||
Reference in new issue
Block a user