An Ansible role to install a signed host SSH certificate and enable user SSH certificates

Alternative Manual Process

For the Nextcloud Deployment Automation series this role configures OpenSSH server to allow the ansible user to connect using an SSH User Certificate. If you just want to follow this series in your own Geeklab all you need to do is:

  1. Create an SSH key pair for you Ansible control host
  2. Put your private key in the control directory
  3. Update the nextcloud.sh script
  4. When you create the ansible user on the target VM (host-adduser) add the public key to its ~/.ssh/authorized_keys file and then confirm you can login without a password prompt using the SSH key

And you only need this for [playbook 2] onwards as Playbook 1 - First Contact uses password authentication.

If you really want to configure this entirely by hand then the tasks/main.yaml will give you everything you need to know apart from how to create your SSH Certificate Authorities.

Ansible Role: host-ssh-server

Note: This is one of my larger roles, and it's taking me a little while to document fully.

It performs several steps in one go, although it requires proper setup (see Playbook Preparation) as it needs the host signing key to be placed in the control directory and the password to that key in an Ansible vault which in turn needs a credential from the control directory.

At the end it also sends an email notification to an admin that a host certifcate has been generated and the expiry date so it can be added into a certificate register which is of course checked periodically so the certificates can be renewed in good time to save embarrassment.

 

File Structure

host-ssh-server
   defaults
      main.yaml
   files
      user-ca.pub
      sshd-35-mycerts.conf
   handlers
      main.yaml
meta
      main.yaml
   tasks
      main.yaml
   templates
      banner.txt.j2
      mail-notify.j2
      sshd-30-house.conf.j2

 

defaults/main.yaml

Set defaults for the parameters, most should be set for your environment though.

--- # Ansible Role Defaults

ssh_hostca_key: ./secrets/host-ca
ssh_hostca_cred: "Password-Should-Be-In-A-Vault"
ssh_hostcert_id: "{{ inventory_hostname_short }}-{{ ansible_date_time.year }}"
ssh_hostcert_hostnames: "{{ inventory_hostname }},{{ inventory_hostname_short }}"
ssh_hostcert_expiry: "+104w"
ssh_userca_cert: "user-ca.pub"
ssh_flush_handlers: true

...

files/user-ca.pub

This file should contain public key of your SSH User CA. e.g

ssh-ed25519 AAAA-a-lot-more-key-text-here My User CA

files/sshd-35-housecerts.conf

This is a placeholder file we add to later

# Include file for sshd config
# List of signed HostCertificates (generated)

handlers/main.yaml

If we make any changes to the OpenSSH config on the target host then we want to restart sshd so they take effect. And since we're using SSH Host Certificates we don't need to keep track of systems in the known_hosts file on our Ansible control node so we nicely remove them too.

--- # Ansible Role Handlers

- name: Restart SSHd
  become: true
  ansible.builtin.systemd:
    name: sshd
    state: restarted

- name: Clean control SSH knownhosts file
  delegate_to: localhost
  ansible.builtin.known_hosts:
    name: "{{ inventory_hostname }}"
    state: absent
...

meta/main.yaml

--- # Meta for role host-ssh-srv
galaxy_info:
  #role_name: host-ssh-srv
  author: Richard Neighbour
  description: Configure SSH server with signed host key and User CA.
  license: GPLv3
  min_ansible_version: "2.9"

  galaxy_tags: []

dependencies: []

...

tasks/main.yaml

Documentation in progress

--- # Ansible Role Tasks
# Ubuntu 20.04 LTS
# Role for new host setup: Configure SSH host certficate and user CA


- name: Drop banner file
  become: true
  ansible.builtin.template:
    src: banner.txt.j2
    dest: /etc/ssh/banner.txt
    owner: root
    group: root
    mode: '0644'


- name: Drop UserCA public key
  become: true
  ansible.builtin.copy:
    src: "{{ ssh_userca_cert }}"
    dest: "/etc/ssh/{{ ssh_userca_cert }}"
    owner: root
    group: root
    mode: '0644'

 

