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

39 lines
1.8 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 (router) 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. For the administrator of
.lm-dev-config/vm-PROJECT-VM.json, the key is made from the file's otp_url when it has one, and a generated key's
otpauth URI is kept there as otp_url. lm-dev-otp-code.py prints the user's codes.
"""
import argparse
import sys
from lm_dev import PROJECT, VM, 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=VM, help=f'the name of the VM (default {VM})')
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()