37 lines
1.5 KiB
Python
37 lines
1.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Prepare private Secret JSON outside Git; do not print credentials."""
|
|
import argparse
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import re
|
|
import secrets
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--key-file", type=Path, required=True)
|
|
parser.add_argument("--output", type=Path, required=True)
|
|
args = parser.parse_args()
|
|
root = Path(__file__).resolve().parent.parent
|
|
if args.output.resolve().is_relative_to(root):
|
|
parser.error("Store the Secret outside the repository")
|
|
key = args.key_file.read_text().strip()
|
|
key = re.sub(r"^(?:export\s+)?(?:KANEO_API_KEY|API_KEY|apiKey|key|token)\s*[:=]\s*", "", key, flags=re.I).strip().strip("\"'")
|
|
if key.lower().startswith("bearer "):
|
|
key = key[7:].strip()
|
|
if not key or any(c.isspace() for c in key):
|
|
parser.error("Expected one API key or KANEO_API_KEY=value; file contents were not printed")
|
|
values = json.loads(args.output.read_text())["stringData"] if args.output.exists() else {
|
|
"webhook-secret": secrets.token_urlsafe(48),
|
|
"grafana-admin-password": secrets.token_urlsafe(24),
|
|
}
|
|
values["kaneo-api-key"] = key
|
|
document = {"apiVersion": "v1", "kind": "Secret", "metadata": {
|
|
"name": "logging-secrets", "namespace": "observability"}, "type": "Opaque", "stringData": values}
|
|
args.output.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
|
|
fd = os.open(args.output, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
|
os.fchmod(fd, 0o600)
|
|
with os.fdopen(fd, "w") as stream:
|
|
json.dump(document, stream)
|
|
stream.write("\n")
|
|
print("Private Secret prepared: " + str(args.output))
|