feat: complete lab 7 grade 3 security controls

This commit is contained in:
Dmitrii Grishko
2026-11-14 20:08:27 +03:00
parent 8986ba6bef
commit c5e3ac3aff
49 changed files with 1670 additions and 149 deletions
+68
View File
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
"""Configure an initialized, unsealed local OpenBao using an operator token."""
import argparse
import getpass
import json
import ssl
from pathlib import Path
from urllib.error import HTTPError
from urllib.request import Request, urlopen
ROOT = Path(__file__).resolve().parents[1]
SECRETS = ROOT / "secrets"
TLS = SECRETS / "tls"
def main():
context = ssl.create_default_context(cafile=str(TLS / "lab7-ca.crt"))
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--operator-file", type=Path)
args = parser.parse_args()
token = (json.loads(args.operator_file.read_text())["root_token"]
if args.operator_file else getpass.getpass("OpenBao operator token: "))
def request(path, body=None):
data = None if body is None else json.dumps(body).encode()
req = Request(
"https://localhost:8200/v1/" + path,
data=data,
headers={"Content-Type": "application/json", "X-Vault-Token": token},
)
try:
with urlopen(req, context=context, timeout=5) as response:
return json.load(response) if response.status != 204 else {}
except HTTPError as error:
raise SystemExit(f"OpenBao {path}: HTTP {error.code}")
if "cert/" not in request("sys/auth"):
request("sys/auth/cert", {"type": "cert"})
if "messenger/" not in request("sys/mounts"):
request("sys/mounts/messenger", {"type": "kv", "options": {"version": "2"}})
for role in ("api", "worker", "dispatcher", "cleanup"):
request(f"sys/policies/acl/{role}", {
"policy": f'path "messenger/data/{role}" {{ capabilities = ["read"] }}',
})
request(f"auth/cert/certs/{role}", {
"certificate": (TLS / f"bao-{role}.crt").read_text(),
"token_policies": [role],
"token_no_default_policy": True,
"token_ttl": "15m",
"token_max_ttl": "15m",
})
names = {"database_url": f"database_{role}_url"}
if role == "api":
names["cache_url"] = "cache_tls_url"
if role in ("worker", "dispatcher"):
names["broker_url"] = f"broker_{role}_url"
if role != "dispatcher":
for key in ("access_key_id", "secret_access_key"):
names[f"s3_{key}"] = f"s3_{role}_{key}"
values = {key: (SECRETS / name).read_text().strip() for key, name in names.items()}
request(f"messenger/data/{role}", {"data": values})
print(f"Configured OpenBao role {role}")
if __name__ == "__main__":
main()