feat(lab7): implement secure messenger infrastructure with TLS, OpenBao and observability
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,156 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Replace previously issued OpenBao bootstrap credentials."""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import runpy
|
||||
import shutil
|
||||
import ssl
|
||||
import stat
|
||||
import sys
|
||||
import tempfile
|
||||
import urllib.error
|
||||
from pathlib import Path
|
||||
|
||||
HELPERS = runpy.run_path(
|
||||
str(Path(__file__).with_name("issue-openbao-bootstrap.py"))
|
||||
)
|
||||
|
||||
BASE = HELPERS["BASE"]
|
||||
OPERATOR = HELPERS["OPERATOR"]
|
||||
INIT_FILE = HELPERS["INIT_FILE"]
|
||||
SERVICES = HELPERS["SERVICES"]
|
||||
request = HELPERS["request"]
|
||||
write_private = HELPERS["write_private"]
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Reissue existing OpenBao AppRole credentials"
|
||||
)
|
||||
parser.add_argument("service", choices=sorted(SERVICES))
|
||||
args = parser.parse_args()
|
||||
|
||||
role = SERVICES[args.service]
|
||||
parent = OPERATOR / "approle"
|
||||
destination = parent / args.service
|
||||
|
||||
if not destination.is_dir() or destination.is_symlink():
|
||||
raise RuntimeError(
|
||||
"Existing bootstrap directory required"
|
||||
)
|
||||
|
||||
if stat.S_IMODE(INIT_FILE.stat().st_mode) != 0o600:
|
||||
raise RuntimeError("Operator file must have mode 600")
|
||||
|
||||
old_role_file = destination / "role-id"
|
||||
old_secret_file = destination / "secret-id"
|
||||
|
||||
for path in (old_role_file, old_secret_file):
|
||||
if not path.is_file() or path.is_symlink():
|
||||
raise RuntimeError("Invalid existing credential file")
|
||||
|
||||
old_secret_id = old_secret_file.read_text(
|
||||
encoding="utf-8"
|
||||
).strip()
|
||||
|
||||
with INIT_FILE.open(encoding="utf-8") as file:
|
||||
root_token = json.load(file)["root_token"]
|
||||
|
||||
tls = ssl.create_default_context(
|
||||
cafile=str(BASE / "pki/ca.crt")
|
||||
)
|
||||
|
||||
role_result = request(
|
||||
"GET",
|
||||
f"/auth/approle/role/{role}/role-id",
|
||||
root_token,
|
||||
tls,
|
||||
)
|
||||
role_id = role_result["data"]["role_id"]
|
||||
|
||||
os.umask(0o077)
|
||||
|
||||
with tempfile.TemporaryDirectory(
|
||||
prefix=f".{args.service}-new-",
|
||||
dir=parent,
|
||||
) as temp:
|
||||
staging = Path(temp) / "credentials"
|
||||
staging.mkdir(mode=0o700)
|
||||
|
||||
secret_result = request(
|
||||
"POST",
|
||||
f"/auth/approle/role/{role}/secret-id",
|
||||
root_token,
|
||||
tls,
|
||||
{},
|
||||
)
|
||||
|
||||
new_secret_id = secret_result["data"]["secret_id"]
|
||||
|
||||
try:
|
||||
write_private(staging / "role-id", role_id)
|
||||
write_private(staging / "secret-id", new_secret_id)
|
||||
except Exception:
|
||||
try:
|
||||
request(
|
||||
"POST",
|
||||
f"/auth/approle/role/{role}/secret-id/destroy",
|
||||
root_token,
|
||||
tls,
|
||||
{"secret_id": new_secret_id},
|
||||
)
|
||||
except Exception:
|
||||
print(
|
||||
"WARNING: unused new SecretID cleanup failed",
|
||||
file=sys.stderr,
|
||||
)
|
||||
raise
|
||||
|
||||
backup = Path(temp) / "previous"
|
||||
|
||||
os.replace(destination, backup)
|
||||
|
||||
try:
|
||||
os.replace(staging, destination)
|
||||
except Exception:
|
||||
os.replace(backup, destination)
|
||||
raise
|
||||
|
||||
# The replacement is now installed.
|
||||
# Revoke the previous SecretID if it is still valid.
|
||||
try:
|
||||
request(
|
||||
"POST",
|
||||
f"/auth/approle/role/{role}/secret-id/destroy",
|
||||
root_token,
|
||||
tls,
|
||||
{"secret_id": old_secret_id},
|
||||
)
|
||||
except urllib.error.HTTPError as exc:
|
||||
# A previously consumed SecretID may already be absent.
|
||||
print(
|
||||
f"INFO: previous SecretID cleanup HTTP {exc.code}"
|
||||
)
|
||||
|
||||
shutil.rmtree(backup)
|
||||
|
||||
print(f"PASS: credentials reissued for {args.service}")
|
||||
print("PASS: previous credential directory replaced")
|
||||
print("PASS: new files stored outside Git")
|
||||
print("PASS: credentials not printed")
|
||||
print("INFO: new SecretID expires in 30 minutes")
|
||||
print("INFO: new SecretID supports one authentication")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except Exception as exc:
|
||||
print(
|
||||
"FAIL: reissuance:",
|
||||
type(exc).__name__,
|
||||
file=sys.stderr,
|
||||
)
|
||||
raise SystemExit(1)
|
||||
Reference in New Issue
Block a user