update: the history of the package is CHANGELOG.md, from which the build writes debian/changelog, the build, publish and development scripts are in Python, and the development tools keep their settings and credentials in .lm-dev-config.
Release / release (push) Failing after 14m43s
Release / release (push) Failing after 14m43s
This commit is contained in:
1 parent
a7bfabcb89
commit
6911d16fa3
26 files changed
+1966
-2105
No files matched your search
+59
-16
@@ -1,5 +1,5 @@
|
||||
"""What the lm-dev scripts share: the repository's paths, lxc in an instance, the .env of the repository, the
|
||||
persistent state in .lm-dev-config, the Vault of a test project, the AppRole of each instance in it, the hand-over of
|
||||
"""What the lm-dev scripts share: the repository's paths, lxc in an instance, the persistent state and settings in
|
||||
.lm-dev-config, the Vault of a test project, the AppRole of each instance in it, the hand-over of
|
||||
secrets to an instance through it, and the TOTP tokens of the users of a VM, kept in the Vault's TOTP engine.
|
||||
|
||||
A secret never reaches an instance as a command-line argument or on standard input. Each instance has an AppRole of
|
||||
@@ -25,8 +25,10 @@ ROOT = pathlib.Path(__file__).resolve().parent.parent
|
||||
TESTS = ROOT / 'tests' / 'remote'
|
||||
# The project of the manual tests, which lm-dev-lxd-project.py makes.
|
||||
PROJECT = 'lm-dev'
|
||||
# The state of the scripts that outlives a clean-up of tmp/: project and Vault states, keys and secrets; 0700.
|
||||
# The state of the scripts that outlives a clean-up of tmp/: settings, project and Vault states, keys and secrets; 0700.
|
||||
CONFIG = ROOT / '.lm-dev-config'
|
||||
# The settings of the scripts, the defaults of their options: a JSON object of LXD_* names and their values.
|
||||
SETTINGS = CONFIG / 'settings.json'
|
||||
|
||||
# The Vault: its port, the KV v2 and TOTP mounts, the lifetimes of a wrapping token and of an instance's token.
|
||||
VAULT_PORT = 8200
|
||||
@@ -66,15 +68,42 @@ def lxc_push(remote, project, instance, source, path, mode):
|
||||
'--project', project], capture_output=True, text=True).returncode == 0
|
||||
|
||||
|
||||
def set_dotenv(values):
|
||||
"""Set the variables in the repository's .env, readable by its owner only, keeping its other lines."""
|
||||
dotenv = ROOT / '.env'
|
||||
lines = dotenv.read_text(encoding='utf-8').splitlines() if dotenv.is_file() else []
|
||||
kept = [line for line in lines if line.split('=', 1)[0] not in values]
|
||||
dotenv.touch(mode=0o600)
|
||||
dotenv.chmod(0o600)
|
||||
dotenv.write_text(''.join(f'{line}\n' for line in kept + [f'{key}={value}' for key, value in values.items()]),
|
||||
encoding='utf-8')
|
||||
class MissingSetting(Exception):
|
||||
"""A setting without a default that neither the environment nor SETTINGS has; name is the setting."""
|
||||
|
||||
def __init__(self, name):
|
||||
super().__init__(f'{name}, in the environment or {SETTINGS.relative_to(ROOT)}, is required')
|
||||
self.name = name
|
||||
|
||||
|
||||
# The default of a setting that has none.
|
||||
REQUIRED = object()
|
||||
|
||||
|
||||
def setting(name, default=REQUIRED):
|
||||
"""The value of the setting name, such as LXD_REMOTE: the environment's, else that of SETTINGS, else default;
|
||||
MissingSetting without a default."""
|
||||
if name in os.environ:
|
||||
return os.environ[name]
|
||||
try:
|
||||
values = json.loads(SETTINGS.read_text(encoding='utf-8'))
|
||||
except FileNotFoundError:
|
||||
values = {}
|
||||
except ValueError as error:
|
||||
raise SystemExit(f'{SETTINGS} is not JSON: {error}')
|
||||
if name in values:
|
||||
return values[name]
|
||||
if default is REQUIRED:
|
||||
raise MissingSetting(name)
|
||||
return default
|
||||
|
||||
|
||||
def save_setting(name, value):
|
||||
"""Set the setting name to the value in SETTINGS, keeping its other settings."""
|
||||
path = config_path(SETTINGS.name)
|
||||
values = json.loads(path.read_text(encoding='utf-8')) if path.is_file() else {}
|
||||
values[name] = value
|
||||
write_private(path, json.dumps(values, indent=2) + '\n')
|
||||
|
||||
|
||||
def newest_deb():
|
||||
@@ -250,6 +279,11 @@ def hand_over(remote, project, instance, state, name, content, destination, mode
|
||||
return None
|
||||
|
||||
|
||||
def credentials_path(project, instance):
|
||||
"""The file of the administrator of the VM lm-dev-vm.py made, JSON of username, password and otp_url; 0600."""
|
||||
return config_path(f'vm-{project}-{instance}.json')
|
||||
|
||||
|
||||
def totp_key(instance, user):
|
||||
"""The name of the TOTP key of the user of the instance in the Vault's TOTP engine."""
|
||||
return f'{instance}-{user}'
|
||||
@@ -258,8 +292,9 @@ def totp_key(instance, user):
|
||||
def enrol_otp(remote, project, instance, user):
|
||||
"""Enrol a new TOTP token for the user of the VM, replacing one the user had; the problem, or None.
|
||||
|
||||
The VM has the Vault generate the key with its AppRole and enrols its secret with its current code; the secret is
|
||||
held by the Vault and the VM only, and the codes are read from the Vault (otp_code).
|
||||
The VM has the Vault generate the key with its AppRole and enrols its secret with its current code; the codes are
|
||||
read from the Vault (otp_code). When the user is the administrator of the VM's credentials_path, the key's otpauth
|
||||
URI, read from the VM's file before it is removed, is kept there as otp_url, to add the token to an authenticator.
|
||||
"""
|
||||
state = vault_state(project)
|
||||
if state is None:
|
||||
@@ -270,11 +305,19 @@ def enrol_otp(remote, project, instance, user):
|
||||
except VaultError as error:
|
||||
return f'the TOTP key {key} cannot be replaced: {error}'
|
||||
target = f'{HANDOVER_DIR}/otp-url'
|
||||
for command in (('install', '-d', '-m', '0700', HANDOVER_DIR), (HELPER, 'totp', key, instance, user, target, '0600'),
|
||||
('python3', '-c', OTP_ENROL, user, target)):
|
||||
credentials = credentials_path(project, instance)
|
||||
known = json.loads(credentials.read_text(encoding='utf-8')) if credentials.is_file() else {}
|
||||
keep = known.get('username') == user
|
||||
steps = (('install', '-d', '-m', '0700', HANDOVER_DIR), (HELPER, 'totp', key, instance, user, target, '0600'),
|
||||
*((('cat', target),) if keep else ()), ('python3', '-c', OTP_ENROL, user, target))
|
||||
for command in steps:
|
||||
result = lxc_exec(remote, project, instance, *command)
|
||||
if result.returncode != 0:
|
||||
return f'{user} was not enrolled: {(result.stderr.strip().splitlines() or ["lxc exec failed"])[-1]}'
|
||||
if command[0] == 'cat':
|
||||
known['otp_url'] = result.stdout.strip()
|
||||
if keep:
|
||||
write_private(credentials, json.dumps(known, indent=2) + '\n')
|
||||
return None
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user