maintenance: add set-admin-password, generating the password in the container
This commit is contained in:
1 parent
eb254f721a
commit
8ca24efd70
1 file changed
+71
-3
+71
-3
@@ -5,22 +5,35 @@ up. It is the entry point the StartOS actions call inside a temporary
|
||||
subcontainer, and it can be run by hand:
|
||||
|
||||
python3 maintenance.py delete-history
|
||||
python3 maintenance.py set-admin-password
|
||||
|
||||
The database path comes from the ``DATABASE_PATH`` environment variable and
|
||||
falls back to the application default, so the container's own environment is
|
||||
enough. The schema is created or migrated first, exactly as the server does at
|
||||
startup, so the command is safe to run against a database that has never been
|
||||
opened (section 31 point 2).
|
||||
startup, so the commands are safe to run against a database that has never been
|
||||
opened (section 31 points 1 and 2).
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import secrets
|
||||
import sqlite3
|
||||
import sys
|
||||
|
||||
from auth import get_admin, normalize_admin_username, set_admin
|
||||
from database import delete_history, initialize_database
|
||||
|
||||
|
||||
DEFAULT_DATABASE_PATH = "data/vardkey.sqlite3"
|
||||
# The username a fresh database gets when the action creates the account. The
|
||||
# app's own create-admin step lets the user pick one; this is the fallback the
|
||||
# StartOS action uses when nothing is stored yet.
|
||||
DEFAULT_ADMIN_USERNAME = "admin"
|
||||
# Unambiguous characters only, so the generated password can be read from a
|
||||
# screen and typed back without confusion.
|
||||
PASSWORD_ALPHABET = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789"
|
||||
PASSWORD_LENGTH = 24
|
||||
|
||||
|
||||
def get_database_path():
|
||||
@@ -28,6 +41,20 @@ def get_database_path():
|
||||
return os.environ.get("DATABASE_PATH", DEFAULT_DATABASE_PATH)
|
||||
|
||||
|
||||
def generate_password():
|
||||
"""Return a strong random admin password, generated inside the container.
|
||||
|
||||
The password never travels from the StartOS host into the container as an
|
||||
argument or on stdin, because the SDK does not document a way to feed stdin
|
||||
to a ``withTemp`` command and an argument is visible in the process list.
|
||||
Generating it here and printing it once is the packaging guide's own
|
||||
password-reset pattern (section 31 point 1).
|
||||
"""
|
||||
return "".join(
|
||||
secrets.choice(PASSWORD_ALPHABET) for _ in range(PASSWORD_LENGTH)
|
||||
)
|
||||
|
||||
|
||||
def delete_history_command(args):
|
||||
"""Delete the stored history and report how many checks went with it.
|
||||
|
||||
@@ -41,6 +68,26 @@ def delete_history_command(args):
|
||||
return 0
|
||||
|
||||
|
||||
def set_admin_password_command(args):
|
||||
"""Set a new admin password and print the credentials once.
|
||||
|
||||
The stored admin username is kept when none is given, so a reset does not
|
||||
rename the account by accident. A username that does not fit is refused
|
||||
before anything is written. The credentials are printed as one JSON object,
|
||||
which the StartOS action reads back and shows (section 31 point 1).
|
||||
"""
|
||||
initialize_database(args.database)
|
||||
username = (args.username or "").strip()
|
||||
if username:
|
||||
username = normalize_admin_username(username)
|
||||
else:
|
||||
username = get_admin(args.database)[0] or DEFAULT_ADMIN_USERNAME
|
||||
password = generate_password()
|
||||
set_admin(username, password, args.database)
|
||||
print(json.dumps({"username": username, "password": password}))
|
||||
return 0
|
||||
|
||||
|
||||
def build_parser():
|
||||
"""Return the argument parser for the maintenance commands.
|
||||
|
||||
@@ -68,6 +115,20 @@ def build_parser():
|
||||
help="Delete every saved health check and the follow-up it closed.",
|
||||
)
|
||||
delete.set_defaults(handler=delete_history_command)
|
||||
password = commands.add_parser(
|
||||
"set-admin-password",
|
||||
parents=[common],
|
||||
help="Set a new admin password and print the credentials once.",
|
||||
)
|
||||
password.add_argument(
|
||||
"--username",
|
||||
default=None,
|
||||
help=(
|
||||
"Admin username to store. Defaults to the stored one, or "
|
||||
f"{DEFAULT_ADMIN_USERNAME} when none is stored yet."
|
||||
),
|
||||
)
|
||||
password.set_defaults(handler=set_admin_password_command)
|
||||
return parser
|
||||
|
||||
|
||||
@@ -77,7 +138,14 @@ def main(argv=None):
|
||||
args = parser.parse_args(argv)
|
||||
if args.database is None:
|
||||
args.database = get_database_path()
|
||||
return args.handler(args)
|
||||
try:
|
||||
return args.handler(args)
|
||||
except (ValueError, sqlite3.Error) as error:
|
||||
# The StartOS action shows the standard error as the alert, so a bad
|
||||
# value or an unreadable database becomes a message instead of a
|
||||
# traceback.
|
||||
print(error, file=sys.stderr)
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user