Skip to content

Commit 3094e39

Browse files
authored
Merge pull request #12646 from whatsthecraic/feature/s3-auth-iam-role
S3: opt-in the STSProfileCredentialsProvider
2 parents a46ca4f + 5107360 commit 3094e39

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

packaging/dependencies.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ scope: {
3838
aws-sdk-cpp =
3939
(pkgs.aws-sdk-cpp.override {
4040
apis = [
41+
"identity-management"
4142
"s3"
4243
"transfer"
4344
];

src/libstore/meson.build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ if aws_s3.found()
135135
'-L' + aws_s3.get_variable('libdir'),
136136
'-laws-cpp-sdk-transfer',
137137
'-laws-cpp-sdk-s3',
138+
'-laws-cpp-sdk-identity-management',
139+
'-laws-cpp-sdk-cognito-identity',
140+
'-laws-cpp-sdk-sts',
138141
'-laws-cpp-sdk-core',
139142
'-laws-crt-cpp',
140143
],

src/libstore/s3-binary-cache-store.cc

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <aws/core/utils/logging/FormattedLogSystem.h>
2222
#include <aws/core/utils/logging/LogMacros.h>
2323
#include <aws/core/utils/threading/Executor.h>
24+
#include <aws/identity-management/auth/STSProfileCredentialsProvider.h>
2425
#include <aws/s3/S3Client.h>
2526
#include <aws/s3/model/GetObjectRequest.h>
2627
#include <aws/s3/model/HeadObjectRequest.h>
@@ -71,6 +72,29 @@ class AwsLogger : public Aws::Utils::Logging::FormattedLogSystem
7172
#endif
7273
};
7374

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+
7498
static void initAWS()
7599
{
76100
static std::once_flag flag;
@@ -102,13 +126,8 @@ S3Helper::S3Helper(
102126
const std::string & endpoint)
103127
: config(makeConfig(region, scheme, endpoint))
104128
, 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),
110130
*config,
111-
// FIXME: https://github.com/aws/aws-sdk-cpp/issues/759
112131
#if AWS_SDK_VERSION_MAJOR == 1 && AWS_SDK_VERSION_MINOR < 3
113132
false,
114133
#else

0 commit comments

Comments
 (0)