Skip to content

Commit 59f0ced

Browse files
committed
feat: Verify passphrase before provision new one
1 parent 3346e7b commit 59f0ced

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Improvements
1010

1111
- Make encryption step optional [#1123](https://github.com/opencrvs/opencrvs-countryconfig/pull/1123)
12+
- Added validation for ENCRYPTION_KEY [#10896](https://github.com/opencrvs/opencrvs-core/issues/10896)
1213

1314
## 1.9.0
1415

infrastructure/server-setup/playbook.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,16 @@
114114
tags:
115115
- data-partition
116116

117+
- include_tasks:
118+
file: tasks/validate-data-partition.yml
119+
apply:
120+
tags:
121+
- data-partition
122+
- decrypt-on-boot
123+
tags:
124+
- data-partition
125+
- decrypt-on-boot
126+
117127
- include_tasks:
118128
file: tasks/swap.yml
119129
apply:

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

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
1-
- name: Save disk encryption key into a file as an example (in production use a hardware security module)
1+
- name: Ensure 'cryptsetup' utility is installed
2+
ansible.builtin.package:
3+
name: cryptsetup
4+
state: present
5+
when: disk_encryption_key is defined
6+
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: /root/disk-encryption-key.txt
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: /root/disk-encryption-key.txt
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
236
when: disk_encryption_key is defined
337
ansible.builtin.copy:
438
dest: /root/disk-encryption-key.txt
@@ -8,6 +42,23 @@
842
content: |
943
DISK_ENCRYPTION_KEY={{ disk_encryption_key }}
1044
45+
- name: Ensure destination directory exists
46+
ansible.builtin.file:
47+
path: /opt/opencrvs/scripts/cryptfs
48+
state: directory
49+
mode: '0755'
50+
owner: root
51+
group: root
52+
recurse: yes
53+
54+
- name: Install k8s-help script
55+
copy:
56+
src: ../cryptfs/decrypt.sh
57+
dest: "/opt/opencrvs/scripts/cryptfs/decrypt.sh"
58+
owner: "{{ ansible_user }}"
59+
group: 'application'
60+
mode: '0755'
61+
1162
- name: Copy reboot.service systemd file. Must decrypt disk on reboot
1263
ansible.builtin.copy:
1364
dest: /etc/systemd/system/reboot.service
@@ -19,7 +70,7 @@
1970
Description=Mount encrypted dir
2071
2172
[Service]
22-
ExecStart=bash /opt/opencrvs/infrastructure/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 /root/disk-encryption-key.txt >> /var/log/cryptfs-reboot.log 2>&1
2374
2475
[Install]
2576
WantedBy=multi-user.target
@@ -28,7 +79,6 @@
2879
- encryptedFileSystemPostCheck.stat.exists
2980

3081
- name: 'Setup systemd to mount encrypted folder'
31-
when: disk_encryption_key is defined
3282
shell: systemctl daemon-reload && systemctl enable reboot.service
3383
when:
3484
- 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)