Allow config file reloading with SIGUSR2

* move config validation to Config.hs
* Add tests for jwt-secret/app.settings.*/db-schema reload
This commit is contained in:
steve-chavez
2020-07-13 11:30:16 -05:00
committed by Steve Chavez
parent e272ea47be
commit e8b4e3771c
8 changed files with 172 additions and 53 deletions
+4 -3
View File
@@ -65,12 +65,13 @@ import PostgREST.Types
import Protolude hiding (Proxy, intercalate, toS)
import Protolude.Conv (toS)
postgrest :: LogSetup -> AppConfig -> IORef (Maybe DbStructure) -> P.Pool -> IO UTCTime -> IO () -> Application
postgrest logs conf refDbStructure pool getTime worker =
pgrstMiddleware logs $ \ req respond -> do
postgrest :: LogSetup -> IORef AppConfig -> IORef (Maybe DbStructure) -> P.Pool -> IO UTCTime -> IO () -> Application
postgrest logS refConf refDbStructure pool getTime worker =
pgrstMiddleware logS $ \ req respond -> do
time <- getTime
body <- strictRequestBody req
maybeDbStructure <- readIORef refDbStructure
conf <- readIORef refConf
case maybeDbStructure of
Nothing -> respond . errorResponseFor $ ConnectionLostError
Just dbStructure -> do
+25 -9
View File
@@ -19,12 +19,10 @@ Other hardcoded options such as the minimum version number also belong here.
module PostgREST.Config ( prettyVersion
, docsVersion
, readPathShowHelp
, readAppConfig
, AppConfig (..)
, configPoolTimeout'
, loadSecretFile
, loadDbUriFile
, readPathShowHelp
, readValidateConfig
)
where
@@ -32,6 +30,7 @@ import qualified Data.ByteString as B
import qualified Data.ByteString.Base64 as B64
import qualified Data.ByteString.Char8 as BS
import qualified Data.Configurator as C
import Data.Either.Combinators (whenLeft)
import qualified Text.PrettyPrint.ANSI.Leijen as L
import Control.Lens (preview)
@@ -56,11 +55,14 @@ import Options.Applicative hiding (str)
import Text.Heredoc
import Text.PrettyPrint.ANSI.Leijen hiding ((<$>), (<>))
import PostgREST.Parsers (pRoleClaimKey)
import PostgREST.Types (JSPath, JSPathExp (..))
import Protolude hiding (concat, hPutStrLn, intercalate, null,
replace, take, toS, (<>))
import Protolude.Conv (toS)
import PostgREST.Auth (parseSecret)
import PostgREST.Parsers (pRoleClaimKey)
import PostgREST.Private.ProxyUri (isMalformedProxyUri)
import PostgREST.Types (JSPath, JSPathExp (..))
import Protolude hiding (concat, hPutStrLn,
intercalate, null, replace, take,
toS, (<>))
import Protolude.Conv (toS)
-- | Config file settings for the server
@@ -300,6 +302,20 @@ readAppConfig cfgPath = do
hPutStrLn stderr err
exitFailure
-- | Parse the AppConfig and validate it. Panic on invalid config options.
readValidateConfig :: FilePath -> IO AppConfig
readValidateConfig path = do
conf <- loadDbUriFile =<< loadSecretFile =<< readAppConfig path
-- Checks that the provided proxy uri is formated correctly
when (isMalformedProxyUri $ toS <$> configOpenAPIProxyUri conf) $
panic
"Malformed proxy uri, a correct example: https://example.com:8443/basePath"
-- Checks that the provided jspath is valid
whenLeft (configRoleClaimKey conf) panic
-- Check the file mode is valid
whenLeft (configSocketMode conf) panic
return $ conf { configJWKS = parseSecret <$> configJwtSecret conf}
{-|
The purpose of this function is to load the JWT secret from a file if
configJwtSecret is actually a filepath and replaces some characters if the JWT
-1
View File
@@ -7,7 +7,6 @@ Description : Generates the OpenAPI output
module PostgREST.OpenAPI (
encodeOpenAPI
, pickProxy
, isMalformedProxyUri
) where
import qualified Data.HashSet.InsOrd as Set