|
21 | 21 | #include <aws/core/utils/logging/FormattedLogSystem.h> |
22 | 22 | #include <aws/core/utils/logging/LogMacros.h> |
23 | 23 | #include <aws/core/utils/threading/Executor.h> |
| 24 | +#include <aws/identity-management/auth/STSProfileCredentialsProvider.h> |
24 | 25 | #include <aws/s3/S3Client.h> |
25 | 26 | #include <aws/s3/model/GetObjectRequest.h> |
26 | 27 | #include <aws/s3/model/HeadObjectRequest.h> |
@@ -71,6 +72,29 @@ class AwsLogger : public Aws::Utils::Logging::FormattedLogSystem |
71 | 72 | #endif |
72 | 73 | }; |
73 | 74 |
|
| 75 | +/* Retrieve the credentials from the list of AWS default providers, with the addition of the STS creds provider. This |
| 76 | + last can be used to acquire further permissions with a specific IAM role. |
| 77 | + Roughly based on https://github.com/aws/aws-sdk-cpp/issues/150#issuecomment-538548438 |
| 78 | +*/ |
| 79 | +struct CustomAwsCredentialsProviderChain : public Aws::Auth::AWSCredentialsProviderChain |
| 80 | +{ |
| 81 | + CustomAwsCredentialsProviderChain(const std::string & profile) |
| 82 | + { |
| 83 | + if (profile.empty()) { |
| 84 | + // Use all the default AWS providers, plus the possibility to acquire a IAM role directly via a profile. |
| 85 | + Aws::Auth::DefaultAWSCredentialsProviderChain default_aws_chain; |
| 86 | + for (auto provider : default_aws_chain.GetProviders()) |
| 87 | + AddProvider(provider); |
| 88 | + AddProvider(std::make_shared<Aws::Auth::STSProfileCredentialsProvider>()); |
| 89 | + } else { |
| 90 | + // Override the profile name to retrieve from the AWS config and credentials. I believe this option |
| 91 | + // comes from the ?profile querystring in nix.conf. |
| 92 | + AddProvider(std::make_shared<Aws::Auth::ProfileConfigFileAWSCredentialsProvider>(profile.c_str())); |
| 93 | + AddProvider(std::make_shared<Aws::Auth::STSProfileCredentialsProvider>(profile)); |
| 94 | + } |
| 95 | + } |
| 96 | +}; |
| 97 | + |
74 | 98 | static void initAWS() |
75 | 99 | { |
76 | 100 | static std::once_flag flag; |
@@ -102,13 +126,8 @@ S3Helper::S3Helper( |
102 | 126 | const std::string & endpoint) |
103 | 127 | : config(makeConfig(region, scheme, endpoint)) |
104 | 128 | , client(make_ref<Aws::S3::S3Client>( |
105 | | - profile == "" |
106 | | - ? std::dynamic_pointer_cast<Aws::Auth::AWSCredentialsProvider>( |
107 | | - std::make_shared<Aws::Auth::DefaultAWSCredentialsProviderChain>()) |
108 | | - : std::dynamic_pointer_cast<Aws::Auth::AWSCredentialsProvider>( |
109 | | - std::make_shared<Aws::Auth::ProfileConfigFileAWSCredentialsProvider>(profile.c_str())), |
| 129 | + std::make_shared<CustomAwsCredentialsProviderChain>(profile), |
110 | 130 | *config, |
111 | | - // FIXME: https://github.com/aws/aws-sdk-cpp/issues/759 |
112 | 131 | #if AWS_SDK_VERSION_MAJOR == 1 && AWS_SDK_VERSION_MINOR < 3 |
113 | 132 | false, |
114 | 133 | #else |
|
0 commit comments