Lab7: HTTPS, mTLS, OpenBao, трек 2 PKI на оценку 5
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Allow/deny OpenBao policy для API AppRole (без печати token)."""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import ssl
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
CA = ROOT / "certs" / "ca.crt"
|
||||
ADDR = "https://127.0.0.1:8200"
|
||||
|
||||
|
||||
def ctx():
|
||||
c = ssl.create_default_context(cafile=str(CA))
|
||||
c.check_hostname = False
|
||||
return c
|
||||
|
||||
|
||||
def call(method, path, body=None, token=None):
|
||||
data = None
|
||||
headers = {"Accept": "application/json"}
|
||||
if body is not None:
|
||||
data = json.dumps(body).encode("utf-8")
|
||||
headers["Content-Type"] = "application/json"
|
||||
if token:
|
||||
headers["X-Vault-Token"] = token
|
||||
req = urllib.request.Request(ADDR + path, data=data, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(req, context=ctx(), timeout=5) as response:
|
||||
raw = response.read().decode("utf-8")
|
||||
return response.status, json.loads(raw) if raw else {}
|
||||
except urllib.error.HTTPError as exc:
|
||||
raw = exc.read().decode("utf-8")
|
||||
return exc.code, json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def main():
|
||||
role = (ROOT / "secrets/bao_api_role_id").read_text(encoding="utf-8").strip()
|
||||
secret = (ROOT / "secrets/bao_api_secret_id").read_text(encoding="utf-8").strip()
|
||||
code, payload = call("POST", "/v1/auth/approle/login", {"role_id": role, "secret_id": secret})
|
||||
if code >= 400 or "auth" not in payload:
|
||||
raise SystemExit("FAIL: approle login")
|
||||
token = payload["auth"]["client_token"]
|
||||
ok_code, _ = call("GET", "/v1/secret/data/messenger/api", token=token)
|
||||
deny_code, _ = call("GET", "/v1/secret/data/messenger/worker", token=token)
|
||||
if ok_code != 200:
|
||||
raise SystemExit(f"FAIL: allow path status {ok_code}")
|
||||
if deny_code not in (403, 404):
|
||||
raise SystemExit(f"FAIL: deny path expected 403/404 got {deny_code}")
|
||||
print("PASS: AppRole allow api secret; deny worker secret")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user