ЛР 7: Безопасность, mTLS и OpenBao
Skeleton contracts / contracts (pull_request) Blocked by required conditions
Skeleton contracts / contracts (pull_request) Blocked by required conditions
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Add independent random service credentials to the local ignored .env file."""
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
ENV_FILE = ROOT / ".env"
|
||||
PASSWORD_KEYS = (
|
||||
"API_DB_PASSWORD",
|
||||
"WORKER_DB_PASSWORD",
|
||||
"S3_API_SECRET_KEY",
|
||||
"S3_WORKER_SECRET_KEY",
|
||||
"BROKER_API_PASSWORD",
|
||||
"BROKER_WORKER_PASSWORD",
|
||||
)
|
||||
FIXED_VALUES = {
|
||||
"API_DB_USER": "messenger_api",
|
||||
"WORKER_DB_USER": "messenger_worker",
|
||||
"S3_API_ACCESS_KEY": "messenger-api",
|
||||
"S3_WORKER_ACCESS_KEY": "messenger-worker",
|
||||
"BROKER_API_USER": "messenger-api",
|
||||
"BROKER_WORKER_USER": "messenger-worker",
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
if not ENV_FILE.is_file():
|
||||
raise SystemExit("Copy .env.example to .env before generating local credentials")
|
||||
|
||||
lines = ENV_FILE.read_text(encoding="utf-8").splitlines()
|
||||
existing = {}
|
||||
for line in lines:
|
||||
if "=" in line and not line.lstrip().startswith("#"):
|
||||
key, value = line.split("=", 1)
|
||||
existing[key.strip()] = value.strip()
|
||||
|
||||
additions = {}
|
||||
for key, value in FIXED_VALUES.items():
|
||||
if not existing.get(key):
|
||||
additions[key] = value
|
||||
for key in PASSWORD_KEYS:
|
||||
if not existing.get(key) or "replace-with" in existing[key]:
|
||||
additions[key] = secrets.token_urlsafe(36)
|
||||
|
||||
if additions:
|
||||
lines.extend(["", "# Generated locally; do not commit these service credentials."])
|
||||
lines.extend(key + "=" + value for key, value in additions.items())
|
||||
temporary = ENV_FILE.with_suffix(".env.tmp")
|
||||
temporary.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
||||
temporary.chmod(0o600)
|
||||
temporary.replace(ENV_FILE)
|
||||
ENV_FILE.chmod(0o600)
|
||||
|
||||
print("PASS: local service credentials are present; values were not displayed")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user