aws / aws-sdk-php

Official repository of the AWS SDK for PHP (@awsforphp)
http://aws.amazon.com/sdkforphp
Apache License 2.0
6.01k stars 1.22k forks source link

Support for parsing both config and credentials files #2939

Open BVMiko opened 3 months ago

BVMiko commented 3 months ago

Issue #, if available: #2794

Description of changes: AWS CLI as well as boto3 implementation handle config & credentials files by reading the contents of both and consolidating them into a single associative array, prior to doing any processing. aws-sdk-php, on the other hand, only reads in one file when processing. This causes an issue when using assumed roles while organizing credentials the way recommended in the AWS CLI docs (the "IAM Role" tab here).

The current aws-sdk-php implementation looks like it has set up a workaround to the situation by allowing a specific filename to be passed into several of the functions, so that the developer can choose between the config or credentials file. However this would not work in the aforementioned situation where data is split between both files.

Note that aws-sdk-php does actually have a segment of code which combines the files into a single associative array; however there are a few issues:

  1. It's only used by WebIdentity Credentials; it cannot be used by regular configuration file
  2. It doesn't properly merge sections which exist in both files (as is done in boto3)
  3. It doesn't respect the AWS_CONFIG_FILE environment variable which can be used to override the ~/.aws/config file path. (Note this issue seems to be present in many other locations as well)

This PR has two commits:

  1. The first commit fixes the problem in the most minimal way. It replaces the single-ini-file handler CredentialProvider::loadProfiles() with a call to the multi-ini-file handler CredentialProvider::loadDefaultProfiles(). It also updates the latter to fix the two additional issues mentioned above. There was an additional modification added to handle the legacy filename workaround.
  2. The second commit applies the same fix to TokenProvider / SsoTokenProvider. Additionally it updates the CredentialProvider::ini(), CredentialProvider::process() and CredentialProvider::sso() functions to support a null value for the filename override.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.