Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw dedicated error for attempting EC2 auth not on EC2 #983

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/amazonka/src/Amazonka/Auth.hs
Expand Up @@ -115,7 +115,7 @@ discover =
fromContainerEnv,
\env -> do
onEC2 <- isEC2 $ manager env
unless onEC2 $ throwM CredentialChainExhausted
unless onEC2 $ throwM NotOnEC2Instance
fromDefaultInstanceProfile env
]

Expand Down
10 changes: 10 additions & 0 deletions lib/amazonka/src/Amazonka/Auth/Exception.hs
Expand Up @@ -21,6 +21,7 @@ data AuthError
| MissingFileError FilePath
| InvalidFileError Text
| InvalidIAMError Text
| NotOnEC2Instance
| CredentialChainExhausted
deriving stock (Show, Generic)

Expand All @@ -33,6 +34,7 @@ instance ToLog AuthError where
MissingFileError f -> "[MissingFileError] { path = " <> build f <> "}"
InvalidFileError e -> "[InvalidFileError] { message = " <> build e <> "}"
InvalidIAMError e -> "[InvalidIAMError] { message = " <> build e <> "}"
NotOnEC2Instance -> "[NotOnEC2Instance]"
CredentialChainExhausted -> "[CredentialChainExhausted]"

class AsAuthError a where
Expand All @@ -57,11 +59,15 @@ class AsAuthError a where
-- | The specified IAM profile could not be found or deserialised.
_InvalidIAMError :: Prism' a Text

-- | Using an EC2 Instance profile was attempted not on an EC2 instance.
_NotOnEC2Instance :: Prism' a AuthError

_RetrievalError = _AuthError . _RetrievalError
_MissingEnvError = _AuthError . _MissingEnvError
_MissingFileError = _AuthError . _MissingFileError
_InvalidFileError = _AuthError . _InvalidFileError
_InvalidIAMError = _AuthError . _InvalidIAMError
_NotOnEC2Instance = _AuthError . _NotOnEC2Instance

instance AsAuthError SomeException where
_AuthError = exception
Expand All @@ -88,3 +94,7 @@ instance AsAuthError AuthError where
_InvalidIAMError = prism InvalidIAMError $ \case
InvalidIAMError e -> Right e
x -> Left x

_NotOnEC2Instance = prism (const NotOnEC2Instance) $ \case
e@NotOnEC2Instance -> Right e
x -> Left x