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

38 lines
1.6 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): the project's Vault generates
the key in its TOTP engine as <vm>-<user> and hands it to the VM, where linuxmanager_users stores
it with its current code, replacing a token the user had. The secret stays in the Vault and the
VM; lm-dev-otp-code.py prints the user's codes.
"""
import argparse
import sys
from lm_dev import PROJECT, MissingSetting, enrol_otp, setting, totp_key
def main():
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument('--remote', help='the lxc remote (LXD_REMOTE)')
parser.add_argument('--project', default=setting('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)')
args = parser.parse_args()
try:
args.remote = args.remote or setting('LXD_REMOTE')
except MissingSetting as missing:
parser.error(f'--remote or {missing}')
problem = enrol_otp(args.remote, args.project, args.vm, args.user)
if problem:
sys.exit(problem)
print(f'{args.user} of {args.remote}:{args.vm} has a new TOTP token, the key {totp_key(args.vm, args.user)} '
f'of the Vault of {args.project}')
if __name__ == '__main__':
main()