From 0d6d112b38e7b51f31c5ba9cf25ad344cf801c96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E4=B8=B0?= Date: Wed, 5 Dec 2018 02:58:29 +0800 Subject: [PATCH] Read db-uri configuration from a separate file. (#1215) * Read dburi configuration from a separate file. * Add changelog entry. --- CHANGELOG.md | 1 + main/Main.hs | 19 ++++++++++++++++-- test/io-tests.sh | 21 ++++++++++++++++++++ test/io-tests/configs/dburi-from-file.config | 7 +++++++ test/io-tests/dburis/uri.noeol | 1 + test/io-tests/dburis/uri.txt | 1 + 6 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 test/io-tests/configs/dburi-from-file.config create mode 100644 test/io-tests/dburis/uri.noeol create mode 100644 test/io-tests/dburis/uri.txt diff --git a/CHANGELOG.md b/CHANGELOG.md index f3f5486db..e7e19e4eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added - #1205, Add support for parsing JSON Web Key Sets -@russelldavies +- #1203, Add support for reading db-uri from a separate file - @zhoufeng1989 ### Fixed diff --git a/main/Main.hs b/main/Main.hs index 757a18311..7d3989236 100644 --- a/main/Main.hs +++ b/main/Main.hs @@ -25,7 +25,7 @@ import Data.IORef (IORef, atomicWriteIORef, import Data.String (IsString (..)) import Data.Text (pack, replace, stripPrefix, strip) import Data.Text.Encoding (decodeUtf8, encodeUtf8) -import Data.Text.IO (hPutStrLn) +import Data.Text.IO (hPutStrLn, readFile) import Data.Time.Clock (getCurrentTime) import qualified Hasql.Pool as P import qualified Hasql.Session as H @@ -139,7 +139,7 @@ main = do -- -- readOptions builds the 'AppConfig' from the config file specified on the -- command line - conf <- loadSecretFile =<< readOptions + conf <- loadDbUriFile =<< loadSecretFile =<< readOptions let host = configHost conf port = configPort conf proxy = configProxyUri conf @@ -278,3 +278,18 @@ loadSecretFile conf = extractAndTransform mSecret -- replace: Replace every occurrence of one substring with another replaceUrlChars = replace "_" "/" . replace "-" "+" . replace "." "=" + +{- + Load database uri from a separate file if `db-uri` is a filepath. +-} +loadDbUriFile :: AppConfig -> IO AppConfig +loadDbUriFile conf = extractDbUri mDbUri + where + mDbUri = configDatabase conf + extractDbUri :: Text -> IO AppConfig + extractDbUri dbUri = + fmap setDbUri $ + case stripPrefix "@" dbUri of + Nothing -> return dbUri + Just filename -> strip <$> readFile (toS filename) + setDbUri dbUri = conf {configDatabase = dbUri} diff --git a/test/io-tests.sh b/test/io-tests.sh index ef5f7cac5..ef188e659 100755 --- a/test/io-tests.sh +++ b/test/io-tests.sh @@ -82,6 +82,24 @@ readSecretFromFile(){ pgrStop } +readDbUriFromFile(){ + pgrConfig="dburi-from-file.config" + pgrStartRead "./configs/$pgrConfig" "./dburis/$1" + while pgrStarted && test "$( rootStatus )" -ne 200 + do + # wait for the server to start + sleep 0.1 \ + || sleep 1 # fallback: subsecond sleep is not standard and may fail + done + if pgrStarted + then + ok "connection with $2 dburi read from a file" + else + ko "failed to read $2 dburi from a file" + fi + pgrStop +} + reqWithRoleClaimKey(){ export ROLE_CLAIM_KEY=$1 pgrStart "./configs/role-claim-key.config" @@ -187,6 +205,9 @@ readSecretFromFile ascii.b64 'Base64 (ASCII)' readSecretFromFile utf8.b64 'Base64 (UTF-8)' readSecretFromFile binary.b64 'Base64 (binary)' +readDbUriFromFile uri.noeol "(no EOL)" +readDbUriFromFile uri.txt "(EOL)" + reqWithRoleClaimKey '.postgrest.a_role' '{"postgrest":{"a_role":"postgrest_test_author"}}' 200 reqWithRoleClaimKey '.customObject.manyRoles[1]' '{"customObject":{"manyRoles": ["other", "postgrest_test_author"]}}' 200 reqWithRoleClaimKey '."https://www.example.com/roles"[0].value' '{"https://www.example.com/roles":[{"value":"postgrest_test_author"}]}' 200 diff --git a/test/io-tests/configs/dburi-from-file.config b/test/io-tests/configs/dburi-from-file.config new file mode 100644 index 000000000..fd384e017 --- /dev/null +++ b/test/io-tests/configs/dburi-from-file.config @@ -0,0 +1,7 @@ +db-uri = "@/dev/stdin" +db-schema = "test" +db-anon-role = "postgrest_test_anonymous" +db-pool = 1 +server-host = "127.0.0.1" +server-port = 49421 +jwt-secret = "reallyreallyreallyreallyverysafe" diff --git a/test/io-tests/dburis/uri.noeol b/test/io-tests/dburis/uri.noeol new file mode 100644 index 000000000..efc05a698 --- /dev/null +++ b/test/io-tests/dburis/uri.noeol @@ -0,0 +1 @@ +postgres:///postgrest_test \ No newline at end of file diff --git a/test/io-tests/dburis/uri.txt b/test/io-tests/dburis/uri.txt new file mode 100644 index 000000000..2d6269854 --- /dev/null +++ b/test/io-tests/dburis/uri.txt @@ -0,0 +1 @@ +postgres:///postgrest_test