2021-07-13 18:49:19 +03:00
|
|
|
{-# LANGUAGE DeriveAnyClass #-}
|
2021-04-12 13:18:29 +03:00
|
|
|
{-# LANGUAGE NoGeneralisedNewtypeDeriving #-}
|
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
|
|
|
|
module Hasura.Backends.BigQuery.Source where
|
|
|
|
|
2021-07-30 18:42:36 +03:00
|
|
|
import Hasura.Prelude
|
2021-04-12 13:18:29 +03:00
|
|
|
|
|
|
|
import qualified Crypto.PubKey.RSA.Types as Cry
|
2021-07-13 18:49:19 +03:00
|
|
|
import qualified Data.Aeson as J
|
|
|
|
import qualified Data.Aeson.Casing as J
|
|
|
|
import qualified Data.Aeson.TH as J
|
|
|
|
import qualified Data.ByteString.Lazy as BL
|
|
|
|
import qualified Data.HashMap.Strict as HM
|
|
|
|
import qualified Data.Text.Encoding as TE
|
|
|
|
import qualified Data.X509 as X509
|
|
|
|
import qualified Data.X509.Memory as X509
|
2021-07-30 18:42:36 +03:00
|
|
|
|
|
|
|
import Control.Concurrent.MVar
|
|
|
|
|
2021-07-13 18:49:19 +03:00
|
|
|
import Hasura.Incremental (Cacheable (..))
|
2021-04-12 13:18:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
data PKey = PKey
|
2021-07-13 18:49:19 +03:00
|
|
|
{ unPKey :: Cry.PrivateKey
|
2021-04-12 13:18:29 +03:00
|
|
|
, originalBS :: Text
|
|
|
|
}
|
|
|
|
deriving (Show, Eq, Data, Generic, NFData, Hashable)
|
|
|
|
deriving instance Generic Cry.PrivateKey -- orphan
|
|
|
|
deriving instance Generic Cry.PublicKey -- orphan
|
|
|
|
deriving instance J.ToJSON Cry.PrivateKey -- orphan
|
|
|
|
deriving instance J.ToJSON Cry.PublicKey -- orphan
|
|
|
|
deriving instance Hashable Cry.PrivateKey -- orphan
|
|
|
|
deriving instance Hashable Cry.PublicKey -- orphan
|
|
|
|
instance J.FromJSON PKey where
|
|
|
|
parseJSON = J.withText "private_key" $ \k ->
|
|
|
|
case X509.readKeyFileFromMemory $ TE.encodeUtf8 k of
|
|
|
|
[X509.PrivKeyRSA k'] -> return $ PKey k' k
|
|
|
|
_ -> fail "unable to parse private key"
|
|
|
|
instance J.ToJSON PKey where
|
|
|
|
toJSON PKey{..} = J.toJSON originalBS
|
|
|
|
|
|
|
|
|
|
|
|
newtype GoogleAccessToken
|
|
|
|
= GoogleAccessToken Text
|
|
|
|
deriving (Show, Eq, J.FromJSON, J.ToJSON, Hashable, Generic, Data, NFData)
|
|
|
|
|
|
|
|
|
|
|
|
data TokenResp
|
|
|
|
= TokenResp
|
|
|
|
{ _trAccessToken :: !GoogleAccessToken
|
|
|
|
, _trExpiresAt :: !Integer -- Number of seconds until expiry from `now`, but we add `now` seconds to this for easy tracking
|
|
|
|
} deriving (Eq, Show, Data, NFData, Generic, Hashable)
|
|
|
|
instance J.FromJSON TokenResp where
|
|
|
|
parseJSON = J.withObject "TokenResp" $ \o -> TokenResp
|
|
|
|
<$> o J..: "access_token"
|
|
|
|
<*> o J..: "expires_in"
|
|
|
|
|
|
|
|
|
|
|
|
dummyTokenResp :: TokenResp
|
|
|
|
dummyTokenResp = TokenResp (GoogleAccessToken "DUMMY") 0
|
|
|
|
|
|
|
|
|
|
|
|
data ServiceAccount
|
|
|
|
= ServiceAccount
|
|
|
|
{ _saClientEmail :: !Text
|
|
|
|
, _saPrivateKey :: !PKey
|
2021-07-13 18:49:19 +03:00
|
|
|
, _saProjectId :: !Text
|
2021-04-12 13:18:29 +03:00
|
|
|
} deriving (Eq, Show, Data, NFData, Generic, Hashable)
|
|
|
|
$(J.deriveJSON (J.aesonDrop 3 J.snakeCase){J.omitNothingFields=False} ''ServiceAccount)
|
|
|
|
|
|
|
|
|
|
|
|
data ConfigurationJSON a
|
|
|
|
= FromEnvJSON Text
|
|
|
|
| FromYamlJSON a
|
|
|
|
deriving stock (Show, Eq, Generic)
|
|
|
|
deriving (NFData, Hashable)
|
|
|
|
instance J.FromJSON a => J.FromJSON (ConfigurationJSON a) where
|
|
|
|
parseJSON = \case
|
|
|
|
J.Object o | Just (J.String text) <- HM.lookup "from_env" o -> pure (FromEnvJSON text)
|
|
|
|
J.String s -> case J.eitherDecode . BL.fromStrict . TE.encodeUtf8 $ s of
|
2021-07-13 18:49:19 +03:00
|
|
|
Left {} -> fail "error parsing configuration json"
|
2021-04-12 13:18:29 +03:00
|
|
|
Right sa -> pure sa
|
|
|
|
j -> fmap FromYamlJSON (J.parseJSON j)
|
|
|
|
instance J.ToJSON a => J.ToJSON (ConfigurationJSON a) where
|
|
|
|
toJSON = \case
|
2021-07-13 18:49:19 +03:00
|
|
|
FromEnvJSON i -> J.object ["from_env" J..= i]
|
2021-04-12 13:18:29 +03:00
|
|
|
FromYamlJSON j -> J.toJSON j
|
|
|
|
|
|
|
|
|
|
|
|
-- | Configuration inputs when they are a YAML array or an Env var whos value is
|
|
|
|
-- a comma-separated string
|
|
|
|
data ConfigurationInputs
|
|
|
|
= FromYamls ![Text]
|
|
|
|
| FromEnvs !Text
|
|
|
|
deriving stock (Show, Eq, Generic)
|
|
|
|
deriving (NFData, Hashable)
|
|
|
|
instance J.ToJSON ConfigurationInputs where
|
|
|
|
toJSON = \case
|
|
|
|
FromYamls i -> J.toJSON i
|
2021-07-13 18:49:19 +03:00
|
|
|
FromEnvs i -> J.object ["from_env" J..= i]
|
2021-04-12 13:18:29 +03:00
|
|
|
instance J.FromJSON ConfigurationInputs where
|
|
|
|
parseJSON = \case
|
2021-07-13 18:49:19 +03:00
|
|
|
J.Object o -> FromEnvs <$> o J..: "from_env"
|
2021-04-12 13:18:29 +03:00
|
|
|
s@(J.Array _) -> FromYamls <$> J.parseJSON s
|
2021-07-13 18:49:19 +03:00
|
|
|
_ -> fail "one of array or object must be provided"
|
2021-04-12 13:18:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
-- | Configuration input when the YAML value as well as the Env var have
|
|
|
|
-- singlular values
|
|
|
|
data ConfigurationInput
|
|
|
|
= FromYaml !Text
|
|
|
|
| FromEnv !Text
|
|
|
|
deriving stock (Show, Eq, Generic)
|
|
|
|
deriving (NFData, Hashable)
|
|
|
|
instance J.ToJSON ConfigurationInput where
|
|
|
|
toJSON = \case
|
|
|
|
FromYaml i -> J.toJSON i
|
2021-07-13 18:49:19 +03:00
|
|
|
FromEnv i -> J.object ["from_env" J..= i]
|
2021-04-12 13:18:29 +03:00
|
|
|
instance J.FromJSON ConfigurationInput where
|
|
|
|
parseJSON = \case
|
2021-07-13 18:49:19 +03:00
|
|
|
J.Object o -> FromEnv <$> o J..: "from_env"
|
2021-04-12 13:18:29 +03:00
|
|
|
s@(J.String _) -> FromYaml <$> J.parseJSON s
|
2021-07-30 10:54:50 +03:00
|
|
|
(J.Number n) -> FromYaml <$> J.parseJSON (J.String (tshow n))
|
2021-07-13 18:49:19 +03:00
|
|
|
_ -> fail "one of string or number or object must be provided"
|
2021-04-12 13:18:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
data BigQueryConnSourceConfig
|
|
|
|
= BigQueryConnSourceConfig
|
2021-07-13 18:49:19 +03:00
|
|
|
{ _cscServiceAccount :: !(ConfigurationJSON ServiceAccount)
|
|
|
|
, _cscDatasets :: !ConfigurationInputs
|
|
|
|
, _cscProjectId :: !ConfigurationInput -- this is part of service-account.json, but we put it here on purpose
|
2021-07-19 14:39:22 +03:00
|
|
|
, _cscGlobalSelectLimit :: !(Maybe ConfigurationInput)
|
2021-04-12 13:18:29 +03:00
|
|
|
} deriving (Eq, Generic, NFData)
|
|
|
|
$(J.deriveJSON (J.aesonDrop 4 J.snakeCase){J.omitNothingFields=True} ''BigQueryConnSourceConfig)
|
|
|
|
deriving instance Show BigQueryConnSourceConfig
|
|
|
|
deriving instance Hashable BigQueryConnSourceConfig
|
|
|
|
instance Cacheable BigQueryConnSourceConfig where
|
|
|
|
unchanged _ = (==)
|
|
|
|
|
|
|
|
|
|
|
|
data BigQuerySourceConfig
|
|
|
|
= BigQuerySourceConfig
|
2021-07-13 18:49:19 +03:00
|
|
|
{ _scServiceAccount :: !ServiceAccount
|
|
|
|
, _scDatasets :: ![Text]
|
|
|
|
, _scProjectId :: !Text -- this is part of service-account.json, but we put it here on purpose
|
|
|
|
, _scAccessTokenMVar :: !(MVar (Maybe TokenResp))
|
2021-07-19 14:39:22 +03:00
|
|
|
, _scGlobalSelectLimit :: !Int
|
2021-07-30 18:42:36 +03:00
|
|
|
} deriving (Eq)
|
2021-04-12 13:18:29 +03:00
|
|
|
instance Cacheable BigQuerySourceConfig where
|
|
|
|
unchanged _ = (==)
|
2021-07-30 18:42:36 +03:00
|
|
|
instance J.ToJSON BigQuerySourceConfig where
|
|
|
|
toJSON BigQuerySourceConfig{..} =
|
|
|
|
J.object
|
|
|
|
[ "service_account" J..= _scServiceAccount
|
|
|
|
, "datasets" J..= _scDatasets
|
|
|
|
, "project_id" J..= _scProjectId
|
|
|
|
, "global_select_limit" J..= _scGlobalSelectLimit
|
|
|
|
]
|