forked from mtusi2026/mtusi2026lab7
feat: complete lab 7 grade 3 security controls
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Prove each runtime identity can read only its own OpenBao secret."""
|
||||
import json
|
||||
import ssl
|
||||
from pathlib import Path
|
||||
from urllib.error import HTTPError
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
TLS = Path(__file__).resolve().parents[1] / "secrets/tls"
|
||||
ROLES = ("api", "worker", "dispatcher", "cleanup")
|
||||
|
||||
|
||||
def request(context, path, *, body=None, token="", status=200):
|
||||
req = Request(
|
||||
f"https://localhost:8200/v1/{path}",
|
||||
data=None if body is None else json.dumps(body).encode(),
|
||||
headers={"X-Vault-Token": token, "Content-Type": "application/json"},
|
||||
)
|
||||
try:
|
||||
with urlopen(req, context=context, timeout=5) as response:
|
||||
assert response.status == status, (path, response.status, status)
|
||||
return json.load(response)
|
||||
except HTTPError as error:
|
||||
assert error.code == status, (path, error.code, status)
|
||||
|
||||
|
||||
def main():
|
||||
for role in ROLES:
|
||||
context = ssl.create_default_context(cafile=str(TLS / "lab7-ca.crt"))
|
||||
context.load_cert_chain(TLS / f"bao-{role}.crt", TLS / f"bao-{role}.key")
|
||||
auth = request(context, "auth/cert/login", body={"name": role})["auth"]
|
||||
assert set(auth["policies"]) == {role}
|
||||
assert 0 < auth["lease_duration"] <= 900
|
||||
token = auth["client_token"]
|
||||
request(context, f"messenger/data/{role}", token=token)
|
||||
request(context, f"messenger/data/{role}", token=token, body={"data": {}}, status=403)
|
||||
request(context, f"sys/policies/acl/{role}", token=token, body={"policy": ""}, status=403)
|
||||
for other in set(ROLES) - {role}:
|
||||
request(context, f"messenger/data/{other}", token=token, status=403)
|
||||
request(context, "auth/cert/login", body={"name": other}, status=400)
|
||||
print(f"PASS {role}: scoped read, cross-role login/read and write denied, TTL <= 900 s")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user