Copyright | (c) 2013-2023 Brendan Hay |
---|---|
License | Mozilla Public License, v. 2.0. |
Maintainer | Brendan Hay <brendan.g.hay+amazonka@gmail.com> |
Stability | provisional |
Portability | non-portable (GHC extensions) |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Explicitly specify your Amazon AWS security credentials, or retrieve them from the underlying OS.
The format of environment variables and the credentials file follows the official AWS SDK guidelines.
Synopsis
- data Auth
- withAuth :: MonadIO m => Auth -> (AuthEnv -> m a) -> m a
- discover :: (MonadCatch m, MonadIO m, Foldable withAuth) => Env' withAuth -> m Env
- runCredentialChain :: MonadCatch m => [a -> m b] -> a -> m b
- fromKeys :: AccessKey -> SecretKey -> Env' withAuth -> Env
- fromSession :: AccessKey -> SecretKey -> SessionToken -> Env' withAuth -> Env
- fromTemporarySession :: AccessKey -> SecretKey -> SessionToken -> UTCTime -> Env' withAuth -> Env
- fromKeysEnv :: MonadIO m => Env' withAuth -> m Env
- fromFilePath :: forall m withAuth. (MonadIO m, Foldable withAuth) => Text -> FilePath -> FilePath -> Env' withAuth -> m Env
- fromFileEnv :: (MonadIO m, Foldable withAuth) => Env' withAuth -> m Env
- fromContainer :: MonadIO m => Text -> Env' withAuth -> m Env
- fromContainerEnv :: MonadIO m => Env' withAuth -> m Env
- fromAssumedRole :: MonadIO m => Text -> Text -> Env -> m Env
- fromWebIdentity :: MonadIO m => FilePath -> Text -> Maybe Text -> Env' withAuth -> m Env
- fromWebIdentityEnv :: MonadIO m => Env' withAuth -> m Env
- fromDefaultInstanceProfile :: MonadIO m => Env' withAuth -> m Env
- fromNamedInstanceProfile :: MonadIO m => Text -> Env' withAuth -> m Env
- fromSSO :: forall m withAuth. MonadIO m => FilePath -> Region -> Text -> Text -> Env' withAuth -> m Env
- newtype AccessKey = AccessKey ByteString
- newtype SecretKey = SecretKey ByteString
- newtype SessionToken = SessionToken ByteString
- class AsAuthError a where
- data AuthError
- type Env = Env' Identity
- type EnvNoAuth = Env' Proxy
- data Env' withAuth = Env {}
Authentication
Retrieving Authentication
An authorisation environment containing AWS credentials, and potentially a reference which can be refreshed out-of-band as temporary credentials expire.
Instances
ToLog Auth | |
Defined in Amazonka.Types build :: Auth -> ByteStringBuilder # |
Automatically Fetching Credentials
discover :: (MonadCatch m, MonadIO m, Foldable withAuth) => Env' withAuth -> m Env Source #
Attempt to fetch credentials in a way similar to the official AWS SDKs. The C++ SDK lists the following sequence:
- Check environment variables for keys provided directly
(
AWS_ACCESS_KEY_ID
,AWS_SECRET_ACCESS_KEY
, optionallyAWS_SESSION_TOKEN
) - Check credentials/config files for authentication information,
respecting the
AWS_PROFILE
environment variable. - Exchange a Web Identity for AWS Credentials using
sts:AssumeRoleWithWebIdentity
, respecting theAWS_WEB_IDENTITY_TOKEN_FILE
,AWS_ROLE_ARN
, and optionally theAWS_ROLE_SESSION_NAME
environment variables. - Retrieve credentials from the ECS Container Agent if the
AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
environment variable is set. If we think we're running on EC2, retrieve the first available IAM profile from the instance identity document, and use this to set the
Placement
. We attempt to resolve http://instance-data rather than directly retrieving http://169.254.169.254 for IAM profile information. This ensures that the DNS lookup terminates promptly if not running on EC2, but means that your VPC must haveenableDnsSupport
andenableDnsHostnames
set.NOTE: This is not 100% consistent with the AWS SDKs, which does not attempt to query the ECS service if either
AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
orAWS_CONTAINER_CREDENTIALS_FULL_URI
are set.
Credential Providers
runCredentialChain :: MonadCatch m => [a -> m b] -> a -> m b Source #
Compose a list of credential-providing functions by testing each
until one returns successfully. If they throw AuthError
, the next
function in the chain will be tried. Throws
CredentialChainExhausted
if the list is exhausted.
fromSession :: AccessKey -> SecretKey -> SessionToken -> Env' withAuth -> Env Source #
Temporary credentials from a STS session consisting of the access key, secret key, and session token.
See: fromTemporarySession
fromTemporarySession :: AccessKey -> SecretKey -> SessionToken -> UTCTime -> Env' withAuth -> Env Source #
Temporary credentials from a STS session consisting of the access key, secret key, session token, and expiration time.
See: fromSession
fromKeysEnv :: MonadIO m => Env' withAuth -> m Env Source #
Retrieve access key, secret key and a session token from environment variables. We copy the behaviour of the SDKs and respect the following variables:
AWS_ACCESS_KEY_ID
(and its alternate name,AWS_ACCESS_KEY
)AWS_SECRET_ACCESS_KEY
(and its alternate name,AWS_SECRET_KEY
)AWS_SESSION_TOKEN
(if present)
Throws MissingEnvError
if a required environment variable is
empty or unset.
:: forall m withAuth. (MonadIO m, Foldable withAuth) | |
=> Text | Profile name |
-> FilePath | Credentials file |
-> FilePath | Config file |
-> Env' withAuth | |
-> m Env |
Retrieve credentials from the AWS config/credentials files, as Amazonka currently understands them:
- AWS recommends credentials do not live in the config file, but allows it.
- Sections in the config file start should either be named
[default]
or[profile foo]
. Unprefixed[foo]
currently "happens to work" but is not officially supported, to match the observed behaviour of the AWS SDK/CLI. - Sections in the credentials file are always unprefixed -
[default]
or[foo]
.
See: the ConfigProfile
type, to understand the methods Amazonka
currently supports.
fromFileEnv :: (MonadIO m, Foldable withAuth) => Env' withAuth -> m Env Source #
Loads the default config/credentials INI files and selects a
profile by environment variable (AWS_PROFILE
).
Throws MissingFileError
if credFile
is missing, or InvalidFileError
if an error occurs during parsing.
This looks in in the HOME
directory as determined by the
directory library.
- Not Windows:
$HOME/.aws/credentials
- Windows:
%USERPROFILE%\.aws\credentials
Obtain credentials exposed to a task via the ECS container agent, as described in the IAM Roles for Tasks section of the AWS ECS documentation. The credentials are obtained by making a request to the given URL.
The ECS container agent provides an access key, secret key, session token, and expiration time. As these are temporary credentials, this function also starts a refresh thread that will periodically fetch fresh credentials before the current ones expire.
fromContainerEnv :: MonadIO m => Env' withAuth -> m Env Source #
Obtain credentials from the ECS container agent, by querying
http://169.254.170.2 at the path contained by the
AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
environment variable.
Throws MissingEnvError
if the AWS_CONTAINER_CREDENTIALS_RELATIVE_URI
environment variable is not set or InvalidIAMError
if the payload returned
by the ECS container agent is not of the expected format.
NOTE: We do not currently respect the
AWS_CONTAINER_CREDENTIALS_FULL_URI
or AWS_CONTAINTER_AUTHORIZATION_TOKEN
environment variable. If you need support for these, please file a PR.
Assume a role using the sts:AssumeRole
API.
This is a simplified interface suitable for most purposes, but if
you need the full functionality of the sts:AssumeRole
API, you
will need to craft your own requests using amazonka-sts
. If you
do this, remember to use fetchAuthInBackground
so that your
application does not get stuck holding temporary credentials which
have expired.
:: MonadIO m | |
=> FilePath | Path to token file |
-> Text | Role ARN |
-> Maybe Text | Role Session Name |
-> Env' withAuth | |
-> m Env |
https://aws.amazon.com/blogs/opensource/introducing-fine-grained-iam-roles-service-accounts/
Obtain temporary credentials from sts:AssumeRoleWithWebIdentity
.
The STS service provides an access key, secret key, session token, and expiration time. Also spawns a refresh thread that will periodically fetch fresh credentials before the current ones expire.
The implementation is modelled on the C++ SDK: https://github.com/aws/aws-sdk-cpp/blob/6d6dcdbfa377393306bf79585f61baea524ac124/aws-cpp-sdk-core/source/auth/STSCredentialsProvider.cpp#L33
fromWebIdentityEnv :: MonadIO m => Env' withAuth -> m Env Source #
Obtain temporary credentials from
sts:AssumeRoleWithWebIdentity
, sourcing arguments from standard
environment variables:
AWS_WEB_IDENTITY_TOKEN_FILE
AWS_ROLE_ARN
AWS_ROLE_SESSION_NAME
(optional)
Throws MissingEnvError
if a required environment variable is
empty or unset.
fromDefaultInstanceProfile :: MonadIO m => Env' withAuth -> m Env Source #
Retrieve the default IAM Profile from the local EC2 instance-data.
The default IAM profile is determined by Amazon as the first profile found
in the response from:
http://169.254.169.254/latest/meta-data/iam/security-credentials/
Throws RetrievalError
if the HTTP call fails, or InvalidIAMError
if
the default IAM profile cannot be read.
fromNamedInstanceProfile :: MonadIO m => Text -> Env' withAuth -> m Env Source #
Lookup a specific IAM Profile by name from the local EC2 instance-data.
Additionally starts a refresh thread for the given authentication environment.
The resulting IORef
wrapper + timer is designed so that multiple concurrent
accesses of AuthEnv
from the AWS
environment are not required to calculate
expiry and sequentially queue to update it.
The forked timer ensures a singular owner and pre-emptive refresh of the temporary session credentials before expiration.
A weak reference is used to ensure that the forked thread will eventually
terminate when Auth
is no longer referenced.
If no session token or expiration time is present the credentials will be returned verbatim.
:: forall m withAuth. MonadIO m | |
=> FilePath | |
-> Region | |
-> Text | Account ID |
-> Text | Role Name |
-> Env' withAuth | |
-> m Env |
Assume a role using an SSO Token.
The user must have previously called aws sso login
, and pass in the path to
the cached token file, along with SSO region, account ID and role name.
(fromFilePath
understands the sso_
variables
used by the official AWS CLI and will call fromSSO
for you.) This function
uses fetchAuthInBackground
to refresh the credentials as long as the token
in the sso/cache
file is not expired. When it has, the user will need to
aws sso login
again.
https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-sso.html
Keys
An access key ID.
For example: AKIAIOSFODNN7EXAMPLE
Instances
FromJSON AccessKey | |
ToJSON AccessKey | |
Defined in Amazonka.Types | |
ToByteString AccessKey | |
Defined in Amazonka.Types toBS :: AccessKey -> ByteString # | |
ToLog AccessKey | |
Defined in Amazonka.Types build :: AccessKey -> ByteStringBuilder # | |
ToQuery AccessKey | |
Defined in Amazonka.Types toQuery :: AccessKey -> QueryString # | |
FromText AccessKey | |
ToText AccessKey | |
Defined in Amazonka.Types | |
FromXML AccessKey | |
ToXML AccessKey | |
Defined in Amazonka.Types | |
IsString AccessKey | |
Defined in Amazonka.Types fromString :: String -> AccessKey # | |
Generic AccessKey | |
Read AccessKey | |
Show AccessKey | |
NFData AccessKey | |
Defined in Amazonka.Types | |
Eq AccessKey | |
Hashable AccessKey | |
Defined in Amazonka.Types | |
type Rep AccessKey | |
Defined in Amazonka.Types type Rep AccessKey = D1 ('MetaData "AccessKey" "Amazonka.Types" "amazonka-core-2.0-BFuA7FRvuklLoYhn4b6A6p" 'True) (C1 ('MetaCons "AccessKey" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ByteString))) |
Secret access key credential.
For example: wJalrXUtnFEMIK7MDENGbPxRfiCYEXAMPLEKE
Instances
FromJSON SecretKey | |
ToJSON SecretKey | |
Defined in Amazonka.Types | |
ToByteString SecretKey | |
Defined in Amazonka.Types toBS :: SecretKey -> ByteString # | |
FromText SecretKey | |
ToText SecretKey | |
Defined in Amazonka.Types | |
FromXML SecretKey | |
ToXML SecretKey | |
Defined in Amazonka.Types | |
IsString SecretKey | |
Defined in Amazonka.Types fromString :: String -> SecretKey # | |
Generic SecretKey | |
NFData SecretKey | |
Defined in Amazonka.Types | |
Eq SecretKey | |
Hashable SecretKey | |
Defined in Amazonka.Types | |
type Rep SecretKey | |
Defined in Amazonka.Types type Rep SecretKey = D1 ('MetaData "SecretKey" "Amazonka.Types" "amazonka-core-2.0-BFuA7FRvuklLoYhn4b6A6p" 'True) (C1 ('MetaCons "SecretKey" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ByteString))) |
newtype SessionToken #
A session token used by STS to temporarily authorise access to an AWS resource.
Instances
Handling Errors
class AsAuthError a where Source #
_AuthError :: Prism' a AuthError Source #
A general authentication error.
_RetrievalError :: Prism' a HttpException Source #
An error occured while communicating over HTTP with the local metadata endpoint.
_MissingEnvError :: Prism' a Text Source #
The named environment variable was not found.
_MissingFileError :: Prism' a FilePath Source #
The specified credentials file could not be found.
_InvalidFileError :: Prism' a Text Source #
An error occured parsing the credentials file.
_InvalidIAMError :: Prism' a Text Source #
The specified IAM profile could not be found or deserialised.
Instances
AsAuthError AuthError Source # | |
AsAuthError SomeException Source # | |
Defined in Amazonka.Auth.Exception |
An error thrown when attempting to read AuthN/AuthZ information.
RetrievalError HttpException | |
MissingEnvError Text | |
MissingFileError FilePath | |
InvalidFileError Text | |
InvalidIAMError Text | |
CredentialChainExhausted |
Instances
Env'
type Env = Env' Identity Source #
An environment with auth credentials. Most AWS requests need one
of these, and you can create one with newEnv
.
type EnvNoAuth = Env' Proxy Source #
An environment with no auth credentials. Used for certain
requests which need to be unsigned, like
sts:AssumeRoleWithWebIdentity
, and you can create one with
newEnvNoAuth
if you need it.
The environment containing the parameters required to make AWS requests.
This type tracks whether or not we have credentials at the type level, to avoid "presigning" requests when we lack auth information.