feat(user_management): add the user_management role
Copied from the environment repository's local role. The files are identical except: galaxy namespace mortis; comments and the README scrubbed of environment references (issue numbers, playbooks, inventory groups and group_vars paths, the provisioning roles by name, the history of the variable rename), with the technical explanation kept; the README asks for the role by its FQCN, names the run order generically (after the step that creates the guest, before mortis.kamino.sshd) and its example play uses mortis.kamino.user_management; the molecule scenario calls the role by its FQCN, follows the Kamino scenario conventions (kamino-molecule header, scenario.test_sequence) and no longer sets ANSIBLE_COLLECTIONS_PATH or ANSIBLE_ROLES_PATH. No variable, default, argument_specs entry or task logic changed: the key-only root fallback, the handback and user_management_lock_root_password (default false) are as in the source, and prepare/verify are unchanged. The role uses ansible.posix (authorized_key), already a galaxy.yml dependency, so galaxy.yml is unchanged. The README Roadmap no longer lists wave 1; CLAUDE.md names the role for the next release. Not verified locally: the molecule scenario (it runs on the kamino-molecule runner in CI) and ansible-test sanity (runs on push). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01MmzvM9skAvYcph2c9EiPcm
This commit is contained in:
1 parent
3d7a4356e6
commit
2713753039
18 files changed
+706
-2
No files matched your search
@@ -0,0 +1,31 @@
|
||||
---
|
||||
# ansible_user is a connection variable: under the docker connection it would
|
||||
# make `docker exec -u <user>` before the user exists. So the primary user is
|
||||
# set explicitly instead of through the default ({{ ansible_user }}).
|
||||
# bootstrap.yml: wait_for_connection succeeds over the docker connection, so the
|
||||
# root fallback is never taken.
|
||||
- name: Converge
|
||||
hosts: all
|
||||
roles:
|
||||
- role: mortis.kamino.user_management
|
||||
vars:
|
||||
user_management_ansible_user:
|
||||
name: um-admin
|
||||
password_hash: "{{ user_management_become_password_hashed | mandatory }}"
|
||||
groups: [sudo]
|
||||
shell: /bin/bash
|
||||
create_home: true
|
||||
state: present
|
||||
authorized_keys:
|
||||
- "{{ user_management_test_admin_key }}"
|
||||
user_management_authoritative: true
|
||||
user_management_lock_root_password: true
|
||||
user_management_other_users:
|
||||
- name: um-alice
|
||||
groups: [sudo]
|
||||
shell: /bin/bash
|
||||
comment: Molecule test user
|
||||
authorized_keys:
|
||||
- "{{ user_management_test_alice_key }}"
|
||||
- name: um-olduser
|
||||
state: absent
|
||||
@@ -0,0 +1,51 @@
|
||||
---
|
||||
# Runs on the repository's own runner (label kamino-molecule), in the molecule-ubuntu2404 image
|
||||
# (MOLECULE_IMAGE, the pin in .gitea/workflows/molecule.yml). No privileged mode: systemd needs a
|
||||
# writable cgroup; NET_ADMIN keeps the container the same as the other role scenarios.
|
||||
driver:
|
||||
name: docker
|
||||
platforms:
|
||||
- name: user-management-instance
|
||||
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
|
||||
capabilities:
|
||||
- NET_ADMIN
|
||||
security_opts:
|
||||
- apparmor=unconfined
|
||||
provisioner:
|
||||
name: ansible
|
||||
inventory:
|
||||
group_vars:
|
||||
all:
|
||||
# Public halves of throwaway ed25519 keys (private halves were deleted).
|
||||
user_management_test_admin_key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIM1r3pGQ7wmNJ7blsnv5IYeQZryMi99c2OZDv141YX8I molecule-test"
|
||||
user_management_test_alice_key: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJ/w+G9RHO2XZabz/B30JRBlJdRy278UlmUiz+IuBb2e molecule-test"
|
||||
# "!" is a locked password: no credential-like string is committed.
|
||||
user_management_become_password_hashed: "!"
|
||||
verifier:
|
||||
name: ansible
|
||||
scenario:
|
||||
# molecule's default sequence minus its first destroy: the workflow removes what a killed run
|
||||
# left before any scenario starts, so that destroy only waits out the
|
||||
# driver's 5 s poll on a container that is not there. After an aborted local run, run
|
||||
# `molecule destroy` yourself.
|
||||
test_sequence:
|
||||
- dependency
|
||||
- cleanup
|
||||
- syntax
|
||||
- create
|
||||
- prepare
|
||||
- converge
|
||||
- idempotence
|
||||
- side_effect
|
||||
- verify
|
||||
- cleanup
|
||||
- destroy
|
||||
@@ -0,0 +1,59 @@
|
||||
---
|
||||
# State the role must correct: a stale user with a home (state absent removes
|
||||
# it), and an existing user holding an extra key (authoritative mode drops it).
|
||||
- name: Prepare
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Create the user the role will remove
|
||||
ansible.builtin.user:
|
||||
name: um-olduser
|
||||
shell: /bin/bash
|
||||
create_home: true
|
||||
|
||||
- name: Create the additional user with an extra key already planted
|
||||
ansible.builtin.user:
|
||||
name: um-alice
|
||||
shell: /bin/sh
|
||||
create_home: true
|
||||
|
||||
- name: Create alice's .ssh directory
|
||||
ansible.builtin.file:
|
||||
path: /home/um-alice/.ssh
|
||||
state: directory
|
||||
owner: um-alice
|
||||
group: um-alice
|
||||
mode: "0700"
|
||||
|
||||
- name: Plant an extra key that authoritative mode must remove
|
||||
ansible.builtin.copy:
|
||||
content: "{{ user_management_test_admin_key | regex_replace('molecule-test$', 'rogue-extra') }}\n"
|
||||
dest: /home/um-alice/.ssh/authorized_keys
|
||||
owner: um-alice
|
||||
group: um-alice
|
||||
mode: "0600"
|
||||
|
||||
# user_management_lock_root_password: root starts with an unlocked password field (the image
|
||||
# ships "*", which already counts as unusable but does not start with "!") and a key the lock
|
||||
# must leave alone. The value is a placeholder, not a hash: nothing can log in with it.
|
||||
- name: Give root an unlocked password field
|
||||
ansible.builtin.user:
|
||||
name: root
|
||||
password: molecule-unlocked-placeholder
|
||||
update_password: always
|
||||
|
||||
- name: Create root's .ssh directory
|
||||
ansible.builtin.file:
|
||||
path: /root/.ssh
|
||||
state: directory
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0700"
|
||||
|
||||
- name: Plant a key in root's authorized_keys that the lock must not touch
|
||||
ansible.builtin.copy:
|
||||
content: "{{ user_management_test_admin_key | regex_replace('molecule-test$', 'root-key') }}\n"
|
||||
dest: /root/.ssh/authorized_keys
|
||||
owner: root
|
||||
group: root
|
||||
mode: "0600"
|
||||
@@ -0,0 +1,102 @@
|
||||
---
|
||||
- name: Verify
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Read the account databases
|
||||
ansible.builtin.getent:
|
||||
database: passwd
|
||||
|
||||
- name: The removed user is gone
|
||||
ansible.builtin.assert:
|
||||
that: "'um-olduser' not in getent_passwd"
|
||||
quiet: true
|
||||
|
||||
- name: Read group membership
|
||||
ansible.builtin.command: "id -nG {{ item }}"
|
||||
loop: [um-admin, um-alice]
|
||||
register: user_management_verify_groups
|
||||
changed_when: false
|
||||
|
||||
- name: Both users are in the sudo group
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "'sudo' in item.stdout.split()"
|
||||
quiet: true
|
||||
loop: "{{ user_management_verify_groups.results }}"
|
||||
loop_control:
|
||||
label: "{{ item.item }}"
|
||||
|
||||
- name: Stat the removed user's home
|
||||
ansible.builtin.stat:
|
||||
path: /home/um-olduser
|
||||
register: user_management_verify_olduser_home
|
||||
|
||||
- name: The removed user's home is gone
|
||||
ansible.builtin.assert:
|
||||
that: not user_management_verify_olduser_home.stat.exists
|
||||
quiet: true
|
||||
|
||||
- name: Read the authorized_keys files
|
||||
ansible.builtin.slurp:
|
||||
src: "/home/{{ item }}/.ssh/authorized_keys"
|
||||
loop: [um-admin, um-alice]
|
||||
register: user_management_verify_keys
|
||||
|
||||
- name: The authorized_keys files hold exactly the configured keys (the planted extra key is gone)
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- (user_management_verify_keys.results[0].content | b64decode).split('\n') | select | list == [user_management_test_admin_key]
|
||||
- (user_management_verify_keys.results[1].content | b64decode).split('\n') | select | list == [user_management_test_alice_key]
|
||||
- "'rogue-extra' not in (user_management_verify_keys.results[1].content | b64decode)"
|
||||
quiet: true
|
||||
|
||||
- name: Validate the sudoers configuration
|
||||
ansible.builtin.command: visudo -c
|
||||
changed_when: false
|
||||
|
||||
- name: Show what the sudo users may run
|
||||
ansible.builtin.command: "sudo -l -U {{ item }}"
|
||||
loop: [um-admin, um-alice]
|
||||
register: user_management_verify_sudo
|
||||
changed_when: false
|
||||
|
||||
- name: Membership of the sudo group grants sudo
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "'(ALL : ALL) ALL' in item.stdout or '(ALL) ALL' in item.stdout"
|
||||
quiet: true
|
||||
loop: "{{ user_management_verify_sudo.results }}"
|
||||
loop_control:
|
||||
label: "{{ item.item }}"
|
||||
|
||||
- name: The ansible user is not locked out (key present, account usable)
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- user_management_test_admin_key in (user_management_verify_keys.results[0].content | b64decode)
|
||||
- getent_passwd['um-admin'][5] != '/usr/sbin/nologin'
|
||||
quiet: true
|
||||
|
||||
- name: Read root's shadow entry
|
||||
become: true
|
||||
ansible.builtin.getent:
|
||||
database: shadow
|
||||
key: root
|
||||
|
||||
- name: Root's password is locked
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- getent_shadow['root'][0] == '!'
|
||||
quiet: true
|
||||
|
||||
- name: Read root's authorized_keys
|
||||
ansible.builtin.slurp:
|
||||
src: /root/.ssh/authorized_keys
|
||||
register: user_management_verify_root_keys
|
||||
|
||||
- name: The lock left root's key and shell alone
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- (user_management_verify_root_keys.content | b64decode) is search('root-key$', multiline=true)
|
||||
- getent_passwd['root'][5] == '/bin/bash'
|
||||
quiet: true
|
||||
Reference in new issue
Block a user