134 lines
4.8 KiB
Python
134 lines
4.8 KiB
Python
import json
|
|
import os
|
|
from pathlib import Path
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from urllib.error import HTTPError, URLError
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "tests"))
|
|
sys.path.insert(0, str(ROOT / "scripts"))
|
|
from client import Client, Failure, check
|
|
import bao_operator
|
|
|
|
|
|
def compose(*args, input=None):
|
|
result = subprocess.run(["docker", "compose", *args], cwd=ROOT, input=input,
|
|
text=True, check=True, timeout=90,
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
return result.stdout.strip()
|
|
|
|
|
|
def ready(client):
|
|
deadline = time.monotonic() + 45
|
|
while True:
|
|
try:
|
|
client.call("GET", "/health/ready", schema="Health")
|
|
return
|
|
except Failure:
|
|
check(time.monotonic() < deadline, "stack did not recover")
|
|
time.sleep(0.5)
|
|
|
|
|
|
def main():
|
|
client = Client("https://localhost:18443", str(ROOT / "secrets/operator/ca.pem"))
|
|
actor = client.user("rotation")
|
|
old_url = json.loads((ROOT / "secrets/operator/kv-bootstrap.json").read_text())["api-a"]["DATABASE_URL"]
|
|
image = compose("images", "-q", "api-a")
|
|
subprocess.run([sys.executable, "scripts/rotate.py", "secret"], cwd=ROOT, check=True, timeout=90)
|
|
ready(client)
|
|
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
|
|
code = f"""
|
|
import psycopg
|
|
try:
|
|
connection=psycopg.connect({old_url!r},connect_timeout=3)
|
|
except psycopg.OperationalError as error:
|
|
assert "password authentication failed" in str(error)
|
|
print("PASS: old DB credential rejected on new connection")
|
|
else:
|
|
connection.close()
|
|
raise AssertionError("old DB credential accepted")
|
|
"""
|
|
print(compose("exec", "-T", "api-a", "python", "-", input=code), flush=True)
|
|
check(compose("images", "-q", "api-a") == image, "image changed during secret rotation")
|
|
|
|
from cryptography import x509
|
|
from unittest.mock import patch
|
|
from pki import issue
|
|
|
|
serial = (x509.random_serial_number() & ((1 << 156) - 1)) | (1 << 152)
|
|
with patch("pki.x509.random_serial_number", return_value=serial):
|
|
issue("api-a")
|
|
compose("restart", "api-a", "api-a-tls")
|
|
ready(client)
|
|
subprocess.run([sys.executable, "scripts/rotate.py", "certificate"], cwd=ROOT, check=True, timeout=90)
|
|
ready(client)
|
|
code = """
|
|
import sys
|
|
sys.path.insert(0,"/checks")
|
|
from check_security import exchange,denied,request
|
|
payload=b"POST /v1/traces HTTP/1.0\\r\\nHost: otel-collector\\r\\nContent-Length: 0\\r\\n\\r\\n"
|
|
denied(lambda: exchange("otel-collector",4319,"revoked-api-a",payload))
|
|
try:
|
|
request("https://openbao:8200/v1/auth/cert/login","revoked-api-a",{"name":"api-a"})
|
|
except Exception:
|
|
pass
|
|
else:
|
|
raise AssertionError("revoked certificate authenticated to OpenBao")
|
|
print("PASS: revoked leaf rejected by Collector and OpenBao")
|
|
"""
|
|
print(compose("run", "--rm", "--no-deps", "-T", "security-check", "-", input=code), flush=True)
|
|
check(compose("images", "-q", "api-a") == image, "image changed during certificate rotation")
|
|
|
|
state = json.loads((ROOT / "secrets/operator/bao-init.json").read_text())
|
|
bao_operator.request("auth/cert/certs/api-a", {
|
|
"certificate": (ROOT / "secrets/operator/ca.pem").read_text(),
|
|
"allowed_dns_sans": ["api-a"], "token_policies": ["api-a"],
|
|
"token_ttl": 2, "token_max_ttl": 2,
|
|
}, state["root_token"])
|
|
try:
|
|
code = """
|
|
import sys,time
|
|
sys.path.insert(0,"/checks")
|
|
from check_security import credentials,request
|
|
from urllib.error import HTTPError
|
|
values,auth=credentials("api-a")
|
|
assert auth["lease_duration"]<=2
|
|
time.sleep(3)
|
|
try:
|
|
request("https://openbao:8200/v1/secret/data/api-a","api-a",token=auth["client_token"])
|
|
except HTTPError as error:
|
|
assert error.code==403
|
|
else:
|
|
raise AssertionError("expired service token accepted")
|
|
print("PASS: expired OpenBao service token rejected")
|
|
"""
|
|
print(compose("run", "--rm", "--no-deps", "-T", "security-check", "-", input=code), flush=True)
|
|
finally:
|
|
bao_operator.main()
|
|
bao_operator.request("sys/seal", {}, state["root_token"], "PUT")
|
|
try:
|
|
code = """
|
|
import sys
|
|
sys.path.insert(0,"/checks")
|
|
from check_security import request
|
|
from urllib.error import HTTPError
|
|
try:
|
|
request("https://openbao:8200/v1/auth/cert/login","api-a",{"name":"api-a"})
|
|
except HTTPError as error:
|
|
assert error.code==503
|
|
else:
|
|
raise AssertionError("sealed OpenBao issued credentials")
|
|
print("PASS: sealed OpenBao refuses service authentication")
|
|
"""
|
|
print(compose("run", "--rm", "--no-deps", "-T", "security-check", "-", input=code), flush=True)
|
|
finally:
|
|
bao_operator.main()
|
|
client.call("GET", "/api/v1/users/me", actor=actor, schema="User")
|
|
print("PASS: original session valid after credential/certificate rotation and unseal")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|