forked from mtusi2026/mtusi2026lab7
Добавил TLS и защиту мессенджера для лабораторной 7
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Проверка AppRole: own KV path разрешён, чужой path и запись запрещены."""
|
||||
import json
|
||||
import ssl
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
|
||||
BASE = "https://openbao:8200"
|
||||
CA = "/run/pki/ca.crt"
|
||||
IDENTITY = Path("/run/identity")
|
||||
|
||||
|
||||
def call(path, method="GET", payload=None, token=None):
|
||||
data = json.dumps(payload).encode() if payload is not None else None
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if token:
|
||||
headers["X-Vault-Token"] = token
|
||||
request = urllib.request.Request(BASE + "/v1/" + path, data=data, headers=headers, method=method)
|
||||
context = ssl.create_default_context(cafile=str(CA))
|
||||
try:
|
||||
with urllib.request.urlopen(request, context=context, timeout=4) as response:
|
||||
return response.status
|
||||
except urllib.error.HTTPError as exc:
|
||||
return exc.code
|
||||
|
||||
|
||||
def expect(condition, label):
|
||||
if not condition:
|
||||
raise SystemExit("FAIL: " + label)
|
||||
print("PASS: " + label)
|
||||
|
||||
|
||||
role = (IDENTITY / "bao-api-role-id").read_text().strip()
|
||||
secret = (IDENTITY / "bao-api-secret-id").read_text().strip()
|
||||
with urllib.request.urlopen(
|
||||
urllib.request.Request(
|
||||
BASE + "/v1/auth/approle/login",
|
||||
data=json.dumps({"role_id": role, "secret_id": secret}).encode(),
|
||||
headers={"Content-Type": "application/json"},
|
||||
method="POST",
|
||||
),
|
||||
context=ssl.create_default_context(cafile=str(CA)),
|
||||
timeout=4,
|
||||
) as response:
|
||||
token = json.load(response)["auth"]["client_token"]
|
||||
|
||||
expect(call("secret/data/api", token=token) == 200, "API reads its own scoped secret")
|
||||
expect(call("secret/data/worker", token=token) == 403, "API cannot read worker secret")
|
||||
expect(call("secret/data/api", method="POST", payload={"data": {"x": "y"}}, token=token) == 403,
|
||||
"API cannot write its secret path")
|
||||
Reference in New Issue
Block a user