Files
mtusi2026lab7/scripts/bao_policy_check.py
T

46 lines
1.6 KiB
Python

"""Check allowed and denied paths under real TLS cert-auth, without printing tokens."""
import ssl
from urllib.error import HTTPError
from bao_operator import BASE, api_call
from pki import CA_CERT, PRIVATE
def context(role):
client = ssl.create_default_context(cafile=CA_CERT)
client.load_cert_chain(PRIVATE / f"{role}-client.crt", PRIVATE / f"{role}-client.key")
return client
def denied(client, path, token, body=None):
try:
api_call(BASE, client, path, body, token)
except HTTPError as exc:
if exc.code == 403:
return
raise
raise AssertionError(f"Unexpected successful action on {path}")
def main():
for role, foreign in (("api", "worker"), ("worker", "api"), ("relay", "api")):
client = context(role)
auth = api_call(BASE, client, "auth/cert/login", {"name": role})["auth"]
token = auth["client_token"]
try:
data = api_call(BASE, client, f"messenger/data/{role}", token=token)["data"]["data"]
if not data or not 0 < auth["lease_duration"] <= 180:
raise AssertionError("Expected a bounded service token and a populated KV path")
denied(client, f"messenger/data/{foreign}", token)
denied(client, "sys/policies/acl/unauthorized-check", token, {"policy": ""})
print(f"PASS {role}: own KV read, foreign KV and policy write denied")
finally:
api_call(BASE, client, "auth/token/revoke-self", {}, token)
denied(client, f"messenger/data/{role}", token)
print(f"PASS {role}: revoked token denied")
if __name__ == "__main__":
main()