test_key_status.py
python
sha256:4defb0407ee7fe26b1addb8df5cf3d31dae4ebf1ab92eeca1c05348b9d4c8458
merge: integrate R1 into the publishable #114 line (private…
Opus 5.5
minor
⚠ breaking
12 days ago
| 1 | """TDD -- musehub#223: GET /api/keys/{key_id}/status didn't exist, so |
| 2 | `muse verify-commit --check-key-status` always reported "unknown". |
| 3 | |
| 4 | Root cause of the deeper gap: revoke_key() hard-deleted the key row, so |
| 5 | there was no way to distinguish "revoked" from "never registered" even |
| 6 | once the endpoint existed. Design decision (confirmed via check-in): |
| 7 | soft-delete via a new revoked_at column, with every authentication/ |
| 8 | signature-verification query site updated to exclude revoked keys. |
| 9 | |
| 10 | Test IDs: |
| 11 | KS_01 — active key returns {"status": "active"} |
| 12 | KS_02 — revoked key returns {"status": "revoked"} |
| 13 | KS_03 — never-registered key_id returns 404 |
| 14 | KS_04 — revoke_key soft-deletes (row still exists with revoked_at set) |
| 15 | KS_05 — a revoked key's fingerprint can no longer authenticate via |
| 16 | verify_and_authenticate (the actual security-critical guarantee) |
| 17 | KS_06 — get_keys_for_identity (list_keys) excludes revoked keys |
| 18 | KS_07 — the last active key cannot be revoked (409) |
| 19 | """ |
| 20 | from __future__ import annotations |
| 21 | |
| 22 | import base64 |
| 23 | import hashlib as _hashlib |
| 24 | from datetime import datetime, timezone |
| 25 | |
| 26 | import pytest |
| 27 | from httpx import AsyncClient |
| 28 | from sqlalchemy import select |
| 29 | from sqlalchemy.ext.asyncio import AsyncSession |
| 30 | |
| 31 | from muse.core.types import encode_pubkey |
| 32 | from musehub.core.genesis import compute_identity_id, compute_key_id |
| 33 | from musehub.crypto.keys import key_fingerprint |
| 34 | from musehub.db.musehub_auth_models import MusehubAuthKey |
| 35 | from musehub.db.musehub_identity_models import MusehubIdentity |
| 36 | from musehub.services.musehub_auth import AuthError, get_keys_for_identity, revoke_key |
| 37 | |
| 38 | pytestmark = pytest.mark.asyncio |
| 39 | |
| 40 | _NOW = datetime.now(timezone.utc) |
| 41 | _COUNTER: list[int] = [0] |
| 42 | |
| 43 | |
| 44 | def _uid(tag: str = "") -> str: |
| 45 | _COUNTER[0] += 1 |
| 46 | return f"ks{tag}{_COUNTER[0]}" |
| 47 | |
| 48 | |
| 49 | def _key_for(handle: str) -> str: |
| 50 | raw = _hashlib.sha256(handle.encode()).digest() |
| 51 | return encode_pubkey("ed25519", raw) |
| 52 | |
| 53 | |
| 54 | async def _seed_identity_with_key(session: AsyncSession, handle: str) -> tuple[MusehubIdentity, MusehubAuthKey]: |
| 55 | pubkey_b64 = _key_for(handle) |
| 56 | identity_id = compute_identity_id(handle.encode()) |
| 57 | identity = MusehubIdentity( |
| 58 | identity_id=identity_id, |
| 59 | handle=handle, |
| 60 | identity_type="human", |
| 61 | is_admin=False, |
| 62 | agent_capabilities=[], |
| 63 | pinned_repo_ids=[], |
| 64 | is_verified=False, |
| 65 | created_at=_NOW, |
| 66 | updated_at=_NOW, |
| 67 | ) |
| 68 | session.add(identity) |
| 69 | await session.flush() |
| 70 | |
| 71 | _, raw_b64 = pubkey_b64.split(":", 1) |
| 72 | raw_bytes = base64.urlsafe_b64decode(raw_b64 + "==") |
| 73 | fp = key_fingerprint(raw_bytes) |
| 74 | key = MusehubAuthKey( |
| 75 | key_id=compute_key_id(identity_id, pubkey_b64), |
| 76 | identity_id=identity_id, |
| 77 | algorithm="ed25519", |
| 78 | public_key_b64=pubkey_b64, |
| 79 | fingerprint=fp, |
| 80 | label="test-key", |
| 81 | created_at=_NOW, |
| 82 | ) |
| 83 | session.add(key) |
| 84 | await session.flush() |
| 85 | return identity, key |
| 86 | |
| 87 | |
| 88 | async def _add_spare_key(session: AsyncSession, identity_id: str, handle: str) -> None: |
| 89 | """Give the identity a second active key so its first key may be revoked. |
| 90 | |
| 91 | The server refuses to revoke an identity's last active key (security R1). |
| 92 | """ |
| 93 | pubkey_b64 = _key_for(f"{handle}-spare") |
| 94 | _, raw_b64 = pubkey_b64.split(":", 1) |
| 95 | session.add(MusehubAuthKey( |
| 96 | key_id=compute_key_id(identity_id, pubkey_b64), |
| 97 | identity_id=identity_id, |
| 98 | algorithm="ed25519", |
| 99 | public_key_b64=pubkey_b64, |
| 100 | fingerprint=key_fingerprint(base64.urlsafe_b64decode(raw_b64 + "==")), |
| 101 | label="spare-key", |
| 102 | created_at=_NOW, |
| 103 | )) |
| 104 | await session.flush() |
| 105 | |
| 106 | |
| 107 | # ── KS_01 — active key ──────────────────────────────────────────────────────── |
| 108 | |
| 109 | |
| 110 | async def test_ks01_active_key_status(client: AsyncClient, db_session: AsyncSession) -> None: |
| 111 | handle = f"active-{_uid()}" |
| 112 | _, key = await _seed_identity_with_key(db_session, handle) |
| 113 | await db_session.commit() |
| 114 | |
| 115 | resp = await client.get(f"/api/keys/{key.key_id}/status") |
| 116 | assert resp.status_code == 200, resp.text |
| 117 | assert resp.json()["status"] == "active" |
| 118 | |
| 119 | |
| 120 | # ── KS_02 — revoked key ─────────────────────────────────────────────────────── |
| 121 | |
| 122 | |
| 123 | async def test_ks02_revoked_key_status(client: AsyncClient, db_session: AsyncSession) -> None: |
| 124 | handle = f"revoked-{_uid()}" |
| 125 | identity, key = await _seed_identity_with_key(db_session, handle) |
| 126 | await _add_spare_key(db_session, identity.identity_id, handle) |
| 127 | await db_session.commit() |
| 128 | |
| 129 | revoked = await revoke_key(db_session, identity.identity_id, key.key_id) |
| 130 | assert revoked is True |
| 131 | await db_session.commit() |
| 132 | |
| 133 | resp = await client.get(f"/api/keys/{key.key_id}/status") |
| 134 | assert resp.status_code == 200, resp.text |
| 135 | assert resp.json()["status"] == "revoked" |
| 136 | |
| 137 | |
| 138 | # ── KS_03 — unknown key_id ──────────────────────────────────────────────────── |
| 139 | |
| 140 | |
| 141 | async def test_ks03_unknown_key_id_returns_404(client: AsyncClient) -> None: |
| 142 | resp = await client.get(f"/api/keys/sha256:nonexistent-{_uid()}/status") |
| 143 | assert resp.status_code == 404, resp.text |
| 144 | |
| 145 | |
| 146 | # ── KS_04 — soft-delete, not hard-delete ───────────────────────────────────── |
| 147 | |
| 148 | |
| 149 | async def test_ks04_revoke_key_soft_deletes(db_session: AsyncSession) -> None: |
| 150 | handle = f"soft-{_uid()}" |
| 151 | identity, key = await _seed_identity_with_key(db_session, handle) |
| 152 | await _add_spare_key(db_session, identity.identity_id, handle) |
| 153 | await db_session.commit() |
| 154 | |
| 155 | revoked = await revoke_key(db_session, identity.identity_id, key.key_id) |
| 156 | assert revoked is True |
| 157 | await db_session.commit() |
| 158 | |
| 159 | row = (await db_session.execute( |
| 160 | select(MusehubAuthKey).where(MusehubAuthKey.key_id == key.key_id) |
| 161 | )).scalar_one_or_none() |
| 162 | assert row is not None, "revoke_key must not hard-delete the row" |
| 163 | assert row.revoked_at is not None |
| 164 | |
| 165 | |
| 166 | # ── KS_05 — revoked key can never authenticate again ───────────────────────── |
| 167 | |
| 168 | |
| 169 | async def test_ks05_revoked_key_cannot_authenticate(db_session: AsyncSession) -> None: |
| 170 | from musehub.services.musehub_auth import verify_and_authenticate |
| 171 | |
| 172 | handle = f"authrevoked-{_uid()}" |
| 173 | identity, key = await _seed_identity_with_key(db_session, handle) |
| 174 | await _add_spare_key(db_session, identity.identity_id, handle) |
| 175 | await db_session.commit() |
| 176 | |
| 177 | revoked = await revoke_key(db_session, identity.identity_id, key.key_id) |
| 178 | assert revoked is True |
| 179 | await db_session.commit() |
| 180 | |
| 181 | # Confirm the fingerprint lookup used by the login/registration path |
| 182 | # genuinely excludes this now-revoked row. |
| 183 | active_match = (await db_session.execute( |
| 184 | select(MusehubAuthKey).where( |
| 185 | MusehubAuthKey.fingerprint == key.fingerprint, |
| 186 | MusehubAuthKey.revoked_at.is_(None), |
| 187 | ) |
| 188 | )).scalar_one_or_none() |
| 189 | assert active_match is None, ( |
| 190 | "a revoked key's fingerprint must not match the active-key lookup " |
| 191 | "used by verify_and_authenticate -- otherwise a revoked key could " |
| 192 | "still authenticate" |
| 193 | ) |
| 194 | |
| 195 | |
| 196 | # ── KS_06 — list_keys excludes revoked ──────────────────────────────────────── |
| 197 | |
| 198 | |
| 199 | async def test_ks06_get_keys_for_identity_excludes_revoked(db_session: AsyncSession) -> None: |
| 200 | handle = f"listexcl-{_uid()}" |
| 201 | identity, key = await _seed_identity_with_key(db_session, handle) |
| 202 | await _add_spare_key(db_session, identity.identity_id, handle) |
| 203 | await db_session.commit() |
| 204 | |
| 205 | before = await get_keys_for_identity(db_session, identity.identity_id) |
| 206 | assert any(k.key_id == key.key_id for k in before) |
| 207 | |
| 208 | await revoke_key(db_session, identity.identity_id, key.key_id) |
| 209 | await db_session.commit() |
| 210 | |
| 211 | after = await get_keys_for_identity(db_session, identity.identity_id) |
| 212 | assert not any(k.key_id == key.key_id for k in after), ( |
| 213 | "a revoked key must not appear in get_keys_for_identity (list_keys)" |
| 214 | ) |
| 215 | |
| 216 | |
| 217 | # ── KS_07 — the last active key cannot be revoked ──────────────────────────── |
| 218 | |
| 219 | |
| 220 | async def test_ks07_last_active_key_cannot_be_revoked(db_session: AsyncSession) -> None: |
| 221 | handle = f"lastkey-{_uid()}" |
| 222 | identity, key = await _seed_identity_with_key(db_session, handle) |
| 223 | await db_session.commit() |
| 224 | |
| 225 | with pytest.raises(AuthError) as exc_info: |
| 226 | await revoke_key(db_session, identity.identity_id, key.key_id) |
| 227 | assert exc_info.value.status_code == 409 |
| 228 | |
| 229 | after = await get_keys_for_identity(db_session, identity.identity_id) |
| 230 | assert any(k.key_id == key.key_id for k in after), "the only key must stay active" |
File History
3 commits
sha256:4defb0407ee7fe26b1addb8df5cf3d31dae4ebf1ab92eeca1c05348b9d4c8458
merge: integrate R1 into the publishable #114 line (private…
Opus 5.5
minor
⚠
12 days ago
sha256:83227647a82e1733a20b0b6d225df3d153769630d3c710d255e0c1b8aab8ae61
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Opus 5.5
minor
⚠
13 days ago
sha256:0ce9daa6cc1a86e370a7c023184881065b392d52dd276b224582d7b936d9644a
feat: GET /api/keys/{key_id}/status + key revocation soft-d…
Sonnet 5
minor
⚠
25 days ago