-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathSTSProfileCredentialsProvider.cpp
355 lines (309 loc) · 12.4 KB
/
STSProfileCredentialsProvider.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/identity-management/auth/STSProfileCredentialsProvider.h>
#include <aws/sts/model/AssumeRoleRequest.h>
#include <aws/sts/STSClient.h>
#include <aws/core/utils/logging/LogMacros.h>
#include <aws/core/utils/Outcome.h>
#include <aws/core/utils/UUID.h>
#include <utility>
using namespace Aws;
using namespace Aws::Auth;
constexpr char CLASS_TAG[] = "STSProfileCredentialsProvider";
STSProfileCredentialsProvider::STSProfileCredentialsProvider()
: STSProfileCredentialsProvider(GetConfigProfileName(), std::chrono::minutes(60)/*duration*/, nullptr/*stsClientFactory*/)
{
}
STSProfileCredentialsProvider::STSProfileCredentialsProvider(const Aws::String& profileName, std::chrono::minutes duration)
: STSProfileCredentialsProvider(profileName, duration, nullptr/*stsClientFactory*/)
{
}
STSProfileCredentialsProvider::STSProfileCredentialsProvider(const Aws::String& profileName, std::chrono::minutes duration, const std::function<Aws::STS::STSClient*(const AWSCredentials&)> &stsClientFactory)
: m_profileName(profileName),
m_duration(duration),
m_reloadFrequency(std::chrono::minutes(std::max(int64_t(5), static_cast<int64_t>(duration.count()))) - std::chrono::minutes(5)),
m_stsClientFactory(stsClientFactory)
{
}
AWSCredentials STSProfileCredentialsProvider::GetAWSCredentials()
{
RefreshIfExpired();
Utils::Threading::ReaderLockGuard guard(m_reloadLock);
return m_credentials;
}
void STSProfileCredentialsProvider::RefreshIfExpired()
{
Utils::Threading::ReaderLockGuard guard(m_reloadLock);
if (!IsTimeToRefresh(static_cast<long>(m_reloadFrequency.count())) || !m_credentials.IsExpiredOrEmpty())
{
return;
}
guard.UpgradeToWriterLock();
if (!IsTimeToRefresh(static_cast<long>(m_reloadFrequency.count())) || !m_credentials.IsExpiredOrEmpty()) // double-checked lock to avoid refreshing twice
{
return;
}
Reload();
}
enum class ProfileState
{
Invalid,
Static,
Process,
SourceProfile,
SelfReferencing, // special case of SourceProfile.
};
/*
* A valid profile can be in one of the following states. Any other state is considered invalid.
+---------+-----------+-----------+--------------+
| | | | |
| Role | Source | Process | Static |
| ARN | Profile | | Credentials |
+------------------------------------------------+
| | | | |
| false | false | false | TRUE |
| | | | |
| false | false | TRUE | false |
| | | | |
| TRUE | TRUE | false | false |
| | | | |
| TRUE | TRUE | false | TRUE |
| | | | |
+---------+-----------+-----------+--------------+
*/
static ProfileState CheckProfile(const Aws::Config::Profile& profile, bool topLevelProfile)
{
constexpr int STATIC_CREDENTIALS = 1;
constexpr int PROCESS_CREDENTIALS = 2;
constexpr int SOURCE_PROFILE = 4;
constexpr int ROLE_ARN = 8;
int state = 0;
if (!profile.GetCredentials().IsExpiredOrEmpty())
{
state += STATIC_CREDENTIALS;
}
if (!profile.GetCredentialProcess().empty())
{
state += PROCESS_CREDENTIALS;
}
if (!profile.GetSourceProfile().empty())
{
state += SOURCE_PROFILE;
}
if (!profile.GetRoleArn().empty())
{
state += ROLE_ARN;
}
if (topLevelProfile)
{
switch(state)
{
case 1:
return ProfileState::Static;
case 2:
return ProfileState::Process;
case 12: // just source profile && role arn available
return ProfileState::SourceProfile;
case 13: // static creds && source profile && role arn
if (profile.GetName() == profile.GetSourceProfile())
{
return ProfileState::SelfReferencing;
}
// source-profile over-rule static credentials in top-level profiles (except when self-referencing)
return ProfileState::SourceProfile;
default:
// All other cases are considered malformed configuration.
return ProfileState::Invalid;
}
}
else
{
switch(state)
{
case 1:
return ProfileState::Static;
case 2:
return ProfileState::Process;
case 12: // just source profile && role arn available
return ProfileState::SourceProfile;
case 13: // static creds && source profile && role arn
if (profile.GetName() == profile.GetSourceProfile())
{
return ProfileState::SelfReferencing;
}
return ProfileState::Static; // static credentials over-rule source-profile (except when self-referencing)
default:
// All other cases are considered malformed configuration.
return ProfileState::Invalid;
}
}
}
void STSProfileCredentialsProvider::Reload()
{
// make a copy of the profiles map to be able to set credentials on the individual profiles when assuming role
auto loadedProfiles = Aws::Config::GetCachedConfigProfiles();
auto profileIt = loadedProfiles.find(m_profileName);
if(profileIt == loadedProfiles.end())
{
AWS_LOGSTREAM_ERROR(CLASS_TAG, "Profile " << m_profileName <<" was not found in the shared configuration file.");
m_credentials = {};
return;
}
ProfileState profileState = CheckProfile(profileIt->second, true/*topLevelProfile*/);
if (profileState == ProfileState::Static)
{
m_credentials = profileIt->second.GetCredentials();
AWSCredentialsProvider::Reload();
return;
}
if (profileState == ProfileState::Process)
{
const auto& creds = GetCredentialsFromProcess(profileIt->second.GetCredentialProcess());
if (!creds.IsExpiredOrEmpty())
{
m_credentials = creds;
AWSCredentialsProvider::Reload();
}
return;
}
if (profileState == ProfileState::Invalid)
{
AWS_LOGSTREAM_ERROR(CLASS_TAG, "Profile " << profileIt->second.GetName() << " is invalid. Check its configuration.");
m_credentials = {};
return;
}
if (profileState == ProfileState::SourceProfile)
{
// A top level profile with a 'SourceProfile' state (determined by CheckProfile rules) means that its static
// credentials will be ignored. So, it's ok to clear them out here to simplify the logic in the chaining loop
// below.
profileIt->second.SetCredentials({});
}
AWS_LOGSTREAM_INFO(CLASS_TAG, "Profile " << profileIt->second.GetName()
<< " has a role ARN. Attempting to load its source credentials from profile "
<< profileIt->second.GetSourceProfile());
Aws::Vector<Config::AWSProfileConfigLoader::ProfilesContainer::iterator> sourceProfiles;
Aws::Set<Aws::String> visitedProfiles;
auto currentProfile = profileIt;
sourceProfiles.push_back(currentProfile);
// build the chain (DAG)
while(!currentProfile->second.GetSourceProfile().empty())
{
ProfileState currentProfileState = CheckProfile(currentProfile->second, false /*topLevelProfile*/);
auto currentProfileName = currentProfile->second.GetName();
if (currentProfileState == ProfileState::Invalid)
{
AWS_LOGSTREAM_ERROR(CLASS_TAG, "Profile " << profileIt->second.GetName() << " is invalid. Check its configuration.");
m_credentials = {};
return;
}
// terminate the chain as soon as we hit a profile with either static credentials or credential process
if (currentProfileState == ProfileState::Static || currentProfileState == ProfileState::Process)
{
break;
}
if (currentProfileState == ProfileState::SelfReferencing)
{
sourceProfiles.push_back(currentProfile);
break;
}
// check if we have a circular reference in the graph.
if (visitedProfiles.find(currentProfileName) != visitedProfiles.end())
{
// TODO: print the whole DAG for better debugging
AWS_LOGSTREAM_ERROR(CLASS_TAG, "Profile " << currentProfileName << " has a circular reference. Aborting.");
m_credentials = {};
return;
}
visitedProfiles.emplace(currentProfileName);
const auto it = loadedProfiles.find(currentProfile->second.GetSourceProfile());
if(it == loadedProfiles.end())
{
// TODO: print the whole DAG for better debugging
AWS_LOGSTREAM_ERROR(CLASS_TAG, "Profile " << currentProfileName << " has an invalid source profile " << currentProfile->second.GetSourceProfile());
m_credentials = {};
return;
}
currentProfile = it;
sourceProfiles.push_back(currentProfile);
}
// The last profile added to the stack is not checked. Check it now.
if (!sourceProfiles.empty())
{
if (CheckProfile(sourceProfiles.back()->second, false /*topLevelProfile*/) == ProfileState::Invalid)
{
AWS_LOGSTREAM_ERROR(CLASS_TAG, "Profile " << sourceProfiles.back()->second.GetName() << " is invalid. Check its configuration.");
m_credentials = {};
return;
}
}
while (sourceProfiles.size() > 1)
{
const auto profile = sourceProfiles.back()->second;
sourceProfiles.pop_back();
AWSCredentials stsCreds;
if (profile.GetCredentialProcess().empty())
{
assert(!profile.GetCredentials().IsEmpty());
stsCreds = profile.GetCredentials();
}
else
{
stsCreds = GetCredentialsFromProcess(profile.GetCredentialProcess());
}
// get the role arn from the profile at the top of the stack (which hasn't been popped out yet)
const auto& arn = sourceProfiles.back()->second.GetRoleArn();
const auto& externalId = sourceProfiles.back()->second.GetExternalId();
const auto& assumedCreds = GetCredentialsFromSTS(stsCreds, arn, externalId);
sourceProfiles.back()->second.SetCredentials(assumedCreds);
}
if (!sourceProfiles.empty())
{
assert(profileIt == sourceProfiles.back());
assert(!profileIt->second.GetCredentials().IsEmpty());
}
m_credentials = profileIt->second.GetCredentials();
AWSCredentialsProvider::Reload();
}
AWSCredentials STSProfileCredentialsProvider::GetCredentialsFromSTSInternal(const Aws::String& roleArn, const Aws::String& externalId, Aws::STS::STSClient* client)
{
using namespace Aws::STS::Model;
AssumeRoleRequest assumeRoleRequest;
assumeRoleRequest
.WithRoleArn(roleArn)
.WithRoleSessionName(Aws::Utils::UUID::PseudoRandomUUID())
.WithDurationSeconds(static_cast<int>(std::chrono::seconds(m_duration).count()));
if (!externalId.empty())
{
assumeRoleRequest.SetExternalId(externalId);
}
auto outcome = client->AssumeRole(assumeRoleRequest);
if (outcome.IsSuccess())
{
const auto& modelCredentials = outcome.GetResult().GetCredentials();
return {modelCredentials.GetAccessKeyId(),
modelCredentials.GetSecretAccessKey(),
modelCredentials.GetSessionToken(),
modelCredentials.GetExpiration()};
}
else
{
AWS_LOGSTREAM_ERROR(CLASS_TAG, "Failed to assume role " << roleArn);
}
return {};
}
AWSCredentials STSProfileCredentialsProvider::GetCredentialsFromSTS(const AWSCredentials& credentials, const Aws::String& roleArn)
{
return GetCredentialsFromSTS(credentials, roleArn, "");
}
AWSCredentials STSProfileCredentialsProvider::GetCredentialsFromSTS(const AWSCredentials& credentials, const Aws::String& roleArn, const Aws::String& externalId)
{
using namespace Aws::STS::Model;
if (m_stsClientFactory) {
return GetCredentialsFromSTSInternal(roleArn, externalId m_stsClientFactory(credentials));
}
Aws::STS::STSClient stsClient {credentials};
return GetCredentialsFromSTSInternal(roleArn, externalId, &stsClient);
}