Skip to content

Add pre-config hook#2803

Merged
steve-chavez merged 2 commits intoPostgREST:mainfrom
steve-chavez:pre-config
May 28, 2023
Merged

Add pre-config hook#2803
steve-chavez merged 2 commits intoPostgREST:mainfrom
steve-chavez:pre-config

Conversation

@steve-chavez
Copy link
Member

@steve-chavez steve-chavez commented May 26, 2023

Closes #2703.

Allows using the in-database configuration without SUPERUSER. You can have a function that configures postgREST like so:

create or replace function postgrest.pre_config()
returns void as $$
begin
  if current_user = 'other_authenticator' then
    perform
    , set_config('pgrst.db_schemas', 'schema1, schema2', true)
    , set_config('pgrst.jwt_role_claim_key', '."other"."pre_config_role"', true)
    , set_config('pgrst.db_anon_role', 'pre_config_role', true)
    , set_config('pgrst.db_tx_end', 'rollback-allow-override', true);
  else
    null;
  end if;
end $$ language plpgsql;

The function name and schema is arbitrary. It should be kept separate from public APIs(not unsafe to expose it bc it doesn't return anything, but for maintenance).

A single function can be used for different postgREST instances. Each instance can use a different connection role and different settings can be applied(like the above example with the condition on current_user).

Allows using the in-database configuration without SUPERUSER
@steve-chavez
Copy link
Member Author

Just tried this for reproducing #2791. No dice.

It happens before the schema cache reload on Workers.hs, which makes sense(schema cache needs the db-schema config).

We need a way to inject a function into the schema cache for reproducing #2791. This would also be useful for inducing a Hasql error and increase code coverage(currently we cannot cover the InternalErrorCode00/PGRSTX00 errors). This should be an internal only config parameter bc there's no feature that needs this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Allow reading configuration indirectly using SQL provided as configuration parameter(pre-config hook)

2 participants