Skip to content

Commit a8e95ad

Browse files
authored
Merge pull request #157 from opencrvs/ocrvs-10896-validate
Verify passphrase before provision new one
2 parents 0549064 + e4d2f44 commit a8e95ad

File tree

7 files changed

+80
-88
lines changed

7 files changed

+80
-88
lines changed

.github/workflows/provision.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ on:
2323
- application
2424
- tools
2525
- fail2ban
26+
- data-partition
2627
- decrypt-on-boot
2728
- checks
2829
- containerd-setup

infrastructure/server-setup/group_vars/all.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,21 @@
77
#
88
# Copyright (C) The OpenCRVS Authors located at https://github.com/opencrvs/opencrvs-core/blob/master/AUTHORS.
99
ansible_python_interpreter: /usr/bin/python3
10-
encrypt_data: False
1110

12-
backup_server_user: 'backup'
13-
backup_server_user_home: '/home/backup'
1411
crontab_user: root
1512
provisioning_user: provision
1613
kubernetes_version: "v1.33"
1714
pod_network_cidr: "192.168.0.0/16"
1815
calico_version: "v3.26.1"
16+
17+
# Kubernetes secret in opencrvs-deps-<env name> namespace
1918
backup_server_ssh_credentials_secret: "backup-server-ssh-credentials"
20-
backup_ssh_key_path: /home/backup/.ssh/id_ed25519
19+
# Path on backup server where to private key is stored
20+
backup_ssh_key_path: /home/backup/.ssh/id_ed25519
21+
# User for ssh connection to backup server
22+
backup_server_user: 'backup'
23+
# Home directory for user on backup server
24+
backup_server_user_home: '/home/backup'
25+
26+
# Disk Encryption key location as an example (in production use a hardware security module)
27+
disk_encryption_key_path: /root/disk-encryption-key.txt

infrastructure/server-setup/k8s.yml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,23 @@
2020
- data-partition
2121
tags:
2222
- data-partition
23-
23+
- include_tasks:
24+
file: tasks/k8s/validate-data-partition.yml
25+
apply:
26+
tags:
27+
- data-partition
28+
- decrypt-on-boot
29+
tags:
30+
- data-partition
31+
- decrypt-on-boot
2432
- include_tasks:
2533
file: tasks/k8s/decrypt-on-boot.yml
2634
apply:
2735
tags:
2836
- decrypt-on-boot
2937
tags:
3038
- decrypt-on-boot
39+
3140
- name: Include system preparation tasks
3241
include_tasks: tasks/k8s/system-preparation.yml
3342
tags:

infrastructure/server-setup/tasks/backups/crontab.yml

Lines changed: 0 additions & 58 deletions
This file was deleted.

infrastructure/server-setup/tasks/k8s/data-partition.yml

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,6 @@
173173
mode: ugo+rwx
174174
when: not minio_data_backup.stat.exists
175175

176-
- name: Check wireguard data backup directory
177-
stat:
178-
path: /data/wireguard
179-
register: wireguard_data
180-
181-
- name: 'Create wireguard backup directory'
182-
file:
183-
path: /data/wireguard
184-
state: directory
185-
group: 1000
186-
owner: 1000
187-
mode: ugo+rwx
188-
when: not wireguard_data.stat.exists
189-
190176
- name: Check vsexport data directory
191177
stat:
192178
path: /data/vsexport
@@ -215,14 +201,6 @@
215201
mode: ugo+rwx
216202
when: not vsexport_data_backup.stat.exists
217203

218-
- name: 'Create secrets directory'
219-
file:
220-
path: /data/secrets
221-
state: directory
222-
group: 1000
223-
owner: 1000
224-
mode: g+rwx
225-
226204
- name: 'Create acme file for traefik'
227205
file:
228206
path: /data/traefik/acme.json

infrastructure/server-setup/tasks/k8s/decrypt-on-boot.yml

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,38 @@
44
state: present
55
when: disk_encryption_key is defined
66

