2021-09-24 01:56:37 +03:00
|
|
|
module Hasura.RQL.DDL.RequestTransform
|
|
|
|
( applyRequestTransform,
|
|
|
|
mkRequestTransform,
|
|
|
|
mkRequestTransformDebug,
|
|
|
|
RequestMethod (..),
|
|
|
|
TemplatingEngine (..),
|
|
|
|
TemplateText (..),
|
|
|
|
ContentType (..),
|
|
|
|
TransformHeaders (..),
|
|
|
|
MetadataTransform (..),
|
|
|
|
RequestTransform (..),
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Control.Lens (over)
|
|
|
|
import Data.Aeson qualified as J
|
|
|
|
import Data.Bifunctor (bimap)
|
|
|
|
import Data.CaseInsensitive qualified as CI
|
|
|
|
import Data.HashMap.Strict qualified as M
|
|
|
|
import Data.List (nubBy)
|
|
|
|
import Data.Text qualified as T
|
|
|
|
import Data.Text.Encoding qualified as TE
|
|
|
|
import Hasura.Incremental (Cacheable)
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.Session (SessionVariables)
|
|
|
|
import Kriti (runKriti)
|
|
|
|
import Network.HTTP.Client.Transformable qualified as HTTP
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
{-
|
|
|
|
|
|
|
|
Request Transformations are data transformations used to modify HTTP
|
|
|
|
Requests before those requests are executed.
|
|
|
|
|
|
|
|
`MetadataTransform` values are stored in Metadata within the
|
|
|
|
`CreateAction` and `CreateEventTriggerQuery` Types and then converted
|
|
|
|
into `RequestTransform` values using `mkRequestTransform`.
|
|
|
|
|
|
|
|
`RequestTransforms` are applied to an HTTP Request using
|
|
|
|
`applyRequestTransform`.
|
|
|
|
|
|
|
|
In the case of body transformations, a user specified templating
|
|
|
|
script is applied. Currently a runtime failure of the template script
|
|
|
|
will return the original request body.
|
|
|
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
-- | A set of data transformation functions and substitutions generated from a
|
|
|
|
-- MetadataTransform
|
|
|
|
data RequestTransform = RequestTransform
|
2021-09-24 01:56:37 +03:00
|
|
|
{ -- | Change the request method to one provided here. Nothing means POST.
|
|
|
|
rtRequestMethod :: Maybe RequestMethod,
|
|
|
|
-- | Change the request URL to one provided here. Nothing means original URL.
|
|
|
|
rtRequestURL :: Maybe Text,
|
|
|
|
-- | A function for transforming the request body using a Kriti template.
|
|
|
|
rtBodyTransform :: J.Value -> Maybe SessionVariables -> J.Value,
|
|
|
|
-- | Change content type to one provided here. Nothing means use original.
|
|
|
|
rtContentType :: Maybe ContentType,
|
|
|
|
-- | Change Request query params to those provided here. Nothing means use original.
|
|
|
|
rtQueryParams :: Maybe HTTP.Query,
|
|
|
|
-- | A transformation function for modifying the Request Headers.
|
|
|
|
rtRequestHeaders :: [HTTP.Header] -> [HTTP.Header]
|
2021-09-16 14:03:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
data RequestMethod = GET | POST | PUT | PATCH | DELETE
|
|
|
|
deriving (Show, Eq, Enum, Bounded, Generic)
|
|
|
|
|
|
|
|
renderRequestMethod :: RequestMethod -> Text
|
|
|
|
renderRequestMethod = \case
|
2021-09-24 01:56:37 +03:00
|
|
|
GET -> "GET"
|
|
|
|
POST -> "POST"
|
|
|
|
PUT -> "PUT"
|
|
|
|
PATCH -> "PATCH"
|
2021-09-16 14:03:01 +03:00
|
|
|
DELETE -> "DELETE"
|
|
|
|
|
|
|
|
instance J.ToJSON RequestMethod where
|
|
|
|
toJSON = J.String . renderRequestMethod
|
|
|
|
|
|
|
|
instance J.FromJSON RequestMethod where
|
|
|
|
parseJSON = J.withText "RequestMethod" \case
|
2021-09-24 01:56:37 +03:00
|
|
|
"GET" -> pure GET
|
|
|
|
"POST" -> pure POST
|
|
|
|
"PUT" -> pure PUT
|
|
|
|
"PATCH" -> pure PATCH
|
2021-09-16 14:03:01 +03:00
|
|
|
"DELETE" -> pure DELETE
|
2021-09-24 01:56:37 +03:00
|
|
|
_ -> fail "Invalid Request Method"
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
instance NFData RequestMethod
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-09-16 14:03:01 +03:00
|
|
|
instance Cacheable RequestMethod
|
|
|
|
|
|
|
|
-- | A de/serializable request transformation template which can be stored in
|
|
|
|
-- the metadata associated with an action/event trigger/etc. and used to produce
|
|
|
|
-- a 'RequestTransform'.
|
|
|
|
--
|
|
|
|
-- NOTE: This data type is _only_ intended to be parsed from and stored as user
|
|
|
|
-- metadata; no direct logical transformations should be done upon it.
|
|
|
|
--
|
|
|
|
-- NOTE: Users should convert this to 'RequestTransform' as close as possible to
|
|
|
|
-- the call site that performs a transformed HTTP request, as 'RequestTransform'
|
|
|
|
-- has a representation that makes it more difficult to deserialize for
|
|
|
|
-- debugging.
|
|
|
|
data MetadataTransform = MetadataTransform
|
2021-09-24 01:56:37 +03:00
|
|
|
{ -- | Change the request method to one provided here. Nothing means POST.
|
|
|
|
mtRequestMethod :: Maybe RequestMethod,
|
|
|
|
-- | Change the request URL to one provided here. Nothing means original URL.
|
|
|
|
mtRequestURL :: Maybe Text,
|
|
|
|
-- | Go-Basic template script for transforming the request body
|
|
|
|
mtBodyTransform :: Maybe TemplateText,
|
|
|
|
-- | Only the following Content-Types are allowed (default: application/json):
|
|
|
|
mtContentType :: Maybe ContentType,
|
|
|
|
-- | Replace any existing query params with those provided here
|
|
|
|
mtQueryParams :: Maybe (M.HashMap Text (Maybe Text)),
|
|
|
|
-- | Transform headers as defined here.
|
|
|
|
mtRequestHeaders :: Maybe TransformHeaders,
|
|
|
|
-- | The template engine to use for transformations. Default: Kriti
|
|
|
|
mtTemplatingEngine :: TemplatingEngine
|
|
|
|
}
|
|
|
|
deriving (Show, Eq, Generic)
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
instance NFData MetadataTransform
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-09-16 14:03:01 +03:00
|
|
|
instance Cacheable MetadataTransform
|
|
|
|
|
|
|
|
instance J.ToJSON MetadataTransform where
|
2021-09-24 01:56:37 +03:00
|
|
|
toJSON MetadataTransform {..} =
|
|
|
|
J.object
|
|
|
|
[ "method" J..= mtRequestMethod,
|
|
|
|
"url" J..= mtRequestURL,
|
|
|
|
"body" J..= mtBodyTransform,
|
|
|
|
"content_type" J..= mtContentType,
|
|
|
|
"query_params" J..= mtQueryParams,
|
|
|
|
"request_headers" J..= mtRequestHeaders,
|
|
|
|
"template_engine" J..= mtTemplatingEngine
|
|
|
|
]
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
instance J.FromJSON MetadataTransform where
|
|
|
|
parseJSON = J.withObject "Object" $ \o -> do
|
2021-09-24 01:56:37 +03:00
|
|
|
method <- o J..:? "method"
|
|
|
|
url <- o J..:? "url"
|
|
|
|
body <- o J..:? "body"
|
|
|
|
contentType <- o J..:? "content_type"
|
|
|
|
queryParams <- o J..:? "query_params"
|
|
|
|
headers <- o J..:? "request_headers"
|
|
|
|
templateEngine <- o J..:? "template_engine"
|
|
|
|
let templateEngine' = maybe Kriti id templateEngine
|
|
|
|
pure $ MetadataTransform method url body contentType queryParams headers templateEngine'
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
-- | Available Template Languages
|
|
|
|
data TemplatingEngine = Kriti
|
|
|
|
deriving (Show, Eq, Enum, Bounded, Generic)
|
|
|
|
|
|
|
|
renderTemplatingEngine :: TemplatingEngine -> Text
|
|
|
|
renderTemplatingEngine _ = "Kriti"
|
|
|
|
|
|
|
|
instance J.FromJSON TemplatingEngine where
|
|
|
|
parseJSON = J.withText "TemplatingEngine" \case
|
|
|
|
"Kriti" -> pure Kriti
|
2021-09-24 01:56:37 +03:00
|
|
|
_ -> fail "Invalid TemplatingEngine"
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
instance J.ToJSON TemplatingEngine where
|
|
|
|
toJSON = J.String . renderTemplatingEngine
|
|
|
|
|
|
|
|
instance NFData TemplatingEngine
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-09-16 14:03:01 +03:00
|
|
|
instance Cacheable TemplatingEngine
|
|
|
|
|
|
|
|
newtype TemplateText = TemplateText T.Text
|
|
|
|
deriving (Show, Eq, Generic)
|
|
|
|
|
|
|
|
instance NFData TemplateText
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-09-16 14:03:01 +03:00
|
|
|
instance Cacheable TemplateText
|
|
|
|
|
|
|
|
instance J.FromJSON TemplateText where
|
|
|
|
parseJSON = J.withText "TemplateText" (pure . TemplateText)
|
|
|
|
|
|
|
|
instance J.ToJSON TemplateText where
|
|
|
|
toJSON = J.String . coerce
|
|
|
|
|
|
|
|
data ContentType = JSON | XWWWFORM
|
|
|
|
deriving (Show, Eq, Enum, Bounded, Generic)
|
|
|
|
|
|
|
|
renderContentType :: ContentType -> Text
|
|
|
|
renderContentType = \case
|
2021-09-24 01:56:37 +03:00
|
|
|
JSON -> "application/json"
|
2021-09-16 14:03:01 +03:00
|
|
|
XWWWFORM -> "application/x-www-form-urlencoded"
|
|
|
|
|
|
|
|
instance J.ToJSON ContentType where
|
|
|
|
toJSON = J.String . renderContentType
|
|
|
|
|
|
|
|
instance J.FromJSON ContentType where
|
|
|
|
parseJSON = J.withText "ContentType" \case
|
2021-09-24 01:56:37 +03:00
|
|
|
"application/json" -> pure JSON
|
2021-09-16 14:03:01 +03:00
|
|
|
"application/x-www-form-urlencoded" -> pure XWWWFORM
|
2021-09-24 01:56:37 +03:00
|
|
|
_ -> fail "Invalid ContentType"
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
instance NFData ContentType
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-09-16 14:03:01 +03:00
|
|
|
instance Cacheable ContentType
|
|
|
|
|
|
|
|
-- | This newtype exists solely to anchor a `FromJSON` instance and is
|
|
|
|
-- eliminated in the `TransformHeaders` `FromJSON` instance.
|
2021-09-24 01:56:37 +03:00
|
|
|
newtype HeaderKey = HeaderKey {unHeaderKey :: CI.CI Text}
|
2021-09-16 14:03:01 +03:00
|
|
|
deriving (Show, Eq, Ord, Generic)
|
|
|
|
|
|
|
|
instance NFData HeaderKey
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-09-16 14:03:01 +03:00
|
|
|
instance Cacheable HeaderKey
|
|
|
|
|
|
|
|
instance J.FromJSON HeaderKey where
|
|
|
|
parseJSON = J.withText "HeaderKey" \txt -> case CI.mk txt of
|
|
|
|
"Content-Type" -> fail "Restricted Header: Content-Type"
|
2021-09-24 01:56:37 +03:00
|
|
|
key -> pure $ HeaderKey key
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
data TransformHeaders = TransformHeaders
|
2021-09-24 01:56:37 +03:00
|
|
|
{ addHeaders :: [(CI.CI Text, Text)],
|
|
|
|
removeHeaders :: [CI.CI Text]
|
|
|
|
}
|
|
|
|
deriving (Show, Eq, Ord, Generic)
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
instance NFData TransformHeaders
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-09-16 14:03:01 +03:00
|
|
|
instance Cacheable TransformHeaders
|
|
|
|
|
|
|
|
instance J.ToJSON TransformHeaders where
|
2021-09-24 01:56:37 +03:00
|
|
|
toJSON TransformHeaders {..} =
|
|
|
|
J.object
|
|
|
|
[ "add_headers" J..= M.fromList (fmap (first CI.original) addHeaders),
|
|
|
|
"remove_headers" J..= fmap CI.original removeHeaders
|
|
|
|
]
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
instance J.FromJSON TransformHeaders where
|
|
|
|
parseJSON = J.withObject "TransformHeaders" $ \o -> do
|
|
|
|
addHeaders :: M.HashMap Text Text <- fromMaybe mempty <$> o J..:? "add_headers"
|
|
|
|
let headers = M.toList $ mapKeys CI.mk addHeaders
|
|
|
|
removeHeaders <- o J..:? "remove_headers"
|
|
|
|
let removeHeaders' = unHeaderKey <$> fromMaybe mempty removeHeaders
|
|
|
|
pure $ TransformHeaders headers removeHeaders'
|
|
|
|
|
|
|
|
-- | Construct a `RequestTransform` from its metadata representation.
|
|
|
|
mkRequestTransform :: MetadataTransform -> RequestTransform
|
2021-09-24 01:56:37 +03:00
|
|
|
mkRequestTransform MetadataTransform {..} =
|
2021-09-16 14:03:01 +03:00
|
|
|
let transformParams = fmap (bimap TE.encodeUtf8 (fmap TE.encodeUtf8)) . M.toList
|
|
|
|
queryParams = transformParams <$> mtQueryParams
|
|
|
|
headerTransform = maybe id mkHeaderTransform mtRequestHeaders
|
2021-09-24 01:56:37 +03:00
|
|
|
in RequestTransform mtRequestMethod mtRequestURL (mkBodyTransform mtBodyTransform mtTemplatingEngine) mtContentType queryParams headerTransform
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
mkRequestTransformDebug :: MetadataTransform -> RequestTransform
|
2021-09-24 01:56:37 +03:00
|
|
|
mkRequestTransformDebug mt@MetadataTransform {mtBodyTransform, mtTemplatingEngine} =
|
|
|
|
(mkRequestTransform mt) {rtBodyTransform = mkBodyTransform mtBodyTransform mtTemplatingEngine}
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
-- | Construct a Header Transformation function from a `TransformHeaders` value.
|
|
|
|
mkHeaderTransform :: TransformHeaders -> [HTTP.Header] -> [HTTP.Header]
|
2021-09-24 01:56:37 +03:00
|
|
|
mkHeaderTransform TransformHeaders {..} headers =
|
2021-09-16 14:03:01 +03:00
|
|
|
let toBeRemoved = (fmap . CI.map) (TE.encodeUtf8) removeHeaders
|
|
|
|
filteredHeaders = filter ((`notElem` toBeRemoved) . fst) headers
|
|
|
|
newHeaders = fmap (bimap (CI.map TE.encodeUtf8) TE.encodeUtf8) addHeaders
|
2021-09-24 01:56:37 +03:00
|
|
|
in newHeaders <> filteredHeaders
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
-- | Construct a Body Transformation function
|
|
|
|
mkBodyTransform :: Maybe TemplateText -> TemplatingEngine -> J.Value -> Maybe SessionVariables -> J.Value
|
|
|
|
mkBodyTransform Nothing _ source _ = source
|
|
|
|
mkBodyTransform (Just (TemplateText template)) engine source sessionVars =
|
|
|
|
let context = [("$", source)] <> catMaybes [("$session",) . J.toJSON <$> sessionVars]
|
2021-09-24 01:56:37 +03:00
|
|
|
in case engine of
|
|
|
|
Kriti -> case runKriti template context of
|
|
|
|
Left err -> J.toJSON err
|
|
|
|
Right res -> res
|
2021-09-16 14:03:01 +03:00
|
|
|
|
|
|
|
-- | Transform an `HTTP.Request` with a `RequestTransform`.
|
|
|
|
applyRequestTransform :: RequestTransform -> HTTP.Request -> Maybe SessionVariables -> HTTP.Request
|
2021-09-24 01:56:37 +03:00
|
|
|
applyRequestTransform RequestTransform {..} reqData sessionVars =
|
|
|
|
let method = fmap (TE.encodeUtf8 . renderRequestMethod) rtRequestMethod
|
|
|
|
bodyFunc (Just b) = case J.decode @J.Value b of
|
|
|
|
Just val -> pure $ J.encode $ rtBodyTransform val sessionVars
|
|
|
|
Nothing -> pure b
|
|
|
|
bodyFunc Nothing = Nothing
|
|
|
|
contentType = maybe "application/json" (TE.encodeUtf8 . renderContentType) rtContentType
|
|
|
|
headerFunc = nubBy (\a b -> fst a == fst b) . (:) ("Content-Type", contentType) . rtRequestHeaders
|
|
|
|
in reqData & over HTTP.url (`fromMaybe` rtRequestURL)
|
|
|
|
& over HTTP.method (`fromMaybe` method)
|
|
|
|
& over HTTP.queryParams (`fromMaybe` rtQueryParams)
|
|
|
|
& over HTTP.headers headerFunc
|
|
|
|
& over HTTP.body bodyFunc
|