Repository navigation
Expand file tree
/
Copy pathgenerate_secrets.py
More file actions
executable file
·59 lines (46 loc) · 1.62 KB
/
Copy pathgenerate_secrets.py
File metadata and controls
executable file
·59 lines (46 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env python3
"""Generate strong random passwords for the files under secrets/.
Passwords are drawn from a CSPRNG via the `secrets` module. The alphabet
excludes quotes, backslashes, `$` and whitespace since these values get
interpolated into SQL statements and shell commands by the container
entrypoints.
"""
import argparse
import secrets
import string
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent.parent
SECRETS_DIR = REPO_ROOT / "secrets"
DEFAULT_LENGTH = 32
ALPHABET = string.ascii_letters + string.digits + "!@#%^&*()-_=+[]{}:;,.?/~"
SECRET_FILES = [
"db_root_password.txt",
"db_user_password.txt",
"ftp_user_password.txt",
"wp_admin_password.txt",
"wp_user_password.txt",
]
def generate_password(length: int) -> str:
return "".join(secrets.choice(ALPHABET) for _ in range(length))
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"-f", "--force", action="store_true",
help="overwrite files that already exist",
)
parser.add_argument(
"-l", "--length", type=int, default=DEFAULT_LENGTH,
help="password length (default: %(default)s)",
)
args = parser.parse_args()
SECRETS_DIR.mkdir(exist_ok=True)
for filename in SECRET_FILES:
path = SECRETS_DIR / filename
if path.exists() and not args.force:
print(f"skip {path} (already exists, use --force to overwrite)")
continue
path.write_text(generate_password(args.length) + "\n")
path.chmod(0o600)
print(f"wrote {path}")
if __name__ == "__main__":
main()