amazonka-2.0: Comprehensive Amazon Web Services SDK.
Copyright(c) 2013-2023 Brendan Hay
LicenseMozilla Public License, v. 2.0.
MaintainerBrendan Hay <brendan.g.hay+amazonka@gmail.com>
Stabilityprovisional
Portabilitynon-portable (GHC extensions)
Safe HaskellSafe-Inferred
LanguageHaskell2010

Amazonka.Auth

Description

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

Authentication

Retrieving Authentication

data Auth #

An authorisation environment containing AWS credentials, and potentially a reference which can be refreshed out-of-band as temporary credentials expire.

Instances

Instances details
ToLog Auth 
Instance details

Defined in Amazonka.Types

withAuth :: MonadIO m => Auth -> (AuthEnv -> m a) -> m a #

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, optionally AWS_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 the AWS_WEB_IDENTITY_TOKEN_FILE, AWS_ROLE_ARN, and optionally the AWS_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 have enableDnsSupport and enableDnsHostnames 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 or AWS_CONTAINER_CREDENTIALS_FULL_URI are set.

    See: https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/EC2ContainerCredentialsProviderWrapper.html

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.

fromKeys :: AccessKey -> SecretKey -> Env' withAuth -> Env Source #

Explicit access and secret keys.

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.

fromFilePath Source #

Arguments

:: 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

fromContainer Source #

Arguments

:: MonadIO m 
=> Text

Absolute URL

-> Env' withAuth 
-> m Env 

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.

fromAssumedRole Source #

Arguments

:: MonadIO m 
=> Text

Role ARN

-> Text

Role session name

-> Env 
-> m Env 

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.

fromWebIdentity Source #

Arguments

:: 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.

fromSSO Source #

Arguments

:: 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

newtype AccessKey #

An access key ID.

For example: AKIAIOSFODNN7EXAMPLE

See: Understanding and Getting Your Security Credentials.

Constructors

AccessKey ByteString 

Instances

Instances details
FromJSON AccessKey 
Instance details

Defined in Amazonka.Types

ToJSON AccessKey 
Instance details

Defined in Amazonka.Types

ToByteString AccessKey 
Instance details

Defined in Amazonka.Types

Methods

toBS :: AccessKey -> ByteString #

ToLog AccessKey 
Instance details

Defined in Amazonka.Types

ToQuery AccessKey 
Instance details

Defined in Amazonka.Types

FromText AccessKey 
Instance details

Defined in Amazonka.Types

ToText AccessKey 
Instance details

Defined in Amazonka.Types

Methods

toText :: AccessKey -> Text #

FromXML AccessKey 
Instance details

Defined in Amazonka.Types

ToXML AccessKey 
Instance details

Defined in Amazonka.Types

Methods

toXML :: AccessKey -> XML #

IsString AccessKey 
Instance details

Defined in Amazonka.Types

Generic AccessKey 
Instance details

Defined in Amazonka.Types

Associated Types

type Rep AccessKey :: Type -> Type #

Read AccessKey 
Instance details

Defined in Amazonka.Types

Show AccessKey 
Instance details

Defined in Amazonka.Types

NFData AccessKey 
Instance details

Defined in Amazonka.Types

Methods

rnf :: AccessKey -> () #

Eq AccessKey 
Instance details

Defined in Amazonka.Types

Hashable AccessKey 
Instance details

Defined in Amazonka.Types

type Rep AccessKey 
Instance details

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)))

newtype SecretKey #

Secret access key credential.

For example: wJalrXUtnFEMIK7MDENGbPxRfiCYEXAMPLEKE

See: Understanding and Getting Your Security Credentials.

Constructors

SecretKey ByteString 

Instances

Instances details
FromJSON SecretKey 
Instance details

Defined in Amazonka.Types

ToJSON SecretKey 
Instance details

Defined in Amazonka.Types

ToByteString SecretKey 
Instance details

Defined in Amazonka.Types

Methods

toBS :: SecretKey -> ByteString #

FromText SecretKey 
Instance details

Defined in Amazonka.Types

ToText SecretKey 
Instance details

Defined in Amazonka.Types

Methods

toText :: SecretKey -> Text #

