-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathGeneralHTTPCredentialsProvider.cpp
288 lines (252 loc) · 12.1 KB
/
GeneralHTTPCredentialsProvider.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
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/core/auth/GeneralHTTPCredentialsProvider.h>
#include <aws/crt/Api.h>
#include <aws/core/platform/Environment.h>
#include <aws/core/utils/logging/LogMacros.h>
#include <aws/core/utils/json/JsonSerializer.h>
#include <fstream>
using namespace Aws::Auth;
static const char GEN_HTTP_LOG_TAG[] = "GeneralHTTPCredentialsProvider";
const char GeneralHTTPCredentialsProvider::AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE[] = "AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE";
const char GeneralHTTPCredentialsProvider::AWS_CONTAINER_CREDENTIALS_RELATIVE_URI[] = "AWS_CONTAINER_CREDENTIALS_RELATIVE_URI";
const char GeneralHTTPCredentialsProvider::AWS_CONTAINER_CREDENTIALS_FULL_URI[] = "AWS_CONTAINER_CREDENTIALS_FULL_URI";
const char GeneralHTTPCredentialsProvider::AWS_CONTAINER_AUTHORIZATION_TOKEN[] = "AWS_CONTAINER_AUTHORIZATION_TOKEN";
const char GeneralHTTPCredentialsProvider::AWS_ECS_CONTAINER_HOST[] = "169.254.170.2";
const char GeneralHTTPCredentialsProvider::AWS_EKS_CONTAINER_HOST[] = "169.254.170.23";
const char GeneralHTTPCredentialsProvider::AWS_EKS_CONTAINER_HOST_IPV6[] = "fd00:ec2::23";
bool IsAllowedIp(const Aws::String& authority)
{
// address is an ECS / EKS container host
if (authority == GeneralHTTPCredentialsProvider::AWS_ECS_CONTAINER_HOST) // ECS container host
{
return true;
} else if (authority == GeneralHTTPCredentialsProvider::AWS_EKS_CONTAINER_HOST ||
authority == GeneralHTTPCredentialsProvider::AWS_EKS_CONTAINER_HOST_IPV6) { // EKS container host
return true;
}
// address is within the loop back
// IPv4
if (authority.rfind(Aws::String("127.0.0."), 0) == 0 && authority.size() > 8 && authority.size() <= 11)
{
Aws::String octet = authority.substr(8);
if ((octet.find_first_not_of("0123456789") != Aws::String::npos) ||
(Aws::Utils::StringUtils::ConvertToInt32(octet.c_str()) > 255)) {
AWS_LOGSTREAM_WARN(GEN_HTTP_LOG_TAG, "Can't use General HTTP Provider: AWS_CONTAINER_CREDENTIALS_FULL_URI ip address is malformed: " << authority);
return false;
} else {
return true;
}
}
// IPv6
if (authority == "::1" || authority == "0:0:0:0:0:0:0:1" || authority == "[::1]" || authority == "[0:0:0:0:0:0:0:1]")
{
return true;
}
return false;
}
bool GeneralHTTPCredentialsProvider::ShouldCreateGeneralHTTPProvider(const Aws::String& relativeUri, const Aws::String& absoluteUri, const Aws::String authToken)
{
if (authToken.find("\r\n") != Aws::String::npos)
{
AWS_LOGSTREAM_WARN(GEN_HTTP_LOG_TAG, "Can't use General HTTP Provider: AWS_CONTAINER_AUTHORIZATION_TOKEN env value contains invalid characters (\\r\\n)");
return false;
}
if (!relativeUri.empty())
{
// The provider MAY choose to assert syntactical validity of the resulting URI
// perform very basic check here
if (relativeUri[0] != '/') {
AWS_LOGSTREAM_WARN(GEN_HTTP_LOG_TAG, "Can't use General HTTP Provider: AWS_CONTAINER_CREDENTIALS_RELATIVE_URI does not begin with /");
return false;
} else {
// full URI is not used in case of a relative one present
return true;
}
}
if (!absoluteUri.empty())
{
// If the resolved URI’s scheme is HTTPS, its hostname may be used in the request
if (Aws::Utils::StringUtils::ToLower(absoluteUri.c_str()).rfind(Aws::String("https://"), 0) == 0) // if starts_with
{
return true;
}
Aws::Http::URI absUri(absoluteUri);
const Aws::String& authority = absUri.GetAuthority();
// Otherwise, implementations MUST fail to resolve when the URI hostname does not satisfy any of the following conditions
if (IsAllowedIp(authority))
{
return true;
}
Aws::Crt::Io::HostResolver* pHostResolver = Aws::Crt::ApiHandle::GetOrCreateStaticDefaultHostResolver();
if (pHostResolver)
{
bool shouldAllow = false;
bool hostResolved = false;
std::mutex hostResolverMutex;
std::condition_variable hostResolverCV;
auto onHostResolved = [&shouldAllow, &hostResolved, &hostResolverCV, &hostResolverMutex](Aws::Crt::Io::HostResolver &resolver, const Aws::Crt::Vector<Aws::Crt::Io::HostAddress> &addresses, int errorCode)
{
AWS_UNREFERENCED_PARAM(resolver);
if (AWS_ERROR_SUCCESS == errorCode)
{
for(const auto& address : addresses)
{
if (!IsAllowedIp(Aws::String((const char*) address.address->bytes, address.address->len)))
{
return;
}
}
std::unique_lock<std::mutex> lock(hostResolverMutex);
shouldAllow = !addresses.empty();
hostResolved = true;
hostResolverCV.notify_one();
}
else
{
std::unique_lock<std::mutex> lock(hostResolverMutex);
hostResolverCV.notify_one();
}
};
pHostResolver->ResolveHost(authority.c_str(), onHostResolved);
std::unique_lock<std::mutex> lock(hostResolverMutex);
if (!hostResolved) {
hostResolverCV.wait_for(lock, std::chrono::milliseconds(1000));
}
if (shouldAllow)
{
return true;
}
}
AWS_LOGSTREAM_WARN(GEN_HTTP_LOG_TAG, "Can't use General HTTP Provider: AWS_CONTAINER_CREDENTIALS_FULL_URI is not HTTPS and is not within loop back CIDR: " << authority);
return false;
}
// both relativeUri and absoluteUri are empty
return false;
}
GeneralHTTPCredentialsProvider::GeneralHTTPCredentialsProvider(const Aws::Client::ClientConfiguration& clientConfig,
const Aws::String& relativeUri,
const Aws::String& absoluteUri,
const Aws::String& authToken,
const Aws::String& authTokenFilePath,
long refreshRateMs,
ShouldCreateFunc shouldCreateFunc) :
m_authTokenFilePath(authTokenFilePath),
m_loadFrequencyMs(refreshRateMs)
{
if (shouldCreateFunc(relativeUri, absoluteUri, authToken))
{
AWS_LOGSTREAM_INFO(GEN_HTTP_LOG_TAG, "Creating GeneralHTTPCredentialsProvider with refresh rate " << refreshRateMs);
if (!relativeUri.empty()) {
m_ecsCredentialsClient = Aws::MakeShared<Aws::Internal::ECSCredentialsClient>(GEN_HTTP_LOG_TAG, clientConfig, relativeUri.c_str(), AWS_ECS_CONTAINER_HOST, authToken.c_str());
}
else if (!absoluteUri.empty()) {
m_ecsCredentialsClient = Aws::MakeShared<Aws::Internal::ECSCredentialsClient>(GEN_HTTP_LOG_TAG, clientConfig, "", absoluteUri.c_str(), authToken.c_str());
}
}
}
GeneralHTTPCredentialsProvider::GeneralHTTPCredentialsProvider(const Aws::String& relativeUri,
const Aws::String& absoluteUri,
const Aws::String& authToken,
const Aws::String& authTokenFilePath,
long refreshRateMs,
ShouldCreateFunc shouldCreateFunc) :
m_authTokenFilePath(authTokenFilePath),
m_loadFrequencyMs(refreshRateMs)
{
if (shouldCreateFunc(relativeUri, absoluteUri, authToken))
{
AWS_LOGSTREAM_INFO(GEN_HTTP_LOG_TAG, "Creating GeneralHTTPCredentialsProvider with refresh rate " << refreshRateMs);
if (!relativeUri.empty()) {
m_ecsCredentialsClient = Aws::MakeShared<Aws::Internal::ECSCredentialsClient>(GEN_HTTP_LOG_TAG, relativeUri.c_str(), AWS_ECS_CONTAINER_HOST, authToken.c_str());
} else if (!absoluteUri.empty()) {
m_ecsCredentialsClient = Aws::MakeShared<Aws::Internal::ECSCredentialsClient>(GEN_HTTP_LOG_TAG, "", absoluteUri.c_str(), authToken.c_str());
}
}
}
GeneralHTTPCredentialsProvider::GeneralHTTPCredentialsProvider(
const std::shared_ptr<Aws::Internal::ECSCredentialsClient>& client, long refreshRateMs) :
m_ecsCredentialsClient(client),
m_loadFrequencyMs(refreshRateMs)
{
AWS_LOGSTREAM_INFO(GEN_HTTP_LOG_TAG, "Creating GeneralHTTPCredentialsProvider with a pre-allocated client " << refreshRateMs);
}
AWSCredentials GeneralHTTPCredentialsProvider::GetAWSCredentials()
{
RefreshIfExpired();
Aws::Utils::Threading::ReaderLockGuard guard(m_reloadLock);
return m_credentials;
}
bool GeneralHTTPCredentialsProvider::ExpiresSoon() const
{
return ((m_credentials.GetExpiration() - Aws::Utils::DateTime::Now()).count() < AWS_CREDENTIAL_PROVIDER_EXPIRATION_GRACE_PERIOD);
}
Aws::String GeneralHTTPCredentialsProvider::LoadTokenFromFile() const
{
Aws::IFStream tokenFile(m_authTokenFilePath.c_str());
if (tokenFile.is_open() && tokenFile.good())
{
Aws::StringStream memoryStream;
std::copy(std::istreambuf_iterator<char>(tokenFile), std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(memoryStream));
Aws::String tokenFileStr = memoryStream.str();
if (tokenFileStr.find("\r\n") != Aws::String::npos)
{
AWS_LOGSTREAM_ERROR(GEN_HTTP_LOG_TAG, "Unable to retrieve credentials: file in AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE contains invalid characters (\\r\\n)");
return {};
}
return tokenFileStr;
} else {
AWS_LOGSTREAM_ERROR(GEN_HTTP_LOG_TAG, "Unable to retrieve credentials: failed to open Auth Token file .");
return {};
}
}
void GeneralHTTPCredentialsProvider::Reload()
{
AWS_LOGSTREAM_INFO(GEN_HTTP_LOG_TAG, "Credentials have expired or will expire, attempting to re-pull from ECS IAM Service.");
if (!m_ecsCredentialsClient)
{
AWS_LOGSTREAM_ERROR(GEN_HTTP_LOG_TAG, "Unable to retrieve credentials: ECS Credentials client is not initialized.");
return;
}
if (!m_authTokenFilePath.empty())
{
Aws::String token = LoadTokenFromFile();
m_ecsCredentialsClient->SetToken(std::move(token));
}
auto credentialsStr = m_ecsCredentialsClient->GetECSCredentials();
if (credentialsStr.empty()) return;
Aws::Utils::Json::JsonValue credentialsDoc(credentialsStr);
if (!credentialsDoc.WasParseSuccessful())
{
AWS_LOGSTREAM_ERROR(GEN_HTTP_LOG_TAG, "Failed to parse output from ECSCredentialService.");
return;
}
Aws::String accessKey, secretKey, token;
Utils::Json::JsonView credentialsView(credentialsDoc);
accessKey = credentialsView.GetString("AccessKeyId");
secretKey = credentialsView.GetString("SecretAccessKey");
token = credentialsView.GetString("Token");
AWS_LOGSTREAM_DEBUG(GEN_HTTP_LOG_TAG, "Successfully pulled credentials from metadata service with access key " << accessKey);
m_credentials.SetAWSAccessKeyId(accessKey);
m_credentials.SetAWSSecretKey(secretKey);
m_credentials.SetSessionToken(token);
m_credentials.SetExpiration(Aws::Utils::DateTime(credentialsView.GetString("Expiration"), Aws::Utils::DateFormat::ISO_8601));
AWSCredentialsProvider::Reload();
}
void GeneralHTTPCredentialsProvider::RefreshIfExpired()
{
AWS_LOGSTREAM_DEBUG(GEN_HTTP_LOG_TAG, "Checking if latest credential pull has expired.");
Aws::Utils::Threading::ReaderLockGuard guard(m_reloadLock);
if (!m_credentials.IsEmpty() && !IsTimeToRefresh(m_loadFrequencyMs) && !ExpiresSoon())
{
return;
}
guard.UpgradeToWriterLock();
if (!m_credentials.IsEmpty() && !IsTimeToRefresh(m_loadFrequencyMs) && !ExpiresSoon())
{
return;
}
Reload();
}