Merge branch 'main' into feat/ufw-rule-delete
This commit is contained in:
commit
14a9a72ea2
15 files changed
+717
-1
No files matched your search
@@ -40,7 +40,7 @@ the environment that uses it) are met.
|
||||
|
||||
Still to come:
|
||||
|
||||
- Wave 1 (remaining): `sshd`, `user_management`.
|
||||
- Wave 1 (remaining): `user_management`.
|
||||
- Wave 2: `binary_update` (shared binary-update logic), `pve_vm`, `pve_lxc`, `traefik`,
|
||||
`gitea`, `gitea_runner`, `tailscale`, `cloudflared`, `pihole`.
|
||||
- Wave 3: `rke2`, `proxmox_host`.
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
---
|
||||
minor_changes:
|
||||
- sshd - New role. Writes a hardened sshd drop-in selected by a profile (``pve``, ``ubuntu_lxc``, ``ubuntu_vm``),
|
||||
merged from a baseline, the profile and ``sshd_settings``, checked with ``sshd -t`` and reloaded through a handler.
|
||||
It refuses to run when the result would lock SSH out (password login off without public key login), when the
|
||||
``pve`` profile does not allow root public-key login or lacks ``rsa-sha2-512`` (Proxmox VE cluster SSH needs both),
|
||||
and when any profile allows SHA-1 ``ssh-rsa``.
|
||||
@@ -0,0 +1,63 @@
|
||||
# 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), the image the molecule scenario runs on. 22.04 (jammy) and Debian trixie (Proxmox VE 9 nodes, the `pve` profile) are listed in `meta/main.yml` but not converged by a test; the `pve` drop-in is parsed by `sshd -T` in the scenario. The role uses `ansible.builtin` modules only.
|
||||
|
||||
## Requirements and run order
|
||||
|
||||
- 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 are not declared in `meta/main.yml`; the order above 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"
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
- `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.
|
||||
- Molecule: `molecule/default`, run by the Collection's CI. It converges the `ubuntu_lxc` profile; the `pve` profile is checked by rendering its drop-in to a scratch file and parsing it with `sshd -T -f`.
|
||||
@@ -0,0 +1,79 @@
|
||||
---
|
||||
# PVE sometimes uses ssh instead of sshd
|
||||
sshd_service_name: sshd
|
||||
sshd_config_dir: /etc/ssh/sshd_config.d
|
||||
sshd_dropin_file: 99-ansible-hardening.conf
|
||||
|
||||
sshd_settings_defaults:
|
||||
# Authentication
|
||||
PasswordAuthentication: "no"
|
||||
PubkeyAuthentication: "yes"
|
||||
KbdInteractiveAuthentication: "no"
|
||||
ChallengeResponseAuthentication: "no"
|
||||
PermitEmptyPasswords: "no"
|
||||
UsePAM: "yes"
|
||||
|
||||
# Access control
|
||||
MaxAuthTries: "3"
|
||||
LoginGraceTime: "30"
|
||||
MaxSessions: "5"
|
||||
MaxStartups: "10:30:60"
|
||||
|
||||
# Connection hardening
|
||||
ClientAliveInterval: "300"
|
||||
ClientAliveCountMax: "2"
|
||||
TCPKeepAlive: "no"
|
||||
|
||||
# Forwarding / features
|
||||
X11Forwarding: "no"
|
||||
AllowAgentForwarding: "no"
|
||||
AllowTcpForwarding: "no"
|
||||
PermitTunnel: "no"
|
||||
|
||||
# Crypto hygiene
|
||||
HostKeyAlgorithms: "ssh-ed25519"
|
||||
PubkeyAcceptedAlgorithms: "ssh-ed25519"
|
||||
KexAlgorithms: "curve25519-sha256"
|
||||
Ciphers: "[email protected],[email protected]"
|
||||
MACs: "[email protected],[email protected]"
|
||||
|
||||
# Misc
|
||||
PrintMotd: "no"
|
||||
PrintLastLog: "yes"
|
||||
LogLevel: "VERBOSE"
|
||||
|
||||
# Per-profile settings, layered over sshd_settings_defaults. Selected by sshd_profile (a role
|
||||
# parameter). These are role defaults, so inventory variables override them with normal
|
||||
# Ansible precedence.
|
||||
# Overriding sshd_profiles replaces ALL profiles (dicts are not merged across variable
|
||||
# layers); to change one setting use sshd_settings below.
|
||||
sshd_profiles:
|
||||
pve:
|
||||
# Proxmox cluster SSH logs in as root with keys only (BatchMode). Passwords are already
|
||||
# off through PasswordAuthentication; this also keeps root key-only if they are ever
|
||||
# turned back on. Join nodes through the default API join, not `pvecm add --use_ssh`.
|
||||
PermitRootLogin: "prohibit-password"
|
||||
AllowTcpForwarding: "no"
|
||||
X11Forwarding: "no"
|
||||
AllowAgentForwarding: "no"
|
||||
# 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, so RSA with SHA-2
|
||||
# stays allowed here. Never ssh-rsa (SHA-1). Guarded in tasks/validate.yml.
|
||||
HostKeyAlgorithms: "ssh-ed25519,rsa-sha2-512,rsa-sha2-256"
|
||||
PubkeyAcceptedAlgorithms: "ssh-ed25519,rsa-sha2-512,rsa-sha2-256"
|
||||
ubuntu_lxc:
|
||||
PermitRootLogin: "no"
|
||||
ubuntu_vm:
|
||||
PermitRootLogin: "no"
|
||||
|
||||
# Consumer overrides: merged over the profile, key by key, so one setting can change without
|
||||
# repeating the rest. Values replace; there is no list merging here (all values are strings).
|
||||
sshd_settings: {}
|
||||
|
||||
# The settings written to the drop-in. Read by the template and by validate.yml.
|
||||
sshd_settings_effective: >-
|
||||
{{ sshd_settings_defaults
|
||||
| combine(sshd_profiles[sshd_profile], sshd_settings) }}
|
||||
|
||||
# Safety valve
|
||||
sshd_reload: true
|
||||
@@ -0,0 +1,9 @@
|
||||
---
|
||||
- name: Restart SSH
|
||||
become: true
|
||||
ansible.builtin.service:
|
||||
name: "{{ sshd_service_name }}"
|
||||
state: reloaded
|
||||
when:
|
||||
- sshd_reload
|
||||
- not ansible_check_mode
|
||||
@@ -0,0 +1,62 @@
|
||||
---
|
||||
argument_specs:
|
||||
main:
|
||||
short_description: Write a hardened sshd drop-in
|
||||
description:
|
||||
- >-
|
||||
Validates the profile and the merged settings, runs C(ssh-keygen -A), installs the drop-in checked with C(sshd -t) and
|
||||
reloads sshd through a handler.
|
||||
- >-
|
||||
Refuses to run when password authentication is off and public key authentication is not C(yes); when the C(pve)
|
||||
profile does not permit root key login (C(PermitRootLogin) must be C(prohibit-password), C(without-password) or C(yes));
|
||||
when the C(pve) profile's C(HostKeyAlgorithms) or C(PubkeyAcceptedAlgorithms) lacks C(rsa-sha2-512); and when any
|
||||
profile lists SHA-1 C(ssh-rsa) in either.
|
||||
- Reads C(ansible_user), which is not a role input, and sets C(ansible_ssh_user) to it for the rest of the play.
|
||||
- >-
|
||||
C(sshd_settings_effective), the merged result read by the template and tasks/validate.yml, is deliberately not listed:
|
||||
its default indexes C(sshd_profiles[sshd_profile]), and the spec is templated before tasks/validate.yml runs, so an
|
||||
unknown C(sshd_profile) would fail at role entry with a templating error instead of the validation message.
|
||||
options:
|
||||
sshd_profile:
|
||||
type: str
|
||||
required: true
|
||||
description:
|
||||
- >-
|
||||
Key of C(sshd_profiles) that is layered over C(sshd_settings_defaults). With the shipped C(sshd_profiles) it is C(pve),
|
||||
C(ubuntu_lxc) or C(ubuntu_vm).
|
||||
- Not restricted by C(choices) because C(sshd_profiles) can be overridden; an unknown key is refused by tasks/validate.yml.
|
||||
sshd_service_name:
|
||||
type: str
|
||||
default: sshd
|
||||
description:
|
||||
- Service the handler reloads.
|
||||
sshd_config_dir:
|
||||
type: str
|
||||
default: /etc/ssh/sshd_config.d
|
||||
description:
|
||||
- Directory that holds the drop-in. It is created when missing.
|
||||
sshd_dropin_file:
|
||||
type: str
|
||||
default: 99-ansible-hardening.conf
|
||||
description:
|
||||
- File name of the drop-in inside C(sshd_config_dir).
|
||||
sshd_settings_defaults:
|
||||
type: dict
|
||||
description:
|
||||
- Hardening baseline of sshd directives, applied to every profile. Values are strings.
|
||||
- Overriding it replaces the whole baseline; use C(sshd_settings) to change single directives.
|
||||
sshd_profiles:
|
||||
type: dict
|
||||
description:
|
||||
- Per-profile directives merged over C(sshd_settings_defaults), keyed by profile name.
|
||||
- Overriding it replaces all profiles, because dicts are not merged across variable layers.
|
||||
sshd_settings:
|
||||
type: dict
|
||||
default: {}
|
||||
description:
|
||||
- Directives merged over the selected profile key by key. Values replace, lists are not merged.
|
||||
sshd_reload:
|
||||
type: bool
|
||||
default: true
|
||||
description:
|
||||
- When false the handler does not reload sshd after the drop-in changes. The handler is also skipped in check mode.
|
||||
@@ -0,0 +1,21 @@
|
||||
---
|
||||
galaxy_info:
|
||||
role_name: sshd
|
||||
namespace: mortis
|
||||
author: Defame1297
|
||||
license: GPL-3.0-or-later
|
||||
description: Write a hardened sshd drop-in selected by profile (pve, ubuntu_lxc, ubuntu_vm), guarded against SSH lock-out.
|
||||
min_ansible_version: "2.19"
|
||||
platforms:
|
||||
- name: Ubuntu
|
||||
versions:
|
||||
- jammy
|
||||
- noble
|
||||
- name: Debian
|
||||
versions:
|
||||
- trixie
|
||||
galaxy_tags:
|
||||
- ssh
|
||||
- sshd
|
||||
- security
|
||||
dependencies: []
|
||||
@@ -0,0 +1,234 @@
|
||||
---
|
||||
# The production profile: Ubuntu 24.04 LXCs. Only the profile name and the user are supplied.
|
||||
- name: Converge the role with a valid profile
|
||||
hosts: all
|
||||
vars:
|
||||
sshd_profile: ubuntu_lxc
|
||||
ansible_user: root
|
||||
roles:
|
||||
- role: mortis.kamino.sshd
|
||||
|
||||
# A play without sshd_profile: the role must refuse it and leave the config
|
||||
# converged above untouched. The block proves the refusal (not its wording); the play
|
||||
# still passes, so the idempotence run stays clean.
|
||||
- name: Converge the role without a profile
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Missing sshd_profile is rejected
|
||||
block:
|
||||
- name: Apply the role without sshd_profile
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.sshd
|
||||
- name: Fail when the missing profile was accepted
|
||||
ansible.builtin.fail:
|
||||
msg: the sshd role accepted a run without sshd_profile
|
||||
rescue:
|
||||
- name: The role refused the run
|
||||
ansible.builtin.debug:
|
||||
msg: Rejected as expected
|
||||
|
||||
# The plays below only load the role's defaults and run validate.yml, which changes nothing
|
||||
# on the container. They cover the profiles the converge above cannot run and the guards.
|
||||
- name: Every profile passes validation and keeps its root login policy
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Validate each profile
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.sshd
|
||||
tasks_from: validate.yml
|
||||
public: true
|
||||
vars:
|
||||
sshd_profile: "{{ item }}"
|
||||
loop: [pve, ubuntu_lxc, ubuntu_vm]
|
||||
- name: Assert the profile data
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- sshd_profiles.pve.PermitRootLogin == "prohibit-password"
|
||||
- sshd_profiles.ubuntu_lxc.PermitRootLogin == "no"
|
||||
- sshd_profiles.ubuntu_vm.PermitRootLogin == "no"
|
||||
quiet: true
|
||||
# Proxmox cluster SSH needs RSA with SHA-2, never SHA-1 ssh-rsa; the others stay ed25519 only.
|
||||
- name: Assert the key algorithms per profile
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- sshd_profiles.pve.HostKeyAlgorithms == "ssh-ed25519,rsa-sha2-512,rsa-sha2-256"
|
||||
- sshd_profiles.pve.PubkeyAcceptedAlgorithms == "ssh-ed25519,rsa-sha2-512,rsa-sha2-256"
|
||||
- sshd_settings_defaults.HostKeyAlgorithms == "ssh-ed25519"
|
||||
- sshd_settings_defaults.PubkeyAcceptedAlgorithms == "ssh-ed25519"
|
||||
- "'HostKeyAlgorithms' not in sshd_profiles.ubuntu_lxc"
|
||||
- "'HostKeyAlgorithms' not in sshd_profiles.ubuntu_vm"
|
||||
quiet: true
|
||||
|
||||
# Play vars outrank the role defaults, so the override reaches the merged settings.
|
||||
- name: A consumer override wins and keeps the rest
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
sshd_profile: ubuntu_vm
|
||||
sshd_settings:
|
||||
MaxAuthTries: "5"
|
||||
tasks:
|
||||
- name: Load the role defaults
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.sshd
|
||||
tasks_from: validate.yml
|
||||
public: true
|
||||
- name: Assert the override
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- sshd_settings_effective.MaxAuthTries == "5"
|
||||
- sshd_settings_effective.PermitRootLogin == "no"
|
||||
- sshd_settings_effective.PubkeyAuthentication == "yes"
|
||||
quiet: true
|
||||
|
||||
- name: The lock-out guard refuses a lock-out
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
sshd_profile: ubuntu_lxc
|
||||
sshd_settings:
|
||||
PubkeyAuthentication: "no"
|
||||
tasks:
|
||||
- name: Apply a lock-out configuration
|
||||
block:
|
||||
- name: Include the validation
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.sshd
|
||||
tasks_from: validate.yml
|
||||
- name: Fail when the lock-out was accepted
|
||||
ansible.builtin.fail:
|
||||
msg: the sshd role accepted a lock-out configuration
|
||||
rescue:
|
||||
- name: The lock-out guard stopped the play
|
||||
ansible.builtin.assert:
|
||||
that: "'lock you out' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
|
||||
- name: The Proxmox guard accepts every value that allows root key login
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Validate the pve profile with each accepted value
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.sshd
|
||||
tasks_from: validate.yml
|
||||
vars:
|
||||
sshd_profile: pve
|
||||
sshd_settings:
|
||||
PermitRootLogin: "{{ item }}"
|
||||
loop: [prohibit-password, without-password, "yes", Prohibit-Password]
|
||||
|
||||
- name: The Proxmox guard refuses a pve profile without root login
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
sshd_profile: pve
|
||||
sshd_settings:
|
||||
PermitRootLogin: "no"
|
||||
tasks:
|
||||
- name: Apply a pve configuration without root login
|
||||
block:
|
||||
- name: Include the validation
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.sshd
|
||||
tasks_from: validate.yml
|
||||
- name: Fail when the Proxmox guard was bypassed
|
||||
ansible.builtin.fail:
|
||||
msg: the sshd role accepted a pve profile without root login
|
||||
rescue:
|
||||
- name: The Proxmox guard stopped the play
|
||||
ansible.builtin.assert:
|
||||
that: "'needs root public-key login' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
|
||||
- name: The Proxmox guard refuses forced-commands-only
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
sshd_profile: pve
|
||||
sshd_settings:
|
||||
PermitRootLogin: forced-commands-only
|
||||
tasks:
|
||||
- name: Apply a pve configuration with forced commands only
|
||||
block:
|
||||
- name: Include the validation
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.sshd
|
||||
tasks_from: validate.yml
|
||||
- name: Fail when the Proxmox guard was bypassed
|
||||
ansible.builtin.fail:
|
||||
msg: the sshd role accepted forced-commands-only for the pve profile
|
||||
rescue:
|
||||
- name: The Proxmox guard stopped the play
|
||||
ansible.builtin.assert:
|
||||
that: "'needs root public-key login' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
|
||||
- name: The Proxmox guard refuses a pve profile without RSA keys
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
sshd_profile: pve
|
||||
sshd_settings:
|
||||
PubkeyAcceptedAlgorithms: "ssh-ed25519"
|
||||
tasks:
|
||||
- name: Apply a pve configuration without RSA keys
|
||||
block:
|
||||
- name: Include the validation
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.sshd
|
||||
tasks_from: validate.yml
|
||||
- name: Fail when the RSA guard was bypassed
|
||||
ansible.builtin.fail:
|
||||
msg: the sshd role accepted a pve profile without RSA keys
|
||||
rescue:
|
||||
- name: The RSA guard stopped the play
|
||||
ansible.builtin.assert:
|
||||
that: "'must both include rsa-sha2-512' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
|
||||
- name: The guard refuses SHA-1 ssh-rsa
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
sshd_profile: pve
|
||||
sshd_settings:
|
||||
HostKeyAlgorithms: "ssh-ed25519, rsa-sha2-512, ssh-rsa"
|
||||
tasks:
|
||||
- name: Apply a configuration that allows ssh-rsa
|
||||
block:
|
||||
- name: Include the validation
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.sshd
|
||||
tasks_from: validate.yml
|
||||
- name: Fail when the ssh-rsa guard was bypassed
|
||||
ansible.builtin.fail:
|
||||
msg: the sshd role accepted ssh-rsa
|
||||
rescue:
|
||||
- name: The ssh-rsa guard stopped the play
|
||||
ansible.builtin.assert:
|
||||
that: "'must not list ssh-rsa' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
|
||||
- name: An unknown profile is refused
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
sshd_profile: not_a_profile
|
||||
tasks:
|
||||
- name: Apply the role with an unknown profile
|
||||
block:
|
||||
- name: Include the validation
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.sshd
|
||||
tasks_from: validate.yml
|
||||
- name: Fail when the unknown profile was accepted
|
||||
ansible.builtin.fail:
|
||||
msg: the sshd role accepted an unknown profile
|
||||
rescue:
|
||||
- name: The profile check stopped the play
|
||||
ansible.builtin.assert:
|
||||
that: "'is invalid' in ansible_failed_result.msg | default('')"
|
||||
quiet: true
|
||||
@@ -0,0 +1,44 @@
|
||||
---
|
||||
# Runs on the repository's own runner (label kamino-molecule), in the molecule-ubuntu2404 image
|
||||
# (MOLECULE_IMAGE, the pin in .gitea/workflows/molecule.yml), which ships openssh-server. 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: sshd-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
|
||||
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,91 @@
|
||||
---
|
||||
# Lock-out signals only: the drop-in is in place, the daemon accepts the config and
|
||||
# runs, and the effective settings keep key authentication on and passwords and
|
||||
# root login off. The remaining hardening values restate the role's own defaults
|
||||
# and would only break on a policy change.
|
||||
- name: Verify
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Stat the drop-in
|
||||
ansible.builtin.stat:
|
||||
path: /etc/ssh/sshd_config.d/99-ansible-hardening.conf
|
||||
register: sshd_verify_dropin_stat
|
||||
|
||||
- name: The drop-in exists
|
||||
ansible.builtin.assert:
|
||||
that: sshd_verify_dropin_stat.stat.exists
|
||||
quiet: true
|
||||
|
||||
- name: Test the configuration sshd will load
|
||||
ansible.builtin.command: sshd -t
|
||||
changed_when: false
|
||||
|
||||
- name: Ask systemd about the ssh unit
|
||||
ansible.builtin.systemd:
|
||||
name: ssh
|
||||
register: sshd_verify_unit
|
||||
check_mode: true
|
||||
|
||||
- name: The ssh unit is active
|
||||
ansible.builtin.assert:
|
||||
that: sshd_verify_unit.status.ActiveState == "active"
|
||||
quiet: true
|
||||
|
||||
- name: Dump the effective configuration
|
||||
ansible.builtin.command: sshd -T
|
||||
register: sshd_verify_effective
|
||||
changed_when: false
|
||||
|
||||
- name: Key login stays on, password and root login stay off
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- sshd_verify_effective.stdout_lines is contains('pubkeyauthentication yes')
|
||||
- sshd_verify_effective.stdout_lines is contains('passwordauthentication no')
|
||||
- sshd_verify_effective.stdout_lines is contains('permitrootlogin no')
|
||||
quiet: true
|
||||
|
||||
# The pve profile cannot be converged here (it is not an LXC), so render its drop-in
|
||||
# to a scratch file and let sshd parse it: proves sshd accepts the value and the
|
||||
# profile's algorithm lists, and what it resolves them to.
|
||||
- name: Verify the pve profile parses
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
vars:
|
||||
sshd_profile: pve
|
||||
tasks:
|
||||
- name: Load the role defaults for the pve profile
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.sshd
|
||||
tasks_from: validate.yml
|
||||
public: true
|
||||
|
||||
- name: Create a scratch file for the pve drop-in
|
||||
ansible.builtin.tempfile:
|
||||
suffix: .conf
|
||||
register: sshd_verify_pve_file
|
||||
|
||||
- name: Render the pve drop-in
|
||||
ansible.builtin.copy:
|
||||
content: "{{ lookup('ansible.builtin.template', playbook_dir ~ '/../../templates/99-ansible-hardening.conf.j2') }}"
|
||||
dest: "{{ sshd_verify_pve_file.path }}"
|
||||
mode: "0600"
|
||||
|
||||
- name: Dump the effective pve configuration
|
||||
ansible.builtin.command: sshd -T -f {{ sshd_verify_pve_file.path }}
|
||||
register: sshd_verify_pve_effective
|
||||
changed_when: false
|
||||
|
||||
- name: Root logs in with keys only, passwords stay off
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
# sshd -T prints the first alias in its table for prohibit-password: without-password.
|
||||
- sshd_verify_pve_effective.stdout_lines is contains('permitrootlogin without-password')
|
||||
- sshd_verify_pve_effective.stdout_lines is contains('passwordauthentication no')
|
||||
- sshd_verify_pve_effective.stdout_lines is contains('pubkeyauthentication yes')
|
||||
quiet: true
|
||||
|
||||
- name: Remove the scratch file
|
||||
ansible.builtin.file:
|
||||
path: "{{ sshd_verify_pve_file.path }}"
|
||||
state: absent
|
||||
@@ -0,0 +1,16 @@
|
||||
---
|
||||
- name: Ensure sshd config directory exists
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ sshd_config_dir }}"
|
||||
state: directory
|
||||
mode: "0755"
|
||||
|
||||
- name: Install sshd hardening config
|
||||
become: true
|
||||
ansible.builtin.template:
|
||||
src: 99-ansible-hardening.conf.j2
|
||||
dest: "{{ sshd_config_dir }}/{{ sshd_dropin_file }}"
|
||||
mode: "0644"
|
||||
validate: sshd -t -f %s
|
||||
notify: Restart SSH
|
||||
@@ -0,0 +1,5 @@
|
||||
---
|
||||
- name: Ensure SSH host keys exist
|
||||
become: true
|
||||
ansible.builtin.command: ssh-keygen -A
|
||||
changed_when: false
|
||||
@@ -0,0 +1,18 @@
|
||||
---
|
||||
- name: Pre-flight validation
|
||||
ansible.builtin.include_tasks: validate.yml
|
||||
|
||||
- name: Manage SSH host keys
|
||||
ansible.builtin.include_tasks: keys.yml
|
||||
|
||||
- name: Configure sshd
|
||||
ansible.builtin.include_tasks: config.yml
|
||||
|
||||
# In case the user was bootstrapped to root
|
||||
- name: Ensure ansible_user is used for SSH
|
||||
# Connection var on purpose: restores the SSH user when the connection was bootstrapped as root.
|
||||
ansible.builtin.set_fact: # noqa: var-naming[no-role-prefix]
|
||||
ansible_ssh_user: "{{ ansible_user }}"
|
||||
|
||||
- name: Flush handlers early
|
||||
ansible.builtin.meta: flush_handlers
|
||||
@@ -0,0 +1,62 @@
|
||||
---
|
||||
- name: Fail if sshd_profile is not set
|
||||
ansible.builtin.fail:
|
||||
msg: "sshd_profile must be set (pve, ubuntu_lxc, ubuntu_vm)"
|
||||
when: sshd_profile is not defined
|
||||
|
||||
- name: Fail if sshd_profile is unknown
|
||||
ansible.builtin.fail:
|
||||
msg: "sshd_profile '{{ sshd_profile }}' is invalid. Must be: {{ sshd_profiles.keys() | sort | join(', ') }}."
|
||||
when: sshd_profile not in sshd_profiles
|
||||
|
||||
- name: Fail if no sshd settings are defined
|
||||
ansible.builtin.fail:
|
||||
msg: "No sshd settings resolved after merging defaults and overrides."
|
||||
when: sshd_settings_effective | length == 0
|
||||
|
||||
- name: Fail if SSH would be locked out
|
||||
ansible.builtin.fail:
|
||||
msg: >
|
||||
SSH hardening would lock you out.
|
||||
PasswordAuthentication is disabled and PubkeyAuthentication is not enabled.
|
||||
when:
|
||||
- sshd_settings_effective.PasswordAuthentication | default("yes") == "no"
|
||||
- sshd_settings_effective.PubkeyAuthentication | default("no") != "yes"
|
||||
|
||||
- name: Fail if root key login is disabled on Proxmox
|
||||
ansible.builtin.fail:
|
||||
msg: >
|
||||
Proxmox cluster SSH needs root public-key login. PermitRootLogin must be
|
||||
prohibit-password, without-password or yes, not
|
||||
'{{ sshd_settings_effective.PermitRootLogin }}'.
|
||||
# forced-commands-only is refused too: the cluster's tunnels run arbitrary commands
|
||||
# with keys that carry no command= option. An unset value keeps the OpenSSH default,
|
||||
# prohibit-password. sshd matches the value case-insensitively, so the guard does too;
|
||||
# a YAML boolean (unquoted yes) becomes "true" and is refused, as sshd would refuse it.
|
||||
when:
|
||||
- sshd_profile == "pve"
|
||||
- >-
|
||||
sshd_settings_effective.PermitRootLogin | default("prohibit-password") | string | lower
|
||||
not in ["prohibit-password", "without-password", "yes"]
|
||||
|
||||
- name: Fail if RSA keys are refused on Proxmox
|
||||
ansible.builtin.fail:
|
||||
msg: >
|
||||
Proxmox cluster SSH pins RSA host keys and logs in with root's RSA key.
|
||||
HostKeyAlgorithms and PubkeyAcceptedAlgorithms must both include rsa-sha2-512.
|
||||
# An unset directive keeps the OpenSSH default, which includes rsa-sha2-512. The
|
||||
# +/-/^ list forms are not parsed, so they fail here: write the full list.
|
||||
when:
|
||||
- sshd_profile == "pve"
|
||||
- >-
|
||||
'rsa-sha2-512' not in (sshd_settings_effective.HostKeyAlgorithms | default('rsa-sha2-512') | split(',') | map('trim'))
|
||||
or 'rsa-sha2-512' not in (sshd_settings_effective.PubkeyAcceptedAlgorithms | default('rsa-sha2-512') | split(',') | map('trim'))
|
||||
|
||||
- name: Fail if SHA-1 RSA (ssh-rsa) is allowed
|
||||
ansible.builtin.fail:
|
||||
msg: >
|
||||
HostKeyAlgorithms and PubkeyAcceptedAlgorithms must not list ssh-rsa (RSA with SHA-1);
|
||||
use rsa-sha2-512/rsa-sha2-256.
|
||||
when: >-
|
||||
'ssh-rsa' in (sshd_settings_effective.HostKeyAlgorithms | default('') | split(',') | map('trim'))
|
||||
or 'ssh-rsa' in (sshd_settings_effective.PubkeyAcceptedAlgorithms | default('') | split(',') | map('trim'))
|
||||
@@ -0,0 +1,5 @@
|
||||
# Managed by Ansible – DO NOT EDIT
|
||||
|
||||
{% for key, value in sshd_settings_effective.items() | sort %}
|
||||
{{ key }} {{ value | quote }}
|
||||
{% endfor %}
|
||||
Reference in new issue
Block a user