fix(security): keep database passwords out of every argv
MariaDB dumps and restores read the password from MYSQL_PWD, and docker exec forwards env variables by name only, so no process listing shows a password. baudolo-restore gains --db-password-stdin; --db-password still works but warns. 🎯 Fix: 96 % 💥 Break: 3 % 💸 Cost: – ⏱️ Time: – 🛡️ Security: ~100 % 🏆 Score: 93 🚦 Gate: ✅ 90/5, cost and time <= 100, security 100 % Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d672388f63
commit
328520169b
15 files changed
+320
-67
No files matched your search
@@ -10,6 +10,29 @@ from .files import restore_volume_files
|
||||
from .paths import BackupPaths
|
||||
|
||||
|
||||
def _password(parser: argparse.ArgumentParser, args: argparse.Namespace) -> str:
|
||||
"""
|
||||
Args:
|
||||
parser: reports a password given both ways or not at all.
|
||||
args: the parsed engine subcommand.
|
||||
|
||||
Returns:
|
||||
The database password, from stdin or from the deprecated argument.
|
||||
"""
|
||||
if args.db_password_stdin == (args.db_password is not None):
|
||||
parser.error(
|
||||
"give the password either via --db-password-stdin or --db-password"
|
||||
)
|
||||
if args.db_password_stdin:
|
||||
return sys.stdin.read().removesuffix("\n")
|
||||
print(
|
||||
"WARNING: --db-password is visible in the process list; "
|
||||
"use --db-password-stdin",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return args.db_password
|
||||
|
||||
|
||||
def _add_common_backup_args(p: argparse.ArgumentParser) -> None:
|
||||
p.add_argument("volume_name", help="Docker volume name (target volume)")
|
||||
p.add_argument("backup_hash", help="Hashed machine id")
|
||||
@@ -29,7 +52,16 @@ def _add_common_backup_args(p: argparse.ArgumentParser) -> None:
|
||||
|
||||
def _add_common_engine_args(p: argparse.ArgumentParser) -> None:
|
||||
p.add_argument("--container", required=True)
|
||||
p.add_argument("--db-password", required=True)
|
||||
p.add_argument(
|
||||
"--db-password",
|
||||
default=None,
|
||||
help="Deprecated: visible in the process list; use --db-password-stdin",
|
||||
)
|
||||
p.add_argument(
|
||||
"--db-password-stdin",
|
||||
action="store_true",
|
||||
help="Read the database password from stdin",
|
||||
)
|
||||
p.add_argument("--empty", action="store_true")
|
||||
p.add_argument(
|
||||
"--no-version-check",
|
||||
@@ -91,6 +123,7 @@ def main(argv: list[str] | None = None) -> int:
|
||||
p_mdb.add_argument("--db-user", default=None, help="Defaults to db-name if omitted")
|
||||
|
||||
args = parser.parse_args(argv)
|
||||
password = None if args.cmd == "files" else _password(parser, args)
|
||||
|
||||
try:
|
||||
if args.cmd == "files":
|
||||
@@ -115,7 +148,7 @@ def main(argv: list[str] | None = None) -> int:
|
||||
container=args.container,
|
||||
db_name=args.db_name,
|
||||
user=user,
|
||||
password=args.db_password,
|
||||
password=password,
|
||||
sql_path=BackupPaths(
|
||||
args.volume_name,
|
||||
args.backup_hash,
|
||||
@@ -132,7 +165,7 @@ def main(argv: list[str] | None = None) -> int:
|
||||
restore_cluster_sql(
|
||||
container=args.container,
|
||||
user=args.db_user,
|
||||
password=args.db_password,
|
||||
password=password,
|
||||
sql_path=BackupPaths(
|
||||
args.volume_name,
|
||||
args.backup_hash,
|
||||
@@ -151,7 +184,7 @@ def main(argv: list[str] | None = None) -> int:
|
||||
container=args.container,
|
||||
db_name=args.db_name,
|
||||
user=user,
|
||||
password=args.db_password,
|
||||
password=password,
|
||||
sql_path=BackupPaths(
|
||||
args.volume_name,
|
||||
args.backup_hash,
|
||||
|
||||
@@ -47,6 +47,8 @@ def restore_mariadb_sql(
|
||||
if not Path(sql_path).is_file():
|
||||
raise FileNotFoundError(sql_path)
|
||||
|
||||
docker_env = {"MYSQL_PWD": password}
|
||||
|
||||
if check_version:
|
||||
guard(
|
||||
sql_path=sql_path,
|
||||
@@ -65,12 +67,12 @@ def restore_mariadb_sql(
|
||||
client,
|
||||
"-u",
|
||||
user,
|
||||
f"--password={password}",
|
||||
"-N",
|
||||
"-e",
|
||||
f"SELECT table_name FROM information_schema.tables WHERE table_schema = '{db_name}';", # noqa: S608 - validate_database() constrains the name to ^[a-zA-Z0-9_][a-zA-Z0-9_-]*$
|
||||
],
|
||||
capture=True,
|
||||
docker_env=docker_env,
|
||||
)
|
||||
tables = result.stdout.decode().split()
|
||||
|
||||
@@ -90,15 +92,15 @@ def restore_mariadb_sql(
|
||||
client,
|
||||
"-u",
|
||||
user,
|
||||
f"--password={password}",
|
||||
"-e",
|
||||
drop_sql,
|
||||
],
|
||||
docker_env=docker_env,
|
||||
)
|
||||
|
||||
with Path(sql_path).open("rb") as f:
|
||||
docker_exec(
|
||||
container, [client, "-u", user, f"--password={password}", db_name], stdin=f
|
||||
container, [client, "-u", user, db_name], stdin=f, docker_env=docker_env
|
||||
)
|
||||
|
||||
print(f"MariaDB/MySQL restore complete for db '{db_name}'.")
|
||||
@@ -102,13 +102,13 @@ def server_version(
|
||||
client or "mariadb",
|
||||
"-u",
|
||||
user,
|
||||
f"--password={password}",
|
||||
"-N",
|
||||
"-B",
|
||||
"-e",
|
||||
"SELECT VERSION()",
|
||||
],
|
||||
capture=True,
|
||||
docker_env={"MYSQL_PWD": password},
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
@@ -46,6 +47,38 @@ def stdout_of(completed: subprocess.CompletedProcess) -> str:
|
||||
return (raw.decode() if isinstance(raw, bytes) else raw).strip()
|
||||
|
||||
|
||||
def docker_exec_cmd(
|
||||
container: str, argv: list[str], docker_env: dict[str, str] | None
|
||||
) -> list[str]:
|
||||
"""
|
||||
Args:
|
||||
container: the container to run in.
|
||||
argv: the command, already split.
|
||||
docker_env: variables to hand to the container; only their names
|
||||
reach the returned argv.
|
||||
|
||||
Returns:
|
||||
The ``docker exec -i`` argv.
|
||||
"""
|
||||
forwarded = [arg for name in docker_env or {} for arg in ("-e", name)]
|
||||
return ["docker", "exec", "-i", *forwarded, container, *argv]
|
||||
|
||||
|
||||
def child_env(env: dict | None, docker_env: dict[str, str] | None) -> dict | None:
|
||||
"""
|
||||
Args:
|
||||
env: the docker CLI's environment, or None to inherit this process's.
|
||||
docker_env: variables whose values the docker CLI copies into the
|
||||
container for a bare ``-e NAME``.
|
||||
|
||||
Returns:
|
||||
The environment for the docker CLI, or None to inherit unchanged.
|
||||
"""
|
||||
if not docker_env:
|
||||
return env
|
||||
return {**(os.environ if env is None else env), **docker_env}
|
||||
|
||||
|
||||
def docker_exec(
|
||||
container: str,
|
||||
argv: list[str],
|
||||
@@ -55,12 +88,12 @@ def docker_exec(
|
||||
env: dict | None = None,
|
||||
docker_env: dict[str, str] | None = None,
|
||||
) -> subprocess.CompletedProcess:
|
||||
cmd: list[str] = ["docker", "exec", "-i"]
|
||||
if docker_env:
|
||||
for k, v in docker_env.items():
|
||||
cmd.extend(["-e", f"{k}={v}"])
|
||||
cmd.extend([container, *argv])
|
||||
return run(cmd, stdin=stdin, capture=capture, env=env)
|
||||
return run(
|
||||
docker_exec_cmd(container, argv, docker_env),
|
||||
stdin=stdin,
|
||||
capture=capture,
|
||||
env=child_env(env, docker_env),
|
||||
)
|
||||
|
||||
|
||||
def docker_exec_sh(
|
||||
|
||||
Reference in new issue
Block a user