Implement Supabase Vault wrapper for credential storage
epic-external-system-integration-configuration-foundation-task-004 — Create the vault client layer that wraps Supabase Vault pgsodium functions for storing, retrieving, and rotating API keys. Implement store_credential(), retrieve_credential(), rotate_credential(), and delete_credential() SQL functions. Ensure plaintext is never written to the main database tables and all vault references use opaque vault secret IDs.
Acceptance Criteria
Technical Requirements
Execution Context
Tier 2 - 518 tasks
Can start after Tier 1 completes
Handles integration between different epics or system components. Requires coordination across multiple development streams.
Implementation Notes
Supabase Vault uses the vault schema and vault.secrets table backed by pgsodium. The core insert is: INSERT INTO vault.secrets (name, secret, description) VALUES (p_name, p_secret, p_description) RETURNING id. For retrieval, use: SELECT decrypted_secret FROM vault.decrypted_secrets WHERE id = p_secret_id. The decrypted_secrets view handles the pgsodium decryption transparently.
For rotate_credential(), use: UPDATE vault.secrets SET secret = pgsodium.crypto_aead_det_encrypt(convert_to(p_new_secret, 'utf8'), ...) — check Supabase Vault docs for the exact update mechanism as it may differ from direct pgsodium calls. Create a vault_access_log table (id, secret_id, accessed_at, session_user) and INSERT into it inside retrieve_credential() for auditability. Set a short COMMENT ON FUNCTION to document the security model for each function. Coordinate with task-005 which adds the vault_secret_id column to organization_integrations — this task creates the underlying Vault functions that task-005 relies on.
Testing Requirements
Integration tests against Supabase local environment with pgsodium enabled (required for Vault). Test cases: (1) store_credential() returns a valid UUID and the plaintext is not readable from vault.secrets directly by an authenticated user. (2) retrieve_credential() with the returned UUID returns the original plaintext when called as service_role. (3) retrieve_credential() called as authenticated role throws permission denied.
(4) rotate_credential() with a new value: subsequent retrieve_credential() returns new value, UUID unchanged. (5) delete_credential() removes the secret: subsequent retrieve_credential() throws not-found error. (6) delete_credential() on non-existent UUID returns cleanly without error. (7) store_credential() with NULL secret raises exception.
Wrap all tests in transactions that are rolled back to keep test environment clean.
Supabase Vault API has limited documentation for Dart/Flutter clients; wrapping it correctly for credential rotation and secret reference management may require significant trial and error, delaying the vault component and blocking all downstream credential-dependent work.
Mitigation & Contingency
Mitigation: Spike the Vault integration in the first sprint using a minimal proof-of-concept (store, retrieve, rotate one secret). Document the API surface before building the full vault client. Identify any missing Dart SDK bindings early.
Contingency: If Supabase Vault is too complex, fall back to Supabase's encrypted column approach (pgcrypto) for credential storage as a temporary measure, with a planned migration path to Vault once the API is understood.
Incorrect RLS policy configuration on organization_integrations could allow org admins of one organization to read or modify another organization's integration credentials, creating a serious data breach and compliance violation.
Mitigation & Contingency
Mitigation: Write integration tests that explicitly attempt cross-org data access using different JWT tokens and assert 0 rows returned. Include RLS policy review in PR checklist. Use Supabase's local development stack for policy validation before deployment.
Contingency: If a breach is discovered post-deployment, immediately revoke all integration credentials, rotate vault secrets, notify affected organizations, and apply emergency RLS patches.
JSONB columns for field_mappings and sync_schedule lack database-level schema enforcement; AI-generated or malformed JSON could silently corrupt integration configurations, causing export failures that are hard to diagnose.
Mitigation & Contingency
Mitigation: Define TypeScript/Dart model classes with strict deserialization and validation. Add database check constraints or triggers that validate JSONB structure at write time. Version the JSONB schema to enable forward-compatible migrations.
Contingency: Build a repair script that scans organization_integrations for invalid JSONB and resets corrupted records to a safe default state, alerting the admin of the affected organization.