2018-06-27 16:11:32 +03:00
|
|
|
module Hasura.Server.Init where
|
|
|
|
|
2018-12-14 06:21:41 +03:00
|
|
|
import qualified Database.PG.Query as Q
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
import Options.Applicative
|
2018-12-14 06:21:41 +03:00
|
|
|
import System.Exit (exitFailure)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2019-01-02 14:24:17 +03:00
|
|
|
import qualified Data.Aeson as J
|
2019-02-28 16:53:03 +03:00
|
|
|
import qualified Data.HashSet as Set
|
2019-03-01 14:45:04 +03:00
|
|
|
import qualified Data.String as DataString
|
2018-12-14 06:21:41 +03:00
|
|
|
import qualified Data.Text as T
|
2019-02-14 08:58:38 +03:00
|
|
|
import qualified Text.PrettyPrint.ANSI.Leijen as PP
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
import Hasura.Prelude
|
2018-12-14 06:21:41 +03:00
|
|
|
import Hasura.RQL.Types (RoleName (..))
|
add support for jwt authorization (close #186) (#255)
The API:
1. HGE has `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var. The value of which is a JSON.
2. The structure of this JSON is: `{"type": "<standard-JWT-algorithms>", "key": "<the-key>"}`
`type` : Standard JWT algos : `HS256`, `RS256`, `RS512` etc. (see jwt.io).
`key`:
i. Incase of symmetric key, the key as it is.
ii. Incase of asymmetric keys, only the public key, in a PEM encoded string or as a X509 certificate.
3. The claims in the JWT token must contain the following:
i. `x-hasura-default-role` field: default role of that user
ii. `x-hasura-allowed-roles` : A list of allowed roles for the user. The default role is overriden by `x-hasura-role` header.
4. The claims in the JWT token, can have other `x-hasura-*` fields where their values can only be strings.
5. The JWT tokens are sent as `Authorization: Bearer <token>` headers.
---
To test:
1. Generate a shared secret (for HMAC-SHA256) or RSA key pair.
2. Goto https://jwt.io/ , add the keys
3. Edit the claims to have `x-hasura-role` (mandatory) and other `x-hasura-*` fields. Add permissions related to the claims to test permissions.
4. Start HGE with `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var, which takes a JSON string: `{"type": "HS256", "key": "mylongsharedsecret"}` or `{"type":"RS256", "key": "<PEM-encoded-public-key>"}`
5. Copy the JWT token from jwt.io and use it in the `Authorization: Bearer <token>` header.
---
TODO: Support EC public keys. It is blocked on frasertweedale/hs-jose#61
2018-08-30 13:32:09 +03:00
|
|
|
import Hasura.Server.Auth
|
2019-02-14 08:58:38 +03:00
|
|
|
import Hasura.Server.Cors
|
2019-01-02 14:24:17 +03:00
|
|
|
import Hasura.Server.Logging
|
2018-06-28 13:49:40 +03:00
|
|
|
import Hasura.Server.Utils
|
2019-01-11 14:07:13 +03:00
|
|
|
import Network.Wai.Handler.Warp
|
2019-02-14 08:58:38 +03:00
|
|
|
|
|
|
|
import qualified Hasura.Logging as L
|
2019-01-11 14:07:13 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
initErrExit :: (Show e) => e -> IO a
|
|
|
|
initErrExit e = print e >> exitFailure
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
data RawConnParams
|
|
|
|
= RawConnParams
|
2019-01-18 17:20:41 +03:00
|
|
|
{ rcpStripes :: !(Maybe Int)
|
|
|
|
, rcpConns :: !(Maybe Int)
|
|
|
|
, rcpIdleTime :: !(Maybe Int)
|
|
|
|
, rcpAllowPrepare :: !(Maybe Bool)
|
2018-12-19 14:38:33 +03:00
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
type RawAuthHook = AuthHookG (Maybe T.Text) (Maybe AuthHookType)
|
|
|
|
|
|
|
|
data RawServeOptions
|
|
|
|
= RawServeOptions
|
2019-01-28 16:55:28 +03:00
|
|
|
{ rsoPort :: !(Maybe Int)
|
|
|
|
, rsoHost :: !(Maybe HostPreference)
|
|
|
|
, rsoConnParams :: !RawConnParams
|
|
|
|
, rsoTxIso :: !(Maybe Q.TxIsolation)
|
2019-02-14 12:37:47 +03:00
|
|
|
, rsoAdminSecret :: !(Maybe AdminSecret)
|
2019-01-28 16:55:28 +03:00
|
|
|
, rsoAuthHook :: !RawAuthHook
|
|
|
|
, rsoJwtSecret :: !(Maybe Text)
|
|
|
|
, rsoUnAuthRole :: !(Maybe RoleName)
|
2019-02-14 08:58:38 +03:00
|
|
|
, rsoCorsConfig :: !(Maybe CorsConfig)
|
2019-01-28 16:55:28 +03:00
|
|
|
, rsoEnableConsole :: !Bool
|
|
|
|
, rsoEnableTelemetry :: !(Maybe Bool)
|
2019-03-01 14:45:04 +03:00
|
|
|
, rsoStringifyNum :: !Bool
|
2019-02-28 16:53:03 +03:00
|
|
|
, rsoEnabledAPIs :: !(Maybe [API])
|
2018-12-19 14:38:33 +03:00
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
|
|
|
data ServeOptions
|
|
|
|
= ServeOptions
|
2019-01-28 16:55:28 +03:00
|
|
|
{ soPort :: !Int
|
|
|
|
, soHost :: !HostPreference
|
|
|
|
, soConnParams :: !Q.ConnParams
|
|
|
|
, soTxIso :: !Q.TxIsolation
|
2019-02-14 12:37:47 +03:00
|
|
|
, soAdminSecret :: !(Maybe AdminSecret)
|
2019-01-28 16:55:28 +03:00
|
|
|
, soAuthHook :: !(Maybe AuthHook)
|
|
|
|
, soJwtSecret :: !(Maybe Text)
|
|
|
|
, soUnAuthRole :: !(Maybe RoleName)
|
|
|
|
, soCorsConfig :: !CorsConfig
|
|
|
|
, soEnableConsole :: !Bool
|
|
|
|
, soEnableTelemetry :: !Bool
|
2019-03-01 14:45:04 +03:00
|
|
|
, soStringifyNum :: !Bool
|
2019-02-28 16:53:03 +03:00
|
|
|
, soEnabledAPIs :: !(Set.HashSet API)
|
2018-12-19 14:38:33 +03:00
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
data RawConnInfo =
|
|
|
|
RawConnInfo
|
|
|
|
{ connHost :: !(Maybe String)
|
|
|
|
, connPort :: !(Maybe Int)
|
|
|
|
, connUser :: !(Maybe String)
|
|
|
|
, connPassword :: !String
|
|
|
|
, connUrl :: !(Maybe String)
|
|
|
|
, connDatabase :: !(Maybe String)
|
|
|
|
, connOptions :: !(Maybe String)
|
|
|
|
} deriving (Eq, Read, Show)
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
data HGECommandG a
|
|
|
|
= HCServe !a
|
|
|
|
| HCExport
|
|
|
|
| HCClean
|
|
|
|
| HCExecute
|
|
|
|
| HCVersion
|
|
|
|
deriving (Show, Eq)
|
|
|
|
|
2019-02-28 16:53:03 +03:00
|
|
|
data API
|
|
|
|
= METADATA
|
|
|
|
| GRAPHQL
|
|
|
|
deriving (Show, Eq, Read, Generic)
|
|
|
|
|
|
|
|
instance Hashable API
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
type HGECommand = HGECommandG ServeOptions
|
|
|
|
type RawHGECommand = HGECommandG RawServeOptions
|
|
|
|
|
|
|
|
data HGEOptionsG a
|
|
|
|
= HGEOptionsG
|
|
|
|
{ hoConnInfo :: !RawConnInfo
|
|
|
|
, hoCommand :: !(HGECommandG a)
|
add support for jwt authorization (close #186) (#255)
The API:
1. HGE has `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var. The value of which is a JSON.
2. The structure of this JSON is: `{"type": "<standard-JWT-algorithms>", "key": "<the-key>"}`
`type` : Standard JWT algos : `HS256`, `RS256`, `RS512` etc. (see jwt.io).
`key`:
i. Incase of symmetric key, the key as it is.
ii. Incase of asymmetric keys, only the public key, in a PEM encoded string or as a X509 certificate.
3. The claims in the JWT token must contain the following:
i. `x-hasura-default-role` field: default role of that user
ii. `x-hasura-allowed-roles` : A list of allowed roles for the user. The default role is overriden by `x-hasura-role` header.
4. The claims in the JWT token, can have other `x-hasura-*` fields where their values can only be strings.
5. The JWT tokens are sent as `Authorization: Bearer <token>` headers.
---
To test:
1. Generate a shared secret (for HMAC-SHA256) or RSA key pair.
2. Goto https://jwt.io/ , add the keys
3. Edit the claims to have `x-hasura-role` (mandatory) and other `x-hasura-*` fields. Add permissions related to the claims to test permissions.
4. Start HGE with `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var, which takes a JSON string: `{"type": "HS256", "key": "mylongsharedsecret"}` or `{"type":"RS256", "key": "<PEM-encoded-public-key>"}`
5. Copy the JWT token from jwt.io and use it in the `Authorization: Bearer <token>` header.
---
TODO: Support EC public keys. It is blocked on frasertweedale/hs-jose#61
2018-08-30 13:32:09 +03:00
|
|
|
} deriving (Show, Eq)
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
type RawHGEOptions = HGEOptionsG RawServeOptions
|
|
|
|
type HGEOptions = HGEOptionsG ServeOptions
|
|
|
|
|
2018-12-14 06:21:41 +03:00
|
|
|
type Env = [(String, String)]
|
add support for jwt authorization (close #186) (#255)
The API:
1. HGE has `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var. The value of which is a JSON.
2. The structure of this JSON is: `{"type": "<standard-JWT-algorithms>", "key": "<the-key>"}`
`type` : Standard JWT algos : `HS256`, `RS256`, `RS512` etc. (see jwt.io).
`key`:
i. Incase of symmetric key, the key as it is.
ii. Incase of asymmetric keys, only the public key, in a PEM encoded string or as a X509 certificate.
3. The claims in the JWT token must contain the following:
i. `x-hasura-default-role` field: default role of that user
ii. `x-hasura-allowed-roles` : A list of allowed roles for the user. The default role is overriden by `x-hasura-role` header.
4. The claims in the JWT token, can have other `x-hasura-*` fields where their values can only be strings.
5. The JWT tokens are sent as `Authorization: Bearer <token>` headers.
---
To test:
1. Generate a shared secret (for HMAC-SHA256) or RSA key pair.
2. Goto https://jwt.io/ , add the keys
3. Edit the claims to have `x-hasura-role` (mandatory) and other `x-hasura-*` fields. Add permissions related to the claims to test permissions.
4. Start HGE with `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var, which takes a JSON string: `{"type": "HS256", "key": "mylongsharedsecret"}` or `{"type":"RS256", "key": "<PEM-encoded-public-key>"}`
5. Copy the JWT token from jwt.io and use it in the `Authorization: Bearer <token>` header.
---
TODO: Support EC public keys. It is blocked on frasertweedale/hs-jose#61
2018-08-30 13:32:09 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
class FromEnv a where
|
|
|
|
fromEnv :: String -> Either String a
|
add support for jwt authorization (close #186) (#255)
The API:
1. HGE has `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var. The value of which is a JSON.
2. The structure of this JSON is: `{"type": "<standard-JWT-algorithms>", "key": "<the-key>"}`
`type` : Standard JWT algos : `HS256`, `RS256`, `RS512` etc. (see jwt.io).
`key`:
i. Incase of symmetric key, the key as it is.
ii. Incase of asymmetric keys, only the public key, in a PEM encoded string or as a X509 certificate.
3. The claims in the JWT token must contain the following:
i. `x-hasura-default-role` field: default role of that user
ii. `x-hasura-allowed-roles` : A list of allowed roles for the user. The default role is overriden by `x-hasura-role` header.
4. The claims in the JWT token, can have other `x-hasura-*` fields where their values can only be strings.
5. The JWT tokens are sent as `Authorization: Bearer <token>` headers.
---
To test:
1. Generate a shared secret (for HMAC-SHA256) or RSA key pair.
2. Goto https://jwt.io/ , add the keys
3. Edit the claims to have `x-hasura-role` (mandatory) and other `x-hasura-*` fields. Add permissions related to the claims to test permissions.
4. Start HGE with `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var, which takes a JSON string: `{"type": "HS256", "key": "mylongsharedsecret"}` or `{"type":"RS256", "key": "<PEM-encoded-public-key>"}`
5. Copy the JWT token from jwt.io and use it in the `Authorization: Bearer <token>` header.
---
TODO: Support EC public keys. It is blocked on frasertweedale/hs-jose#61
2018-08-30 13:32:09 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
instance FromEnv String where
|
|
|
|
fromEnv = Right
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2019-01-11 14:07:13 +03:00
|
|
|
instance FromEnv HostPreference where
|
|
|
|
fromEnv = Right . DataString.fromString
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
instance FromEnv Text where
|
|
|
|
fromEnv = Right . T.pack
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
instance FromEnv AuthHookType where
|
|
|
|
fromEnv = readHookType
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
instance FromEnv Int where
|
|
|
|
fromEnv = maybe (Left "Expecting Int value") Right . readMaybe
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
instance FromEnv AdminSecret where
|
|
|
|
fromEnv = Right . AdminSecret . T.pack
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
instance FromEnv RoleName where
|
|
|
|
fromEnv = Right . RoleName . T.pack
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
instance FromEnv Bool where
|
|
|
|
fromEnv = parseStrAsBool
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
instance FromEnv Q.TxIsolation where
|
|
|
|
fromEnv = readIsoLevel
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2019-02-14 08:58:38 +03:00
|
|
|
instance FromEnv CorsConfig where
|
|
|
|
fromEnv = readCorsDomains
|
|
|
|
|
2019-02-28 16:53:03 +03:00
|
|
|
instance FromEnv [API] where
|
|
|
|
fromEnv = readAPIs
|
|
|
|
|
2018-12-14 06:21:41 +03:00
|
|
|
parseStrAsBool :: String -> Either String Bool
|
|
|
|
parseStrAsBool t
|
|
|
|
| t `elem` truthVals = Right True
|
|
|
|
| t `elem` falseVals = Right False
|
|
|
|
| otherwise = Left errMsg
|
|
|
|
where
|
|
|
|
truthVals = ["true", "t", "yes", "y"]
|
|
|
|
falseVals = ["false", "f", "no", "n"]
|
|
|
|
|
|
|
|
errMsg = " Not a valid boolean text. " ++ "True values are "
|
|
|
|
++ show truthVals ++ " and False values are " ++ show falseVals
|
|
|
|
++ ". All values are case insensitive"
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
readIsoLevel :: String -> Either String Q.TxIsolation
|
|
|
|
readIsoLevel isoS =
|
|
|
|
case isoS of
|
|
|
|
"read-comitted" -> return Q.ReadCommitted
|
|
|
|
"repeatable-read" -> return Q.RepeatableRead
|
|
|
|
"serializable" -> return Q.ReadCommitted
|
|
|
|
_ -> Left "Only expecting read-comitted / repeatable-read / serializable"
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
type WithEnv a = ReaderT Env (ExceptT String Identity) a
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
runWithEnv :: Env -> WithEnv a -> Either String a
|
|
|
|
runWithEnv env m = runIdentity $ runExceptT $ runReaderT m env
|
2018-12-14 06:21:41 +03:00
|
|
|
|
|
|
|
returnJust :: Monad m => a -> m (Maybe a)
|
|
|
|
returnJust = return . Just
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
considerEnv :: FromEnv a => String -> WithEnv (Maybe a)
|
2018-12-14 06:21:41 +03:00
|
|
|
considerEnv envVar = do
|
|
|
|
env <- ask
|
2018-12-19 14:38:33 +03:00
|
|
|
case lookup envVar env of
|
2018-12-14 06:21:41 +03:00
|
|
|
Nothing -> return Nothing
|
2018-12-19 14:38:33 +03:00
|
|
|
Just val -> either throwErr returnJust $ fromEnv val
|
2018-12-14 06:21:41 +03:00
|
|
|
where
|
|
|
|
throwErr s = throwError $
|
2018-12-19 14:38:33 +03:00
|
|
|
"Fatal Error:- Environment variable " ++ envVar ++ ": " ++ s
|
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
considerEnvs :: FromEnv a => [String] -> WithEnv (Maybe a)
|
2019-03-01 14:45:04 +03:00
|
|
|
considerEnvs envVars = foldl1 (<|>) <$> mapM considerEnv envVars
|
2019-02-14 12:37:47 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
withEnv :: FromEnv a => Maybe a -> String -> WithEnv (Maybe a)
|
|
|
|
withEnv mVal envVar =
|
|
|
|
maybe (considerEnv envVar) returnJust mVal
|
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
withEnvs :: FromEnv a => Maybe a -> [String] -> WithEnv (Maybe a)
|
|
|
|
withEnvs mVal envVars =
|
|
|
|
maybe (considerEnvs envVars) returnJust mVal
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
withEnvBool :: Bool -> String -> WithEnv Bool
|
|
|
|
withEnvBool bVal envVar =
|
|
|
|
bool considerEnv' (return True) bVal
|
|
|
|
where
|
|
|
|
considerEnv' = do
|
|
|
|
mEnvVal <- considerEnv envVar
|
|
|
|
maybe (return False) return mEnvVal
|
|
|
|
|
|
|
|
mkHGEOptions :: RawHGEOptions -> WithEnv HGEOptions
|
|
|
|
mkHGEOptions (HGEOptionsG rawConnInfo rawCmd) =
|
|
|
|
HGEOptionsG <$> connInfo <*> cmd
|
|
|
|
where
|
|
|
|
connInfo = mkRawConnInfo rawConnInfo
|
|
|
|
cmd = case rawCmd of
|
|
|
|
HCServe rso -> HCServe <$> mkServeOptions rso
|
|
|
|
HCExport -> return HCExport
|
|
|
|
HCClean -> return HCClean
|
|
|
|
HCExecute -> return HCExecute
|
|
|
|
HCVersion -> return HCVersion
|
|
|
|
|
|
|
|
mkRawConnInfo :: RawConnInfo -> WithEnv RawConnInfo
|
|
|
|
mkRawConnInfo rawConnInfo = do
|
|
|
|
withEnvUrl <- withEnv rawDBUrl $ fst databaseUrlEnv
|
|
|
|
return $ rawConnInfo {connUrl = withEnvUrl}
|
|
|
|
where
|
|
|
|
rawDBUrl = connUrl rawConnInfo
|
|
|
|
|
|
|
|
mkServeOptions :: RawServeOptions -> WithEnv ServeOptions
|
|
|
|
mkServeOptions rso = do
|
|
|
|
port <- fromMaybe 8080 <$>
|
|
|
|
withEnv (rsoPort rso) (fst servePortEnv)
|
2019-01-11 14:07:13 +03:00
|
|
|
host <- fromMaybe "*" <$>
|
|
|
|
withEnv (rsoHost rso) (fst serveHostEnv)
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
connParams <- mkConnParams $ rsoConnParams rso
|
|
|
|
txIso <- fromMaybe Q.ReadCommitted <$>
|
|
|
|
withEnv (rsoTxIso rso) (fst txIsoEnv)
|
2019-02-14 12:37:47 +03:00
|
|
|
adminScrt <- withEnvs (rsoAdminSecret rso) $ map fst [adminSecretEnv, accessKeyEnv]
|
2018-12-19 14:38:33 +03:00
|
|
|
authHook <- mkAuthHook $ rsoAuthHook rso
|
2019-01-28 16:55:28 +03:00
|
|
|
jwtSecret <- withEnv (rsoJwtSecret rso) $ fst jwtSecretEnv
|
2018-12-19 14:38:33 +03:00
|
|
|
unAuthRole <- withEnv (rsoUnAuthRole rso) $ fst unAuthRoleEnv
|
|
|
|
corsCfg <- mkCorsConfig $ rsoCorsConfig rso
|
|
|
|
enableConsole <- withEnvBool (rsoEnableConsole rso) $
|
|
|
|
fst enableConsoleEnv
|
2019-01-28 16:55:28 +03:00
|
|
|
enableTelemetry <- fromMaybe True <$>
|
|
|
|
withEnv (rsoEnableTelemetry rso) (fst enableTelemetryEnv)
|
2019-03-01 14:45:04 +03:00
|
|
|
strfyNum <- withEnvBool (rsoStringifyNum rso) $ fst stringifyNumEnv
|
2019-02-28 16:53:03 +03:00
|
|
|
enabledAPIs <- Set.fromList . fromMaybe [METADATA,GRAPHQL] <$>
|
|
|
|
withEnv (rsoEnabledAPIs rso) (fst enabledAPIsEnv)
|
2019-02-14 12:37:47 +03:00
|
|
|
return $ ServeOptions port host connParams txIso adminScrt authHook jwtSecret
|
2019-03-01 14:45:04 +03:00
|
|
|
unAuthRole corsCfg enableConsole enableTelemetry strfyNum enabledAPIs
|
2018-12-19 14:38:33 +03:00
|
|
|
where
|
2019-01-18 17:20:41 +03:00
|
|
|
mkConnParams (RawConnParams s c i p) = do
|
2018-12-19 14:38:33 +03:00
|
|
|
stripes <- fromMaybe 1 <$> withEnv s (fst pgStripesEnv)
|
|
|
|
conns <- fromMaybe 50 <$> withEnv c (fst pgConnsEnv)
|
|
|
|
iTime <- fromMaybe 180 <$> withEnv i (fst pgTimeoutEnv)
|
2019-01-18 17:20:41 +03:00
|
|
|
allowPrepare <- fromMaybe True <$> withEnv p (fst pgUsePrepareEnv)
|
|
|
|
return $ Q.ConnParams stripes conns iTime allowPrepare
|
2018-12-19 14:38:33 +03:00
|
|
|
|
|
|
|
mkAuthHook (AuthHookG mUrl mType) = do
|
|
|
|
mUrlEnv <- withEnv mUrl $ fst authHookEnv
|
2019-01-04 09:42:36 +03:00
|
|
|
authModeM <- withEnv mType (fst authHookModeEnv)
|
|
|
|
ty <- maybe (authHookTyEnv mType) return authModeM
|
2018-12-19 14:38:33 +03:00
|
|
|
return (flip AuthHookG ty <$> mUrlEnv)
|
|
|
|
|
2019-01-04 09:42:36 +03:00
|
|
|
-- Also support HASURA_GRAPHQL_AUTH_HOOK_TYPE
|
|
|
|
-- TODO:- drop this in next major update
|
|
|
|
authHookTyEnv mType = fromMaybe AHTGet <$>
|
|
|
|
withEnv mType "HASURA_GRAPHQL_AUTH_HOOK_TYPE"
|
|
|
|
|
2019-02-14 08:58:38 +03:00
|
|
|
mkCorsConfig mCfg =
|
|
|
|
fromMaybe CCAllowAll <$> withEnv mCfg (fst corsDomainEnv)
|
2018-12-14 06:21:41 +03:00
|
|
|
|
|
|
|
mkExamplesDoc :: [[String]] -> PP.Doc
|
|
|
|
mkExamplesDoc exampleLines =
|
|
|
|
PP.text "Examples: " PP.<$> PP.indent 2 (PP.vsep examples)
|
|
|
|
where
|
|
|
|
examples = map PP.text $ intercalate [""] exampleLines
|
|
|
|
|
|
|
|
mkEnvVarDoc :: [(String, String)] -> PP.Doc
|
|
|
|
mkEnvVarDoc envVars =
|
|
|
|
PP.text "Environment variables: " PP.<$>
|
|
|
|
PP.indent 2 (PP.vsep $ map mkEnvVarLine envVars)
|
|
|
|
where
|
|
|
|
mkEnvVarLine (var, desc) =
|
2019-01-28 16:55:28 +03:00
|
|
|
(PP.fillBreak 40 (PP.text var) PP.<+> prettifyDesc desc) <> PP.hardline
|
2018-12-14 06:21:41 +03:00
|
|
|
prettifyDesc = PP.align . PP.fillSep . map PP.text . words
|
|
|
|
|
|
|
|
mainCmdFooter :: PP.Doc
|
|
|
|
mainCmdFooter =
|
|
|
|
examplesDoc PP.<$> PP.text "" PP.<$> envVarDoc
|
|
|
|
where
|
|
|
|
examplesDoc = mkExamplesDoc examples
|
|
|
|
examples =
|
|
|
|
[
|
|
|
|
[ "# Serve GraphQL Engine on default port (8080) with console disabled"
|
|
|
|
, "graphql-engine --database-url <database-url> serve"
|
|
|
|
]
|
|
|
|
, [ "# For more options, checkout"
|
|
|
|
, "graphql-engine serve --help"
|
|
|
|
]
|
|
|
|
]
|
|
|
|
|
|
|
|
envVarDoc = mkEnvVarDoc [databaseUrlEnv]
|
|
|
|
|
|
|
|
databaseUrlEnv :: (String, String)
|
|
|
|
databaseUrlEnv =
|
|
|
|
( "HASURA_GRAPHQL_DATABASE_URL"
|
|
|
|
, "Postgres database URL. Example postgres://foo:bar@example.com:2345/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
serveCmdFooter :: PP.Doc
|
|
|
|
serveCmdFooter =
|
|
|
|
examplesDoc PP.<$> PP.text "" PP.<$> envVarDoc
|
|
|
|
where
|
|
|
|
examplesDoc = mkExamplesDoc examples
|
|
|
|
examples =
|
|
|
|
[
|
|
|
|
[ "# Start GraphQL Engine on default port (8080) with console enabled"
|
|
|
|
, "graphql-engine --database-url <database-url> serve --enable-console"
|
|
|
|
]
|
|
|
|
, [ "# Start GraphQL Engine on default port (8080) with console disabled"
|
|
|
|
, "graphql-engine --database-url <database-url> serve"
|
|
|
|
]
|
|
|
|
, [ "# Start GraphQL Engine on a different port (say 9090) with console disabled"
|
|
|
|
, "graphql-engine --database-url <database-url> serve --server-port 9090"
|
|
|
|
]
|
2019-02-14 12:37:47 +03:00
|
|
|
, [ "# Start GraphQL Engine with admin secret key"
|
|
|
|
, "graphql-engine --database-url <database-url> serve --admin-secret <adminsecretkey>"
|
2018-12-14 06:21:41 +03:00
|
|
|
]
|
|
|
|
, [ "# Start GraphQL Engine with restrictive CORS policy (only allow https://example.com:8080)"
|
|
|
|
, "graphql-engine --database-url <database-url> serve --cors-domain https://example.com:8080"
|
|
|
|
]
|
2019-02-14 08:58:38 +03:00
|
|
|
, [ "# Start GraphQL Engine with multiple domains for CORS (https://example.com, http://localhost:3000 and https://*.foo.bar.com)"
|
|
|
|
, "graphql-engine --database-url <database-url> serve --cors-domain \"https://example.com, https://*.foo.bar.com, http://localhost:3000\""
|
|
|
|
]
|
2018-12-14 06:21:41 +03:00
|
|
|
, [ "# Start GraphQL Engine with Authentication Webhook (GET)"
|
2019-02-14 12:37:47 +03:00
|
|
|
, "graphql-engine --database-url <database-url> serve --admin-secret <adminsecretkey>"
|
2018-12-14 06:21:41 +03:00
|
|
|
<> " --auth-hook https://mywebhook.com/get"
|
|
|
|
]
|
|
|
|
, [ "# Start GraphQL Engine with Authentication Webhook (POST)"
|
2019-02-14 12:37:47 +03:00
|
|
|
, "graphql-engine --database-url <database-url> serve --admin-secret <adminsecretkey>"
|
2018-12-14 06:21:41 +03:00
|
|
|
<> " --auth-hook https://mywebhook.com/post --auth-hook-mode POST"
|
|
|
|
]
|
2019-01-28 16:55:28 +03:00
|
|
|
, [ "# Start GraphQL Engine with telemetry enabled/disabled"
|
|
|
|
, "graphql-engine --database-url <database-url> serve --enable-telemetry true|false"
|
|
|
|
]
|
2018-12-14 06:21:41 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
envVarDoc = mkEnvVarDoc $ envVars <> eventEnvs
|
|
|
|
envVars =
|
2019-01-11 14:07:13 +03:00
|
|
|
[ servePortEnv, serveHostEnv, pgStripesEnv, pgConnsEnv, pgTimeoutEnv
|
2019-03-01 14:45:04 +03:00
|
|
|
, pgUsePrepareEnv, txIsoEnv, adminSecretEnv
|
|
|
|
, accessKeyEnv, authHookEnv, authHookModeEnv
|
2019-02-14 12:37:47 +03:00
|
|
|
, jwtSecretEnv, unAuthRoleEnv, corsDomainEnv, enableConsoleEnv
|
2019-03-01 14:45:04 +03:00
|
|
|
, enableTelemetryEnv, stringifyNumEnv
|
2018-12-14 06:21:41 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
eventEnvs =
|
|
|
|
[ ( "HASURA_GRAPHQL_EVENTS_HTTP_POOL_SIZE"
|
|
|
|
, "Max event threads"
|
|
|
|
)
|
|
|
|
, ( "HASURA_GRAPHQL_EVENTS_FETCH_INTERVAL"
|
|
|
|
, "Postgres events polling interval"
|
|
|
|
)
|
|
|
|
]
|
|
|
|
|
|
|
|
servePortEnv :: (String, String)
|
|
|
|
servePortEnv =
|
|
|
|
( "HASURA_GRAPHQL_SERVER_PORT"
|
|
|
|
, "Port on which graphql-engine should be served (default: 8080)"
|
|
|
|
)
|
|
|
|
|
2019-01-11 14:07:13 +03:00
|
|
|
serveHostEnv :: (String, String)
|
|
|
|
serveHostEnv =
|
|
|
|
( "HASURA_GRAPHQL_SERVER_HOST"
|
|
|
|
, "Host on which graphql-engine will listen (default: *)"
|
|
|
|
)
|
|
|
|
|
2018-12-14 06:21:41 +03:00
|
|
|
pgConnsEnv :: (String, String)
|
|
|
|
pgConnsEnv =
|
|
|
|
( "HASURA_GRAPHQL_PG_CONNECTIONS"
|
|
|
|
, "Number of conns that need to be opened to Postgres (default: 50)"
|
|
|
|
)
|
|
|
|
|
|
|
|
pgStripesEnv :: (String, String)
|
|
|
|
pgStripesEnv =
|
|
|
|
( "HASURA_GRAPHQL_PG_STRIPES"
|
|
|
|
, "Number of conns that need to be opened to Postgres (default: 1)")
|
|
|
|
|
|
|
|
pgTimeoutEnv :: (String, String)
|
|
|
|
pgTimeoutEnv =
|
|
|
|
( "HASURA_GRAPHQL_PG_TIMEOUT"
|
|
|
|
, "Each connection's idle time before it is closed (default: 180 sec)"
|
|
|
|
)
|
|
|
|
|
2019-01-18 17:20:41 +03:00
|
|
|
pgUsePrepareEnv :: (String, String)
|
|
|
|
pgUsePrepareEnv =
|
|
|
|
( "HASURA_GRAPHQL_USE_PREPARED_STATEMENTS"
|
2019-02-14 08:58:38 +03:00
|
|
|
, "Use prepared statements for queries (default: true)"
|
2019-01-18 17:20:41 +03:00
|
|
|
)
|
|
|
|
|
2018-12-14 06:21:41 +03:00
|
|
|
txIsoEnv :: (String, String)
|
|
|
|
txIsoEnv =
|
|
|
|
( "HASURA_GRAPHQL_TX_ISOLATION"
|
|
|
|
, "transaction isolation. read-committed / repeatable-read / serializable (default: read-commited)"
|
|
|
|
)
|
|
|
|
|
|
|
|
accessKeyEnv :: (String, String)
|
|
|
|
accessKeyEnv =
|
|
|
|
( "HASURA_GRAPHQL_ACCESS_KEY"
|
2019-02-14 12:37:47 +03:00
|
|
|
, "Admin secret key, required to access this instance (deprecated: use HASURA_GRAPHQL_ADMIN_SECRET instead)"
|
|
|
|
)
|
|
|
|
|
|
|
|
adminSecretEnv :: (String, String)
|
|
|
|
adminSecretEnv =
|
|
|
|
( "HASURA_GRAPHQL_ADMIN_SECRET"
|
|
|
|
, "Admin Secret key, required to access this instance"
|
2018-12-14 06:21:41 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
authHookEnv :: (String, String)
|
|
|
|
authHookEnv =
|
|
|
|
( "HASURA_GRAPHQL_AUTH_HOOK"
|
2019-02-14 08:58:38 +03:00
|
|
|
, "URL of the authorization webhook required to authorize requests"
|
2018-12-14 06:21:41 +03:00
|
|
|
)
|
|
|
|
|
2019-01-04 09:42:36 +03:00
|
|
|
authHookModeEnv :: (String, String)
|
|
|
|
authHookModeEnv =
|
|
|
|
( "HASURA_GRAPHQL_AUTH_HOOK_MODE"
|
2019-02-14 08:58:38 +03:00
|
|
|
, "HTTP method to use for authorization webhook (default: GET)"
|
2018-12-14 06:21:41 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
jwtSecretEnv :: (String, String)
|
|
|
|
jwtSecretEnv =
|
|
|
|
( "HASURA_GRAPHQL_JWT_SECRET"
|
|
|
|
, jwtSecretHelp
|
|
|
|
)
|
|
|
|
|
|
|
|
unAuthRoleEnv :: (String, String)
|
|
|
|
unAuthRoleEnv =
|
|
|
|
( "HASURA_GRAPHQL_UNAUTHORIZED_ROLE"
|
2019-02-14 12:37:47 +03:00
|
|
|
, "Unauthorized role, used when admin-secret is not sent in admin-secret only mode "
|
2018-12-14 06:21:41 +03:00
|
|
|
++ "or \"Authorization\" header is absent in JWT mode"
|
|
|
|
)
|
|
|
|
|
|
|
|
corsDomainEnv :: (String, String)
|
|
|
|
corsDomainEnv =
|
|
|
|
( "HASURA_GRAPHQL_CORS_DOMAIN"
|
2019-02-14 08:58:38 +03:00
|
|
|
, "CSV of list of domains, excluding scheme (http/https) and including port, "
|
|
|
|
++ "to allow CORS for. Wildcard domains are allowed. See docs for details."
|
2018-12-14 06:21:41 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
enableConsoleEnv :: (String, String)
|
|
|
|
enableConsoleEnv =
|
|
|
|
( "HASURA_GRAPHQL_ENABLE_CONSOLE"
|
|
|
|
, "Enable API Console"
|
|
|
|
)
|
|
|
|
|
2019-01-28 16:55:28 +03:00
|
|
|
enableTelemetryEnv :: (String, String)
|
|
|
|
enableTelemetryEnv =
|
|
|
|
( "HASURA_GRAPHQL_ENABLE_TELEMETRY"
|
|
|
|
-- TODO: better description
|
|
|
|
, "Enable anonymous telemetry (default: true)"
|
|
|
|
)
|
|
|
|
|
2019-03-01 14:45:04 +03:00
|
|
|
stringifyNumEnv :: (String, String)
|
|
|
|
stringifyNumEnv =
|
|
|
|
( "HASURA_GRAPHQL_STRINGIFY_NUMERIC_TYPES"
|
|
|
|
, "Stringify numeric types (default: false)"
|
|
|
|
)
|
|
|
|
|
2019-02-28 16:53:03 +03:00
|
|
|
enabledAPIsEnv :: (String,String)
|
|
|
|
enabledAPIsEnv =
|
|
|
|
( "HASURA_GRAPHQL_ENABLED_APIS"
|
|
|
|
, "List of comma separated list of allowed APIs. (default: metadata,graphql)"
|
|
|
|
)
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
parseRawConnInfo :: Parser RawConnInfo
|
|
|
|
parseRawConnInfo =
|
2018-12-14 06:21:41 +03:00
|
|
|
RawConnInfo <$> host <*> port <*> user <*> password
|
|
|
|
<*> dbUrl <*> dbName <*> pure Nothing
|
|
|
|
where
|
2018-12-19 14:38:33 +03:00
|
|
|
host = optional $
|
|
|
|
strOption ( long "host" <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<HOST>" <>
|
2018-12-19 14:38:33 +03:00
|
|
|
help "Postgres server host" )
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
port = optional $
|
|
|
|
option auto ( long "port" <>
|
2018-06-27 16:11:32 +03:00
|
|
|
short 'p' <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<PORT>" <>
|
2018-12-19 14:38:33 +03:00
|
|
|
help "Postgres server port" )
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
user = optional $
|
|
|
|
strOption ( long "user" <>
|
2018-06-27 16:11:32 +03:00
|
|
|
short 'u' <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<USER>" <>
|
2018-12-19 14:38:33 +03:00
|
|
|
help "Database user name" )
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
password =
|
2018-12-14 06:21:41 +03:00
|
|
|
strOption ( long "password" <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<PASSWORD>" <>
|
2018-06-27 16:11:32 +03:00
|
|
|
value "" <>
|
2018-12-19 14:38:33 +03:00
|
|
|
help "Password of the user"
|
|
|
|
)
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
dbUrl = optional $
|
|
|
|
strOption
|
|
|
|
( long "database-url" <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<DATABASE-URL>" <>
|
2018-12-19 14:38:33 +03:00
|
|
|
help (snd databaseUrlEnv)
|
|
|
|
)
|
2018-12-14 06:21:41 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
dbName = optional $
|
|
|
|
strOption ( long "dbname" <>
|
2018-06-27 16:11:32 +03:00
|
|
|
short 'd' <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<DBNAME>" <>
|
2018-12-19 14:38:33 +03:00
|
|
|
help "Database name to connect to"
|
|
|
|
)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
|
|
|
connInfoErrModifier :: String -> String
|
|
|
|
connInfoErrModifier s = "Fatal Error : " ++ s
|
|
|
|
|
2018-12-14 06:21:41 +03:00
|
|
|
mkConnInfo ::RawConnInfo -> Either String Q.ConnInfo
|
|
|
|
mkConnInfo (RawConnInfo mHost mPort mUser pass mURL mDB opts) =
|
|
|
|
case (mHost, mPort, mUser, mDB, mURL) of
|
2018-06-28 13:49:40 +03:00
|
|
|
|
2018-06-27 16:11:32 +03:00
|
|
|
(Just host, Just port, Just user, Just db, Nothing) ->
|
|
|
|
return $ Q.ConnInfo host port user pass db opts
|
2018-06-28 13:49:40 +03:00
|
|
|
|
|
|
|
(_, _, _, _, Just dbURL) -> maybe (throwError invalidUrlMsg)
|
|
|
|
return $ parseDatabaseUrl dbURL opts
|
|
|
|
_ -> throwError $ "Invalid options. "
|
|
|
|
++ "Expecting all database connection params "
|
|
|
|
++ "(host, port, user, dbname, password) or "
|
2018-07-06 08:13:46 +03:00
|
|
|
++ "database-url (HASURA_GRAPHQL_DATABASE_URL)"
|
2018-06-28 13:49:40 +03:00
|
|
|
where
|
2018-07-06 08:13:46 +03:00
|
|
|
invalidUrlMsg = "Invalid database-url (HASURA_GRAPHQL_DATABASE_URL). "
|
2018-06-28 13:49:40 +03:00
|
|
|
++ "Example postgres://foo:bar@example.com:2345/database"
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
parseTxIsolation :: Parser (Maybe Q.TxIsolation)
|
|
|
|
parseTxIsolation = optional $
|
|
|
|
option (eitherReader readIsoLevel)
|
|
|
|
( long "tx-iso" <>
|
|
|
|
short 'i' <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<TXISO>" <>
|
2018-12-19 14:38:33 +03:00
|
|
|
help (snd txIsoEnv)
|
|
|
|
)
|
|
|
|
|
|
|
|
parseConnParams :: Parser RawConnParams
|
|
|
|
parseConnParams =
|
2019-01-18 17:20:41 +03:00
|
|
|
RawConnParams <$> stripes <*> conns <*> timeout <*> allowPrepare
|
2018-12-14 06:21:41 +03:00
|
|
|
where
|
2018-12-19 14:38:33 +03:00
|
|
|
stripes = optional $
|
|
|
|
option auto
|
2018-12-14 06:21:41 +03:00
|
|
|
( long "stripes" <>
|
|
|
|
short 's' <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<NO OF STRIPES>" <>
|
2018-12-19 14:38:33 +03:00
|
|
|
help (snd pgStripesEnv)
|
2018-12-14 06:21:41 +03:00
|
|
|
)
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
conns = optional $
|
|
|
|
option auto
|
2018-12-14 06:21:41 +03:00
|
|
|
( long "connections" <>
|
2018-06-27 16:11:32 +03:00
|
|
|
short 'c' <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<NO OF CONNS>" <>
|
2018-12-19 14:38:33 +03:00
|
|
|
help (snd pgConnsEnv)
|
2018-12-14 06:21:41 +03:00
|
|
|
)
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
timeout = optional $
|
|
|
|
option auto
|
2018-12-14 06:21:41 +03:00
|
|
|
( long "timeout" <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<SECONDS>" <>
|
2018-12-19 14:38:33 +03:00
|
|
|
help (snd pgTimeoutEnv)
|
2018-11-15 07:55:39 +03:00
|
|
|
)
|
2019-01-18 17:20:41 +03:00
|
|
|
allowPrepare = optional $
|
|
|
|
option (eitherReader parseStrAsBool)
|
|
|
|
( long "use-prepared-statements" <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<true|false>" <>
|
2019-01-18 17:20:41 +03:00
|
|
|
help (snd pgUsePrepareEnv)
|
|
|
|
)
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
parseServerPort :: Parser (Maybe Int)
|
|
|
|
parseServerPort = optional $
|
|
|
|
option auto
|
2018-12-14 06:21:41 +03:00
|
|
|
( long "server-port" <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<PORT>" <>
|
2018-12-19 14:38:33 +03:00
|
|
|
help (snd servePortEnv)
|
2018-12-14 06:21:41 +03:00
|
|
|
)
|
2018-12-19 14:38:33 +03:00
|
|
|
|
2019-01-11 14:07:13 +03:00
|
|
|
parseServerHost :: Parser (Maybe HostPreference)
|
|
|
|
parseServerHost = optional $ strOption ( long "server-host" <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<HOST>" <>
|
2019-01-11 14:07:13 +03:00
|
|
|
help "Host on which graphql-engine will listen (default: *)"
|
|
|
|
)
|
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
parseAccessKey :: Parser (Maybe AdminSecret)
|
2018-12-19 14:38:33 +03:00
|
|
|
parseAccessKey =
|
2019-02-14 12:37:47 +03:00
|
|
|
optional $ AdminSecret <$>
|
2018-12-19 14:38:33 +03:00
|
|
|
strOption ( long "access-key" <>
|
2019-02-14 12:37:47 +03:00
|
|
|
metavar "ADMIN SECRET KEY (DEPRECATED: USE --admin-secret)" <>
|
|
|
|
help (snd adminSecretEnv)
|
|
|
|
)
|
|
|
|
|
|
|
|
parseAdminSecret :: Parser (Maybe AdminSecret)
|
|
|
|
parseAdminSecret =
|
|
|
|
optional $ AdminSecret <$>
|
|
|
|
strOption ( long "admin-secret" <>
|
|
|
|
metavar "ADMIN SECRET KEY" <>
|
|
|
|
help (snd adminSecretEnv)
|
2018-12-19 14:38:33 +03:00
|
|
|
)
|
add support for jwt authorization (close #186) (#255)
The API:
1. HGE has `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var. The value of which is a JSON.
2. The structure of this JSON is: `{"type": "<standard-JWT-algorithms>", "key": "<the-key>"}`
`type` : Standard JWT algos : `HS256`, `RS256`, `RS512` etc. (see jwt.io).
`key`:
i. Incase of symmetric key, the key as it is.
ii. Incase of asymmetric keys, only the public key, in a PEM encoded string or as a X509 certificate.
3. The claims in the JWT token must contain the following:
i. `x-hasura-default-role` field: default role of that user
ii. `x-hasura-allowed-roles` : A list of allowed roles for the user. The default role is overriden by `x-hasura-role` header.
4. The claims in the JWT token, can have other `x-hasura-*` fields where their values can only be strings.
5. The JWT tokens are sent as `Authorization: Bearer <token>` headers.
---
To test:
1. Generate a shared secret (for HMAC-SHA256) or RSA key pair.
2. Goto https://jwt.io/ , add the keys
3. Edit the claims to have `x-hasura-role` (mandatory) and other `x-hasura-*` fields. Add permissions related to the claims to test permissions.
4. Start HGE with `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var, which takes a JSON string: `{"type": "HS256", "key": "mylongsharedsecret"}` or `{"type":"RS256", "key": "<PEM-encoded-public-key>"}`
5. Copy the JWT token from jwt.io and use it in the `Authorization: Bearer <token>` header.
---
TODO: Support EC public keys. It is blocked on frasertweedale/hs-jose#61
2018-08-30 13:32:09 +03:00
|
|
|
|
2018-12-03 14:19:08 +03:00
|
|
|
readHookType :: String -> Either String AuthHookType
|
|
|
|
readHookType tyS =
|
|
|
|
case tyS of
|
|
|
|
"GET" -> Right AHTGet
|
|
|
|
"POST" -> Right AHTPost
|
|
|
|
_ -> Left "Only expecting GET / POST"
|
|
|
|
|
2019-02-28 16:53:03 +03:00
|
|
|
readAPIs :: String -> Either String [API]
|
|
|
|
readAPIs = mapM readAPI . T.splitOn "," . T.pack
|
|
|
|
where readAPI si = case T.toUpper $ T.strip si of
|
|
|
|
"METADATA" -> Right METADATA
|
|
|
|
"GRAPHQL" -> Right GRAPHQL
|
|
|
|
_ -> Left "Only expecting list of comma separated API types metadata / graphql"
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
parseWebHook :: Parser RawAuthHook
|
|
|
|
parseWebHook =
|
|
|
|
AuthHookG <$> url <*> urlType
|
2018-12-14 06:21:41 +03:00
|
|
|
where
|
2018-12-19 14:38:33 +03:00
|
|
|
url = optional $
|
|
|
|
strOption ( long "auth-hook" <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<WEB HOOK URL>" <>
|
2018-12-19 14:38:33 +03:00
|
|
|
help (snd authHookEnv)
|
|
|
|
)
|
|
|
|
urlType = optional $
|
|
|
|
option (eitherReader readHookType)
|
2018-12-14 06:21:41 +03:00
|
|
|
( long "auth-hook-mode" <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<GET|POST>" <>
|
2019-01-04 09:42:36 +03:00
|
|
|
help (snd authHookModeEnv)
|
2018-12-14 06:21:41 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
parseJwtSecret :: Parser (Maybe Text)
|
|
|
|
parseJwtSecret =
|
|
|
|
optional $ strOption
|
2018-12-14 06:21:41 +03:00
|
|
|
( long "jwt-secret" <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<JSON CONFIG>" <>
|
2018-12-19 14:38:33 +03:00
|
|
|
help (snd jwtSecretEnv)
|
2018-12-14 06:21:41 +03:00
|
|
|
)
|
add support for jwt authorization (close #186) (#255)
The API:
1. HGE has `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var. The value of which is a JSON.
2. The structure of this JSON is: `{"type": "<standard-JWT-algorithms>", "key": "<the-key>"}`
`type` : Standard JWT algos : `HS256`, `RS256`, `RS512` etc. (see jwt.io).
`key`:
i. Incase of symmetric key, the key as it is.
ii. Incase of asymmetric keys, only the public key, in a PEM encoded string or as a X509 certificate.
3. The claims in the JWT token must contain the following:
i. `x-hasura-default-role` field: default role of that user
ii. `x-hasura-allowed-roles` : A list of allowed roles for the user. The default role is overriden by `x-hasura-role` header.
4. The claims in the JWT token, can have other `x-hasura-*` fields where their values can only be strings.
5. The JWT tokens are sent as `Authorization: Bearer <token>` headers.
---
To test:
1. Generate a shared secret (for HMAC-SHA256) or RSA key pair.
2. Goto https://jwt.io/ , add the keys
3. Edit the claims to have `x-hasura-role` (mandatory) and other `x-hasura-*` fields. Add permissions related to the claims to test permissions.
4. Start HGE with `--jwt-secret` flag or `HASURA_GRAPHQL_JWT_SECRET` env var, which takes a JSON string: `{"type": "HS256", "key": "mylongsharedsecret"}` or `{"type":"RS256", "key": "<PEM-encoded-public-key>"}`
5. Copy the JWT token from jwt.io and use it in the `Authorization: Bearer <token>` header.
---
TODO: Support EC public keys. It is blocked on frasertweedale/hs-jose#61
2018-08-30 13:32:09 +03:00
|
|
|
|
|
|
|
jwtSecretHelp :: String
|
|
|
|
jwtSecretHelp = "The JSON containing type and the JWK used for verifying. e.g: "
|
2018-09-07 09:00:50 +03:00
|
|
|
<> "`{\"type\": \"HS256\", \"key\": \"<your-hmac-shared-secret>\", \"claims_namespace\": \"<optional-custom-claims-key-name>\"}`,"
|
|
|
|
<> "`{\"type\": \"RS256\", \"key\": \"<your-PEM-RSA-public-key>\", \"claims_namespace\": \"<optional-custom-claims-key-name>\"}`"
|
2018-06-27 16:11:32 +03:00
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
parseUnAuthRole :: Parser (Maybe RoleName)
|
|
|
|
parseUnAuthRole = optional $
|
|
|
|
RoleName <$> strOption ( long "unauthorized-role" <>
|
2019-02-14 08:58:38 +03:00
|
|
|
metavar "<ROLE>" <>
|
2018-12-19 14:38:33 +03:00
|
|
|
help (snd unAuthRoleEnv)
|
2018-12-14 06:21:41 +03:00
|
|
|
)
|
2018-07-06 08:13:46 +03:00
|
|
|
|
2019-02-14 08:58:38 +03:00
|
|
|
parseCorsConfig :: Parser (Maybe CorsConfig)
|
|
|
|
parseCorsConfig = mapCC <$> disableCors <*> corsDomain
|
2018-12-14 06:21:41 +03:00
|
|
|
where
|
2019-02-14 08:58:38 +03:00
|
|
|
corsDomain = optional $
|
|
|
|
option (eitherReader readCorsDomains)
|
|
|
|
( long "cors-domain" <>
|
|
|
|
metavar "<DOMAINS>" <>
|
|
|
|
help (snd corsDomainEnv)
|
|
|
|
)
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
disableCors =
|
2018-12-14 06:21:41 +03:00
|
|
|
switch ( long "disable-cors" <>
|
2019-02-14 08:58:38 +03:00
|
|
|
help "Disable CORS. Do not send any CORS headers on any request"
|
2018-06-27 16:11:32 +03:00
|
|
|
)
|
|
|
|
|
2019-02-14 08:58:38 +03:00
|
|
|
mapCC isDisabled domains =
|
|
|
|
bool domains (Just CCDisabled) isDisabled
|
|
|
|
|
2018-12-19 14:38:33 +03:00
|
|
|
parseEnableConsole :: Parser Bool
|
|
|
|
parseEnableConsole =
|
|
|
|
switch ( long "enable-console" <>
|
|
|
|
help (snd enableConsoleEnv)
|
|
|
|
)
|
2019-01-02 14:24:17 +03:00
|
|
|
|
2019-01-28 16:55:28 +03:00
|
|
|
parseEnableTelemetry :: Parser (Maybe Bool)
|
|
|
|
parseEnableTelemetry = optional $
|
|
|
|
option (eitherReader parseStrAsBool)
|
|
|
|
( long "enable-telemetry" <>
|
|
|
|
help (snd enableTelemetryEnv)
|
|
|
|
)
|
|
|
|
|
2019-03-01 14:45:04 +03:00
|
|
|
parseStringifyNum :: Parser Bool
|
|
|
|
parseStringifyNum =
|
|
|
|
switch ( long "stringify-numeric-types" <>
|
|
|
|
help (snd stringifyNumEnv)
|
|
|
|
)
|
|
|
|
|
2019-02-28 16:53:03 +03:00
|
|
|
parseEnabledAPIs :: Parser (Maybe [API])
|
|
|
|
parseEnabledAPIs = optional $
|
|
|
|
option (eitherReader readAPIs)
|
|
|
|
( long "enabled-apis" <>
|
|
|
|
help (snd enabledAPIsEnv)
|
|
|
|
)
|
|
|
|
|
2019-01-02 14:24:17 +03:00
|
|
|
-- Init logging related
|
|
|
|
connInfoToLog :: Q.ConnInfo -> StartupLog
|
|
|
|
connInfoToLog (Q.ConnInfo host port user _ db _) =
|
|
|
|
StartupLog L.LevelInfo "postgres_connection" infoVal
|
|
|
|
where
|
|
|
|
infoVal = J.object [ "host" J..= host
|
|
|
|
, "port" J..= port
|
|
|
|
, "user" J..= user
|
|
|
|
, "database" J..= db
|
|
|
|
]
|
|
|
|
|
|
|
|
serveOptsToLog :: ServeOptions -> StartupLog
|
|
|
|
serveOptsToLog so =
|
|
|
|
StartupLog L.LevelInfo "serve_options" infoVal
|
|
|
|
where
|
|
|
|
infoVal = J.object [ "port" J..= soPort so
|
2019-02-14 12:37:47 +03:00
|
|
|
, "admin_secret_set" J..= isJust (soAdminSecret so)
|
2019-01-02 14:24:17 +03:00
|
|
|
, "auth_hook" J..= (ahUrl <$> soAuthHook so)
|
|
|
|
, "auth_hook_mode" J..= (show . ahType <$> soAuthHook so)
|
|
|
|
, "unauth_role" J..= soUnAuthRole so
|
2019-02-14 08:58:38 +03:00
|
|
|
, "cors_config" J..= soCorsConfig so
|
2019-01-02 14:24:17 +03:00
|
|
|
, "enable_console" J..= soEnableConsole so
|
2019-01-28 16:55:28 +03:00
|
|
|
, "enable_telemetry" J..= soEnableTelemetry so
|
2019-01-18 17:20:41 +03:00
|
|
|
, "use_prepared_statements" J..= (Q.cpAllowPrepare . soConnParams) so
|
2019-03-01 14:45:04 +03:00
|
|
|
, "stringify_numeric_types" J..= soStringifyNum so
|
2019-01-02 14:24:17 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
mkGenericStrLog :: T.Text -> String -> StartupLog
|
|
|
|
mkGenericStrLog k msg =
|
|
|
|
StartupLog L.LevelInfo k $ J.toJSON msg
|