7-
- name: Save disk encryption key into a file as an example (in production use a hardware security module)
7+
- name: 'Register encrypted file system'
8+
stat:
9+
path: /cryptfs_file_sparse.img
10+
get_checksum: False
11+
register: encryptedFileSystemPostCheck
12+
when: disk_encryption_key is defined
13+
14+
- name: Check if Disk encryption key file exists
15+
ansible.builtin.stat:
16+
path: "{{ disk_encryption_key_path }}"
17+
register: encryption_keyfile_stat
18+
when: disk_encryption_key is defined
19+
20+
- name: Read existing Disk encryption key
21+
ansible.builtin.slurp:
22+
src: "{{ disk_encryption_key_path }}"
23+
register: existing_disk_encryption_key
24+
when:
25+
- disk_encryption_key is defined
26+
- encryption_keyfile_stat.stat.exists
27+
28+
- name: Fail if key exists and differs
29+
ansible.builtin.fail:
30+
msg: "Disk encryption key on disk does not match ansible variable! GitHub secret ENCRYPTION_KEY was updated!"
31+
when:
32+
- encryption_keyfile_stat.stat.exists
33+
- existing_disk_encryption_key['content'] | b64decode != ("DISK_ENCRYPTION_KEY=" ~ disk_encryption_key ~ "\n")
34+
35+
- name: Save disk encryption key into a file
836
when: disk_encryption_key is defined
937
ansible.builtin.copy:
10-
dest: /root/disk-encryption-key.txt
38+
dest: "{{ disk_encryption_key_path }}"
1139
group: 1000
1240
owner: 1000
1341
mode: g+rwx
@@ -42,7 +70,7 @@
4270
Description=Mount encrypted dir
4371
4472
[Service]
45-
ExecStart=bash /opt/opencrvs/scripts/cryptfs/decrypt.sh -key /root/disk-encryption-key.txt >> /var/log/cryptfs-reboot.log 2>&1
73+
ExecStart=bash /opt/opencrvs/scripts/cryptfs/decrypt.sh -key {{ disk_encryption_key_path }} >> /var/log/cryptfs-reboot.log 2>&1
4674
4775
[Install]
4876
WantedBy=multi-user.target
@@ -51,6 +79,7 @@
5179
- encryptedFileSystemPostCheck.stat.exists
5280

5381
- name: 'Setup systemd to mount encrypted folder'
82+
when: disk_encryption_key is defined
5483
shell: systemctl daemon-reload && systemctl enable reboot.service
5584
when:
5685
- disk_encryption_key is defined
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
- name: Verify LUKS disk encryption key
2+
when: disk_encryption_key is defined
3+
block:
4+
- name: Backup LUKS header
5+
shell: |
6+
loop_device=$(losetup -j /cryptfs_file_sparse.img | cut -d: -f1)
7+
rm -vf /tmp/luks-header.img
8+
cryptsetup luksHeaderBackup $loop_device --header-backup-file /tmp/luks-header.img
9+
10+
- name: Test disk encryption key for LUKS header
11+
when: disk_encryption_key is defined
12+
shell: 'echo "{{ disk_encryption_key }}" | cryptsetup -d - luksOpen /tmp/luks-header.img test --test-passphrase'
13+
register: luks_test
14+
15+
- name: Display success message
16+
debug:
17+
msg: "✓ LUKS passphrase test successful - the disk encryption key is valid: {{ luks_test.stdout }}"
18+
rescue:
19+
- name: DO NOT REBOOT the SERVER
20+
fail:
21+
msg: |
22+
{% if luks_test.rc == 2 and 'No key available with this passphrase' in luks_test.stderr -%}
23+
LUKS passphrase test failed: Please make sure your key in GitHub secret is correct. Otherwise system will not be able to decrypt /data. MAKE SURE DATA BACKUP IS RESTORABLE BEFORE SERVER REBOOT.
24+
{%- else -%}
25+
cryptsetup error (RC: {{ luks_test.rc }}) - {{ luks_test.stderr | default('Unknown error') }}
26+
{%- endif %}

0 commit comments

Comments
 (0)