Files
linux-cloud-router/bin/lm-dev-otp-register.py
T

63 lines
3.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""Register a TOTP token for a user of a Linux Manager VM on an LXD remote: lm-dev-otp-register.py.
The token is enrolled for --user (admin by default) of the VM --vm (lm-dev) in the project --project
(LXD_PROJECT, lm-dev by default) on the remote --remote (LXD_REMOTE), through lxc exec: inside the
VM, as root, linuxmanager_users makes a new secret, and stores it once a code computed here from it
verifies, replacing a token the user had. The secret, base32, is then written to the file
--secret-file or OTP_FILE names, readable by its owner only, so otp.py code prints the user's codes.
"""
import argparse
import json
import os
import pathlib
import sys
from lm_dev import PROJECT, lxc_exec
from otp import code_of
# Run in the VM as python3 -c CODE ARGUMENTS..., with the package's modules on the path.
NEW = ("import json, sys; sys.path.insert(0, '/usr/lib/linux-manager'); import linuxmanager_users as users\n"
"with users.session() as connection: print(json.dumps(users.otp_new(connection, sys.argv[1])))")
ENROL = ("import sys; sys.path.insert(0, '/usr/lib/linux-manager'); import linuxmanager_users as users\n"
"with users.session() as connection: users.otp_enrol(connection, sys.argv[1], sys.argv[2], sys.argv[3])")
def main():
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument('--remote', default=os.environ.get('LXD_REMOTE'), help='the lxc remote (LXD_REMOTE)')
parser.add_argument('--project', default=os.environ.get('LXD_PROJECT', PROJECT),
help=f'the project of the VM (LXD_PROJECT, default {PROJECT})')
parser.add_argument('--vm', default='lm-dev', help='the name of the VM (default lm-dev)')
parser.add_argument('--user', default='admin', help='the user to enrol (default admin)')
parser.add_argument('--secret-file', default=os.environ.get('OTP_FILE'),
help='the file the base32 secret is written to (OTP_FILE)')
args = parser.parse_args()
if not args.remote:
parser.error('--remote or LXD_REMOTE is required')
if not args.secret_file:
parser.error('--secret-file or OTP_FILE is required')
secret_file = pathlib.Path(args.secret_file)
def failure(result):
return (result.stderr.strip().splitlines() or ['lxc exec failed'])[-1]
created = lxc_exec(args.remote, args.project, args.vm, 'python3', '-c', NEW, args.user)
if created.returncode != 0:
sys.exit(f'no new secret for {args.user}: {failure(created)}')
secret = json.loads(created.stdout)['secret']
enrolled = lxc_exec(args.remote, args.project, args.vm, 'python3', '-c', ENROL, args.user, secret, code_of(secret))
if enrolled.returncode != 0:
sys.exit(f'{args.user} was not enrolled: {failure(enrolled)}')
secret_file.parent.mkdir(parents=True, exist_ok=True)
secret_file.touch(mode=0o600)
secret_file.chmod(0o600)
secret_file.write_text(f'{secret}\n', encoding='utf-8')
print(f'{args.user} of {args.remote}:{args.vm} has a new TOTP token; its secret is in {secret_file}')
if __name__ == '__main__':
main()