module Ecluse.Runtime.Aws.Env.Internal (
AwsEndpoint (..),
newAwsEnv,
singleAttemptEnv,
newAwsEnvWith,
) where
import Amazonka qualified as AWS
import Amazonka.Data.Headers (hAMZToken)
import Amazonka.Env.Hooks qualified as Hooks
import Network.HTTP.Client qualified as HTTP
import Network.HTTP.Types (HeaderName)
import Ecluse.Core.Fault.Http (typedTlsManagerSettings)
import Ecluse.Runtime.Aws.Fault (retypeAwsFailure)
singleAttemptEnv :: AWS.Env -> AWS.Env
singleAttemptEnv :: Env -> Env
singleAttemptEnv Env
env = Env -> Env
forall (withAuth :: * -> *). Env' withAuth -> Env' withAuth
AWS.once Env
env{AWS.retryCheck = \Int
_ HttpException
_ -> Bool
False}
data AwsEndpoint = AwsEndpoint
{ AwsEndpoint -> Bool
endpointSecure :: Bool
, AwsEndpoint -> Text
endpointHost :: Text
, AwsEndpoint -> Int
endpointPort :: Int
}
deriving stock (AwsEndpoint -> AwsEndpoint -> Bool
(AwsEndpoint -> AwsEndpoint -> Bool)
-> (AwsEndpoint -> AwsEndpoint -> Bool) -> Eq AwsEndpoint
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AwsEndpoint -> AwsEndpoint -> Bool
== :: AwsEndpoint -> AwsEndpoint -> Bool
$c/= :: AwsEndpoint -> AwsEndpoint -> Bool
/= :: AwsEndpoint -> AwsEndpoint -> Bool
Eq, Int -> AwsEndpoint -> ShowS
[AwsEndpoint] -> ShowS
AwsEndpoint -> String
(Int -> AwsEndpoint -> ShowS)
-> (AwsEndpoint -> String)
-> ([AwsEndpoint] -> ShowS)
-> Show AwsEndpoint
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AwsEndpoint -> ShowS
showsPrec :: Int -> AwsEndpoint -> ShowS
$cshow :: AwsEndpoint -> String
show :: AwsEndpoint -> String
$cshowList :: [AwsEndpoint] -> ShowS
showList :: [AwsEndpoint] -> ShowS
Show)
newAwsEnv :: Maybe Text -> Maybe AwsEndpoint -> AWS.Service -> IO AWS.Env
newAwsEnv :: Maybe Text -> Maybe AwsEndpoint -> Service -> IO Env
newAwsEnv = (EnvNoAuth -> IO Env)
-> Maybe Text -> Maybe AwsEndpoint -> Service -> IO Env
newAwsEnvWith EnvNoAuth -> IO Env
forall (m :: * -> *) (withAuth :: * -> *).
(MonadCatch m, MonadIO m, Foldable withAuth) =>
Env' withAuth -> m Env
AWS.discover
newAwsEnvWith :: (AWS.EnvNoAuth -> IO AWS.Env) -> Maybe Text -> Maybe AwsEndpoint -> AWS.Service -> IO AWS.Env
newAwsEnvWith :: (EnvNoAuth -> IO Env)
-> Maybe Text -> Maybe AwsEndpoint -> Service -> IO Env
newAwsEnvWith EnvNoAuth -> IO Env
discovery Maybe Text
mRegion Maybe AwsEndpoint
mEndpoint Service
service = do
manager <- ManagerSettings -> IO Manager
HTTP.newManager ManagerSettings
typedTlsManagerSettings
base <- retypeAwsFailure (AWS.newEnvNoAuthFromManager manager >>= discovery . redactingCredentialHeaders)
pure (overridden (scoped base))
where
scoped :: Env' withAuth -> Env' withAuth
scoped Env' withAuth
env = Env' withAuth
-> (Text -> Env' withAuth) -> Maybe Text -> Env' withAuth
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Env' withAuth
env (\Text
region -> Env' withAuth
env{AWS.region = AWS.Region' region}) Maybe Text
mRegion
overridden :: Env' withAuth -> Env' withAuth
overridden Env' withAuth
env = Env' withAuth
-> (AwsEndpoint -> Env' withAuth)
-> Maybe AwsEndpoint
-> Env' withAuth
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Env' withAuth
env (\AwsEndpoint
endpoint -> Service -> Env' withAuth -> Env' withAuth
forall (withAuth :: * -> *).
Service -> Env' withAuth -> Env' withAuth
AWS.configureService (AwsEndpoint -> Service
pointedAt AwsEndpoint
endpoint) Env' withAuth
env) Maybe AwsEndpoint
mEndpoint
pointedAt :: AwsEndpoint -> Service
pointedAt AwsEndpoint
endpoint =
Bool -> ByteString -> Int -> Service -> Service
AWS.setEndpoint
(AwsEndpoint -> Bool
endpointSecure AwsEndpoint
endpoint)
(Text -> ByteString
forall a b. ConvertUtf8 a b => a -> b
encodeUtf8 (AwsEndpoint -> Text
endpointHost AwsEndpoint
endpoint))
(AwsEndpoint -> Int
endpointPort AwsEndpoint
endpoint)
Service
service
redactingCredentialHeaders :: AWS.Env' withAuth -> AWS.Env' withAuth
Env' withAuth
env =
Env' withAuth
env{AWS.hooks = Hooks.clientRequestHook (\Hook ClientRequest
previous Env' withAuth
current ClientRequest
request -> ClientRequest -> ClientRequest
redacted (ClientRequest -> ClientRequest)
-> IO ClientRequest -> IO ClientRequest
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Env' withAuth -> ClientRequest -> IO ClientRequest
Hook ClientRequest
previous Env' withAuth
current ClientRequest
request) (AWS.hooks env)}
where
redacted :: ClientRequest -> ClientRequest
redacted ClientRequest
request = ClientRequest
request{HTTP.redactHeaders = HTTP.redactHeaders request <> credentialHeaders}
credentialHeaders :: Set HeaderName
= [Item (Set HeaderName)] -> Set HeaderName
forall l. IsList l => [Item l] -> l
fromList [Item (Set HeaderName)
HeaderName
hAMZToken, Item (Set HeaderName)
HeaderName
"x-amz-sso_bearer_token"]