forked from mtusi2026/mtusi2026lab7
252 lines
5.9 KiB
Python
252 lines
5.9 KiB
Python
#!/usr/bin/env python3
|
|
"""OpenBao AppRole and KV v2 ACL tests for lab7."""
|
|
|
|
import json
|
|
import ssl
|
|
import stat
|
|
import uuid
|
|
import urllib.error
|
|
import urllib.request
|
|
from pathlib import Path
|
|
|
|
BASE = Path.home() / ".local/share/mtusi2026lab7"
|
|
INIT_FILE = BASE / "operator/openbao-init.json"
|
|
URL = "https://localhost:8200/v1"
|
|
|
|
if stat.S_IMODE(INIT_FILE.stat().st_mode) != 0o600:
|
|
raise SystemExit("FAIL: operator secret file permissions")
|
|
|
|
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")
|
|
)
|
|
|
|
TEST_ID = uuid.uuid4().hex
|
|
|
|
PATHS = {
|
|
"api": f"api/acl-test-{TEST_ID}",
|
|
"worker": f"worker/acl-test-{TEST_ID}",
|
|
}
|
|
|
|
TOKENS = []
|
|
|
|
|
|
def request(method, path, token=None, payload=None):
|
|
body = None
|
|
|
|
if payload is not None:
|
|
body = json.dumps(payload).encode("utf-8")
|
|
|
|
headers = {}
|
|
|
|
if token is not None:
|
|
headers["X-Vault-Token"] = token
|
|
|
|
if body is not None:
|
|
headers["Content-Type"] = "application/json"
|
|
|
|
req = urllib.request.Request(
|
|
URL + path,
|
|
data=body,
|
|
headers=headers,
|
|
method=method,
|
|
)
|
|
|
|
with urllib.request.urlopen(
|
|
req,
|
|
context=TLS,
|
|
timeout=15,
|
|
) as response:
|
|
raw = response.read()
|
|
|
|
return (
|
|
response.status,
|
|
json.loads(raw) if raw else {},
|
|
)
|
|
|
|
|
|
def passed(message):
|
|
print("PASS:", message)
|
|
|
|
|
|
def must_deny(method, path, token, payload=None):
|
|
try:
|
|
request(method, path, token, payload)
|
|
except urllib.error.HTTPError as exc:
|
|
if exc.code == 403:
|
|
return
|
|
raise AssertionError(
|
|
f"Expected HTTP 403, received {exc.code}"
|
|
) from None
|
|
|
|
raise AssertionError("Forbidden operation unexpectedly allowed")
|
|
|
|
|
|
def login(role):
|
|
name = f"messenger-{role}"
|
|
|
|
_, role_data = request(
|
|
"GET",
|
|
f"/auth/approle/role/{name}/role-id",
|
|
ROOT_TOKEN,
|
|
)
|
|
|
|
role_id = role_data["data"]["role_id"]
|
|
|
|
_, secret_data = request(
|
|
"POST",
|
|
f"/auth/approle/role/{name}/secret-id",
|
|
ROOT_TOKEN,
|
|
{},
|
|
)
|
|
|
|
secret_id = secret_data["data"]["secret_id"]
|
|
credentials = {
|
|
"role_id": role_id,
|
|
"secret_id": secret_id,
|
|
}
|
|
|
|
_, login_data = request(
|
|
"POST",
|
|
"/auth/approle/login",
|
|
payload=credentials,
|
|
)
|
|
|
|
auth = login_data["auth"]
|
|
token = auth["client_token"]
|
|
TOKENS.append(token)
|
|
|
|
assert 0 < auth["lease_duration"] <= 1200
|
|
assert name in auth["policies"]
|
|
assert "root" not in auth["policies"]
|
|
|
|
passed(f"{role}: scoped token issued with limited TTL")
|
|
|
|
# SecretID may only be used once.
|
|
try:
|
|
request(
|
|
"POST",
|
|
"/auth/approle/login",
|
|
payload=credentials,
|
|
)
|
|
except urllib.error.HTTPError as exc:
|
|
if exc.code not in (400, 403):
|
|
raise
|
|
passed(f"{role}: SecretID reuse denied")
|
|
else:
|
|
raise AssertionError("SecretID reused successfully")
|
|
|
|
return token
|
|
|
|
|
|
def main():
|
|
print("OpenBao HTTPS:", URL)
|
|
print("TLS verification: enabled")
|
|
|
|
# Create unique synthetic secrets using operator identity.
|
|
for role, path in PATHS.items():
|
|
request(
|
|
"POST",
|
|
"/lab7/data/" + path,
|
|
ROOT_TOKEN,
|
|
{"data": {"test_role": role}},
|
|
)
|
|
|
|
passed("temporary KV v2 secrets prepared")
|
|
|
|
api_token = login("api")
|
|
worker_token = login("worker")
|
|
|
|
# Allowed: each role reads its own path.
|
|
for role, token in (
|
|
("api", api_token),
|
|
("worker", worker_token),
|
|
):
|
|
_, result = request(
|
|
"GET",
|
|
"/lab7/data/" + PATHS[role],
|
|
token,
|
|
)
|
|
|
|
assert result["data"]["data"]["test_role"] == role
|
|
passed(f"{role}: own secret read allowed")
|
|
|
|
# Denied: cross-role reads.
|
|
must_deny(
|
|
"GET",
|
|
"/lab7/data/" + PATHS["worker"],
|
|
api_token,
|
|
)
|
|
passed("API cannot read worker secrets")
|
|
|
|
must_deny(
|
|
"GET",
|
|
"/lab7/data/" + PATHS["api"],
|
|
worker_token,
|
|
)
|
|
passed("worker cannot read API secrets")
|
|
|
|
# Denied: updates, even within own path.
|
|
for role, token in (
|
|
("api", api_token),
|
|
("worker", worker_token),
|
|
):
|
|
must_deny(
|
|
"POST",
|
|
"/lab7/data/" + PATHS[role],
|
|
token,
|
|
{"data": {"test_role": "modified"}},
|
|
)
|
|
passed(f"{role}: secret modification denied")
|
|
|
|
# Denied: management of ACL policies.
|
|
for role, token in (
|
|
("api", api_token),
|
|
("worker", worker_token),
|
|
):
|
|
must_deny(
|
|
"GET",
|
|
"/sys/policies/acl/messenger-api",
|
|
token,
|
|
)
|
|
passed(f"{role}: policy administration denied")
|
|
|
|
print("PASS: all OpenBao AppRole ACL tests passed")
|
|
|
|
|
|
try:
|
|
main()
|
|
finally:
|
|
# Revoke only tokens created by this test.
|
|
for token in TOKENS:
|
|
try:
|
|
request(
|
|
"POST",
|
|
"/auth/token/revoke",
|
|
ROOT_TOKEN,
|
|
{"token": token},
|
|
)
|
|
except Exception as exc:
|
|
print(
|
|
"WARNING: token cleanup:",
|
|
type(exc).__name__,
|
|
)
|
|
|
|
# Remove only the unique test secrets.
|
|
for path in PATHS.values():
|
|
try:
|
|
request(
|
|
"DELETE",
|
|
"/lab7/metadata/" + path,
|
|
ROOT_TOKEN,
|
|
)
|
|
except Exception as exc:
|
|
print(
|
|
"WARNING: KV cleanup:",
|
|
type(exc).__name__,
|
|
)
|
|
|
|
print("OpenBao test cleanup attempted")
|