feat: rotate and revoke credentials on the running stand

Add operator commands for leaf and secret rotation, identity revocation
and CRL renewal without rebuilding images, and a drill under load.
A rejected PostgreSQL role password is now 503 instead of 500.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-01 06:46:55 +03:00
co-authored by Claude Opus 5.5
parent 21b8a08e46
commit c40fbef36a
40 changed files with 5804 additions and 127 deletions
+67
View File
@@ -326,6 +326,73 @@ async def test_handshake_outcomes(
]
def _server_reason(root: Path, cert: tuple[Path, Path]) -> str:
"""Причина отказа клиенту контекстом сервера API (рукопожатие через
буферы в памяти, без uvicorn)."""
to_server, to_client = ssl.MemoryBIO(), ssl.MemoryBIO()
context = ssl.create_default_context(cafile=root / "stand" / "ca.crt")
context.load_cert_chain(certfile=cert[0], keyfile=cert[1])
client = context.wrap_bio(to_client, to_server, server_hostname="api")
server = build_server_context(_stand_tls(root)).wrap_bio(
to_server,
to_client,
server_side=True,
)
for _ in range(4):
with contextlib.suppress(ssl.SSLWantReadError):
client.do_handshake()
try:
server.do_handshake()
except ssl.SSLWantReadError:
continue
except ssl.SSLCertVerificationError as e:
return e.verify_message
return "accepted"
return "no outcome"
async def test_expired_client_leaf(
pki: Path,
mtls_server: tuple[int, _Recorder],
) -> None:
"""Сценарий «Истёкший leaf» (change `rotation-revocation`, design, решение
12): leaf разрешённой identity с концом срока через секунды принят до него
и отвергнут после."""
port, recorder = mtls_server
end = time.time() + 4
_pki(
{
"OPENSSL": _openssl() or "openssl",
"PKI_OPERATOR_DIR": str(pki / "op"),
"PKI_STAND_DIR": str(pki / "stand"),
},
"issue",
"haproxy",
"--profile",
"client",
"--san",
"haproxy",
"--not-after",
time.strftime("%Y%m%d%H%M%SZ", time.gmtime(end)),
"--out",
str(pki / "short"),
)
cert = (pki / "short" / "haproxy.crt", pki / "short" / "haproxy.key")
before = await asyncio.to_thread(_client_exchange, port, pki, cert)
assert before.startswith("HTTP/1.1 200"), before
await asyncio.sleep(max(0.0, end - time.time()) + 1.5)
after = await asyncio.to_thread(_client_exchange, port, pki, cert)
assert not after.startswith("HTTP/"), after
# alert asyncio не отправляет: причину называет контекст сервера
reason = await asyncio.to_thread(_server_reason, pki, cert)
assert reason == "certificate has expired"
stand = pki / "stand"
fresh = (stand / "haproxy.crt", stand / "haproxy.key")
alive = await asyncio.to_thread(_client_exchange, port, pki, fresh)
assert alive.startswith("HTTP/1.1 200"), alive
assert recorder.records == []
@pytest.fixture(scope="module")
def revocation(tmp_path_factory: pytest.TempPathFactory) -> Path:
"""Свой PKI (общий `pki` не трогаем): отозванный `haproxy` (копия пары до