Skip to content

Commit b93e3c7

Browse files
authored
Merge pull request #525 from stackhpc/feat/hostkey-secrets
Generate and persist hostkeys for ondemand and login nodes
2 parents 6929272 + 895f302 commit b93e3c7

File tree

7 files changed

+48
-69
lines changed

7 files changed

+48
-69
lines changed
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# persist_hostkeys
22

3-
Save hostkeys to persistent storage and restore them after a rebuild/reimage.
3+
Idempotently generates a persistent set of hostkeys and restores them after a rebuild/reimage.
44

5-
Add hosts to the `persist_hostkeys` group to enable.
6-
7-
This role has no variables but hosts in this group must have `appliances_state_dir`
8-
defined as a directory they can write to on persistent storage.
5+
Add hosts to the `persist_hostkeys` group to enable. All hosts in group will share the same set hostkeys.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
persist_hostkeys_state_server: "{{ groups['control'] | first }}"
2+
persist_hostkeys_state_dir: "{{ hostvars[persist_hostkeys_state_server]['appliances_state_dir'] }}/hostkeys"

ansible/roles/persist_hostkeys/tasks/main.yml

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
---
22

3-
- name: Ensure hostkeys directory exists on persistent storage
4-
file:
5-
path: "{{ appliances_state_dir }}/hostkeys/{{ inventory_hostname }}"
6-
state: directory
7-
owner: root
8-
group: root
9-
mode: 0600
3+
- name: Generate persistent hostkeys in state directory
4+
delegate_to: "{{ persist_hostkeys_state_server }}"
5+
block:
6+
- name: Ensure hostkeys directory exists on persistent storage
7+
file:
8+
path: "{{ persist_hostkeys_state_dir }}"
9+
state: directory
10+
owner: root
11+
group: root
12+
mode: 0600
1013

11-
- name: Copy hostkeys from persistent storage
12-
# won't fail if no keys are in persistent storage
13-
copy:
14-
src: "{{ appliances_state_dir }}/hostkeys/{{ inventory_hostname }}/"
15-
dest: /etc/ssh/
16-
remote_src: true
14+
- name: Check for existing hostkeys
15+
find:
16+
paths: "{{ persist_hostkeys_state_dir }}/"
17+
register: _files_found
18+
19+
- name: Generate hostkeys
20+
when: _files_found.matched == 0
21+
shell:
22+
# ssh-keygen -A needs a directory with an /etc/ssh suffix to write hostkeys into
23+
cmd: |
24+
mkdir -p {{ persist_hostkeys_state_dir }}/etc/ssh
25+
ssh-keygen -A -N '' -f {{ persist_hostkeys_state_dir }}
26+
mv {{ persist_hostkeys_state_dir }}/etc/ssh/* {{ persist_hostkeys_state_dir }}
27+
rm -rf {{ persist_hostkeys_state_dir }}/etc/ssh
28+
29+
- name: Get created key names
30+
find:
31+
path: "{{ persist_hostkeys_state_dir }}/"
32+
register: _find_ssh_keys
1733

18-
- name: Find hostkeys
19-
find:
20-
path: /etc/ssh/
21-
patterns: ssh_host_*_key*
22-
register: _find_ssh_keys
34+
- name: Create in-memory copies of keys
35+
ansible.builtin.slurp:
36+
src: "{{ item.path }}"
37+
loop: "{{ _find_ssh_keys.files }}"
38+
register: _slurp_keys
2339

24-
- name: Persist hostkeys
40+
- name: Copy keys to hosts
41+
no_log: true
2542
copy:
26-
dest: "{{ appliances_state_dir }}/hostkeys/{{ inventory_hostname }}/"
27-
src: "{{ item }}"
28-
remote_src: true
29-
mode: preserve
30-
loop: "{{ _find_ssh_keys.files | map(attribute='path') }}"
43+
content: "{{ item.content | b64decode }}"
44+
dest: "/etc/ssh/{{ item.source | regex_search('[^/]+$') }}"
45+
loop: "{{ _slurp_keys.results }}"
3146

3247
- meta: reset_connection
33-

docs/production.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -122,30 +122,6 @@ and referenced from the `site` and `production` environments, e.g.:
122122
- If floating IPs are required for login nodes, modify the OpenTofu configurations
123123
appropriately.
124124

125-
- Enable persisting login node hostkeys so users do not get annoying ssh warning
126-
messages on reimage:
127-
128-
```yaml
129-
# environments/site/inventory/groups:
130-
[persist_hostkeys:children]
131-
login
132-
```
133-
And configure NFS to include exporting the state directory to these hosts:
134-
135-
```yaml
136-
# environments/common/inventory/group_vars/all/nfs.yml:
137-
nfs_configurations:
138-
# ... potentially, /home defintion from common environment
139-
- comment: Export state directory to login nodes
140-
nfs_enable:
141-
server: "{{ inventory_hostname in groups['control'] }}"
142-
clients: "{{ inventory_hostname in groups['login'] }}"
143-
nfs_server: "{{ nfs_server_default }}"
144-
nfs_export: "/var/lib/state"
145-
nfs_client_mnt_point: "/var/lib/state"
146-
```
147-
See [issue 506](https://github.com/stackhpc/ansible-slurm-appliance/issues/506).
148-
149125
- Consider whether mapping of baremetal nodes to ironic nodes is required. See
150126
[PR 485](https://github.com/stackhpc/ansible-slurm-appliance/pull/485).
151127

environments/.caas/inventory/extra_groups

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,3 @@ compute
1414

1515
[podman:children]
1616
zenith
17-
18-
[persist_hostkeys:children]
19-
openondemand
Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
nfs_server: "{{ nfs_server_default }}"
22

3-
caas_nfs_ood_state:
4-
- comment: Export /var/lib/state from Slurm control node to OOD
5-
nfs_enable:
6-
server: "{{ inventory_hostname in groups['control'] }}"
7-
clients: "{{ inventory_hostname in groups['openondemand'] }}"
8-
nfs_export: "{{ appliances_state_dir }}"
9-
nfs_client_mnt_point: "{{ appliances_state_dir }}"
10-
nfs_client_mnt_options: "x-systemd.required-by=zenith-ood.service,x-systemd.before=zenith-ood.service"
11-
123
caas_nfs_home:
134
- comment: Export /exports/home from Slurm control node as /home
145
nfs_enable:
@@ -17,4 +8,4 @@ caas_nfs_home:
178
nfs_export: "/exports/home" # assumes skeleton TF is being used
189
nfs_client_mnt_point: "/home"
1910

20-
nfs_configurations: "{{ caas_nfs_ood_state + (caas_nfs_home if not cluster_home_manila_share | bool else []) }}"
11+
nfs_configurations: "{{ caas_nfs_home if not cluster_home_manila_share | bool else [] }}"

environments/common/layouts/everything

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,10 @@ openhpc
7171
[manila]
7272
# Hosts to configure for manila fileshares
7373

74-
[persist_hostkeys]
75-
# Hosts to persist hostkeys for across reimaging. NB: Requires appliances_state_dir on hosts.
74+
[persist_hostkeys:children]
75+
# Hosts to use common set of hostkeys which persist across reimaging.
76+
login
77+
openondemand
7678

7779
[squid]
7880
# Hosts to run squid proxy

0 commit comments

Comments
 (0)