-
Notifications
You must be signed in to change notification settings - Fork 0
/
automount
44 lines (35 loc) · 1.31 KB
/
automount
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python3
### BEGIN INIT INFO
# Provides: automount
# Required-Start: $ALL
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start: 2 3 5
# Default-Stop:
# Description: Automated LUKS Unlock
### END INIT INFO
# Import libraries
import subprocess
import os
from requests import get, ConnectionError, Timeout
# Change directory to the location of secretfs
os.chdir('/home/ubuntu/')
# First part: Establish a client connection and fetch key
try:
request = get("https://emp.phoenixnap.com", timeout=60)
except(ConnectionError, Timeout):
print("Connection error, retrying...")
c = client.ProxyKmipClient(config_file="<path to pykmip.conf>")
with c:
SECRET = str(c.get('<UUID of security object from EMP>'))
print("Success! Unlocking and mounting the device.")
# Second part: Automating the commands to attach, unlock and mount the device
LUKS_DEVICE = "/dev/loop101"
LUKS_DEVICE_MAP = "secretfs"
LUKS_DEVICE_MOUNT_POINT = "/mnt/encrypted"
MAPPER = '/dev/mapper/' + LUKS_DEVICE_MAP
subprocess.run(['sudo', 'losetup', LUKS_DEVICE, LUKS_DEVICE_MAP])
ps = subprocess.Popen(('echo', SECRET), stdout=subprocess.PIPE)
subprocess.check_output(('sudo', 'cryptsetup', 'luksOpen', LUKS_DEVICE, LUKS_DEVICE_MAP), stdin=ps.stdout)
subprocess.run(['sudo', 'mount' ,MAPPER ,LUKS_DEVICE_MOUNT_POINT])