supabase / wrappers

Postgres Foreign Data Wrapper development framework in Rust.
http://fdw.dev
Apache License 2.0
562 stars 56 forks source link

Cannot fetch api_key_id from vault using stripe fdw #266

Closed maxmckenzie closed 5 months ago

maxmckenzie commented 6 months ago

Bug report

Describe the bug

When trying to set up the stripe fdw with the api key in the vault schema (only tested locally) The stripe fdw does not show any data and returns invalid secret id <key_ID>: failed to parse a UUID

I've followed the docs to set up the stripe FDW locally, I've created a migration with the cli supabase migration new stripe-fdw

Then written a basic migration

Example

create schema if not exists stripe;
create extension if not exists wrappers with schema extensions;
create foreign data wrapper stripe_wrapper
  handler stripe_fdw_handler
  validator stripe_fdw_validator;

insert into vault.secrets (name, secret)
values (
  'stripe',
  'sk_test_1234' -- replace with your sk test key this is just an example
)
returning key_id;

create server stripe_server
  foreign data wrapper stripe_wrapper
  options (
    api_key_id '<key_ID>'
  );

create foreign table stripe.events (
  id text,
  type text,
  api_version text,
  created timestamp,
  attrs jsonb
)
  server stripe_server
  options (
    object 'events'
  );

However i get this error everytime i try to view the data in the stripe schema. the key is indeed present in the vault table.

Error: ERROR:  HV000: invalid secret id "&lt;key_ID&gt;": failed to parse a UUID
DETAIL:  Wrappers
Try refreshing your browser, but if the issue persists, please reach out to us via support.
image

However if you hard code the key like below it works.

create server stripe_server
  foreign data wrapper stripe_wrapper
  options (
    api_key_id 'sk_test_1234'  -- replace with your sk test key this is just an example
  );

Attempted to fix in the following ways

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. set up a new supabase project mkdir test && cd test && supabase init
  2. start supabase supabase start
  3. create a migration with the CLI supabase migration new stripe-fdw
  4. paste the SQL example from above
  5. reset the db to run the migration supabase db reset
  6. go to the studio URL and check the stripe schema and the events table
  7. it should error and not return results.

Expected behavior

I expect the stripe data to be there and for it to correctly retireve the api key form the vault

System information

imor commented 6 months ago

The key_id returned by this query:

insert into vault.secrets (name, secret)
values (
  'stripe',
  'sk_test_1234' -- replace with your sk test key this is just an example
)
returning key_id;

Should be used in place of <key_id> in the following code:

create server stripe_server
  foreign data wrapper stripe_wrapper
  options (
    api_key_id '<key_id>'
  );

So that the actual query looks something like:

create server stripe_server
  foreign data wrapper stripe_wrapper
  options (
    api_key_id '34661cd0-6e94-490a-8269-e3475446a9b5'
  );

Assuming the insert into vault.secrets... query returned 34661cd0-6e94-490a-8269-e3475446a9b5 key_id.

maxmckenzie commented 5 months ago

The key_id returned by this query:

insert into vault.secrets (name, secret)
values (
  'stripe',
  'sk_test_1234' -- replace with your sk test key this is just an example
)
returning key_id;

Should be used in place of <key_id> in the following code:

create server stripe_server
  foreign data wrapper stripe_wrapper
  options (
    api_key_id '<key_id>'
  );

So that the actual query looks something like:

create server stripe_server
  foreign data wrapper stripe_wrapper
  options (
    api_key_id '34661cd0-6e94-490a-8269-e3475446a9b5'
  );

Assuming the insert into vault.secrets... query returned 34661cd0-6e94-490a-8269-e3475446a9b5 key_id.

Thanks for the reply.

It's worth noting i'm running this as a migration that runs on supabase start, So i'm trying to retrieve the key_id and then have it passed to the create server command from just an SQL statement without me being involved (I'm not running any sql statements via the supabase dashboard).

I think i need to store the key_id as a variable in my SQL statement with DECLARE? This is what i thought was happening in the docs, but i misunderstood.

imor commented 5 months ago

Yeah, you'll need to store it in a variable and use that.