- name: Drop SSH main config.d file
  become: true
  ansible.builtin.template:
    src: sshd-30-house.conf.j2
    dest: /etc/ssh/sshd_config.d/30-house.conf
    owner: root
    group: root
    mode: '0644'
  notify: Clean control SSH knownhosts file


- name: Drop SSH empty certs config.d file
  become: true
  ansible.builtin.copy:
    src: sshd-35-housecerts.conf
    dest: /etc/ssh/sshd_config.d/35-housecerts.conf
    owner: root
    group: root
    mode: '0644'

We start by dropping some SSH config files in place. Although the SSH User CA is a file, we can have several files loaded into the role and then chose which one to deploy.

 

- name: Find weak keys
  become: true
  ansible.builtin.find:
    path: /etc/ssh
    patterns:
      - "ssh_host_dsa_*"
      - "ssh_host_ecdsa_*"
  register: find_weak_keys

- name: Delete weak keys
  become: true
  ansible.builtin.file:
    path: "{{ item['path'] }}"
    state: absent
  with_items: "{{ find_weak_keys['files'] }}"

We then delete any default DSA and ECDSA keys as there's concerns about their security. DSA is now deprecated in OpenSSH anyway. We can't spot DSA or ECDSA keys using different filenames, but on a newly deployed host this shouldn't be an issue.

 

- name: Find SSH host keys.
  become: true
  ansible.builtin.find:
    path: /etc/ssh
    pattern: ssh_host_*_key
  register: private_keys
  failed_when: "\"matched\" not in private_keys or private_keys.matched == 0"


- name: Create local temporary directory.
  delegate_to: localhost
  ansible.builtin.tempfile:
    suffix: ansible-ssh-hostca
    state: directory
  changed_when: false
  register: temporary_directory


- name: Fetch public keys.
  ansible.builtin.fetch:
    src: "{{ item.path }}.pub"
    dest: "{{ temporary_directory.path }}/public_keys/"
  with_items: "{{ private_keys.files }}"
  changed_when: false

Next we get a list of any remaining private keys on the target host and then copy back their corresponding public key back to a temporary directory on the Ansible control node. We do not copy the private keys! This is making an assumption the filename follow the usual convention too.

 

- name: Sign SSH keys.
  delegate_to: localhost
  ansible.builtin.command:
    argv:
      - ssh-keygen
      - -s
      - "{{ ssh_hostca_key }}"
      - -I
      - "{{ ssh_hostcert_id }}"
      - -h
      - -n
      - "{{ ssh_hostcert_hostnames }}"
      - -V
      - "{{ ssh_hostcert_expiry }}"
      - -P
      - "{{ ssh_hostca_cred }}"
      - "{{ temporary_directory.path }}/public_keys/{{ inventory_hostname }}{{ item.path }}.pub"
  with_items: "{{ private_keys.files }}"
  changed_when: false

We then make call to ssh-keygen to do the actual signing of any pubic keys we've retrieved, using the Host CA key and its credential passed in as parameters from the playbook.

To be continued...

 

templates/banner.txt.j2

This just creates a banner.txt file with the hostname in it to be displayed to users at login

{{ ansible_hostname }}
Authorised users only.

 

templates/mail-notify.j2

This is the text of the email message sent to the administrator (me!) whenever this role issues a new SSH Host Certificate.

Hi

Automation has prepared a new host {{ ansible_hostname }} and issued an SSH certificate to it. The details are provided below.

{% for cert in SSH_status.results %}
Certificate path: {{ cert.filename }}
Info:
{{ cert.info | to_nice_json }}

{% endfor %}

Thanks

[Automation Scripting]

 

templates/sshd-30-house.conf.j2

This config file disables the use of ecdsa host keys, trusts our User CA so users can sign in with our SSH User Certificates and displays the banner. The SSH Host Certificates we generate are added to the files/sshd-35-housecerts.conf file programatically hence it's commented out here.

# Include file for sshd

HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key

# Enable all signed host certifcates?
# HostCertificate /etc/ssh/ssh_host_ed25519_key-cert.pub

# Trust signed user keys
TrustedUserCAKeys /etc/ssh/{{ ssh_userca_cert }}

Banner /etc/ssh/banner.txt