Creates a task file outlining the automated testing strategy for the Auth-Yes platform. The plan covers testing strategies for Tier 1 & 2 (Traefik ForwardAuth), Tier 3 (ConnectRPC with SPIFFE/mTLS), RBAC Default-Deny, and the @auth-yes/sdk Hono middleware using Deno native mocking capabilities, strictly avoiding external Docker dependencies. Co-authored-by: mrteye <1945243+mrteye@users.noreply.github.com>
5.4 KiB
5.4 KiB
TASK METADATA
- Target Files:
server/main.test.ts(new)server/db.test.ts(new)server/spire_ffi.test.ts(new)server/valkey.test.ts(new)sdk/mod.test.tssdk/mod.ts
- Core Objective: Design a comprehensive multi-tier automated test harness and verification suite for Auth-Yes, validating Tier 1 & 2 (Traefik ForwardAuth), Tier 3 (ConnectRPC with SPIFFE/mTLS workload attestation), decoupled Default-Deny RBAC authorization, and
@auth-yes/sdkclient middleware using native Deno mocking. - Dependencies: Deno standard library
@std/testing/mock,@std/testing/bdd, andhono. - Additional Important Notes: Do NOT use Testcontainers or Docker dependencies. Mock external services (Postgres, Valkey, SPIFFE API) using Deno's native mocking capabilities to ensure hermetic, fast, and deterministic test execution.
Architectural Considerations & Risks
Risks
- Mocking Accuracy: Using mocks for PostgreSQL (
db.ts) and Valkey (valkey.ts) risks diverging from actual production behavior if the database schema or RESP3 client tracking intricacies change. We must ensure the mocked responses accurately reflect both success cases and edge cases (e.g., specific Valkey error codes, Postgres constraint violations). - SPIFFE Workload Mismatches: Relying on explicit Deno stubs for
extractSpiffeIdFromCertandfetchSpiffeIdentitymust be robust enough to simulate corrupted X.509 certificates or unrecognized SPIFFE IDs deterministically. If stubs aren't well isolated between tests, state bleed could cause flaky test runs. - SDK Modularity: Integrating a Hono-specific middleware into
@auth-yes/sdkrisks violating the generic nature of the SDK. TheAuthSdkcore must remain strictly agnostic, with the Hono helper imported optionally or cleanly segregated from the corevalidateSessionlogic.
Alternatives
- Alternative 1: E2E Containers (Rejected): Spinning up live Postgres, Valkey, and SPIRE agent containers using Docker/Testcontainers would provide higher fidelity but drastically slow down the test suite and introduce environment dependency friction. As requested, we are prioritizing hermetic Deno native mocking.
- Alternative 2: Full Integration Test (Hono App Mocking): Rather than unit testing individual modules in isolation, we can heavily leverage Hono's
.request()helper on the root app instance to test API endpoints (Tier 1 & 2) in memory while stubbing the underlyingsqlandvalkeyexports. This is generally preferred for testing Hono apps and is cleaner than spinning up a real HTTP server.
Proposed Implementation
Phase 1: Test Environment Mocking Architecture
- Database & Valkey Isolation:
- Create generic mock setups using
@std/testing/mock(stubandspy). - Stub the
sqlexport inserver/db.tsto return controlled JSON objects representing mock users, apps, and grants. - Stub
valkey.getandvalkey.setexinserver/valkey.tsto simulate session cache hits and misses.
- Create generic mock setups using
- SPIFFE/mTLS Mocking (Tier 3):
- Stub
extractSpiffeIdFromCertinserver/spire_ffi.ts. - Provide test cases for valid SPIFFE URIs (e.g.,
spiffe://system.local/ed-droid-backend), invalid URIs, missing extensions, and unparseable certs.
- Stub
Phase 2: Tier 1 & 2 (Edge ForwardAuth) Verification
- Target:
GET /api/forward-auth - Test Cases:
- Valid session cookie: Assert HTTP
200 OKand presence ofX-Forwarded-UserandX-Forwarded-User-Idheaders. - Missing session cookie: Assert HTTP
401 Unauthorized. - Invalid/Expired session (simulated via Valkey cache miss): Assert HTTP
401 Unauthorized. - Suspended account (simulated via mocked
sqlreturningaccount_status: 'suspended'): Assert HTTP403 Forbidden.
- Valid session cookie: Assert HTTP
Phase 3: Tier 3 (Zero-Trust Mesh & RBAC) Verification
- Target:
AuthService.validateSessionConnectRPC endpoint (POST /auth.v1.AuthService/ValidateSession). - Test Cases:
- Default-Deny: Unassigned user attempting to access a valid SPIFFE ID. Assert
valid: falseand HTTP403or ConnectRPC validation failure. - Valid RBAC Grant: Authenticated user with explicit grant for the calling app's SPIFFE ID. Assert
valid: true, correct UUID, and correct scope (e.g.,viewer,admin). - SPIFFE Attestation Failure: Missing or invalid
x-peer-certheader. Assert validation failure.
- Default-Deny: Unassigned user attempting to access a valid SPIFFE ID. Assert
Phase 4: Audit Ledger Verification
- Target:
auditLogfunction calls across endpoints. - Test Cases:
- Spy on
auditLoginserver/audit.ts. - Verify that successful logins (
login_success), login failures (login_failed), and explicit access denials generate the correct event payload structures and are dispatched to the mocked DB.
- Spy on
Phase 5: SDK Ergonomics & Hono Middleware
- Target:
sdk/mod.ts - Implementation Steps:
- Extend
sdk/mod.ts(or create an adjunct file likesdk/hono.ts) to export acreateAuthMiddleware(sdk: AuthSdk)function. - The middleware will extract the
session_idcookie orAuthorizationheader, callsdk.requireAuth(), and inject the resultinguserIdinto the Hono context (c.set('userId', userId)). - If
requireAuththrows, the middleware catches it and returns HTTP401 Unauthorized.
- Extend
- Verification:
- In
sdk/mod.test.ts, create a mock Hono app, apply the middleware, and assert that authorized requests pass and unauthorized requests are halted cleanly.
- In