fix: in-db config values not loading for pgrst.server_trace_header and pgrst.server_cors_allowed_origins

This commit is contained in:
Laurence Isla
2024-03-27 15:59:35 -05:00
parent d02540ac44
commit 428a6fef63
8 changed files with 27 additions and 19 deletions
+1
View File
@@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- #3330, Incorrect admin server `/ready` response on slow schema cache loads - @steve-chavez - #3330, Incorrect admin server `/ready` response on slow schema cache loads - @steve-chavez
- #3327, Fix slow responses on schema cache reloads - @steve-chavez - #3327, Fix slow responses on schema cache reloads - @steve-chavez
- #3340, Log when the LISTEN channel gets a notification - @steve-chavez - #3340, Log when the LISTEN channel gets a notification - @steve-chavez
- #3345, Fix in-database configuration values not loading for `pgrst.server_trace_header` and `pgrst.server_cors_allowed_origins` - @laurenceisla
### Deprecated ### Deprecated
+12 -11
View File
@@ -9,7 +9,6 @@ Some of its functionality includes:
- Producing HTTP Headers according to RFCs. - Producing HTTP Headers according to RFCs.
- Content Negotiation - Content Negotiation
-} -}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RecordWildCards #-} {-# LANGUAGE RecordWildCards #-}
module PostgREST.App module PostgREST.App
( postgrest ( postgrest
@@ -49,7 +48,7 @@ import PostgREST.ApiRequest (Action (..),
ApiRequest (..), Mutation (..)) ApiRequest (..), Mutation (..))
import PostgREST.AppState (AppState) import PostgREST.AppState (AppState)
import PostgREST.Auth (AuthResult (..)) import PostgREST.Auth (AuthResult (..))
import PostgREST.Config (AppConfig (..)) import PostgREST.Config (AppConfig (..), LogLevel (..))
import PostgREST.Config.PgVersion (PgVersion (..)) import PostgREST.Config.PgVersion (PgVersion (..))
import PostgREST.Error (Error) import PostgREST.Error (Error)
import PostgREST.Observation (Observation (..)) import PostgREST.Observation (Observation (..))
@@ -81,7 +80,7 @@ run appState observer = do
Admin.runAdmin conf appState (serverSettings conf) observer Admin.runAdmin conf appState (serverSettings conf) observer
let app = postgrest conf appState (AppState.connectionWorker appState) observer let app = postgrest configLogLevel appState (AppState.connectionWorker appState) observer
case configServerUnixSocket of case configServerUnixSocket of
Just path -> do Just path -> do
@@ -100,12 +99,12 @@ serverSettings AppConfig{..} =
& setServerName ("postgrest/" <> prettyVersion) & setServerName ("postgrest/" <> prettyVersion)
-- | PostgREST application -- | PostgREST application
postgrest :: AppConfig -> AppState.AppState -> IO () -> (Observation -> IO ()) -> Wai.Application postgrest :: LogLevel -> AppState.AppState -> IO () -> (Observation -> IO ()) -> Wai.Application
postgrest conf appState connWorker observer = postgrest logLevel appState connWorker observer =
traceHeaderMiddleware conf . traceHeaderMiddleware appState .
Cors.middleware (configServerCorsAllowedOrigins conf) . Cors.middleware appState .
Auth.middleware appState . Auth.middleware appState .
Logger.middleware (configLogLevel conf) $ Logger.middleware logLevel $
-- fromJust can be used, because the auth middleware will **always** add -- fromJust can be used, because the auth middleware will **always** add
-- some AuthResult to the vault. -- some AuthResult to the vault.
\req respond -> case fromJust $ Auth.getResult req of \req respond -> case fromJust $ Auth.getResult req of
@@ -251,9 +250,11 @@ calcTiming timingEnabled f = if timingEnabled
r <- f r <- f
pure (Nothing, r) pure (Nothing, r)
traceHeaderMiddleware :: AppConfig -> Wai.Middleware traceHeaderMiddleware :: AppState -> Wai.Middleware
traceHeaderMiddleware AppConfig{configServerTraceHeader} app req respond = traceHeaderMiddleware appState app req respond = do
case configServerTraceHeader of conf <- AppState.getConfig appState
case configServerTraceHeader conf of
Nothing -> app req respond Nothing -> app req respond
Just hdr -> Just hdr ->
let hdrVal = L.lookup hdr $ Wai.requestHeaders req in let hdrVal = L.lookup hdr $ Wai.requestHeaders req in
+1
View File
@@ -65,6 +65,7 @@ dbSettingsNames =
,"openapi_security_active" ,"openapi_security_active"
,"openapi_server_proxy_uri" ,"openapi_server_proxy_uri"
,"raw_media_types" ,"raw_media_types"
,"server_cors_allowed_origins"
,"server_trace_header" ,"server_trace_header"
,"server_timing_enabled" ,"server_timing_enabled"
] ]
+7 -2
View File
@@ -15,10 +15,15 @@ import qualified Network.Wai.Middleware.Cors as Wai
import Data.List (lookup) import Data.List (lookup)
import PostgREST.AppState (AppState, getConfig)
import PostgREST.Config (AppConfig (..))
import Protolude import Protolude
middleware :: Maybe [Text] -> Wai.Middleware middleware :: AppState -> Wai.Middleware
middleware corsAllowedOrigins = Wai.cors $ corsPolicy corsAllowedOrigins middleware appState app req res = do
conf <- getConfig appState
Wai.cors (corsPolicy $ configServerCorsAllowedOrigins conf) app req res
-- | CORS policy to be used in by Wai Cors middleware -- | CORS policy to be used in by Wai Cors middleware
corsPolicy :: Maybe [Text] -> Wai.Request -> Maybe Wai.CorsResourcePolicy corsPolicy :: Maybe [Text] -> Wai.Request -> Maybe Wai.CorsResourcePolicy
@@ -27,7 +27,7 @@ log-level = "info"
openapi-mode = "disabled" openapi-mode = "disabled"
openapi-security-active = false openapi-security-active = false
openapi-server-proxy-uri = "https://otherexample.org/api" openapi-server-proxy-uri = "https://otherexample.org/api"
server-cors-allowed-origins = "http://example.com" server-cors-allowed-origins = "http://otherorigin.com"
server-host = "0.0.0.0" server-host = "0.0.0.0"
server-port = 80 server-port = 80
server-trace-header = "traceparent" server-trace-header = "traceparent"
@@ -27,7 +27,7 @@ log-level = "info"
openapi-mode = "ignore-privileges" openapi-mode = "ignore-privileges"
openapi-security-active = true openapi-security-active = true
openapi-server-proxy-uri = "https://example.org/api" openapi-server-proxy-uri = "https://example.org/api"
server-cors-allowed-origins = "http://example.com" server-cors-allowed-origins = "http://origin.com"
server-host = "0.0.0.0" server-host = "0.0.0.0"
server-port = 80 server-port = 80
server-trace-header = "CF-Ray" server-trace-header = "CF-Ray"
+2 -2
View File
@@ -19,7 +19,7 @@ ALTER ROLE db_config_authenticator SET pgrst.db_pre_request = 'test.custom_heade
ALTER ROLE db_config_authenticator SET pgrst.db_max_rows = '1000'; ALTER ROLE db_config_authenticator SET pgrst.db_max_rows = '1000';
ALTER ROLE db_config_authenticator SET pgrst.db_extra_search_path = 'public, extensions'; ALTER ROLE db_config_authenticator SET pgrst.db_extra_search_path = 'public, extensions';
ALTER ROLE db_config_authenticator SET pgrst.not_existing = 'should be ignored'; ALTER ROLE db_config_authenticator SET pgrst.not_existing = 'should be ignored';
ALTER ROLE db_config_authenticator SET pgrst.server_cors_allowed_origins = 'http://example.com'; ALTER ROLE db_config_authenticator SET pgrst.server_cors_allowed_origins = 'http://origin.com';
ALTER ROLE db_config_authenticator SET pgrst.server_trace_header = 'CF-Ray'; ALTER ROLE db_config_authenticator SET pgrst.server_trace_header = 'CF-Ray';
ALTER ROLE db_config_authenticator SET pgrst.server_timing_enabled = 'true'; ALTER ROLE db_config_authenticator SET pgrst.server_timing_enabled = 'true';
@@ -66,7 +66,7 @@ ALTER ROLE other_authenticator SET pgrst.db_max_rows = '100';
ALTER ROLE other_authenticator SET pgrst.db_extra_search_path = 'public, extensions, other'; ALTER ROLE other_authenticator SET pgrst.db_extra_search_path = 'public, extensions, other';
ALTER ROLE other_authenticator SET pgrst.openapi_mode = 'disabled'; ALTER ROLE other_authenticator SET pgrst.openapi_mode = 'disabled';
ALTER ROLE other_authenticator SET pgrst.openapi_security_active = 'false'; ALTER ROLE other_authenticator SET pgrst.openapi_security_active = 'false';
ALTER ROLE other_authenticator SET pgrst.server_cors_allowed_origins = 'http://example.com'; ALTER ROLE other_authenticator SET pgrst.server_cors_allowed_origins = 'http://otherorigin.com';
ALTER ROLE other_authenticator SET pgrst.server_trace_header = 'traceparent'; ALTER ROLE other_authenticator SET pgrst.server_trace_header = 'traceparent';
ALTER ROLE other_authenticator SET pgrst.db_pre_config = 'postgrest.pre_config'; ALTER ROLE other_authenticator SET pgrst.db_pre_config = 'postgrest.pre_config';
ALTER ROLE other_authenticator SET pgrst.server_timing_enabled = 'true'; ALTER ROLE other_authenticator SET pgrst.server_timing_enabled = 'true';
+2 -2
View File
@@ -83,7 +83,7 @@ main = do
appState <- AppState.initWithPool sockets pool config noObs appState <- AppState.initWithPool sockets pool config noObs
AppState.putPgVersion appState actualPgVersion AppState.putPgVersion appState actualPgVersion
AppState.putSchemaCache appState (Just baseSchemaCache) AppState.putSchemaCache appState (Just baseSchemaCache)
return ((), postgrest config appState (pure ()) noObs) return ((), postgrest (configLogLevel config) appState (pure ()) noObs)
-- For tests that run with a different SchemaCache(depends on configSchemas) -- For tests that run with a different SchemaCache(depends on configSchemas)
appDbs config = do appDbs config = do
@@ -91,7 +91,7 @@ main = do
appState <- AppState.initWithPool sockets pool config noObs appState <- AppState.initWithPool sockets pool config noObs
AppState.putPgVersion appState actualPgVersion AppState.putPgVersion appState actualPgVersion
AppState.putSchemaCache appState (Just customSchemaCache) AppState.putSchemaCache appState (Just customSchemaCache)
return ((), postgrest config appState (pure ()) noObs) return ((), postgrest (configLogLevel config) appState (pure ()) noObs)
let withApp = app testCfg let withApp = app testCfg
maxRowsApp = app testMaxRowsCfg maxRowsApp = app testMaxRowsCfg