Skip to content

Commit 943c507

Browse files
committed
Reworking codesigning service to be mostly python.
RE:natcap#1580
1 parent 673e16d commit 943c507

File tree

3 files changed

+82
-25
lines changed

3 files changed

+82
-25
lines changed

codesigning/signing-worker/natcap-codesign.py

100644100755
+80-24
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,88 @@
11
#!/usr/bin/env python3
22

3+
import logging
4+
import os
5+
import shutil
6+
import subprocess
37
import sys
48
import textwrap
9+
import time
510

611
import pexpect # apt install python3-pexpect
12+
import requests # apt install python3-requests
713

14+
LOGGER = logging.getLogger(__name__)
15+
logging.basicConfig(level=logging.INFO)
816
CERTIFICATE = sys.argv[1]
9-
FILETOSIGN = sys.argv[2]
10-
SIGNED = sys.argv[3]
11-
12-
SIGNCODE_COMMAND = textwrap.dedent(f"""\
13-
osslsigncode sign \
14-
-pkcs11engine /usr/lib/aarch64-linux-gnu/engines-3/pkcs11.so \
15-
-pkcs11module /usr/lib/aarch64-linux-gnu/libykcs11.so.2 \
16-
-key "pkcs11:id=%02;type=private" \
17-
-certs {CERTIFICATE} \
18-
-h sha256 \
19-
-ts http://timestamp.sectigo.com \
20-
-readpass pass.txt \
21-
-verbose \
22-
-in {FILETOSIGN} \
23-
-out {SIGNED}""")
24-
25-
26-
process = pexpect.spawnu(SIGNCODE_COMMAND)
27-
process.expect('Enter PKCS#11 key PIN for Private key for Digital Signature:')
28-
with open('pass.txt') as passfile:
29-
process.sendline(passfile.read().strip())
30-
31-
# print remainder of program output for our logging.
32-
print(process.read())
17+
18+
19+
def get_from_queue(url):
20+
response = requests.get(
21+
"https://us-west1-natcap-servers.cloudfunctions.net/codesigning-queue",
22+
data={"token": str(os.environ['ACCESS_TOKEN'])})
23+
if response.status_code == 204:
24+
return None
25+
else:
26+
return response.json()
27+
28+
29+
# See https://stackoverflow.com/a/16696317
30+
def download_file(url):
31+
local_filename = url.split('/')[-1]
32+
with requests.get(url, stream=True) as r:
33+
r.raise_for_status()
34+
with open(local_filename, 'wb') as f:
35+
for chunk in r.iter_content(chunk_size=8192):
36+
f.write(chunk)
37+
return local_filename
38+
39+
40+
def upload_to_bucket(filename, path_on_bucket):
41+
subprocess.run(['gsutil', 'cp', filename, path_on_bucket], check=True)
42+
43+
44+
def sign_file(file_to_sign):
45+
signed_file = f"{file_to_sign}.signed"
46+
47+
signcode_command = textwrap.dedent(f"""\
48+
osslsigncode sign \
49+
-pkcs11engine /usr/lib/aarch64-linux-gnu/engines-3/pkcs11.so \
50+
-pkcs11module /usr/lib/aarch64-linux-gnu/libykcs11.so.2 \
51+
-key "pkcs11:id=%02;type=private" \
52+
-certs {CERTIFICATE} \
53+
-h sha256 \
54+
-ts http://timestamp.sectigo.com \
55+
-readpass pass.txt \
56+
-verbose \
57+
-in {file_to_sign} \
58+
-out {signed_file}""")
59+
60+
process = pexpect.spawnu(signcode_command)
61+
process.expect('Enter PKCS#11 key PIN for Private key for Digital Signature:')
62+
with open('pass.txt') as passfile:
63+
process.sendline(passfile.read().strip())
64+
65+
# print remainder of program output for our logging.
66+
print(process.read())
67+
68+
shutil.move(signed_file, file_to_sign)
69+
70+
71+
def main():
72+
while True:
73+
try:
74+
file_to_sign = get_from_queue()
75+
if file_to_sign is None:
76+
LOGGER.info('No items in the queue')
77+
else:
78+
filename = download_file(file_to_sign['https-url'])
79+
sign_file(filename)
80+
upload_to_bucket(filename, file_to_sign['gs-uri'])
81+
os.remove(filename)
82+
except Exception:
83+
LOGGER.exception("Unexpected error signing file")
84+
time.sleep(15)
85+
86+
87+
if __name__ == '__main__':
88+
main()

codesigning/signing-worker/natcap-codesign.service

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@ WorkingDirectory=/tmp
3030
# Run in the foreground
3131
Type=simple
3232
Restart=always
33-
ExecStart=/opt/natcap-codesign/natcap-codesign.sh
33+
ExecStart=python3 /opt/natcap-codesign/natcap-codesign.py

codesigning/signing-worker/playbook.yml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
pkg:
3232
- python3
3333
- python3-pexpect
34+
- python3-requests
3435
- wget
3536
- vim-nox
3637
- yubico-piv-tool

0 commit comments

Comments
 (0)