Skip to content
MSO CloudDocumentation

ADR 0013 — Org-level credential store (shared connector credentials)

On this page
  • Status: Accepted (implementing)
  • Date: 2026-07-20
  • Context owner: Platform
  • Related: ADR 0008 (marketing connector family), root invariant 4 (envelope encryption), §56 (decrypt = permanent), §66 (RLS)

Context#

Every connector row (connector_credentials) embeds its own envelope-encrypted secret in four columns (credentials_ciphertext/iv/tag/key_version). The Google family (GA4, Search Console, Google Sheets — and future sheet-backed connectors) all authenticate with the SAME service account: today the operator pastes the same SA JSON into each connector, the client flattens it to {client_email, private_key, token_uri} (parseServiceAccountJson, connectors-client.tsx), and each row stores an independent copy. Rotating the key means re-pasting it N times; nothing tells the operator which connectors share an identity.

Precedents already in the codebase:

  • ai_connections — org-scoped multi-row credential CRUD with priority + partial unique index (packages/schema/src/tables/ai-connections.ts).
  • Market-data platform key — super-admin-gated shared secret with env fallback (market-intel.ts marketDataConnector).

Decision#

  1. New table org_credentials — an org-scoped, envelope-encrypted, reusable credential row: {id, org_id, brand_id?, credential_type, label, ciphertext/iv/tag/key_version, fingerprint, is_active, is_sample, created_by, timestamps}.
    • credential_type keys the auth scheme, not the platform: google_service_account is ONE type shared by ga4 / google-search-console / google-sheets (and the new sheet-backed connectors). Future: google_oauth, meta_oauth, api_key_<vendor>.
    • fingerprint is a NON-secret discriminator shown in pickers (SA → client_email; API key → masked suffix). Never derived from secret material beyond what the operator already sees.
    • RLS: same org-scoped policy family as connector_credentials; is_sample per §3.
  2. connector_credentials.org_credential_id — nullable FK. Resolution order in ONE shared helper resolveConnectorSecret(row) (used by web ping/test AND worker sync): org_credential_id set → decrypt the shared row; else → legacy embedded columns. Existing rows keep working untouched; no forced migration.
  3. Auto-create on first entry (the n8n rule). The connector create form's credential section offers [Dùng credential đã lưu ▾] | [Nhập mới]. Choosing "Nhập mới" and saving creates the org_credentials row transparently (label auto-derived, e.g. Google Service Account — mso-ynm…@….iam.gserviceaccount.com) and links the connector to it. The operator NEVER has to visit a management screen before creating a connector.
  4. Management surface /settings/credentials (capability manage_connectors): list with linked-connector counts, rename, rotate (re-paste → all linked connectors pick the new secret up on next decrypt), deactivate. Delete is blocked while connectors reference the row.
  5. Platform→accepted-credential-type mapping is data on the platform definition (PlatformDef.credentialTypes), not scattered conditionals — the picker lists only compatible saved credentials.
  6. Mutations are rate-limited + audit-logged (org.credential.create/rotate/…); secrets never appear in list/read responses (labels + fingerprints only).

Consequences#

  • Rotation becomes one operation per identity instead of one per connector; §56 decrypt-DLQ floods after a key change shrink accordingly.
  • resolveConnectorSecret is the single choke point (worker loadAndDecryptConnector, web connectors.test/testDraft, connector-ping, OAuth write-back). OAuth flows that REWRITE tokens (tiktok-shop, lazada, linkedin-page, shopee-ads rotation) keep writing to wherever the secret actually lives — shared row if linked, else the legacy columns.
  • The "one credential row per platform per brand" convention (root invariant 4) is unchanged: it governs connector_credentials rows (connector instances), not org_credentials (identities).
  • Out of scope now: cross-org/platform-level credentials, secret versioning history, automatic re-wrap tooling (covered by the existing key-rotation runbook).