FromXML SecretKey 
Instance details

Defined in Amazonka.Types

ToXML SecretKey 
Instance details

Defined in Amazonka.Types

Methods

toXML :: SecretKey -> XML #

IsString SecretKey 
Instance details

Defined in Amazonka.Types

Generic SecretKey 
Instance details

Defined in Amazonka.Types

Associated Types

type Rep SecretKey :: Type -> Type #

NFData SecretKey 
Instance details

Defined in Amazonka.Types

Methods

rnf :: SecretKey -> () #

Eq SecretKey 
Instance details

Defined in Amazonka.Types

Hashable SecretKey 
Instance details

Defined in Amazonka.Types

type Rep SecretKey 
Instance details

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.

See: Temporary Security Credentials.

Constructors

SessionToken ByteString 

Instances

Instances details
FromJSON SessionToken 
Instance details

Defined in Amazonka.Types

ToJSON SessionToken 
Instance details

Defined in Amazonka.Types

ToByteString SessionToken 
Instance details

Defined in Amazonka.Types

FromText SessionToken 
Instance details

Defined in Amazonka.Types

ToText SessionToken 
Instance details

Defined in Amazonka.Types

Methods

toText :: SessionToken -> Text #

FromXML SessionToken 
Instance details

Defined in Amazonka.Types

ToXML SessionToken 
Instance details

Defined in Amazonka.Types

Methods

toXML :: SessionToken -> XML #

IsString SessionToken 
Instance details

Defined in Amazonka.Types

Generic SessionToken 
Instance details

Defined in Amazonka.Types

Associated Types

type Rep SessionToken :: Type -> Type #

NFData SessionToken 
Instance details

Defined in Amazonka.Types

Methods

rnf :: SessionToken -> () #

Eq SessionToken 
Instance details

Defined in Amazonka.Types

Hashable SessionToken 
Instance details

Defined in Amazonka.Types

type Rep SessionToken 
Instance details

Defined in Amazonka.Types

type Rep SessionToken = D1 ('MetaData "SessionToken" "Amazonka.Types" "amazonka-core-2.0-BFuA7FRvuklLoYhn4b6A6p" 'True) (C1 ('MetaCons "SessionToken" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ByteString)))

Handling Errors

class AsAuthError a where Source #

Minimal complete definition

_AuthError

Methods

_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.

data AuthError Source #

An error thrown when attempting to read AuthN/AuthZ information.

Instances

Instances details
AsAuthError AuthError Source # 
Instance details

Defined in Amazonka.Auth.Exception

ToLog AuthError Source # 
Instance details

Defined in Amazonka.Auth.Exception

Exception AuthError Source # 
Instance details

Defined in Amazonka.Auth.Exception

Generic AuthError Source # 
Instance details

Defined in Amazonka.Auth.Exception

Associated Types

type Rep AuthError :: Type -> Type #

Show AuthError Source # 
Instance details

Defined in Amazonka.Auth.Exception

type Rep AuthError Source # 
Instance details

Defined in Amazonka.Auth.Exception

type Rep AuthError = D1 ('MetaData "AuthError" "Amazonka.Auth.Exception" "amazonka-2.0-48plDWnPMAk3PGO79vdSa0" 'False) ((C1 ('MetaCons "RetrievalError" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 HttpException)) :+: (C1 ('MetaCons "MissingEnvError" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Text)) :+: C1 ('MetaCons "MissingFileError" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 FilePath)))) :+: (C1 ('MetaCons "InvalidFileError" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Text)) :+: (C1 ('MetaCons "InvalidIAMError" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 Text)) :+: C1 ('MetaCons "CredentialChainExhausted" 'PrefixI 'False) (U1 :: Type -> Type))))

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.

data Env' withAuth Source #

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.

Constructors

Env 

Instances

Instances details
Generic (Env' withAuth) Source # 
Instance details

Defined in Amazonka.Env

Associated Types

type Rep (Env' withAuth) :: Type -> Type #

Methods

from :: Env' withAuth -> Rep (Env' withAuth) x #

to :: Rep (Env' withAuth) x -> Env' withAuth #

type Rep (Env' withAuth) Source # 
Instance details

Defined in Amazonka.Env