forked from mtusi2026/mtusi2026lab7
Implement lab7 security and compromised worker recovery evidence
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import json
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
from tls_matrix import context
|
||||
|
||||
|
||||
def request(role, path, body=None, token=None):
|
||||
headers = {"Content-Type": "application/json"}
|
||||
if token:
|
||||
headers["X-Vault-Token"] = token
|
||||
req = urllib.request.Request(
|
||||
"https://openbao:8200/v1/" + path,
|
||||
None if body is None else json.dumps(body).encode(),
|
||||
headers,
|
||||
)
|
||||
with urllib.request.urlopen(req, context=context(role), timeout=3) as response:
|
||||
raw = response.read()
|
||||
return json.loads(raw) if raw else {}
|
||||
|
||||
|
||||
def denied(role, path, token, body=None):
|
||||
try:
|
||||
request(role, path, body, token)
|
||||
except urllib.error.HTTPError as error:
|
||||
assert error.code == 403
|
||||
return
|
||||
raise AssertionError("Scoped policy did not deny operation")
|
||||
|
||||
|
||||
def main():
|
||||
results = {}
|
||||
old = None
|
||||
started = time.monotonic()
|
||||
for role in ("api", "worker", "dispatcher"):
|
||||
auth = request(role, "auth/cert/login", {"name": role})["auth"]
|
||||
token = auth["client_token"]
|
||||
assert 0 < auth["lease_duration"] <= 60
|
||||
assert auth["policies"] == [role]
|
||||
data = request(role, "secret/data/" + role, token=token)["data"]["data"]
|
||||
assert data["postgres_user"] == role
|
||||
for other in {"api", "worker", "dispatcher"} - {role}:
|
||||
denied(role, "secret/data/" + other, token)
|
||||
denied(
|
||||
role, "sys/policies/acl/escape", token, {"policy": 'path "*" {capabilities=["sudo"]}'}
|
||||
)
|
||||
denied(role, "secret/data/" + role, token, {"data": {"escape": "denied"}})
|
||||
results[role] = dict(
|
||||
ttl=auth["lease_duration"],
|
||||
only_own_path=True,
|
||||
other_secrets_denied=True,
|
||||
policy_write_denied=True,
|
||||
secret_write_denied=True,
|
||||
)
|
||||
if old is None:
|
||||
old = (role, token)
|
||||
else:
|
||||
request(role, "auth/token/revoke-self", {}, token)
|
||||
time.sleep(max(0, 63 - (time.monotonic() - started)))
|
||||
denied(old[0], "secret/data/" + old[0], old[1])
|
||||
print(json.dumps(dict(status="PASS", policies=results, expired_token_rejected=True), indent=2))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user