diff --git a/scripts/pki.py b/scripts/pki.py index 81dedfb..1a8fb7d 100644 --- a/scripts/pki.py +++ b/scripts/pki.py @@ -145,6 +145,18 @@ def preserve_lb(): } +def cert_auth_denied(response): + body = response.json() + if body.get("auth"): + return False + if response.status_code in {401, 403}: + return bool(body.get("errors")) + return response.status_code == 400 and any( + "no chain matching all constraints could be found" in error + for error in body.get("errors", []) + ) + + def revoke(operator, leaf): assert leaf["expires_at"] > time.time() + 5, ( "Revocation must be tested before natural expiry" @@ -213,7 +225,7 @@ def revoke(operator, leaf): "ttl": "120s", }, ) - assert auth.status_code in {401, 403}, ( + assert cert_auth_denied(auth), ( "Revoked certificate could authenticate again" ) assert issue.status_code in {401, 403}, ( @@ -227,6 +239,8 @@ def revoke(operator, leaf): "still_unexpired_at_check": leaf["expires_at"] > time.time(), "new_tls_connection_denied": True, "bao_crl_updated": True, + "bao_old_certificate_login_status": auth.status_code, + "bao_old_token_issue_status": issue.status_code, "revoked_lb_service_tokens": revoked_tokens, "old_certificate_new_auth_denied": True, "old_token_pki_issue_denied": True, diff --git a/student-unit/test_pki_denial.py b/student-unit/test_pki_denial.py new file mode 100644 index 0000000..87f837a --- /dev/null +++ b/student-unit/test_pki_denial.py @@ -0,0 +1,34 @@ +import httpx +import pytest + +from scripts.pki import cert_auth_denied + + +@pytest.mark.parametrize( + ("status", "body", "denied"), + [ + ( + 400, + { + "errors": [ + "no chain matching all constraints could be found " + "for this login certificate" + ] + }, + True, + ), + (403, {"errors": ["permission denied"]}, True), + (400, {"errors": ["invalid JSON"]}, False), + (200, {"auth": {"client_token": "synthetic-token"}}, False), + ( + 403, + { + "auth": {"client_token": "synthetic-token"}, + "errors": ["permission denied"], + }, + False, + ), + ], +) +def test_revoked_login_requires_an_actual_denial(status, body, denied): + assert cert_auth_denied(httpx.Response(status, json=body)) is denied