Files
mtusi2026lab7/scripts/test_bao.py
T
2026-12-03 19:25:38 +03:00

61 lines
1.6 KiB
Python

import ssl
import json
import urllib.request
import urllib.error
base = "https://localhost:8200/v1/"
for role in ("api", "worker"):
ctx = ssl.create_default_context(cafile="secrets/ca.crt")
ctx.load_cert_chain(
f"secrets/{role}/cert.crt",
f"secrets/{role}/cert.key"
)
req = urllib.request.Request(
base + "auth/cert/login",
data=json.dumps({"name": role}).encode(),
headers={"Content-Type": "application/json"}
)
with urllib.request.urlopen(req, context=ctx) as r:
auth = json.load(r)["auth"]
token = auth["client_token"]
print(f"\n{role}: login OK, TTL={auth['lease_duration']}")
for target in ("api", "worker", "operator"):
req = urllib.request.Request(
base + "secret/data/" + target,
headers={"X-Vault-Token": token}
)
try:
with urllib.request.urlopen(req, context=ctx) as r:
print(f"{role} -> {target}: {r.status}")
except urllib.error.HTTPError as e:
print(f"{role} -> {target}: {e.code}")
req = urllib.request.Request(
base + "auth/token/revoke-self",
data=b"{}",
headers={"X-Vault-Token": token}
)
with urllib.request.urlopen(req, context=ctx) as r:
print(f"{role}: revoke -> {r.status}")
req = urllib.request.Request(
base + "secret/data/" + role,
headers={"X-Vault-Token": token}
)
try:
urllib.request.urlopen(req, context=ctx)
print("FAIL: revoked token still works")
except urllib.error.HTTPError as e:
print(f"{role}: after revoke -> {e.code}")
assert e.code == 403