2020-04-03 03:00:13 +03:00
|
|
|
module Hasura.Server.Auth.WebHook
|
|
|
|
( AuthHookType (..),
|
2022-08-04 05:24:35 +03:00
|
|
|
AuthHook (..),
|
2020-04-03 03:00:13 +03:00
|
|
|
userInfoFromAuthHook,
|
|
|
|
)
|
|
|
|
where
|
2021-09-24 01:56:37 +03:00
|
|
|
|
2021-05-11 18:18:31 +03:00
|
|
|
import Control.Exception.Lifted (try)
|
|
|
|
import Control.Lens
|
|
|
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
2021-02-03 10:10:39 +03:00
|
|
|
import Data.Aeson
|
2021-05-11 18:18:31 +03:00
|
|
|
import Data.Aeson qualified as J
|
2021-02-03 10:10:39 +03:00
|
|
|
import Data.ByteString.Lazy qualified as BL
|
|
|
|
import Data.HashMap.Strict qualified as Map
|
2021-11-09 15:00:21 +03:00
|
|
|
import Data.Parser.CacheControl (parseMaxAge)
|
2020-04-03 03:00:13 +03:00
|
|
|
import Data.Parser.Expires
|
2021-02-03 10:10:39 +03:00
|
|
|
import Data.Text qualified as T
|
2021-05-11 18:18:31 +03:00
|
|
|
import Data.Time.Clock (UTCTime, addUTCTime, getCurrentTime)
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.GraphQL.Transport.HTTP.Protocol qualified as GH
|
|
|
|
import Hasura.HTTP
|
2020-04-03 03:00:13 +03:00
|
|
|
import Hasura.Logging
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Prelude
|
2020-04-03 03:00:13 +03:00
|
|
|
import Hasura.Server.Logging
|
|
|
|
import Hasura.Server.Utils
|
|
|
|
import Hasura.Session
|
2022-02-16 10:08:51 +03:00
|
|
|
import Network.HTTP.Client.Transformable qualified as HTTP
|
2021-02-03 10:10:39 +03:00
|
|
|
import Network.Wreq qualified as Wreq
|
2020-04-03 03:00:13 +03:00
|
|
|
|
|
|
|
data AuthHookType
|
|
|
|
= AHTGet
|
|
|
|
| AHTPost
|
|
|
|
deriving (Eq)
|
|
|
|
|
|
|
|
instance Show AuthHookType where
|
|
|
|
show AHTGet = "GET"
|
|
|
|
show AHTPost = "POST"
|
|
|
|
|
2022-08-04 05:24:35 +03:00
|
|
|
data AuthHook = AuthHook
|
|
|
|
{ ahUrl :: Text,
|
2023-03-14 21:27:21 +03:00
|
|
|
ahType :: AuthHookType,
|
|
|
|
-- | Whether to send the request body to the auth hook
|
|
|
|
ahSendRequestBody :: Bool
|
2020-04-03 03:00:13 +03:00
|
|
|
}
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2022-02-16 10:08:51 +03:00
|
|
|
hookMethod :: AuthHook -> HTTP.StdMethod
|
2020-04-03 03:00:13 +03:00
|
|
|
hookMethod authHook = case ahType authHook of
|
2022-02-16 10:08:51 +03:00
|
|
|
AHTGet -> HTTP.GET
|
|
|
|
AHTPost -> HTTP.POST
|
2020-04-03 03:00:13 +03:00
|
|
|
|
|
|
|
-- | Makes an authentication request to the given AuthHook and returns
|
|
|
|
-- UserInfo parsed from the response, plus an expiration time if one
|
2021-02-03 10:10:39 +03:00
|
|
|
-- was returned. Optionally passes a batch of raw GraphQL requests
|
|
|
|
-- for finer-grained auth. (#2666)
|
2020-04-03 03:00:13 +03:00
|
|
|
userInfoFromAuthHook ::
|
2020-07-14 22:00:58 +03:00
|
|
|
forall m.
|
Rewrite `Tracing` to allow for only one `TraceT` in the entire stack.
This PR is on top of #7789.
### Description
This PR entirely rewrites the API of the Tracing library, to make `interpTraceT` a thing of the past. Before this change, we ran traces by sticking a `TraceT` on top of whatever we were doing. This had several major drawbacks:
- we were carrying a bunch of `TraceT` across the codebase, and the entire codebase had to know about it
- we needed to carry a second class constraint around (`HasReporterM`) to be able to run all of those traces
- we kept having to do stack rewriting with `interpTraceT`, which went from inconvenient to horrible
- we had to declare several behavioral instances on `TraceT m`
This PR rewrite all of `Tracing` using a more conventional model: there is ONE `TraceT` at the bottom of the stack, and there is an associated class constraint `MonadTrace`: any part of the code that happens to satisfy `MonadTrace` is able to create new traces. We NEVER have to do stack rewriting, `interpTraceT` is gone, and `TraceT` and `Reporter` become implementation details that 99% of the code is blissfully unaware of: code that needs to do tracing only needs to declare that the monad in which it operates implements `MonadTrace`.
In doing so, this PR revealed **several bugs in the codebase**: places where we were expecting to trace something, but due to the default instance of `HasReporterM IO` we would actually not do anything. This PR also splits the code of `Tracing` in more byte-sized modules, with the goal of potentially moving to `server/lib` down the line.
### Remaining work
This PR is a draft; what's left to do is:
- [x] make Pro compile; i haven't updated `HasuraPro/Main` yet
- [x] document Tracing by writing a note that explains how to use the library, and the meaning of "reporter", "trace" and "span", as well as the pitfalls
- [x] discuss some of the trade-offs in the implementation, which is why i'm opening this PR already despite it not fully building yet
- [x] it depends on #7789 being merged first
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7791
GitOrigin-RevId: cadd32d039134c93ddbf364599a2f4dd988adea8
2023-03-13 20:37:16 +03:00
|
|
|
(MonadIO m, MonadBaseControl IO m, MonadError QErr m) =>
|
2020-04-03 03:00:13 +03:00
|
|
|
Logger Hasura ->
|
2022-02-16 10:08:51 +03:00
|
|
|
HTTP.Manager ->
|
2020-04-03 03:00:13 +03:00
|
|
|
AuthHook ->
|
2022-02-16 10:08:51 +03:00
|
|
|
[HTTP.Header] ->
|
2021-09-15 11:29:34 +03:00
|
|
|
Maybe GH.ReqsText ->
|
2022-02-16 10:08:51 +03:00
|
|
|
m (UserInfo, Maybe UTCTime, [HTTP.Header])
|
2021-02-03 10:10:39 +03:00
|
|
|
userInfoFromAuthHook logger manager hook reqHeaders reqs = do
|
2020-07-14 22:00:58 +03:00
|
|
|
resp <- (`onLeft` logAndThrow) =<< try performHTTPRequest
|
2020-04-03 03:00:13 +03:00
|
|
|
let status = resp ^. Wreq.responseStatus
|
|
|
|
respBody = resp ^. Wreq.responseBody
|
2021-11-09 15:00:21 +03:00
|
|
|
cookieHeaders = filter (\(headerName, _) -> headerName == "Set-Cookie") (resp ^. Wreq.responseHeaders)
|
|
|
|
|
|
|
|
mkUserInfoFromResp logger (ahUrl hook) (hookMethod hook) status respBody cookieHeaders
|
2020-04-03 03:00:13 +03:00
|
|
|
where
|
2020-07-14 22:00:58 +03:00
|
|
|
performHTTPRequest :: m (Wreq.Response BL.ByteString)
|
2020-07-28 21:51:56 +03:00
|
|
|
performHTTPRequest = do
|
2020-04-03 03:00:13 +03:00
|
|
|
let url = T.unpack $ ahUrl hook
|
2022-02-16 10:08:51 +03:00
|
|
|
req <- liftIO $ HTTP.mkRequestThrow $ T.pack url
|
Rewrite `Tracing` to allow for only one `TraceT` in the entire stack.
This PR is on top of #7789.
### Description
This PR entirely rewrites the API of the Tracing library, to make `interpTraceT` a thing of the past. Before this change, we ran traces by sticking a `TraceT` on top of whatever we were doing. This had several major drawbacks:
- we were carrying a bunch of `TraceT` across the codebase, and the entire codebase had to know about it
- we needed to carry a second class constraint around (`HasReporterM`) to be able to run all of those traces
- we kept having to do stack rewriting with `interpTraceT`, which went from inconvenient to horrible
- we had to declare several behavioral instances on `TraceT m`
This PR rewrite all of `Tracing` using a more conventional model: there is ONE `TraceT` at the bottom of the stack, and there is an associated class constraint `MonadTrace`: any part of the code that happens to satisfy `MonadTrace` is able to create new traces. We NEVER have to do stack rewriting, `interpTraceT` is gone, and `TraceT` and `Reporter` become implementation details that 99% of the code is blissfully unaware of: code that needs to do tracing only needs to declare that the monad in which it operates implements `MonadTrace`.
In doing so, this PR revealed **several bugs in the codebase**: places where we were expecting to trace something, but due to the default instance of `HasReporterM IO` we would actually not do anything. This PR also splits the code of `Tracing` in more byte-sized modules, with the goal of potentially moving to `server/lib` down the line.
### Remaining work
This PR is a draft; what's left to do is:
- [x] make Pro compile; i haven't updated `HasuraPro/Main` yet
- [x] document Tracing by writing a note that explains how to use the library, and the meaning of "reporter", "trace" and "span", as well as the pitfalls
- [x] discuss some of the trade-offs in the implementation, which is why i'm opening this PR already despite it not fully building yet
- [x] it depends on #7789 being merged first
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7791
GitOrigin-RevId: cadd32d039134c93ddbf364599a2f4dd988adea8
2023-03-13 20:37:16 +03:00
|
|
|
liftIO do
|
2020-07-14 22:00:58 +03:00
|
|
|
case ahType hook of
|
|
|
|
AHTGet -> do
|
|
|
|
let isCommonHeader = (`elem` commonClientHeadersIgnored)
|
|
|
|
filteredHeaders = filter (not . isCommonHeader . fst) reqHeaders
|
Rewrite `Tracing` to allow for only one `TraceT` in the entire stack.
This PR is on top of #7789.
### Description
This PR entirely rewrites the API of the Tracing library, to make `interpTraceT` a thing of the past. Before this change, we ran traces by sticking a `TraceT` on top of whatever we were doing. This had several major drawbacks:
- we were carrying a bunch of `TraceT` across the codebase, and the entire codebase had to know about it
- we needed to carry a second class constraint around (`HasReporterM`) to be able to run all of those traces
- we kept having to do stack rewriting with `interpTraceT`, which went from inconvenient to horrible
- we had to declare several behavioral instances on `TraceT m`
This PR rewrite all of `Tracing` using a more conventional model: there is ONE `TraceT` at the bottom of the stack, and there is an associated class constraint `MonadTrace`: any part of the code that happens to satisfy `MonadTrace` is able to create new traces. We NEVER have to do stack rewriting, `interpTraceT` is gone, and `TraceT` and `Reporter` become implementation details that 99% of the code is blissfully unaware of: code that needs to do tracing only needs to declare that the monad in which it operates implements `MonadTrace`.
In doing so, this PR revealed **several bugs in the codebase**: places where we were expecting to trace something, but due to the default instance of `HasReporterM IO` we would actually not do anything. This PR also splits the code of `Tracing` in more byte-sized modules, with the goal of potentially moving to `server/lib` down the line.
### Remaining work
This PR is a draft; what's left to do is:
- [x] make Pro compile; i haven't updated `HasuraPro/Main` yet
- [x] document Tracing by writing a note that explains how to use the library, and the meaning of "reporter", "trace" and "span", as well as the pitfalls
- [x] discuss some of the trade-offs in the implementation, which is why i'm opening this PR already despite it not fully building yet
- [x] it depends on #7789 being merged first
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7791
GitOrigin-RevId: cadd32d039134c93ddbf364599a2f4dd988adea8
2023-03-13 20:37:16 +03:00
|
|
|
req' = req & set HTTP.headers (addDefaultHeaders filteredHeaders)
|
2023-03-22 02:59:42 +03:00
|
|
|
HTTP.httpLbs req' manager
|
2020-07-14 22:00:58 +03:00
|
|
|
AHTPost -> do
|
|
|
|
let contentType = ("Content-Type", "application/json")
|
|
|
|
headersPayload = J.toJSON $ Map.fromList $ hdrsToText reqHeaders
|
Rewrite `Tracing` to allow for only one `TraceT` in the entire stack.
This PR is on top of #7789.
### Description
This PR entirely rewrites the API of the Tracing library, to make `interpTraceT` a thing of the past. Before this change, we ran traces by sticking a `TraceT` on top of whatever we were doing. This had several major drawbacks:
- we were carrying a bunch of `TraceT` across the codebase, and the entire codebase had to know about it
- we needed to carry a second class constraint around (`HasReporterM`) to be able to run all of those traces
- we kept having to do stack rewriting with `interpTraceT`, which went from inconvenient to horrible
- we had to declare several behavioral instances on `TraceT m`
This PR rewrite all of `Tracing` using a more conventional model: there is ONE `TraceT` at the bottom of the stack, and there is an associated class constraint `MonadTrace`: any part of the code that happens to satisfy `MonadTrace` is able to create new traces. We NEVER have to do stack rewriting, `interpTraceT` is gone, and `TraceT` and `Reporter` become implementation details that 99% of the code is blissfully unaware of: code that needs to do tracing only needs to declare that the monad in which it operates implements `MonadTrace`.
In doing so, this PR revealed **several bugs in the codebase**: places where we were expecting to trace something, but due to the default instance of `HasReporterM IO` we would actually not do anything. This PR also splits the code of `Tracing` in more byte-sized modules, with the goal of potentially moving to `server/lib` down the line.
### Remaining work
This PR is a draft; what's left to do is:
- [x] make Pro compile; i haven't updated `HasuraPro/Main` yet
- [x] document Tracing by writing a note that explains how to use the library, and the meaning of "reporter", "trace" and "span", as well as the pitfalls
- [x] discuss some of the trade-offs in the implementation, which is why i'm opening this PR already despite it not fully building yet
- [x] it depends on #7789 being merged first
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7791
GitOrigin-RevId: cadd32d039134c93ddbf364599a2f4dd988adea8
2023-03-13 20:37:16 +03:00
|
|
|
req' =
|
2022-02-16 10:08:51 +03:00
|
|
|
req
|
|
|
|
& set HTTP.method "POST"
|
|
|
|
& set HTTP.headers (addDefaultHeaders [contentType])
|
2023-03-14 21:27:21 +03:00
|
|
|
& set
|
|
|
|
HTTP.body
|
2023-03-22 02:59:42 +03:00
|
|
|
( HTTP.RequestBodyLBS $
|
2023-03-14 21:27:21 +03:00
|
|
|
J.encode $
|
|
|
|
object
|
|
|
|
( ["headers" J..= headersPayload]
|
|
|
|
-- We will only send the request if `ahSendRequestBody` is set to true
|
|
|
|
<> ["request" J..= reqs | ahSendRequestBody hook]
|
|
|
|
)
|
|
|
|
)
|
2023-03-22 02:59:42 +03:00
|
|
|
HTTP.httpLbs req' manager
|
2020-04-03 03:00:13 +03:00
|
|
|
|
2022-02-16 10:08:51 +03:00
|
|
|
logAndThrow :: HTTP.HttpException -> m a
|
2020-04-03 03:00:13 +03:00
|
|
|
logAndThrow err = do
|
|
|
|
unLogger logger $
|
|
|
|
WebHookLog
|
|
|
|
LevelError
|
|
|
|
Nothing
|
|
|
|
(ahUrl hook)
|
|
|
|
(hookMethod hook)
|
|
|
|
(Just $ HttpException err)
|
|
|
|
Nothing
|
|
|
|
Nothing
|
2020-04-24 12:10:53 +03:00
|
|
|
throw500 "webhook authentication request failed"
|
2020-04-03 03:00:13 +03:00
|
|
|
|
|
|
|
mkUserInfoFromResp ::
|
|
|
|
(MonadIO m, MonadError QErr m) =>
|
|
|
|
Logger Hasura ->
|
2020-10-27 16:53:49 +03:00
|
|
|
Text ->
|
2022-02-16 10:08:51 +03:00
|
|
|
HTTP.StdMethod ->
|
|
|
|
HTTP.Status ->
|
2020-04-03 03:00:13 +03:00
|
|
|
BL.ByteString ->
|
2022-02-16 10:08:51 +03:00
|
|
|
[HTTP.Header] ->
|
|
|
|
m (UserInfo, Maybe UTCTime, [HTTP.Header])
|
2021-11-09 15:00:21 +03:00
|
|
|
mkUserInfoFromResp (Logger logger) url method statusCode respBody respHdrs
|
2022-02-16 10:08:51 +03:00
|
|
|
| statusCode == HTTP.status200 =
|
2020-04-03 03:00:13 +03:00
|
|
|
case eitherDecode respBody of
|
|
|
|
Left e -> do
|
|
|
|
logError
|
|
|
|
throw500 $ "Invalid response from authorization hook: " <> T.pack e
|
2021-11-09 15:00:21 +03:00
|
|
|
Right rawHeaders -> getUserInfoFromHdrs rawHeaders respHdrs
|
2022-02-16 10:08:51 +03:00
|
|
|
| statusCode == HTTP.status401 = do
|
2020-04-03 03:00:13 +03:00
|
|
|
logError
|
|
|
|
throw401 "Authentication hook unauthorized this request"
|
|
|
|
| otherwise = do
|
|
|
|
logError
|
|
|
|
throw500 "Invalid response from authorization hook"
|
|
|
|
where
|
2021-11-09 15:00:21 +03:00
|
|
|
getUserInfoFromHdrs rawHeaders responseHdrs = do
|
2020-05-05 22:57:17 +03:00
|
|
|
userInfo <-
|
|
|
|
mkUserInfo URBFromSessionVariables UAdminSecretNotSent $
|
2020-12-20 09:52:43 +03:00
|
|
|
mkSessionVariablesText rawHeaders
|
2020-04-24 12:10:53 +03:00
|
|
|
logWebHookResp LevelInfo Nothing Nothing
|
|
|
|
expiration <- runMaybeT $ timeFromCacheControl rawHeaders <|> timeFromExpires rawHeaders
|
2021-11-09 15:00:21 +03:00
|
|
|
pure (userInfo, expiration, responseHdrs)
|
2020-04-03 03:00:13 +03:00
|
|
|
|
2020-10-27 16:53:49 +03:00
|
|
|
logWebHookResp :: MonadIO m => LogLevel -> Maybe BL.ByteString -> Maybe Text -> m ()
|
2020-04-03 03:00:13 +03:00
|
|
|
logWebHookResp logLevel mResp message =
|
|
|
|
logger $
|
|
|
|
WebHookLog
|
|
|
|
logLevel
|
|
|
|
(Just statusCode)
|
|
|
|
url
|
|
|
|
method
|
|
|
|
Nothing
|
|
|
|
(bsToTxt . BL.toStrict <$> mResp)
|
|
|
|
message
|
|
|
|
logWarn message = logWebHookResp LevelWarn (Just respBody) (Just message)
|
|
|
|
logError = logWebHookResp LevelError (Just respBody) Nothing
|
|
|
|
|
|
|
|
timeFromCacheControl headers = do
|
|
|
|
header <- afold $ Map.lookup "Cache-Control" headers
|
|
|
|
duration <- parseMaxAge header `onLeft` \err -> logWarn (T.pack err) *> empty
|
|
|
|
addUTCTime (fromInteger duration) <$> liftIO getCurrentTime
|
|
|
|
timeFromExpires headers = do
|
|
|
|
header <- afold $ Map.lookup "Expires" headers
|
|
|
|
parseExpirationTime header `onLeft` \err -> logWarn (T.pack err) *> empty
|