Files
accounted/supabase/migrations/20260831190000_connector_keys.sql
T
0ff1b05553 feat(connect): hosted connector-key registry + validate RPC + entitlements endpoint; instance sync writes connector grants hourly (#1748)
* feat(entitlements): partition the self-host bypass so connector capabilities fall through to grants; capability_grants.source accepts 'connector'

Sovereign plan WS3 PR3: ships dark, nothing changes for hosted.

- lib/entitlements/keys.ts: CONNECTOR_CAPABILITIES = bank_sync,
  skatteverket, org_lookup, migration (services Accounted operates that a
  self-hosted instance cannot provide itself) + isConnectorCapability().
  Separate from PAID_CAPABILITIES and outside the trial-seed trigger on
  purpose: a hosted company can never hold a connector grant.
- lib/entitlements/has-capability.ts: isPaywallBypassed() -> isBypassedFor(key).
  Hosted: byte-identical (dev / DISABLE_PAYWALL bypass, FORCE_PAYWALL wins,
  else the grant lookup). Self-host: local capabilities always on
  (FORCE_PAYWALL included, as the existing test demands); connector
  capabilities behave like hosted, i.e. dev bypass, FORCE_PAYWALL, else the
  grant lookup where the connector sync will write source='connector' rows.
  getCompanyEntitlements on a self-host: local paid keys + active connector
  keys, state 'paid' with an active connector grant else 'none' (never the
  hosted trial copy).
- Migration 20260820122000: capability_grants.source CHECK gains
  'connector', found through pg_constraint (the CHECK was declared inline
  and auto-named; Postgres stores IN as = ANY, matched accordingly).
  pg-real test: connector accepted, unknown source rejected, upsert on the
  (scope, key, source) identity, trial seed writes no connector rows.
- Tests: self-hosted connector matrix (local all-on without DB, connector
  gated by grant/expiry, dev bypass all-on, FORCE_PAYWALL gates connector
  keys only, bulk resolution, entitlements shape); two pre-existing tests
  that asserted the old "self-host holds connector keys" contract updated
  to the new one.

Verified: full unit suite green, pg-real suite for lib/entitlements green
against a local supabase/postgres with every migration applied, lint
ratchet, guards. Deferred to the instance-wiring PR: adding the connector
extensions to the self-host Docker preset (dead-end upsells until a key can
be issued).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* refactor(entitlements): fold the self-host branch into the existing grants query

One .or(scopeFilter), not two: the duplicated helper pushed the
no-phantom-columns unresolvable-expression count to 380/379. Behaviour is
unchanged; the self-host matrix tests still pass.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* feat(connect): hosted connector-key registry + validate RPC + entitlements endpoint; instance sync writes connector grants hourly

Sovereign plan WS3 PR4 ("key infra enabling manual sales"), stacked on the
entitlement partition (#1747). Nothing is purchasable yet; this is the
plumbing both ends need before the first manually issued key.

Hosted side:
- Migration 20260820123000: connector_keys (SHA-256 key_hash, prefix,
  org_number, pinned instance_url, scopes, status, Stripe ids,
  current_period_end, per-minute rate limit, active_company_count,
  last_seen/synced) and connector_usage_events (per-request metering,
  separate from metered_events whose company_id references hosted
  companies). RLS on, NO policies: service role only. RPC
  validate_and_increment_connector_key copies the api_keys pattern (FOR
  UPDATE, minute window, suspended reported not counted, revoked = no row)
  and is REVOKEd from PUBLIC/anon/authenticated, GRANTed to service_role.
  pg-real test covers validate/count, unknown+revoked, suspended, rate
  limit, execute privileges per role, RLS invisibility, usage cascade.
- lib/connect/contract.ts (shared wire types), lib/connect/hosted/keys.ts
  (generate/hash/validate -> 401/403/429 mapping),
  with-connector-auth.ts (Bearer or X-Connector-Key, one usage row per
  request, 500 envelope on handler throw), /api/connect/entitlements GET +
  POST (records active_company_count, pins instance_url on first report,
  never moves a pinned one), scripts/issue-connector-key.ts (dry run unless
  --confirm, prints the key once + the .env lines).

Instance side:
- lib/connect/instance/config.ts (GNUBOK_CONNECTOR_KEY, GNUBOK_CONNECT_URL
  default https://app.gnubok.se), sync.ts: reports the active company count
  and writes source='connector' grants for every company x covered scope,
  expires_at = min(now+72h, period_end+3d); 401/403 or a non-active status
  deletes them (freeze-and-retain); network/5xx/429 leave them alone.
  /api/connector/sync/cron (hourly) runs it; not_configured without a key.
- Crontab generator gains EXTRA_JOBS (variant-only jobs not in vercel.json,
  with reasons) + drift tests; docker/crontab.self-hosted regenerated with
  the hourly sync. Docs (SELF-HOSTING connector section, env templates),
  DECISIONS.

Tests: 52 new unit tests (keys, auth wrapper, route, config, sync outcomes
and grant arithmetic, cron route, crontab EXTRA_JOBS) + 7 pg-real tests
run locally against supabase/postgres with every migration applied.
no-phantom-columns ceiling +1 with a reason (the bulk grant upsert).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* chore(connect): update pg test to re-versioned migration 20260831190000

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UzNkSsR18pLFitJdYn8QEb

* fix(connect): RPC errors answer 503 not 401; X-Connector-Key wins over Authorization

A hosted DB error mapped to 401 made the instance sync treat a pooler
blip as key revocation and delete its entire connector grant cache,
zeroing the 72h offline grace. 503 lands in the sync's keep-grants
branch (already test-pinned). Bearer-first extraction hashed the
upstream token on dual-header proxied calls, 401ing the exact shape
X-Connector-Key exists for.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UzNkSsR18pLFitJdYn8QEb

* fix(connect): sync deletes grants only on a body-proven connector rejection, never bare 401/403

A WAF challenge page, edge deployment protection, or an egress proxy
answers 401/403 without the hosted app ever running; trusting status
alone wiped the instance's 72h offline grant cache within the hour.
Deletion now requires the hosted route's own rejection code in the
JSON body; codeless 401/403 keeps grants (server_error branch).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UzNkSsR18pLFitJdYn8QEb

* fix(connect): PR #1748 review batch: https-only connect URL, atomic pin, prefix-gated Bearer, deferred metering, entitlements validation, integer months

- GNUBOK_CONNECT_URL must be https (http only for loopback); invalid or
  plaintext URLs disable the connector instead of sending the key.
- instance_url pin update filters on IS NULL; a lost race re-reads and
  reports the winner's pin.
- extractConnectorKey: a Bearer is the connector credential only with
  the gnubok_ck_ prefix; upstream Bearer falls through to X-Connector-Key.
- Usage metering runs via after() off the response path (inline outside
  a request scope).
- Sync validates entitlements shape: unknown status or malformed
  current_period_end keeps grants (server_error), never deletes.
- issue-connector-key rejects fractional --months.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UzNkSsR18pLFitJdYn8QEb

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: Emil <emilmattsson14@gmail.com>
2026-08-31 20:51:17 +02:00

176 lines
7.9 KiB
PL/PgSQL

-- Connector keys: the hosted side of the sovereign self-host subscription.
--
-- A self-hosted Accounted instance runs everything itself except the services
-- only Accounted can operate (bank sync via our PSD2/AISP credentials, the
-- Skatteverket API client, TIC org lookup, the migration gateway). A
-- connector key (`gnubok_ck_...`) is what an instance presents to the hosted
-- proxy endpoints under /api/connect/*; this table is the registry of those
-- keys, the proxy's rate-limit state, and the billing hook for the
-- per-active-company subscription. The instance never stores accounting data
-- here: bank/SKV tokens and the ledger stay in the instance's own database,
-- the proxy is stateless. Enforcement is key auth at the proxy, never a
-- call-home licence check inside the AGPL code.
--
-- Service-role only: RLS is enabled with NO policies, so neither anon nor
-- authenticated can read a row (the hosted UI has no connector-key surface;
-- keys are issued by scripts/issue-connector-key.ts and, later, the Stripe
-- webhook). The validate RPC is SECURITY DEFINER and executable by
-- service_role alone (see the REVOKE/GRANT at the end): this is the lesson
-- of the SECURITY DEFINER exposure audit, applied up front.
CREATE TABLE public.connector_keys (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
-- SHA-256 of the key. The key itself is shown once at issue time and never
-- stored: the hash IS the credential lookup (same reasoning as api_keys).
key_hash text NOT NULL UNIQUE,
-- Display prefix: 'gnubok_ck_' + 8 chars, safe to show in lists/logs.
key_prefix text NOT NULL,
-- The licensee. Ten digits, no hyphen.
org_number text NOT NULL,
licensee_name text,
-- The self-hosted instance this key belongs to (pinned: callbacks and
-- usage are only ever honoured for this origin). NULL until first sync
-- pins it for a manually issued key.
instance_url text,
-- Capability keys the subscription covers. Subset of
-- CONNECTOR_CAPABILITIES in lib/entitlements/keys.ts.
scopes text[] NOT NULL DEFAULT ARRAY['bank_sync', 'skatteverket', 'org_lookup', 'migration'],
status text NOT NULL DEFAULT 'active' CHECK (status IN ('active', 'suspended', 'revoked')),
-- Billing hooks (Stripe self-serve comes later; manual keys leave these NULL).
stripe_customer_id text,
stripe_subscription_id text,
-- End of the paid period. The instance's grants expire at
-- min(now + 72h, current_period_end + 3d): a lapsed period freezes the
-- connector capabilities within days even if the sync keeps running.
current_period_end timestamptz,
-- Proxy rate limit, per key (requests per minute), same window scheme as
-- api_keys.
rate_limit_rpm integer NOT NULL DEFAULT 600,
request_count integer NOT NULL DEFAULT 0,
rate_limit_window_start timestamptz,
-- Quantity billing input: the instance reports its active company count on
-- every sync.
active_company_count integer NOT NULL DEFAULT 0,
last_seen_at timestamptz,
last_synced_at timestamptz,
notes text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
revoked_at timestamptz
);
CREATE INDEX idx_connector_keys_org_number ON public.connector_keys (org_number);
CREATE INDEX idx_connector_keys_status ON public.connector_keys (status) WHERE revoked_at IS NULL;
ALTER TABLE public.connector_keys ENABLE ROW LEVEL SECURITY;
-- No policies on purpose: service role only.
CREATE TRIGGER set_updated_at_connector_keys
BEFORE UPDATE ON public.connector_keys
FOR EACH ROW EXECUTE FUNCTION public.update_updated_at_column();
-- Usage metering per key: one row per proxied request. Kept separate from
-- metered_events on purpose: that table's company_id references HOSTED
-- companies, and a connector key belongs to an instance, not a company here.
CREATE TABLE public.connector_usage_events (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
connector_key_id uuid NOT NULL REFERENCES public.connector_keys(id) ON DELETE CASCADE,
-- Which hosted service answered: entitlements | bank | skatteverket | org | migration.
service text NOT NULL,
endpoint text,
status_code integer,
occurred_at timestamptz NOT NULL DEFAULT now(),
metadata jsonb NOT NULL DEFAULT '{}'::jsonb
);
CREATE INDEX idx_connector_usage_events_key_time
ON public.connector_usage_events (connector_key_id, occurred_at DESC);
ALTER TABLE public.connector_usage_events ENABLE ROW LEVEL SECURITY;
-- No policies on purpose: service role only.
-- Atomic validate + rate-limit, the connector twin of
-- validate_and_increment_api_key (20260621130000). Returns no row for an
-- unknown or revoked key (caller answers 401), the row with its status for a
-- suspended key (caller answers 403), and rate_limited = true when the
-- per-minute window is exhausted (caller answers 429).
CREATE OR REPLACE FUNCTION public.validate_and_increment_connector_key(p_key_hash text)
RETURNS TABLE(
connector_key_id uuid,
org_number text,
instance_url text,
scopes text[],
status text,
current_period_end timestamptz,
rate_limited boolean
)
LANGUAGE plpgsql SECURITY DEFINER
SET search_path = public
AS $$
DECLARE
v_id uuid;
v_org_number text;
v_instance_url text;
v_scopes text[];
v_status text;
v_period_end timestamptz;
v_rate_limit_rpm integer;
v_request_count integer;
v_window_start timestamptz;
BEGIN
SELECT ck.id, ck.org_number, ck.instance_url, ck.scopes, ck.status, ck.current_period_end,
ck.rate_limit_rpm, ck.request_count, ck.rate_limit_window_start
INTO v_id, v_org_number, v_instance_url, v_scopes, v_status, v_period_end,
v_rate_limit_rpm, v_request_count, v_window_start
FROM public.connector_keys ck
WHERE ck.key_hash = p_key_hash
AND ck.revoked_at IS NULL
AND ck.status <> 'revoked'
FOR UPDATE;
IF v_id IS NULL THEN
RETURN; -- unknown or revoked: caller answers 401
END IF;
-- A suspended key is reported, not rate-counted: the caller answers 403.
IF v_status <> 'active' THEN
UPDATE public.connector_keys SET last_seen_at = now() WHERE id = v_id;
RETURN QUERY SELECT v_id, v_org_number, v_instance_url, v_scopes, v_status, v_period_end, false;
RETURN;
END IF;
IF v_window_start IS NULL OR v_window_start < now() - interval '1 minute' THEN
UPDATE public.connector_keys
SET request_count = 1,
rate_limit_window_start = now(),
last_seen_at = now()
WHERE id = v_id;
RETURN QUERY SELECT v_id, v_org_number, v_instance_url, v_scopes, v_status, v_period_end, false;
RETURN;
END IF;
IF v_request_count >= v_rate_limit_rpm THEN
RETURN QUERY SELECT v_id, v_org_number, v_instance_url, v_scopes, v_status, v_period_end, true;
RETURN;
END IF;
UPDATE public.connector_keys
SET request_count = request_count + 1,
last_seen_at = now()
WHERE id = v_id;
RETURN QUERY SELECT v_id, v_org_number, v_instance_url, v_scopes, v_status, v_period_end, false;
END;
$$;
-- Service role only. Supabase grants EXECUTE on new public functions to PUBLIC
-- and explicitly to anon/authenticated; anon is a member of PUBLIC, so both
-- must be revoked for the revoke to bite.
REVOKE ALL ON FUNCTION public.validate_and_increment_connector_key(text) FROM PUBLIC, anon, authenticated;
GRANT EXECUTE ON FUNCTION public.validate_and_increment_connector_key(text) TO service_role;
COMMENT ON TABLE public.connector_keys IS
'Hosted registry of gnubok_ck_ connector keys presented by self-hosted instances at /api/connect/*. Service-role only.';
COMMENT ON TABLE public.connector_usage_events IS
'Per-request metering for connector keys (entitlements, bank, skatteverket, org, migration). Service-role only.';