From 49f40a44f039f2e3dad87bcb6c20e6780eaae4b4 Mon Sep 17 00:00:00 2001 From: Antoine Leblanc Date: Fri, 23 Jul 2021 13:25:16 +0100 Subject: [PATCH] Enforce that backends use the properly resolved environment variables. ## Description This PR fixes an oversight in the implementation of the resolvers of different backends. To implement resolution from environment variables, both MSSQL and BigQuery were directly fetching the process' environment variables, instead of using the careful curated set we thread from main. It was working just fine on OSS, but is failing on Cloud. This PR fixes this by adding an additional argument to `resolveSourceConfig`, to ensure that backends always use the correct set of variables. https://github.com/hasura/graphql-engine-mono/pull/1891 GitOrigin-RevId: 58644cab7d041a8bf4235e2acfe9cf71533a92a1 --- CHANGELOG.md | 1 + .../Hasura/Backends/BigQuery/DDL/Source.hs | 4 +-- .../Hasura/Backends/MSSQL/Connection.hs | 7 ++-- .../Hasura/Backends/MSSQL/DDL/Source.hs | 8 +++-- .../Hasura/Backends/MySQL/Connection.hs | 35 +++++++++++-------- .../Hasura/Backends/Postgres/DDL/Source.hs | 8 +++-- server/src-lib/Hasura/RQL/DDL/Schema/Cache.hs | 4 +-- .../Hasura/RQL/Types/Metadata/Backend.hs | 3 +- 8 files changed, 44 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8afca7c851..dfd6a920143 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Next release (Add entries below in the order of server, console, cli, docs, others) +- server: Fixed a bug where MSSQL and BigQuery would ignore environment variables set from the console - server: Fixing bug in ReplaceMetadata parser - Moving from Alternative to committed-choice. - server: Relax the unique operation name constraint when adding a query to a query collection - server/bigquery: Fix issues related to adding and querying from non-US datasets (closes [6937](https://github.com/hasura/graphql-engine/issues/6937)). diff --git a/server/src-lib/Hasura/Backends/BigQuery/DDL/Source.hs b/server/src-lib/Hasura/Backends/BigQuery/DDL/Source.hs index 38daa0d2b87..2dad900f11c 100644 --- a/server/src-lib/Hasura/Backends/BigQuery/DDL/Source.hs +++ b/server/src-lib/Hasura/Backends/BigQuery/DDL/Source.hs @@ -39,9 +39,9 @@ resolveSourceConfig :: MonadIO m => SourceName -> BigQueryConnSourceConfig -> + Env.Environment -> m (Either QErr BigQuerySourceConfig) -resolveSourceConfig _name BigQueryConnSourceConfig{..} = runExceptT $ do - env <- liftIO Env.getEnvironment +resolveSourceConfig _name BigQueryConnSourceConfig{..} env = runExceptT $ do eSA <- resolveConfigurationJson env _cscServiceAccount case eSA of Left e -> throw400 Unexpected $ T.pack e diff --git a/server/src-lib/Hasura/Backends/MSSQL/Connection.hs b/server/src-lib/Hasura/Backends/MSSQL/Connection.hs index 41111eb0ca3..0bc6b4270e1 100644 --- a/server/src-lib/Hasura/Backends/MSSQL/Connection.hs +++ b/server/src-lib/Hasura/Backends/MSSQL/Connection.hs @@ -2,6 +2,7 @@ module Hasura.Backends.MSSQL.Connection where import Hasura.Prelude +import qualified Data.Environment as Env import qualified Data.Pool as Pool import qualified Database.ODBC.SQLServer as ODBC @@ -10,11 +11,11 @@ import Data.Aeson import Data.Aeson.Casing import Data.Aeson.TH -import qualified Data.Environment as Env import Data.Text (pack, unpack) import Hasura.Base.Error import Hasura.Incremental (Cacheable (..)) + -- | ODBC connection string for MSSQL server newtype MSSQLConnectionString = MSSQLConnectionString {unMSSQLConnectionString :: Text} @@ -109,9 +110,9 @@ createMSSQLPool :: MonadIO m => QErrM m => MSSQLConnectionInfo + -> Env.Environment -> m (MSSQLConnectionString, MSSQLPool) -createMSSQLPool (MSSQLConnectionInfo iConnString MSSQLPoolSettings{..}) = do - env <- liftIO Env.getEnvironment +createMSSQLPool (MSSQLConnectionInfo iConnString MSSQLPoolSettings{..}) env = do connString <- resolveInputConnectionString env iConnString pool <- liftIO $ MSSQLPool diff --git a/server/src-lib/Hasura/Backends/MSSQL/DDL/Source.hs b/server/src-lib/Hasura/Backends/MSSQL/DDL/Source.hs index 7afc8bf415e..76b3dcec21c 100644 --- a/server/src-lib/Hasura/Backends/MSSQL/DDL/Source.hs +++ b/server/src-lib/Hasura/Backends/MSSQL/DDL/Source.hs @@ -7,6 +7,8 @@ where import Hasura.Prelude +import qualified Data.Environment as Env + import Hasura.Backends.MSSQL.Connection import Hasura.Backends.MSSQL.Meta import Hasura.Base.Error @@ -14,13 +16,15 @@ import Hasura.RQL.Types.Common import Hasura.RQL.Types.Source import Hasura.SQL.Backend + resolveSourceConfig :: (MonadIO m) => SourceName -> MSSQLConnConfiguration + -> Env.Environment -> m (Either QErr MSSQLSourceConfig) -resolveSourceConfig _name (MSSQLConnConfiguration connInfo) = runExceptT do - (connString, mssqlPool) <- createMSSQLPool connInfo +resolveSourceConfig _name (MSSQLConnConfiguration connInfo) env = runExceptT do + (connString, mssqlPool) <- createMSSQLPool connInfo env pure $ MSSQLSourceConfig connString mssqlPool resolveDatabaseMetadata diff --git a/server/src-lib/Hasura/Backends/MySQL/Connection.hs b/server/src-lib/Hasura/Backends/MySQL/Connection.hs index 572cf84ebfa..672ca62f1c5 100644 --- a/server/src-lib/Hasura/Backends/MySQL/Connection.hs +++ b/server/src-lib/Hasura/Backends/MySQL/Connection.hs @@ -1,21 +1,28 @@ module Hasura.Backends.MySQL.Connection where +import Hasura.Prelude + +import qualified Data.Environment as Env +import qualified Data.Text as T import Data.Pool (createPool, withResource) -import qualified Data.Text as T import Database.MySQL.Base + import Hasura.Backends.MySQL.Meta (getMetadata) import Hasura.Backends.MySQL.Types import Hasura.Base.Error -import Hasura.Prelude import Hasura.RQL.Types.Common import Hasura.RQL.Types.Source import Hasura.SQL.Backend -resolveSourceConfig :: (MonadIO m) => - SourceName -> ConnSourceConfig -> m (Either QErr SourceConfig) -resolveSourceConfig _name csc@ConnSourceConfig{_cscPoolSettings = ConnPoolSettings{..}, ..} = +resolveSourceConfig + :: (MonadIO m) + => SourceName + -> ConnSourceConfig + -> Env.Environment + -> m (Either QErr SourceConfig) +resolveSourceConfig _name csc@ConnSourceConfig{_cscPoolSettings = ConnPoolSettings{..}, ..} _env = do let connectInfo = defaultConnectInfo { connectHost = T.unpack _cscHost @@ -24,15 +31,15 @@ resolveSourceConfig _name csc@ConnSourceConfig{_cscPoolSettings = ConnPoolSettin , connectPassword = T.unpack _cscPassword , connectDatabase = T.unpack _cscDatabase } - in runExceptT $ - SourceConfig csc <$> - liftIO - (createPool - (connect connectInfo) - close - 1 - (fromIntegral _cscIdleTimeout) - (fromIntegral _cscMaxConnections)) + runExceptT $ + SourceConfig csc <$> + liftIO + (createPool + (connect connectInfo) + close + 1 + (fromIntegral _cscIdleTimeout) + (fromIntegral _cscMaxConnections)) resolveDatabaseMetadata :: (MonadIO m) => diff --git a/server/src-lib/Hasura/Backends/Postgres/DDL/Source.hs b/server/src-lib/Hasura/Backends/Postgres/DDL/Source.hs index 3e188b26c8a..a582efbfe8e 100644 --- a/server/src-lib/Hasura/Backends/Postgres/DDL/Source.hs +++ b/server/src-lib/Hasura/Backends/Postgres/DDL/Source.hs @@ -11,6 +11,7 @@ module Hasura.Backends.Postgres.DDL.Source import Hasura.Prelude +import qualified Data.Environment as Env import qualified Data.HashMap.Strict as Map import qualified Data.List.NonEmpty as NE import qualified Database.PG.Query as Q @@ -50,8 +51,11 @@ instance ToMetadataFetchQuery 'Citus where resolveSourceConfig :: (MonadIO m, MonadResolveSource m) - => SourceName -> PostgresConnConfiguration -> m (Either QErr (SourceConfig ('Postgres pgKind))) -resolveSourceConfig name config = runExceptT do + => SourceName + -> PostgresConnConfiguration + -> Env.Environment + -> m (Either QErr (SourceConfig ('Postgres pgKind))) +resolveSourceConfig name config _env = runExceptT do sourceResolver <- getSourceResolver liftEitherM $ liftIO $ sourceResolver name config diff --git a/server/src-lib/Hasura/RQL/DDL/Schema/Cache.hs b/server/src-lib/Hasura/RQL/DDL/Schema/Cache.hs index b15fe987279..db76d906a3b 100644 --- a/server/src-lib/Hasura/RQL/DDL/Schema/Cache.hs +++ b/server/src-lib/Hasura/RQL/DDL/Schema/Cache.hs @@ -255,7 +255,7 @@ buildSchemaCacheRule env = proc (metadata, invalidationKeys) -> do :: forall b arr m . ( ArrowChoice arr, Inc.ArrowCache m arr , ArrowWriter (Seq CollectedInfo) arr - , MonadIO m, MonadBaseControl IO m + , MonadIO m , MonadResolveSource m , BackendMetadata b ) @@ -266,7 +266,7 @@ buildSchemaCacheRule env = proc (metadata, invalidationKeys) -> do let metadataObj = MetadataObject (MOSource sourceName) $ toJSON sourceName Inc.dependOn -< Inc.selectKeyD sourceName invalidationKeys (| withRecordInconsistency ( - liftEitherA <<< bindA -< resolveSourceConfig @b sourceName sourceConfig) + liftEitherA <<< bindA -< resolveSourceConfig @b sourceName sourceConfig env) |) metadataObj resolveSourceIfNeeded diff --git a/server/src-lib/Hasura/RQL/Types/Metadata/Backend.hs b/server/src-lib/Hasura/RQL/Types/Metadata/Backend.hs index 1b97b2041bc..9c7b1a1f1bf 100644 --- a/server/src-lib/Hasura/RQL/Types/Metadata/Backend.hs +++ b/server/src-lib/Hasura/RQL/Types/Metadata/Backend.hs @@ -47,9 +47,10 @@ class (Backend b) => BackendMetadata (b :: BackendType) where -- | Function that resolves the connection related source configuration, and -- creates a connection pool (and other related parameters) in the process resolveSourceConfig - :: (MonadIO m, MonadBaseControl IO m, MonadResolveSource m) + :: (MonadIO m, MonadResolveSource m) => SourceName -> SourceConnConfiguration b + -> Env.Environment -> m (Either QErr (SourceConfig b)) -- | Function that introspects a database for tables, columns, functions etc.