Implement Lab 7 security and infrastructure
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
"""Check that AppRole tokens are scoped to their own KV v2 path."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import requests
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
BASE = "https://localhost:18200/v1"
|
||||
CA = ROOT / "secrets" / "tls" / "ca.crt"
|
||||
|
||||
|
||||
def token_for(role: str) -> str:
|
||||
role_dir = ROOT / "secrets" / "bao" / role
|
||||
response = requests.post(
|
||||
BASE + "/auth/approle/login",
|
||||
json={
|
||||
"role_id": (role_dir / "role_id").read_text().strip(),
|
||||
"secret_id": (role_dir / "secret_id").read_text().strip(),
|
||||
},
|
||||
verify=str(CA),
|
||||
timeout=5,
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()["auth"]["client_token"]
|
||||
|
||||
|
||||
def check_role(role: str, allowed: str, denied: str) -> None:
|
||||
token = token_for(role)
|
||||
headers = {"X-Vault-Token": token}
|
||||
own = requests.get(BASE + f"/secret/data/messenger/{allowed}", headers=headers, verify=str(CA), timeout=5)
|
||||
assert own.status_code == 200, f"{role} cannot read its own path"
|
||||
other = requests.get(BASE + f"/secret/data/messenger/{denied}", headers=headers, verify=str(CA), timeout=5)
|
||||
assert other.status_code == 403, f"{role} unexpectedly read {denied}"
|
||||
write = requests.post(
|
||||
BASE + f"/secret/data/messenger/{allowed}",
|
||||
headers=headers,
|
||||
json={"data": {"forbidden_write": "must-not-be-written"}},
|
||||
verify=str(CA),
|
||||
timeout=5,
|
||||
)
|
||||
assert write.status_code == 403, f"{role} unexpectedly wrote its own path"
|
||||
print(f"PASS: {role} reads {allowed}; denied {denied} and write")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
check_role("api", "api", "worker")
|
||||
check_role("worker", "worker", "api")
|
||||
check_role("outbox", "outbox", "api")
|
||||
Reference in New Issue
Block a user