-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathmanaged_identity_source.cpp
561 lines (480 loc) · 18.7 KB
/
managed_identity_source.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "private/managed_identity_source.hpp"
#include "private/identity_log.hpp"
#include <azure/core/internal/environment.hpp>
#include <azure/core/platform.hpp>
#include <fstream>
#include <iterator>
#include <stdexcept>
#include <utility>
#include <sys/stat.h> // for stat() used to check file size
using namespace Azure::Identity::_detail;
using Azure::Core::_internal::Environment;
using Azure::Identity::_detail::IdentityLog;
namespace {
// https://learn.microsoft.com/azure/virtual-machines/instance-metadata-service
// IMDS is a REST API that's available at a well-known, non-routable IP address (169.254.169.254).
// You can only access it from within the VM. Communication between the VM and IMDS never leaves the
// host.
std::string const ImdsEndpoint = "http://169.254.169.254/metadata/identity/oauth2/token";
std::string WithSourceMessage(std::string const& credSource)
{
return " with " + credSource + " source";
}
void PrintEnvNotSetUpMessage(std::string const& credName, std::string const& credSource)
{
IdentityLog::Write(
IdentityLog::Level::Verbose,
credName + ": Environment is not set up for the credential to be created"
+ WithSourceMessage(credSource) + '.');
}
// ExpectedArcKeyDirectory returns the directory expected to contain Azure Arc keys.
std::string ExpectedArcKeyDirectory()
{
using Azure::Core::Credentials::AuthenticationException;
#if defined(AZ_PLATFORM_LINUX)
return "/var/opt/azcmagent/tokens";
#elif defined(AZ_PLATFORM_WINDOWS)
const std::string programDataPath{
Azure::Core::_internal::Environment::GetVariable("ProgramData")};
if (programDataPath.empty())
{
throw AuthenticationException("Unable to get ProgramData folder path.");
}
return programDataPath + "\\AzureConnectedMachineAgent\\Tokens";
#else
throw AuthenticationException("Unsupported OS. Arc supports only Linux and Windows.");
#endif
}
constexpr off_t MaximumAzureArcKeySize = 4096;
#if defined(AZ_PLATFORM_WINDOWS)
constexpr char DirectorySeparator = '\\';
#else
static constexpr char DirectorySeparator = '/';
#endif
// Validates that a given Azure Arc MSI file path is valid for use.
// The specified file must:
// - be in the expected directory for the OS
// - have a .key extension
// - contain at most 4096 bytes
void ValidateArcKeyFile(std::string const& fileName)
{
using Azure::Core::Credentials::AuthenticationException;
std::string directory;
const size_t lastSlashIndex = fileName.rfind(DirectorySeparator);
if (std::string::npos != lastSlashIndex)
{
directory = fileName.substr(0, lastSlashIndex);
}
if (directory != ExpectedArcKeyDirectory() || fileName.size() < 5
|| fileName.substr(fileName.size() - 4) != ".key")
{
throw AuthenticationException(
"The file specified in the 'WWW-Authenticate' header in the response from Azure Arc "
"Managed Identity Endpoint has an unexpected file path.");
}
struct stat s;
if (!stat(fileName.c_str(), &s))
{
if (s.st_size > MaximumAzureArcKeySize)
{
throw AuthenticationException(
"The file specified in the 'WWW-Authenticate' header in the response from Azure Arc "
"Managed Identity Endpoint is larger than 4096 bytes.");
}
}
else
{
throw AuthenticationException("Failed to get file size for '" + fileName + "'.");
}
}
} // namespace
Azure::Core::Url ManagedIdentitySource::ParseEndpointUrl(
std::string const& credName,
std::string const& url,
char const* envVarName,
std::string const& credSource)
{
using Azure::Core::Url;
using Azure::Core::Credentials::AuthenticationException;
try
{
auto const endpointUrl = Url(url);
IdentityLog::Write(
IdentityLog::Level::Informational,
credName + " will be created" + WithSourceMessage(credSource) + '.');
return endpointUrl;
}
catch (std::invalid_argument const&)
{
}
catch (std::out_of_range const&)
{
}
auto const errorMessage = credName + WithSourceMessage(credSource)
+ ": Failed to create: The environment variable \'" + envVarName
+ "\' contains an invalid URL.";
IdentityLog::Write(IdentityLog::Level::Warning, errorMessage);
throw AuthenticationException(errorMessage);
}
template <typename T>
std::unique_ptr<ManagedIdentitySource> AppServiceManagedIdentitySource::Create(
std::string const& credName,
std::string const& clientId,
std::string const& objectId,
std::string const& resourceId,
Azure::Core::Credentials::TokenCredentialOptions const& options,
char const* endpointVarName,
char const* secretVarName,
char const* appServiceVersion)
{
auto const msiEndpoint = Environment::GetVariable(endpointVarName);
auto const msiSecret = Environment::GetVariable(secretVarName);
auto const credSource = std::string("App Service ") + appServiceVersion;
if (!msiEndpoint.empty() && !msiSecret.empty())
{
return std::unique_ptr<ManagedIdentitySource>(new T(
clientId,
objectId,
resourceId,
options,
ParseEndpointUrl(credName, msiEndpoint, endpointVarName, credSource),
msiSecret));
}
PrintEnvNotSetUpMessage(credName, credSource);
return nullptr;
}
AppServiceManagedIdentitySource::AppServiceManagedIdentitySource(
std::string const& clientId,
std::string const& objectId,
std::string const& resourceId,
Azure::Core::Credentials::TokenCredentialOptions const& options,
Azure::Core::Url endpointUrl,
std::string const& secret,
std::string const& apiVersion,
std::string const& secretHeaderName,
std::string const& clientIdHeaderName)
: ManagedIdentitySource(clientId, endpointUrl.GetHost(), options),
m_request(Azure::Core::Http::HttpMethod::Get, std::move(endpointUrl))
{
{
using Azure::Core::Url;
auto& url = m_request.GetUrl();
url.AppendQueryParameter("api-version", apiVersion);
// Only one of clientId, objectId, or resourceId will be set to a non-empty value.
// AppService uses mi_res_id, and not msi_res_id:
// https://learn.microsoft.com/azure/app-service/overview-managed-identity?tabs=portal%2Chttp#rest-endpoint-reference
// Based on the App Service documentation, using principal_id for the query parameter name here
// instead of object_id (which is used as an alias).
if (!clientId.empty())
{
url.AppendQueryParameter(clientIdHeaderName, clientId);
}
else if (!objectId.empty())
{
url.AppendQueryParameter("principal_id", objectId);
}
else if (!resourceId.empty())
{
url.AppendQueryParameter("mi_res_id", resourceId);
}
}
m_request.SetHeader(secretHeaderName, secret);
}
Azure::Core::Credentials::AccessToken AppServiceManagedIdentitySource::GetToken(
Azure::Core::Credentials::TokenRequestContext const& tokenRequestContext,
Azure::Core::Context const& context) const
{
std::string scopesStr;
{
auto const& scopes = tokenRequestContext.Scopes;
if (!scopes.empty())
{
scopesStr = TokenCredentialImpl::FormatScopes(scopes, true);
}
}
// TokenCache::GetToken() and TokenCredentialImpl::GetToken() can only use the lambda argument
// when they are being executed. They are not supposed to keep a reference to lambda argument to
// call it later. Therefore, any capture made here will outlive the possible time frame when the
// lambda might get called.
return m_tokenCache.GetToken(scopesStr, {}, tokenRequestContext.MinimumExpiration, [&]() {
return TokenCredentialImpl::GetToken(context, true, [&]() {
auto request = std::make_unique<TokenRequest>(m_request);
if (!scopesStr.empty())
{
request->HttpRequest.GetUrl().AppendQueryParameter("resource", scopesStr);
}
return request;
});
});
}
std::unique_ptr<ManagedIdentitySource> AppServiceV2017ManagedIdentitySource::Create(
std::string const& credName,
std::string const& clientId,
std::string const& objectId,
std::string const& resourceId,
Core::Credentials::TokenCredentialOptions const& options)
{
return AppServiceManagedIdentitySource::Create<AppServiceV2017ManagedIdentitySource>(
credName, clientId, objectId, resourceId, options, "MSI_ENDPOINT", "MSI_SECRET", "2017");
}
std::unique_ptr<ManagedIdentitySource> AppServiceV2019ManagedIdentitySource::Create(
std::string const& credName,
std::string const& clientId,
std::string const& objectId,
std::string const& resourceId,
Core::Credentials::TokenCredentialOptions const& options)
{
return AppServiceManagedIdentitySource::Create<AppServiceV2019ManagedIdentitySource>(
credName,
clientId,
objectId,
resourceId,
options,
"IDENTITY_ENDPOINT",
"IDENTITY_HEADER",
"2019");
}
std::unique_ptr<ManagedIdentitySource> CloudShellManagedIdentitySource::Create(
std::string const& credName,
std::string const& clientId,
std::string const& objectId,
std::string const& resourceId,
Azure::Core::Credentials::TokenCredentialOptions const& options)
{
using Azure::Core::Credentials::AuthenticationException;
constexpr auto EndpointVarName = "MSI_ENDPOINT";
auto msiEndpoint = Environment::GetVariable(EndpointVarName);
std::string const CredSource = "Cloud Shell";
if (!msiEndpoint.empty())
{
if (!clientId.empty() || !objectId.empty() || !resourceId.empty())
{
throw AuthenticationException(
"User-assigned managed identities are not supported in Cloud Shell environments. Omit "
"the clientId, objectId, or resourceId when constructing the ManagedIdentityCredential.");
}
return std::unique_ptr<ManagedIdentitySource>(new CloudShellManagedIdentitySource(
clientId, options, ParseEndpointUrl(credName, msiEndpoint, EndpointVarName, CredSource)));
}
PrintEnvNotSetUpMessage(credName, CredSource);
return nullptr;
}
CloudShellManagedIdentitySource::CloudShellManagedIdentitySource(
std::string const& clientId,
Azure::Core::Credentials::TokenCredentialOptions const& options,
Azure::Core::Url endpointUrl)
: ManagedIdentitySource(clientId, endpointUrl.GetHost(), options), m_url(std::move(endpointUrl))
{
}
Azure::Core::Credentials::AccessToken CloudShellManagedIdentitySource::GetToken(
Azure::Core::Credentials::TokenRequestContext const& tokenRequestContext,
Azure::Core::Context const& context) const
{
std::string scopesStr;
{
auto const& scopes = tokenRequestContext.Scopes;
if (!scopes.empty())
{
scopesStr = TokenCredentialImpl::FormatScopes(scopes, true);
}
}
// TokenCache::GetToken() and TokenCredentialImpl::GetToken() can only use the lambda argument
// when they are being executed. They are not supposed to keep a reference to lambda argument to
// call it later. Therefore, any capture made here will outlive the possible time frame when the
// lambda might get called.
return m_tokenCache.GetToken(scopesStr, {}, tokenRequestContext.MinimumExpiration, [&]() {
return TokenCredentialImpl::GetToken(context, true, [&]() {
using Azure::Core::Url;
using Azure::Core::Http::HttpMethod;
std::string resource;
if (!scopesStr.empty())
{
resource = "resource=" + scopesStr;
}
auto request = std::make_unique<TokenRequest>(HttpMethod::Post, m_url, resource);
request->HttpRequest.SetHeader("Metadata", "true");
return request;
});
});
}
std::unique_ptr<ManagedIdentitySource> AzureArcManagedIdentitySource::Create(
std::string const& credName,
std::string const& clientId,
std::string const& objectId,
std::string const& resourceId,
Azure::Core::Credentials::TokenCredentialOptions const& options)
{
using Azure::Core::Credentials::AuthenticationException;
constexpr auto EndpointVarName = "IDENTITY_ENDPOINT";
auto identityEndpoint = Environment::GetVariable(EndpointVarName);
std::string const credSource = "Azure Arc";
if (identityEndpoint.empty() || Environment::GetVariable("IMDS_ENDPOINT").empty())
{
PrintEnvNotSetUpMessage(credName, credSource);
return nullptr;
}
if (!clientId.empty() || !objectId.empty() || !resourceId.empty())
{
throw AuthenticationException(
"User assigned identity is not supported by the Azure Arc Managed Identity Endpoint. "
"To authenticate with the system assigned identity, omit the client, object, or resource "
"ID when constructing the ManagedIdentityCredential.");
}
return std::unique_ptr<ManagedIdentitySource>(new AzureArcManagedIdentitySource(
options, ParseEndpointUrl(credName, identityEndpoint, EndpointVarName, credSource)));
}
AzureArcManagedIdentitySource::AzureArcManagedIdentitySource(
Azure::Core::Credentials::TokenCredentialOptions const& options,
Azure::Core::Url endpointUrl)
: ManagedIdentitySource(std::string(), endpointUrl.GetHost(), options),
m_url(std::move(endpointUrl))
{
m_url.AppendQueryParameter("api-version", "2019-11-01");
}
Azure::Core::Credentials::AccessToken AzureArcManagedIdentitySource::GetToken(
Azure::Core::Credentials::TokenRequestContext const& tokenRequestContext,
Azure::Core::Context const& context) const
{
std::string scopesStr;
{
auto const& scopes = tokenRequestContext.Scopes;
if (!scopes.empty())
{
scopesStr = TokenCredentialImpl::FormatScopes(scopes, true);
}
}
auto const createRequest = [&]() {
using Azure::Core::Http::HttpMethod;
using Azure::Core::Http::Request;
auto request = std::make_unique<TokenRequest>(Request(HttpMethod::Get, m_url));
{
auto& httpRequest = request->HttpRequest;
httpRequest.SetHeader("Metadata", "true");
if (!scopesStr.empty())
{
httpRequest.GetUrl().AppendQueryParameter("resource", scopesStr);
}
}
return request;
};
// TokenCache::GetToken() and TokenCredentialImpl::GetToken() can only use the lambda argument
// when they are being executed. They are not supposed to keep a reference to lambda argument to
// call it later. Therefore, any capture made here will outlive the possible time frame when the
// lambda might get called.
return m_tokenCache.GetToken(scopesStr, {}, tokenRequestContext.MinimumExpiration, [&]() {
return TokenCredentialImpl::GetToken(
context,
true,
createRequest,
[&](auto const statusCode, auto const& response) -> std::unique_ptr<TokenRequest> {
using Core::Credentials::AuthenticationException;
using Core::Http::HttpStatusCode;
if (statusCode != HttpStatusCode::Unauthorized)
{
return nullptr;
}
auto const& headers = response.GetHeaders();
auto authHeader = headers.find("WWW-Authenticate");
if (authHeader == headers.end())
{
throw AuthenticationException(
"Did not receive expected 'WWW-Authenticate' header "
"in the response from Azure Arc Managed Identity Endpoint.");
}
constexpr auto ChallengeValueSeparator = '=';
auto const& challenge = authHeader->second;
auto eq = challenge.find(ChallengeValueSeparator);
if (eq == std::string::npos
|| challenge.find(ChallengeValueSeparator, eq + 1) != std::string::npos)
{
throw AuthenticationException(
"The 'WWW-Authenticate' header in the response from Azure Arc "
"Managed Identity Endpoint did not match the expected format.");
}
auto request = createRequest();
const std::string fileName = challenge.substr(eq + 1);
ValidateArcKeyFile(fileName);
std::ifstream secretFile(fileName);
request->HttpRequest.SetHeader(
"Authorization",
"Basic "
+ std::string(
std::istreambuf_iterator<char>(secretFile),
std::istreambuf_iterator<char>()));
return request;
});
});
}
std::unique_ptr<ManagedIdentitySource> ImdsManagedIdentitySource::Create(
std::string const& credName,
std::string const& clientId,
std::string const& objectId,
std::string const& resourceId,
Azure::Core::Credentials::TokenCredentialOptions const& options)
{
IdentityLog::Write(
IdentityLog::Level::Informational,
credName + " will be created" + WithSourceMessage("Azure Instance Metadata Service")
+ ".\nSuccessful creation does not guarantee further successful token retrieval.");
return std::unique_ptr<ManagedIdentitySource>(
new ImdsManagedIdentitySource(clientId, objectId, resourceId, options));
}
ImdsManagedIdentitySource::ImdsManagedIdentitySource(
std::string const& clientId,
std::string const& objectId,
std::string const& resourceId,
Azure::Core::Credentials::TokenCredentialOptions const& options)
: ManagedIdentitySource(clientId, std::string(), options),
m_request(Azure::Core::Http::HttpMethod::Get, Azure::Core::Url(ImdsEndpoint))
{
{
using Azure::Core::Url;
auto& url = m_request.GetUrl();
url.AppendQueryParameter("api-version", "2018-02-01");
// Only one of clientId, objectId, or resourceId will be set to a non-empty value.
// IMDS uses msi_res_id, and not mi_res_id:
// https://learn.microsoft.com/entra/identity/managed-identities-azure-resources/how-to-use-vm-token#get-a-token-using-http
if (!clientId.empty())
{
url.AppendQueryParameter("client_id", clientId);
}
else if (!objectId.empty())
{
url.AppendQueryParameter("object_id", objectId);
}
else if (!resourceId.empty())
{
url.AppendQueryParameter("msi_res_id", resourceId);
}
}
m_request.SetHeader("Metadata", "true");
}
Azure::Core::Credentials::AccessToken ImdsManagedIdentitySource::GetToken(
Azure::Core::Credentials::TokenRequestContext const& tokenRequestContext,
Azure::Core::Context const& context) const
{
std::string scopesStr;
{
auto const& scopes = tokenRequestContext.Scopes;
if (!scopes.empty())
{
scopesStr = TokenCredentialImpl::FormatScopes(scopes, true);
}
}
// TokenCache::GetToken() and TokenCredentialImpl::GetToken() can only use the lambda argument
// when they are being executed. They are not supposed to keep a reference to lambda argument to
// call it later. Therefore, any capture made here will outlive the possible time frame when the
// lambda might get called.
return m_tokenCache.GetToken(scopesStr, {}, tokenRequestContext.MinimumExpiration, [&]() {
return TokenCredentialImpl::GetToken(context, true, [&]() {
auto request = std::make_unique<TokenRequest>(m_request);
if (!scopesStr.empty())
{
request->HttpRequest.GetUrl().AppendQueryParameter("resource", scopesStr);
}
return request;
});
});
}