|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# vim: set ts=4 sw=4 et: |
| 3 | +# |
| 4 | +# add_subject.py |
| 5 | +# |
| 6 | +# (c) Matthias Büchse <[email protected]> |
| 7 | +# SPDX-License-Identifier: Apache-2.0 |
| 8 | +import base64 |
| 9 | +import getpass |
| 10 | +import os |
| 11 | +import os.path |
| 12 | +import re |
| 13 | +import shutil |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | +import tempfile |
| 17 | + |
| 18 | +try: |
| 19 | + from passlib.context import CryptContext |
| 20 | + import argon2 |
| 21 | +except ImportError: |
| 22 | + print('Missing passlib and/or argon2. Please do:\npip install passlib argon2_cffi', file=sys.stderr) |
| 23 | + sys.exit(1) |
| 24 | + |
| 25 | +# see ../compliance-monitor/monitor.py |
| 26 | +CRYPTCTX = CryptContext(schemes=('argon2', 'bcrypt'), deprecated='auto') |
| 27 | +SSH_KEYGEN = shutil.which('ssh-keygen') |
| 28 | +SUBJECT_RE = re.compile(r"[a-zA-Z0-9_\-]+") |
| 29 | + |
| 30 | + |
| 31 | +def main(argv, cwd): |
| 32 | + if len(argv) != 1: |
| 33 | + raise RuntimeError("Need to supply precisely one argument: name of subject") |
| 34 | + subject = argv[0] |
| 35 | + print(f"Attempt to add subject {subject!r}") |
| 36 | + keyfile_path = os.path.join(cwd, '.secret', 'keyfile') |
| 37 | + tokenfile_path = os.path.join(cwd, '.secret', 'tokenfile') |
| 38 | + if os.path.exists(keyfile_path): |
| 39 | + raise RuntimeError(f"Keyfile {keyfile_path} already present. Please proceed manually") |
| 40 | + if os.path.exists(tokenfile_path): |
| 41 | + raise RuntimeError(f"Tokenfile {tokenfile_path} already present. Please proceed manually") |
| 42 | + if not(SUBJECT_RE.fullmatch(subject)): |
| 43 | + raise RuntimeError(f"Subject name {subject!r} using disallowed characters") |
| 44 | + sanitized_subject = subject.replace('-', '_') |
| 45 | + print("Creating API key...") |
| 46 | + while True: |
| 47 | + password = getpass.getpass("Enter passphrase: ") |
| 48 | + if password == getpass.getpass("Repeat passphrase: "): |
| 49 | + break |
| 50 | + print("No match. Try again...") |
| 51 | + token = base64.b64encode(f"{subject}:{password}".encode('utf-8')) |
| 52 | + hash_ = CRYPTCTX.hash(password) |
| 53 | + with open(tokenfile_path, "wb") as fileobj: |
| 54 | + fileobj.write(token) |
| 55 | + print("Creating key file using `ssh-keygen`...") |
| 56 | + subprocess.check_call([SSH_KEYGEN, '-t', 'ed25519', '-C', sanitized_subject, '-f', keyfile_path, '-N', '', '-q']) |
| 57 | + with open(keyfile_path + '.pub', "r") as fileobj: |
| 58 | + pubkey_components = fileobj.readline().split() |
| 59 | + print(f''' |
| 60 | +The following SECRET files have been created: |
| 61 | +
|
| 62 | + - {keyfile_path} |
| 63 | + - {tokenfile_path} |
| 64 | +
|
| 65 | +They are required for submitting test reports. You MUST keep them secure and safe. |
| 66 | +
|
| 67 | +Insert the following snippet into compliance-monitor/bootstrap.yaml: |
| 68 | +
|
| 69 | + - {subject}: |
| 70 | + api_keys: |
| 71 | + - "{hash_}" |
| 72 | + keys: |
| 73 | + - public_key: "{pubkey_components[1]}" |
| 74 | + public_key_type: "{pubkey_components[0]}" |
| 75 | + public_key_name: "primary" |
| 76 | +
|
| 77 | +Make sure to submit a pull request with the changed file. Otherwise, the reports cannot be submitted. |
| 78 | +''', end='') |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + try: |
| 83 | + sys.exit(main(sys.argv[1:], cwd=os.path.dirname(sys.argv[0]) or os.getcwd()) or 0) |
| 84 | + except RuntimeError as e: |
| 85 | + print(str(e), file=sys.stderr) |
| 86 | + sys.exit(1) |
| 87 | + except KeyboardInterrupt: |
| 88 | + print("Interrupted", file=sys.stderr) |
0 commit comments