285 lines
15 KiB
Python
285 lines
15 KiB
Python
#!/usr/bin/env python3
|
|
"""Reset the existing owner of a single-instance MOYI cloud deployment."""
|
|
import argparse
|
|
import base64
|
|
import copy
|
|
import getpass
|
|
import hashlib
|
|
import importlib.util
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import secrets
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import urllib.parse
|
|
|
|
|
|
class MaintenanceError(Exception):
|
|
pass
|
|
|
|
|
|
def require(condition, message):
|
|
if not condition:
|
|
raise MaintenanceError(message)
|
|
|
|
|
|
def run(command, *, cwd=None, env=None, sql=None, timeout=240):
|
|
options = {"input": sql} if sql is not None else {"stdin": subprocess.DEVNULL}
|
|
result = subprocess.run(command, cwd=cwd, env=env, capture_output=True,
|
|
text=True, encoding="utf-8", timeout=timeout, **options)
|
|
# Database/Compose error messages can contain secrets or SQL; never echo them.
|
|
require(result.returncode == 0, Path(command[0]).name + " failed; output withheld to protect credentials.")
|
|
return result.stdout.strip()
|
|
|
|
|
|
def password_hash(password, pepper):
|
|
salt = secrets.token_bytes(16)
|
|
key = hashlib.pbkdf2_hmac("sha256", (password + pepper).encode("utf-8"), salt, 210000, 32)
|
|
encode = lambda value: base64.urlsafe_b64encode(value).decode("ascii").rstrip("=")
|
|
return "pbkdf2$210000$" + encode(salt) + "$" + encode(key)
|
|
|
|
|
|
def sql_literal(value):
|
|
# Hex also prevents dollar-quote delimiters from appearing inside a DO body.
|
|
return "convert_from(decode('" + str(value).encode("utf-8").hex() + "', 'hex'), 'UTF8')"
|
|
|
|
|
|
def updated_environment(original, account, password):
|
|
require(password.strip() and not any(ord(char) < 32 or ord(char) == 127 for char in password),
|
|
"Password must be nonblank and cannot contain control characters.")
|
|
require(password != "admin123", "The development default password is not allowed.")
|
|
# Replace the two owner settings in place before appending the effective
|
|
# values. Compose accepts later overrides, but duplicate assignments make
|
|
# the resulting dotenv file ambiguous and can silently mask a real secret.
|
|
# Do not parse or rewrite arbitrary existing dotenv values, which may be
|
|
# multiline/quoted.
|
|
lines = [line for line in original.splitlines() if not re.match(
|
|
r"^\s*(?:export\s+)?(?:ADMIN_ACCOUNT|ADMIN_PASSWORD)\s*=", line)]
|
|
original = "\n".join(lines)
|
|
values = {"ADMIN_ACCOUNT": account, "ADMIN_PASSWORD": password}
|
|
return original.rstrip("\r\n") + "\n\n# Owner credentials set by admin_credentials.py\n" + "".join(
|
|
key + "=" + json.dumps(value, ensure_ascii=False).replace("$", "$$") + "\n"
|
|
for key, value in values.items())
|
|
|
|
|
|
def validate_staged_config(before, after, account, password):
|
|
expected = copy.deepcopy(before)
|
|
expected["services"]["moyi-api"]["environment"].update(
|
|
ADMIN_ACCOUNT=account, ADMIN_PASSWORD=password)
|
|
require(after == expected, "Staged configuration differs unexpectedly. Check shell environment overrides; nothing was changed.")
|
|
|
|
|
|
def postgres_environment(api, expected_database):
|
|
url = str(api["SPRING_DATASOURCE_URL"])
|
|
require(url.startswith("jdbc:postgresql://"), "PostgreSQL JDBC URL required.")
|
|
target = urllib.parse.urlsplit(url[5:])
|
|
require(urllib.parse.unquote(target.path) == "/" + expected_database and target.hostname
|
|
and not target.username and not target.password, "Database target does not match --database.")
|
|
query = urllib.parse.parse_qs(target.query, keep_blank_values=True)
|
|
supported = {"sslmode": "PGSSLMODE", "sslrootcert": "PGSSLROOTCERT", "sslcert": "PGSSLCERT", "sslkey": "PGSSLKEY"}
|
|
require(all(key in supported and len(value) == 1 for key, value in query.items()),
|
|
"Unsupported JDBC parameters; review TLS settings before maintenance.")
|
|
env = {key: value for key, value in os.environ.items() if not key.startswith("PG")}
|
|
env.update(PGHOST=target.hostname, PGPORT=str(target.port or 5432), PGDATABASE=expected_database,
|
|
PGUSER=str(api["SPRING_DATASOURCE_USERNAME"]), PGPASSWORD=str(api["SPRING_DATASOURCE_PASSWORD"]),
|
|
PGCONNECT_TIMEOUT="10", PGAPPNAME="moyi-admin-maintenance", PGCLIENTENCODING="UTF8")
|
|
for key, value in query.items():
|
|
env[supported[key]] = value[0]
|
|
return env
|
|
|
|
|
|
def query_database(psql, env, sql):
|
|
return run([psql, "-X", "--no-password", "-qAt", "-v", "ON_ERROR_STOP=1", "-f", "-"], env=env, sql=sql)
|
|
|
|
|
|
def read_owner(psql, env, configured_account, new_account):
|
|
rows = json.loads(query_database(psql, env, """
|
|
SELECT COALESCE(json_agg(json_build_object('id', id, 'account', account, 'status', status)), '[]'::json)
|
|
FROM public.users WHERE role = 'super_admin';
|
|
"""))
|
|
require(len(rows) == 1 and rows[0]["status"] == "active", "Expected exactly one active super administrator.")
|
|
normalized = re.sub(r"[\s-]", "", configured_account.strip().lower())
|
|
# The second case permits retry after DB commit but before env installation.
|
|
require(rows[0]["account"] in (normalized, new_account.lower()),
|
|
"Existing owner differs from both configured and requested accounts; refusing to change it.")
|
|
occupied = query_database(psql, env, "SELECT count(*) FROM public.users WHERE account = "
|
|
+ sql_literal(new_account.lower()) + " AND id <> " + sql_literal(rows[0]["id"]) + "::uuid;")
|
|
require(occupied == "0", "Requested account is already used by another user; nothing changed.")
|
|
return rows[0]
|
|
|
|
|
|
def reset_sql(database, owner, account, encoded):
|
|
database, user_id, previous, account, encoded = map(sql_literal, (
|
|
database, owner["id"], owner["account"], account.lower(), encoded))
|
|
return f"""
|
|
BEGIN;
|
|
SET LOCAL standard_conforming_strings = on;
|
|
SET LOCAL lock_timeout = '10s';
|
|
SET LOCAL statement_timeout = '30s';
|
|
LOCK TABLE public.users IN SHARE ROW EXCLUSIVE MODE;
|
|
DO $moyi_reset$
|
|
BEGIN
|
|
IF current_database() <> {database} THEN
|
|
RAISE EXCEPTION 'Wrong database';
|
|
END IF;
|
|
IF (SELECT count(*) FROM public.users WHERE role = 'super_admin') <> 1
|
|
OR NOT EXISTS (SELECT 1 FROM public.users WHERE id = {user_id}::uuid
|
|
AND account = {previous} AND role = 'super_admin' AND status = 'active') THEN
|
|
RAISE EXCEPTION 'Owner changed; refusing reset';
|
|
END IF;
|
|
IF EXISTS (SELECT 1 FROM public.users WHERE account = {account} AND id <> {user_id}::uuid) THEN
|
|
RAISE EXCEPTION 'Account already exists';
|
|
END IF;
|
|
UPDATE public.users SET account = {account}, account_kind = 'username',
|
|
password_hash = {encoded}, updated_at = now() WHERE id = {user_id}::uuid;
|
|
UPDATE public.auth_sessions SET revoked_at = now()
|
|
WHERE user_id = {user_id}::uuid AND revoked_at IS NULL;
|
|
INSERT INTO public.auth_audit_log (actor_user_id, event_type, target_type, target_id, details)
|
|
VALUES ({user_id}::uuid, 'user.credentials_reset', 'user', {user_id},
|
|
json_build_object('fromAccount', {previous}, 'account', {account}, 'method', 'server_cli')::jsonb);
|
|
END;
|
|
$moyi_reset$;
|
|
COMMIT;
|
|
SELECT 'OWNER_RESET_COMMITTED';
|
|
"""
|
|
|
|
|
|
def write_private(path, data):
|
|
descriptor = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
|
|
with os.fdopen(descriptor, "wb") as stream:
|
|
stream.write(data)
|
|
stream.flush()
|
|
os.fsync(stream.fileno())
|
|
|
|
|
|
def install_environment(env_file, content):
|
|
temporary = env_file.with_name(".env.owner-" + secrets.token_hex(8))
|
|
write_private(temporary, content)
|
|
os.replace(temporary, env_file)
|
|
if os.name != "nt":
|
|
descriptor = os.open(env_file.parent, os.O_RDONLY | os.O_DIRECTORY)
|
|
try:
|
|
os.fsync(descriptor)
|
|
finally:
|
|
os.close(descriptor)
|
|
|
|
|
|
def check_container(deploy, config, env_file, mode):
|
|
compose = deploy.compose_command(env_file, mode)
|
|
ids = run(compose + ["ps", "-a", "-q", "moyi-api"], cwd=deploy.HERE).splitlines()
|
|
require(len(ids) == 1, "Expected one existing API container in this Compose project.")
|
|
item = json.loads(run(["docker", "inspect", ids[0]]))[0]
|
|
labels = item["Config"]["Labels"]
|
|
require(Path(labels["com.docker.compose.project.working_dir"]).resolve() == deploy.HERE.resolve(),
|
|
"Container belongs to another release directory.")
|
|
require(item["Config"]["Image"] == config["services"]["moyi-api"]["image"],
|
|
"Running image differs from deployment configuration.")
|
|
actual = dict(value.split("=", 1) for value in item["Config"]["Env"] if "=" in value)
|
|
expected = config["services"]["moyi-api"]["environment"]
|
|
for key in ("SPRING_DATASOURCE_URL", "SPRING_DATASOURCE_USERNAME", "SPRING_DATASOURCE_PASSWORD",
|
|
"AUTH_PASSWORD_PEPPER", "CREDENTIAL_ENCRYPTION_KEY"):
|
|
require(actual.get(key) == expected.get(key), "Container/database or encryption settings differ; refusing reset.")
|
|
return ids[0], compose
|
|
|
|
|
|
def apply_change(deploy, config, env_file, mode, account, password, database, psql, pg_dump, backup_root):
|
|
api = config["services"]["moyi-api"]["environment"]
|
|
pg_env = postgres_environment(api, database)
|
|
owner = read_owner(psql, pg_env, api["ADMIN_ACCOUNT"], account)
|
|
container, compose = check_container(deploy, config, env_file, mode)
|
|
original = env_file.read_bytes()
|
|
content = updated_environment(original.decode("utf-8-sig"), account, password).encode("utf-8")
|
|
backup_root.mkdir(mode=0o700, parents=True, exist_ok=True)
|
|
backup = Path(tempfile.mkdtemp(prefix="owner-reset-", dir=backup_root))
|
|
write_private(backup / "env.cloud.before", original)
|
|
write_private(backup / "env.cloud.after", content)
|
|
staged = deploy.load_config(backup / "env.cloud.after", mode)
|
|
deploy.validate_config(staged, mode)
|
|
validate_staged_config(config, staged, account, password)
|
|
print("Private recovery directory: " + str(backup), flush=True)
|
|
print("Stopping API for backup and session revocation...", flush=True)
|
|
mutation_started = False
|
|
try:
|
|
run(["docker", "stop", "--time", "40", container])
|
|
require(env_file.read_bytes() == original, "Environment file changed concurrently; refusing reset.")
|
|
dump = backup / "moyi_control.dump"
|
|
run([pg_dump, "--no-password", "--format=custom", "--quote-all-identifiers", "--lock-wait-timeout=10000",
|
|
"--file=" + str(dump)], env=pg_env)
|
|
require(dump.stat().st_size > 0, "Database backup is empty.")
|
|
with dump.open("rb") as stream:
|
|
digest = hashlib.file_digest(stream, "sha256").hexdigest()
|
|
write_private(backup / "moyi_control.dump.sha256", (digest + " moyi_control.dump\n").encode("ascii"))
|
|
encoded = password_hash(password, api["AUTH_PASSWORD_PEPPER"])
|
|
mutation_started = True
|
|
result = query_database(psql, pg_env, reset_sql(database, owner, account, encoded))
|
|
require(result == "OWNER_RESET_COMMITTED", "Database outcome uncertain; keep API stopped and rerun this tool.")
|
|
install_environment(env_file, content)
|
|
run(compose + ["up", "-d", "--no-deps", "--no-build", "--pull", "never", "--force-recreate",
|
|
"--wait", "--wait-timeout", "180", "moyi-api"], cwd=deploy.HERE)
|
|
run(compose + ["exec", "-T", "frontend", "nginx", "-t"], cwd=deploy.HERE)
|
|
run(compose + ["exec", "-T", "frontend", "nginx", "-s", "reload"], cwd=deploy.HERE)
|
|
deploy.smoke(staged, mode, login=True)
|
|
except BaseException:
|
|
if not mutation_started:
|
|
run(["docker", "start", container])
|
|
print("Reset was not attempted; original API container restarted.", flush=True)
|
|
else:
|
|
print("Maintenance did not finish. Do not restore the old env or database blindly. "
|
|
"Rerun this tool with the same account/password to reconcile and verify. Recovery: " + str(backup), flush=True)
|
|
raise
|
|
print("SUPER_ADMIN_RESET_OK: " + account + "; existing user ID preserved; previous sessions revoked.")
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--deployment-dir", type=Path, required=True)
|
|
parser.add_argument("--account", required=True)
|
|
parser.add_argument("--database", default="moyi_control")
|
|
parser.add_argument("--mode", choices=("https", "tunnel"), default="https")
|
|
parser.add_argument("--backup-root", type=Path, default=Path("/opt/moyi/backups"))
|
|
parser.add_argument("--apply", action="store_true", help="Stop API, back up, reset owner and restart; password is prompted twice")
|
|
args = parser.parse_args()
|
|
require(re.fullmatch(r"[A-Za-z][A-Za-z0-9_]{0,79}", args.account), "Use an ASCII username starting with a letter.")
|
|
require(re.fullmatch(r"[a-z][a-z0-9_]+", args.database) and args.database not in ("postgres", "template0", "template1"),
|
|
"Use a dedicated application database.")
|
|
require(not args.apply or (os.name == "posix" and os.geteuid() == 0), "Apply must run as root on the Linux server.")
|
|
directory = args.deployment_dir.resolve()
|
|
spec = importlib.util.spec_from_file_location("moyi_deploy", directory / "deploy.py")
|
|
deploy = importlib.util.module_from_spec(spec)
|
|
spec.loader.exec_module(deploy)
|
|
env_file = directory / ".env.cloud"
|
|
psql, pg_dump = shutil.which("psql"), shutil.which("pg_dump")
|
|
require(psql and pg_dump, "Install postgresql-client first.")
|
|
if args.apply:
|
|
import fcntl
|
|
os.umask(0o077)
|
|
lock = open(directory / ".owner-maintenance.lock", "a")
|
|
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
|
|
config = deploy.preflight(env_file, args.mode)
|
|
api = config["services"]["moyi-api"]["environment"]
|
|
env = postgres_environment(api, args.database)
|
|
owner = read_owner(psql, env, api["ADMIN_ACCOUNT"], args.account)
|
|
check_container(deploy, config, env_file, args.mode)
|
|
print("Database: " + args.database + "; existing super administrator: " + owner["account"])
|
|
if not args.apply:
|
|
print("Read-only check passed. Add --apply to change credentials.")
|
|
return
|
|
require(sys.stdin.isatty(), "Run interactively in SSH, not through a heredoc or pipe.")
|
|
password = getpass.getpass("New super administrator password (hidden): ")
|
|
require(password == getpass.getpass("Repeat new password (hidden): "), "Passwords do not match; nothing changed.")
|
|
apply_change(deploy, config, env_file, args.mode, args.account, password, args.database,
|
|
psql, pg_dump, args.backup_root.resolve())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
try:
|
|
main()
|
|
except (MaintenanceError, ValueError, OSError, subprocess.SubprocessError, KeyboardInterrupt) as error:
|
|
message = str(error) if isinstance(error, MaintenanceError) else type(error).__name__ + ": maintenance stopped; no secret output shown."
|
|
print("ERROR: " + message, file=sys.stderr)
|
|
sys.exit(1)
|