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

Credential chain #746

Merged
merged 34 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
bc70c40
amazonka: Extract Auth exceptions into separate module
endgame Jan 7, 2022
6ce5bea
amazonka: Extract module of auth functions for explicit access keys
endgame Jan 7, 2022
dec631c
amazonka: Split instance profile auth into its own module
endgame Jan 7, 2022
c7480c6
amazonka: standardise form of instance profile functions
endgame Jan 7, 2022
9033cd3
amazonka: standardise form of access key functions
endgame Jan 7, 2022
c63b5de
amazonka: Extract fromWebIdentity into its own module
endgame Jan 7, 2022
bd02a61
amazonka: add convenience function to assume roles
endgame Jan 10, 2022
3e40b25
amazonka: Remove Maybe from runCredentialChain
endgame Jan 10, 2022
232875b
amazonka: Support (most) config file auth methods
endgame Jan 10, 2022
189c85e
amazonka: Move ECS Container auth into own module
endgame Jan 10, 2022
ac9145d
amazonka: all auth functions return Region now, remove Maybe
endgame Jan 10, 2022
f5e804c
amazonka: remove Credentials type
endgame Jan 10, 2022
4330d16
Amazonka.Auth: export all credential providers from Amazonka.Auth.*
endgame Jan 10, 2022
6cce278
amazonka: credential providers update Env instead of returning tuple
endgame Jan 10, 2022
1d99cf3
amazonka: return Env to its correct home; break module import cycles
endgame Jan 10, 2022
4c6e930
Amazonka.Auth.Exception: Remove unused exception constructor
endgame Jan 10, 2022
f5bb68e
Amazonka.Auth.ConfigFile: Implement credential_source actions
endgame Jan 10, 2022
58aa27a
Amazonka.Auth.ConfigFile: respect AWS_REGION after evaluating config
endgame Jan 10, 2022
c60e819
Doc fixes and CHANGELOG.md entry
endgame Jan 10, 2022
f90c291
CHANGELOG.md: clarify removed `Credentials(FromEnv)`
endgame Jan 11, 2022
5acbc71
Amazonka.Auth.ConfigFile: rename helper for clarity
endgame Jan 11, 2022
3ce1ac5
Amazonka.Auth.ConfigFile: on Windows, read from %USERPROFILE%\.aws
endgame Jan 14, 2022
0d40dd6
Amazonka.Auth.InstanceProfile: favour `mconcat` over `(<>)`
endgame Jan 14, 2022
15bf229
Amazonka.Auth.Container: take and return `Text`, as per convention
endgame Jan 14, 2022
5d15fe1
Amazonka.Auth.Background: Rewrite computation for clarity
endgame Jan 14, 2022
caa1e60
Amazonka.Auth,CHANGELOG: apply PR feedback to docs and haddocks
endgame Jan 14, 2022
3cb488d
Amazonka.Env: Fix Env always being created in us-east-1
endgame Jan 18, 2022
368a464
Fix bazel build environment
endgame Feb 3, 2022
df06185
Amazonka.Env: Remove underscore prefix from Env record fields
endgame Feb 3, 2022
0ddc6d8
Amazonka.Auth: export fromAssumedRole
endgame Feb 3, 2022
192c67f
Amazonka.Auth.ConfigFile: Unprefixed profile names are unsupported
endgame Feb 3, 2022
6d021f0
Amazonka.Auth.ConfigFile: handle missing configFile
endgame Feb 3, 2022
72c45fb
Amazonka.Auth.Background: Use {-# LANGUAGE Strict #-}
endgame Feb 3, 2022
57933fb
Amazonka.Auth.ConfigFile: detect profile loops
endgame Feb 3, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ stack_snapshot(
"unliftio", # keep
"unordered-containers", # keep
"uuid",
"vector",
"xml-conduit",
"xml-types",
"yaml",
Expand Down
2 changes: 1 addition & 1 deletion examples/src/APIGateway.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import System.IO
main :: Region -> IO Method
main r = do
lgr <- newLogger Trace stdout
env <- newEnv Discover <&> set envLogger lgr . set envRegion r
env <- newEnv discover <&> set envLogger lgr . set envRegion r
runResourceT $ do
restApi <- send env (newCreateRestApi "myApi")
let Just apiId = restApi ^. field @"id"
Expand Down
4 changes: 2 additions & 2 deletions examples/src/DynamoDB.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ printTables region secure host port = do
let dynamo = setEndpoint secure host port DynamoDB.defaultService

lgr <- newLogger Debug stdout
env <- newEnv Discover <&> set envLogger lgr . configure dynamo . within region
env <- newEnv discover <&> set envLogger lgr . configure dynamo . within region

runResourceT $ do
say $ "Listing all tables in region " <> toText region
Expand Down Expand Up @@ -65,7 +65,7 @@ insertItem region secure host port table item = do
let dynamo = setEndpoint secure host port DynamoDB.defaultService

lgr <- newLogger Debug stdout
env <- newEnv Discover <&> set envLogger lgr . within region . configure dynamo
env <- newEnv discover <&> set envLogger lgr . within region . configure dynamo

runResourceT $ do
say $
Expand Down
2 changes: 1 addition & 1 deletion examples/src/EC2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import System.IO
instanceOverview :: Region -> IO ()
instanceOverview r = do
lgr <- newLogger Info stdout
env <- newEnv Discover <&> set envLogger lgr . within r
env <- newEnv discover <&> set envLogger lgr . within r

let pp x =
mconcat
Expand Down
2 changes: 1 addition & 1 deletion examples/src/ExceptionSemantics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exceptions ::
IO ()
exceptions r n = do
lgr <- newLogger Info stdout
env <- newEnv Discover <&> set envLogger lgr . within r
env <- newEnv discover <&> set envLogger lgr . within r

let scan = newScan n & field @"attributesToGet" ?~ "foo" :| []

Expand Down
10 changes: 5 additions & 5 deletions examples/src/S3.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ getPresignedURL ::
IO ByteString
getPresignedURL r b k = do
lgr <- newLogger Trace stdout
env <- newEnv Discover <&> set envLogger lgr . set envRegion r
env <- newEnv discover <&> set envLogger lgr . set envRegion r
ts <- getCurrentTime
runResourceT $ presignURL env ts 60 (newGetObject b k)

Expand All @@ -40,7 +40,7 @@ listAll ::
IO ()
listAll r = do
lgr <- newLogger Debug stdout
env <- newEnv Discover <&> set envLogger lgr . set envRegion r
env <- newEnv discover <&> set envLogger lgr . set envRegion r

let val :: ToText a => Maybe a -> Text
val = maybe "Nothing" toText
Expand Down Expand Up @@ -71,7 +71,7 @@ getFile ::
IO ()
getFile r b k f = do
lgr <- newLogger Debug stdout
env <- newEnv Discover <&> set envLogger lgr . set envRegion r
env <- newEnv discover <&> set envLogger lgr . set envRegion r

runResourceT $ do
rs <- send env (newGetObject b k)
Expand All @@ -98,7 +98,7 @@ putChunkedFile ::
IO ()
putChunkedFile r b k c f = do
lgr <- newLogger Debug stdout
env <- newEnv Discover <&> set #_envLogger lgr . set #_envRegion r
env <- newEnv discover <&> set #_envLogger lgr . set #_envRegion r

runResourceT $ do
bdy <- chunkedFile c f
Expand All @@ -121,7 +121,7 @@ tagBucket ::
IO ()
tagBucket r bkt xs = do
lgr <- newLogger Debug stdout
env <- newEnv Discover <&> set envLogger lgr . set envRegion r
env <- newEnv discover <&> set envLogger lgr . set envRegion r

let tags = map (uncurry newTag) xs
kv t = toText (t ^. #key) <> "=" <> (t ^. #value)
Expand Down
2 changes: 1 addition & 1 deletion examples/src/SQS.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ roundTrip ::
IO ()
roundTrip r name xs = do
lgr <- newLogger Debug stdout
env <- newEnv Discover <&> set envLogger lgr . within r
env <- newEnv discover <&> set envLogger lgr . within r

let say = liftIO . Text.putStrLn

Expand Down
9 changes: 9 additions & 0 deletions lib/amazonka/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ haskell_library(
"src/Amazonka.hs",
"src/Amazonka/Auth.hs",
"src/Amazonka/Auth.hs-boot", # keep
"src/Amazonka/Auth/Background.hs",
"src/Amazonka/Auth/ConfigFile.hs",
"src/Amazonka/Auth/Container.hs",
"src/Amazonka/Auth/Exception.hs",
"src/Amazonka/Auth/InstanceProfile.hs",
"src/Amazonka/Auth/Keys.hs",
"src/Amazonka/Auth/STS.hs",
"src/Amazonka/EC2/Metadata.hs",
"src/Amazonka/Env.hs",
"src/Amazonka/HTTP.hs",
Expand Down Expand Up @@ -84,6 +91,7 @@ haskell_library(
"@stackage//:bytestring",
"@stackage//:conduit",
"@stackage//:directory",
"@stackage//:exceptions",
"@stackage//:http-client",
"@stackage//:http-conduit",
"@stackage//:http-types",
Expand All @@ -94,6 +102,7 @@ haskell_library(
"@stackage//:text",
"@stackage//:time",
"@stackage//:transformers",
"@stackage//:unordered-containers",
"@stackage//:uuid",
],
)
28 changes: 28 additions & 0 deletions lib/amazonka/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@
## [2.0.0](https://github.com/brendanhay/amazonka/tree/2.0.0)
Released: **?**, Compare: [2.0.0-rc1](https://github.com/brendanhay/amazonka/compare/2.0.0-rc1...2.0.0)

### Major Changes

- The authentication code in `amazonka` got a full rewrite in PR [\#746](https://github.com/brendanhay/amazonka/pull/746).

- On Windows, the {credential,config} files are read from `%USERPROFILE%\\.aws\\{credentials,config}` to [match the AWS SDK](https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/creds-file.html#creds-file-general-info).
- `newEnv` now takes a function of type `EnvNoAuth -> m Env`, which is to fetch credentials in an appropriate manner
- The `Credentials` type has been removed, you should instead use the following functions corresponding to the departed constructor. All are exported by `Amazonka.Auth`:

| Old Name | New Name | Args/Comment |
|-------------------|------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `FromKeys` | `fromKeys` | `AccessKey`, `SecretKey`. |
| `FromSession` | `fromSession` | `AccessKey`, `SecretKey`, `SessionToken`. |
| | `fromTemporarySession` | `AccessKey`, `SecretKey`, `SessionToken`, `UTCTime` (expiry time). Likely not useful. |
| | `fromKeysEnv` | None - reads `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and optionally `AWS_SESSION_TOKEN`. |
| `FromEnv` | | Removed - took up named environment variables for access key id/secret access key/session token/expiry time. |
| `FromProfile` | `fromNamedInstanceProfile` | `Text` - looks up a named instance profile from the Instance Meta-Data Service (IMDS). |
| | `fromDefaultInstanceProfile` | None - selects the first instance profile, like the `discover` process does as one of its steps. |
| `FromFile` | `fromFilePath` | `Text` (profile name), `FilePath` (credentials file), `FilePath` (config file). **Significantly improved** - now respects the `role_arn` setting in config files, alongside either `source_profile`, `credential_source`, or `web_identity_token_file`. |
| | `fromFileEnv` | None - read config files from their default location, and respects the `AWS_PROFILE` environment variable. |
| | `fromAssumedRole` | `Text` (role arn), `Text` (role session name). Assumes a role using `sts:AssumeRole`. |
| | `fromWebIdentity` | `FilePath` (web identity token file), `Text` (role arn), `Maybe Text` (role session name). Assumes a role using `sts:AssumeRoleWithWebIdentity`. |
| `FromWebIdentity` | `fromWebIdentityEnv` | None - reads `AWS_WEB_IDENTITY_TOKEN_FILE`, `AWS_ROLE_ARN`, and `AWS_ROLE_SESSION_NAME`. |
| | `fromContainer` | `Text` (absolute url to query the ECS Container Agent). |
| `FromContainer` | `fromContainerEnv` | None - reads `AWS_CONTAINER_CREDENTIALS_RELATIVE_URI`. |
| `Discover` | `discover` | A rough mimic of the offical SDK, trying several methods in sequence. |

- The `Amazonka.Auth.runCredentialChain` function allows you to build your own custom credential chains.

### Changed

- `amazonka-dynamodb`: Mark various fields as required
Expand Down
41 changes: 25 additions & 16 deletions lib/amazonka/amazonka.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ library
exposed-modules:
Amazonka
Amazonka.Auth
Amazonka.Auth.Background
Amazonka.Auth.ConfigFile
Amazonka.Auth.Container
Amazonka.Auth.Exception
Amazonka.Auth.InstanceProfile
Amazonka.Auth.Keys
Amazonka.Auth.STS
Amazonka.EC2.Metadata
Amazonka.Env
Amazonka.HTTP
Expand All @@ -88,19 +95,21 @@ library
Amazonka.Data, Amazonka.Types, Amazonka.Bytes, Amazonka.Endpoint, Amazonka.Crypto

build-depends:
, amazonka-core ^>=2.0
, amazonka-sts ^>=2.0
, bytestring >=0.10.8
, conduit >=1.3
, directory >=1.2
, http-client >=0.5 && <0.8
, http-conduit >=2.3 && <3
, http-types >=0.12
, ini >=0.3.5
, lens >=4
, resourcet >=1.1
, retry >=0.7.6.2
, text >=1.1
, time >=1.9
, transformers >=0.2
, uuid >=1.2.6 && <1.4
, amazonka-core ^>=2.0
, amazonka-sts ^>=2.0
, bytestring >=0.10.8
, conduit >=1.3
, directory >=1.2
, exceptions ^>=0.10.4
, http-client >=0.5 && <0.8
, http-conduit >=2.3 && <3
, http-types >=0.12
, ini >=0.3.5
, lens >=4
, resourcet >=1.1
, retry >=0.7.6.2
, text >=1.1
, time >=1.9
, transformers >=0.2
, unordered-containers ^>=0.2.14.0
, uuid >=1.2.6 && <1.4
28 changes: 15 additions & 13 deletions lib/amazonka/src/Amazonka.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ module Amazonka
Env.EnvNoAuth,
Env.newEnv,
Env.newEnvNoAuth,
Env.newEnvWith,
Env.envAuthMaybe,

-- ** Service Configuration
-- $service
Env.authenticate,
Env.override,
Env.configure,
Env.within,
Expand All @@ -48,7 +46,7 @@ module Amazonka
AccessKey (..),
SecretKey (..),
SessionToken (..),
Credentials (..),
discover,
endgame marked this conversation as resolved.
Show resolved Hide resolved
-- $discovery

-- ** Supported Regions
Expand Down Expand Up @@ -150,7 +148,6 @@ import qualified Amazonka.Crypto as Crypto
import qualified Amazonka.Data.Body as Body
import qualified Amazonka.EC2.Metadata as EC2
import qualified Amazonka.Endpoint as Endpoint
import Amazonka.Env (Env)
import qualified Amazonka.Env as Env
import qualified Amazonka.Error as Error
import qualified Amazonka.HTTP as HTTP
Expand Down Expand Up @@ -202,14 +199,14 @@ import qualified Network.HTTP.Client as Client
-- logger <- AWS.newLogger AWS.Debug IO.stdout
--
-- -- To specify configuration preferences, 'newEnv' is used to create a new
-- -- configuration environment. The 'Credentials' parameter is used to specify
-- -- configuration environment. The argument to 'newEnv' is used to specify the
-- -- mechanism for supplying or retrieving AuthN/AuthZ information.
-- -- In this case 'Discover' will cause the library to try a number of options such
-- -- In this case 'discover' will cause the library to try a number of options such
-- -- as default environment variables, or an instance's IAM Profile and identity document:
-- discover <- AWS.newEnv AWS.Discover
-- discoveredEnv <- AWS.newEnv AWS.discover
--
-- let env =
-- discover
-- discoveredEnv
-- { AWS._envLogger = logger
-- , AWS._envRegion = AWS.Frankfurt
-- }
Expand All @@ -230,9 +227,14 @@ import qualified Network.HTTP.Client as Client
-- AuthN/AuthZ information is handled similarly to other AWS SDKs. You can read
-- some of the options available <http://blogs.aws.amazon.com/security/post/Tx3D6U6WSFGOK2H/A-New-and-Standardized-Way-to-Manage-Credentials-in-the-AWS-SDKs here>.
--
-- When running on an EC2 instance and using 'FromProfile' or 'Discover', a thread
-- is forked which transparently handles the expiry and subsequent refresh of IAM
-- profile information. See 'Amazonka.Auth.fromProfileName' for more information.
-- Authentication methods which return short-lived credentials (e.g., when running on
-- an EC2 instance) fork a background thread which transparently handles the expiry
-- and subsequent refresh of IAM profile information. See
-- 'Amazonka.Auth.Background.fetchAuthInBackground' for more information.
--
-- /See:/ "Amazonka.Auth", if you want to commit to specific authentication methods.
--
-- /See:/ 'Amazonka.Auth.runCredentialChain' if you want to build your own credential chain.

-- $sending
-- To send a request you need to create a value of the desired operation type using
Expand Down Expand Up @@ -296,7 +298,7 @@ import qualified Network.HTTP.Client as Client
--
-- The updated configuration is then passed to the 'Env' during setup:
--
-- > env <- AWS.configure dynamo <$> AWS.newEnv AWS.Discover
-- > env <- AWS.configure dynamo <$> AWS.newEnv AWS.discover
-- >
-- > AWS.runResourceT $ do
-- > -- This S3 operation will communicate with remote AWS APIs.
Expand All @@ -310,7 +312,7 @@ import qualified Network.HTTP.Client as Client
--
-- You can also scope the service configuration modifications to specific actions:
--
-- > env <- AWS.newEnv AWS.Discover
-- > env <- AWS.newEnv AWS.discover
-- >
-- > AWS.runResourceT $ do
-- > -- Service operations here will communicate with AWS, even remote DynamoDB.
Expand Down
Loading