2021-11-04 19:08:33 +03:00
|
|
|
module Hasura.RQL.DDL.Network
|
|
|
|
( checkForHostnameInAllowlistObject,
|
|
|
|
dropHostFromAllowList,
|
|
|
|
runAddHostToTLSAllowlist,
|
|
|
|
runDropHostFromTLSAllowlist,
|
|
|
|
)
|
|
|
|
where
|
2021-08-24 10:36:32 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.Text (pack)
|
|
|
|
import Data.Text.Extended
|
|
|
|
import Hasura.Base.Error
|
|
|
|
import Hasura.EncJSON
|
|
|
|
import Hasura.Metadata.Class ()
|
|
|
|
import Hasura.Prelude
|
2022-04-27 16:57:28 +03:00
|
|
|
import Hasura.RQL.Types.Common
|
|
|
|
import Hasura.RQL.Types.Metadata
|
|
|
|
import Hasura.RQL.Types.Metadata.Object
|
|
|
|
import Hasura.RQL.Types.Network
|
|
|
|
import Hasura.RQL.Types.SchemaCache.Build
|
2021-09-24 01:56:37 +03:00
|
|
|
|
|
|
|
runAddHostToTLSAllowlist ::
|
|
|
|
(QErrM m, CacheRWM m, MetadataM m) =>
|
|
|
|
TlsAllow ->
|
|
|
|
m EncJSON
|
|
|
|
runAddHostToTLSAllowlist tlsAllowListEntry@TlsAllow {..} = do
|
2021-08-24 10:36:32 +03:00
|
|
|
networkMetadata <- _metaNetwork <$> getMetadata
|
|
|
|
|
|
|
|
when (null taHost) $ do
|
|
|
|
throw400 BadRequest $ "key \"host\" cannot be empty"
|
|
|
|
|
|
|
|
when (checkForHostInTLSAllowlist taHost (tlsList networkMetadata)) $ do
|
|
|
|
throw400 AlreadyExists $
|
|
|
|
"the host " <> dquote (pack taHost) <> " already exists in the allowlist"
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
withNewInconsistentObjsCheck $
|
|
|
|
buildSchemaCacheFor (MOHostTlsAllowlist taHost) $
|
|
|
|
addHostToTLSAllowList tlsAllowListEntry
|
2021-08-24 10:36:32 +03:00
|
|
|
|
|
|
|
pure successMsg
|
|
|
|
where
|
|
|
|
tlsList nm = networkTlsAllowlist nm
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
runDropHostFromTLSAllowlist ::
|
|
|
|
(QErrM m, CacheRWM m, MetadataM m) =>
|
|
|
|
DropHostFromTLSAllowlist ->
|
|
|
|
m EncJSON
|
2021-08-24 10:36:32 +03:00
|
|
|
runDropHostFromTLSAllowlist (DropHostFromTLSAllowlist hostname) = do
|
|
|
|
networkMetadata <- _metaNetwork <$> getMetadata
|
|
|
|
|
|
|
|
when (null hostname) $ do
|
|
|
|
throw400 BadRequest $ "hostname cannot be empty"
|
|
|
|
|
|
|
|
unless (checkForHostInTLSAllowlist hostname (networkTlsAllowlist networkMetadata)) $ do
|
|
|
|
throw400 NotExists $
|
|
|
|
"the host " <> dquote (pack hostname) <> " isn't present in the allowlist"
|
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
withNewInconsistentObjsCheck $
|
|
|
|
buildSchemaCache $
|
|
|
|
dropHostFromAllowList hostname
|
2021-08-24 10:36:32 +03:00
|
|
|
|
|
|
|
pure successMsg
|
|
|
|
|
|
|
|
addHostToTLSAllowList :: TlsAllow -> MetadataModifier
|
|
|
|
addHostToTLSAllowList tlsaObj = MetadataModifier $ \m ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m {_metaNetwork = Network $ (tlsList m) ++ [tlsaObj]}
|
2021-08-24 10:36:32 +03:00
|
|
|
where
|
|
|
|
tlsList md = networkTlsAllowlist (_metaNetwork md)
|
|
|
|
|
|
|
|
dropHostFromAllowList :: String -> MetadataModifier
|
|
|
|
dropHostFromAllowList host = MetadataModifier $ \m ->
|
2021-09-24 01:56:37 +03:00
|
|
|
m {_metaNetwork = Network $ filteredList m}
|
2021-08-24 10:36:32 +03:00
|
|
|
where
|
|
|
|
tlsList md = networkTlsAllowlist (_metaNetwork md)
|
|
|
|
|
|
|
|
filteredList md = filter (not . checkForHostnameInAllowlistObject host) (tlsList md)
|
|
|
|
|
|
|
|
checkForHostnameInAllowlistObject :: String -> TlsAllow -> Bool
|
|
|
|
checkForHostnameInAllowlistObject host tlsa = host == (taHost tlsa)
|
|
|
|
|
|
|
|
checkForHostInTLSAllowlist :: String -> [TlsAllow] -> Bool
|
|
|
|
checkForHostInTLSAllowlist host tlsAllowList =
|
|
|
|
any (checkForHostnameInAllowlistObject host) tlsAllowList
|