diff --git a/roles/user_management/README.md b/roles/user_management/README.md index 9c93a85..e2ddd3d 100644 --- a/roles/user_management/README.md +++ b/roles/user_management/README.md @@ -11,8 +11,8 @@ Ubuntu 24.04 (noble, covered by the molecule scenario) and 22.04 (jammy) and Deb - Use it by its fully qualified name, `mortis.kamino.user_management`. - Collection `ansible.posix` (`authorized_key`). - `user_management_become_password_hashed` must be defined for the primary user (`| mandatory` in the `user_management_ansible_user` default). Keep it vaulted, for example in host vars. -- The bootstrap tries `wait_for_connection` as the configured user, and on failure switches `ansible_ssh_user` to `root` and pings, with the same deploy key (`ansible_ssh_private_key_file`); no password is ever used. Once the users and keys are written it switches back to the configured user and checks the new connection (`tasks/handback.yml`; skipped in check mode), because the fact would otherwise last for the rest of the run and later plays would connect as root after `mortis.kamino.sshd` refused it. Where the fallback applies: a new LXC, whose root gets the deploy key from the provisioning step and whose stock Ubuntu sshd admits root by key on first boot. A VM never needs it: cloud-init creates the configured user with the key (`ciuser`/`sshkeys`), and root is not given the key. A freshly installed Proxmox node needs the deploy key in root's `authorized_keys` first. After `mortis.kamino.sshd` has run, root login is refused (`PermitRootLogin: no` on `ubuntu_lxc` and `ubuntu_vm`, `prohibit-password` on `pve`), so a host whose configured user broke later is recovered from its Proxmox node: `pct enter` for an LXC, `qm terminal` or the console for a VM, the node's own console for a Proxmox host. -- Runs before `mortis.kamino.sshd`, after the step that creates the guest (LXC or VM), and first in a play for Proxmox nodes, which have no provisioning step. A play for Proxmox nodes sets `user_management_disable_cloud_init: false`. +- The bootstrap tries `wait_for_connection` as the configured user, and on failure switches `ansible_ssh_user` to `root` and pings, with the same deploy key (`ansible_ssh_private_key_file`); no password is ever used. Once the users and keys are written it switches back to the configured user and checks the new connection (`tasks/handback.yml`; skipped in check mode), because the fact would otherwise last for the rest of the run and later plays would connect as root after the `sshd` role of this Collection refused it. Where the fallback applies: a new LXC, whose root gets the deploy key from the provisioning step and whose stock Ubuntu sshd admits root by key on first boot. A VM never needs it: cloud-init creates the configured user with the key (`ciuser`/`sshkeys`), and root is not given the key. A freshly installed Proxmox node needs the deploy key in root's `authorized_keys` first. After the `sshd` role has run, root login is refused (`PermitRootLogin: no` on `ubuntu_lxc` and `ubuntu_vm`, `prohibit-password` on `pve`), so a host whose configured user broke later is recovered from its Proxmox node: `pct enter` for an LXC, `qm terminal` or the console for a VM, the node's own console for a Proxmox host. +- Runs before the `sshd` role, after the step that creates the guest (LXC or VM), and first in a play for Proxmox nodes, which have no provisioning step. A play for Proxmox nodes sets `user_management_disable_cloud_init: false`. - The role has no declared dependencies. ## Role variables @@ -59,5 +59,5 @@ Ubuntu 24.04 (noble, covered by the molecule scenario) and 22.04 (jammy) and Deb - `tasks/ssh_keys.yml` hardcodes the `.ssh` path as `/home//.ssh` and skips creating it for users with `system: true`, but the `authorized_key` task still runs for them (it uses the user's real home), so a system user with keys gets them written. - `tasks/ssh_keys.yml` sets the `.ssh` directory's group to the user's name, and the user tasks set no primary `group`, so it assumes each user's primary group has the same name as the user (the default user-private group). - The spec lists `user_management_become_password_hashed` as `raw` with `no_log`, so the vaulted value is never printed. -- `user_management_lock_root_password` locks root's password after the users and keys are written: `ansible.builtin.user` sets the shadow field to `!` (`update_password: always`), so the old hash is removed rather than kept behind a `!` prefix as `usermod -L` would leave it, and reports ok once it is there. Nothing else of root changes (shell, home, `authorized_keys`). It is meant for guests whose root password is not needed (for example containers created with a shared root password): recovery is `pct enter` from the Proxmox node, which needs no password. The key-only root fallback still works on a locked root, because sshd with `UsePAM yes` (the Ubuntu default and the `mortis.kamino.sshd` setting) does not refuse key logins for a `!` hash. Leave it false on a Proxmox node, where root's password is the web UI and PAM login. The role fails when it is true and the primary user is root, since the lock would disable that user's password. Unlocking is not automated: setting the input back to false leaves the lock in place, and there is no old password to restore (set a new one with `passwd root` by hand). +- `user_management_lock_root_password` locks root's password after the users and keys are written: `ansible.builtin.user` sets the shadow field to `!` (`update_password: always`), so the old hash is removed rather than kept behind a `!` prefix as `usermod -L` would leave it, and reports ok once it is there. Nothing else of root changes (shell, home, `authorized_keys`). It is meant for guests whose root password is not needed (for example containers created with a shared root password): recovery is `pct enter` from the Proxmox node, which needs no password. The key-only root fallback still works on a locked root, because sshd with `UsePAM yes` (the Ubuntu default and the `sshd` role's setting) does not refuse key logins for a `!` hash. Leave it false on a Proxmox node, where root's password is the web UI and PAM login. The role fails when it is true and the primary user is root, since the lock would disable that user's password. Unlocking is not automated: setting the input back to false leaves the lock in place, and there is no old password to restore (set a new one with `passwd root` by hand). - Molecule: `molecule/default`, run by the Collection's CI, with a stale user to remove, a planted extra key to drop and root's password set before converge, then locked by the role. diff --git a/roles/user_management/tasks/bootstrap.yml b/roles/user_management/tasks/bootstrap.yml index 53446d9..d10a8a3 100644 --- a/roles/user_management/tasks/bootstrap.yml +++ b/roles/user_management/tasks/bootstrap.yml @@ -11,7 +11,7 @@ # connection (ansible_ssh_private_key_file). It is for a new LXC: the provisioning step gives root # that key and stock Ubuntu sshd (PermitRootLogin prohibit-password) admits root by key on first # boot. A new VM never needs it (cloud-init creates the configured user with the key; root does not -# get it), and a fresh Proxmox node gets the key from the operator. Once mortis.kamino.sshd has run +# get it), and a fresh Proxmox node gets the key from the operator. Once the sshd role has run # (PermitRootLogin no on ubuntu_lxc/ubuntu_vm), root is refused: a host whose configured user broke # later is recovered from its Proxmox node (pct enter, qm terminal or the console), not here. # tasks/handback.yml switches the connection back to the configured user after it is created. diff --git a/roles/user_management/tasks/handback.yml b/roles/user_management/tasks/handback.yml index fe15859..c8cf67b 100644 --- a/roles/user_management/tasks/handback.yml +++ b/roles/user_management/tasks/handback.yml @@ -1,6 +1,6 @@ --- # The root fallback in bootstrap.yml sets ansible_ssh_user as a fact, and a fact lasts for the -# whole playbook run, not only this play. mortis.kamino.sshd later refuses root (PermitRootLogin +# whole playbook run, not only this play. The sshd role later refuses root (PermitRootLogin # no), so a later play that still connected as root would only get in while ssh's ControlPersist # connection lived. Switch back to the configured user, now that it exists with its keys, and # prove the new connection works. No meta: reset_connection: it ignores `when` (so it ran on diff --git a/roles/user_management/tasks/lock_root.yml b/roles/user_management/tasks/lock_root.yml index 37e1f38..64c33c1 100644 --- a/roles/user_management/tasks/lock_root.yml +++ b/roles/user_management/tasks/lock_root.yml @@ -7,7 +7,7 @@ # authorized_keys are not passed and stay as they are. # # This does not break the root fallback in tasks/bootstrap.yml: it logs root in with the deploy -# key, never a password, and sshd with UsePAM yes (the Ubuntu default and the mortis.kamino.sshd +# key, never a password, and sshd with UsePAM yes (the Ubuntu default and the sshd role's # setting) does not treat a "!" hash as a locked account for key authentication. Recovery from the # host (pct enter, console as root) needs no password either. #