Files
Kamino/roles/sshd/README.md
T
Defame1297 2e1ad033e4
Build / build (pull_request) Successful in 51s
Lint / lint (pull_request) Successful in 3m42s
Molecule / molecule (pull_request) Failing after 20m50s
test(roles): run the Ubuntu roles on 24.04 and 26.04 and declare exactly those
Every role except network_interfaces runs its molecule scenarios on the
24.04 and 26.04 images and declares Ubuntu noble and resolute; jammy and
Ubuntu all are dropped. trixie stays declared until the Debian 13 image
(#226) tests it. docker and gitea stay on noble until the 26.04 image
ships gnupg (#378).

Closes #133

Assisted-by: Claude Code
2026-11-25 20:17:53 +00:00

80 lines
5.8 KiB
Markdown

# sshd
Writes a hardened sshd drop-in, selected by a profile, and refuses configurations that would lock out SSH.
## Supported platforms
Ubuntu 24.04 (noble) and 26.04 (resolute), the platforms the molecule scenario runs on.
Untested platforms: Debian trixie (Proxmox VE 9 nodes, the `pve` profile), listed in `meta/main.yml` but not converged by a test.
The role targets Ubuntu guests (the `ubuntu_lxc` and `ubuntu_vm` profiles) and Proxmox VE nodes (the `pve` profile).
## Requirements and run order
- The role uses `ansible.builtin` modules only.
- Use it by its fully qualified name, `mortis.kamino.sshd`.
- The target must be reachable over SSH and have `ssh-keygen` and `sshd` installed. The role uses `become`.
- Install the public key of `ansible_user` before this role runs (for example with a user-management role earlier in the play), so key login works before password authentication is turned off. The role does not install keys.
- `sshd_profile` is set by the calling play: `pve`, `ubuntu_lxc` or `ubuntu_vm`.
- The role reads `ansible_user` and sets `ansible_ssh_user` to it for the rest of the play, in case the connection was bootstrapped as root.
- Role dependencies: none. The run order above is not declared in `meta/main.yml`; it is the contract.
## Role variables
`meta/argument_specs.yml` is the source of truth for types and descriptions.
| Name | Default | Required | Description |
| --- | --- | --- | --- |
| `sshd_profile` | none | yes | Profile key in `sshd_profiles` |
| `sshd_service_name` | `sshd` | no | Service the handler reloads |
| `sshd_config_dir` | `/etc/ssh/sshd_config.d` | no | Drop-in directory |
| `sshd_dropin_file` | `99-ansible-hardening.conf` | no | Drop-in file name |
| `sshd_settings_defaults` | see `defaults/main.yml` | no | Baseline directives for all profiles |
| `sshd_profiles` | see `defaults/main.yml` | no | Per-profile directives; overriding replaces all profiles |
| `sshd_settings` | `{}` | no | Directives merged over the profile key by key |
| `sshd_settings_effective` | derived | no | Merged result; read-only by convention. Not in spec |
| `sshd_reload` | `true` | no | Reload sshd when the drop-in changes |
The drop-in contains `sshd_settings_defaults`, then the profile, then `sshd_settings`, later values winning.
`sshd_settings_effective` is deliberately not in the argument spec: its default indexes `sshd_profiles[sshd_profile]`, and the spec is templated before `tasks/validate.yml` runs, so an unknown `sshd_profile` would fail at role entry with a templating error instead of the validation message.
## Tags
The role defines none.
## Example play
```yaml
- name: Harden SSH on Ubuntu containers
hosts: containers
roles:
- role: mortis.kamino.sshd
vars:
sshd_profile: ubuntu_lxc
sshd_settings:
MaxAuthTries: "5"
```
## Tests
The scenario `molecule/default/` runs in CI only, on two platforms: `${MOLECULE_IMAGE}` (Ubuntu 24.04) and `${MOLECULE_IMAGE_2604}` (Ubuntu 26.04). Both images ship openssh-server. The sequence runs converge, idempotence and verify; it has no check-mode step.
- Converge applies the `ubuntu_lxc` profile. It then loads the defaults through `tasks_from: validate.yml` without changing the host: every profile (`pve`, `ubuntu_lxc`, `ubuntu_vm`) passes validation and keeps its root login policy and key algorithms, a consumer `sshd_settings` override wins over the profile, and the `pve` guard accepts each value that allows root key login.
- Verify first checks the refusals: a run without `sshd_profile`, `PubkeyAuthentication no` (lock-out), a `pve` profile with `PermitRootLogin no` or `forced-commands-only`, a `pve` profile without `rsa-sha2-512`, SHA-1 `ssh-rsa`, and an unknown profile. Each but the missing profile is matched on its message.
- Verify then checks the converged host: the drop-in exists, `sshd -t` passes, the `ssh` unit is active, and `sshd -T` shows key login on and password and root login off.
- The `pve` profile is checked by rendering its drop-in to a scratch file and parsing it with `sshd -T -f`: root logs in with keys only and passwords stay off.
Not covered: the `pve` profile is not converged and no SSH login is attempted.
## Notes
- Idempotent: yes.
- `tasks/validate.yml` fails the play for an unknown profile, an empty merged result, `PasswordAuthentication no` without `PubkeyAuthentication yes`, a `pve` profile whose `PermitRootLogin` does not allow root key login (`prohibit-password`, its alias `without-password`, or `yes`; `no` and `forced-commands-only` are refused), a `pve` profile whose `HostKeyAlgorithms` or `PubkeyAcceptedAlgorithms` lacks `rsa-sha2-512` (the `+`/`-`/`^` list forms are not parsed and fail; write the full list), and any profile that lists SHA-1 `ssh-rsa` in either.
- The drop-in is installed with `validate: sshd -t -f %s`. The handler named `Restart SSH` reloads the service, and is skipped in check mode or when `sshd_reload` is false.
- `ssh-keygen -A` runs on every play and always reports ok.
- `meta/argument_specs.yml` has only the `main` entry point. `tasks_from: validate.yml` is used by the molecule scenario to load defaults and exercise the guards, not by consumers, so it has no spec.
- The `pve` profile sets `PermitRootLogin prohibit-password`. Proxmox cluster SSH (migration, console proxy, replication) logs in as root with keys only. Passwords are already off through `PasswordAuthentication no`; this keeps root key-only if they are ever turned back on. Join nodes through the default API join, not `pvecm add --use_ssh`, which needs root password SSH.
- Host key and public key algorithms are `ssh-ed25519` only, except in the `pve` profile, which allows `ssh-ed25519,rsa-sha2-512,rsa-sha2-256` for both: Proxmox cluster SSH (migration, console proxy) checks each node's RSA host key against `/etc/pve/nodes/<node>/ssh_known_hosts` and logs in with root's RSA key. SHA-1 `ssh-rsa` stays refused.