Skip to content

Commit 35b7c3a

Browse files
author
cai lin
committed
new configuration support for IMDSConfig
Add ClientConfiguration support for IMDS settings and update related classes Fix: Shared pointer bug in AWSCredentialsProvider allocator mismatch bug fix, change type using c_str() new configuration support for IMDSConfig Add ClientConfiguration support for IMDS settings and update related classes Fix: Shared pointer bug in AWSCredentialsProvider allocator mismatch bug fix, change type using c_str() Fix shared pointer bug and update IMDS config structure - Fix std::stol compilation error with Aws::String by using .c_str() - Update IMDS configuration to use credentialProviderConfig.imdsConfig structure - Add proper environment variable support for AWS_METADATA_SERVICE_TIMEOUT and AWS_METADATA_SERVICE_NUM_ATTEMPTS Update IMDS configuration and credentials provider
1 parent a4a9baf commit 35b7c3a

File tree

6 files changed

+92
-3
lines changed

6 files changed

+92
-3
lines changed

src/aws-cpp-sdk-core/include/aws/core/auth/AWSCredentialsProvider.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
namespace Aws
2424
{
25+
namespace Client
26+
{
27+
struct ClientConfiguration;
28+
}
2529
namespace Auth
2630
{
2731
constexpr int REFRESH_THRESHOLD = 1000 * 60 * 5;
@@ -212,6 +216,11 @@ namespace Aws
212216
*/
213217
InstanceProfileCredentialsProvider(const std::shared_ptr<Aws::Config::EC2InstanceProfileConfigLoader>&, long refreshRateMs = REFRESH_THRESHOLD);
214218

219+
/**
220+
* Initializes the provider using ClientConfiguration for IMDS settings.
221+
*/
222+
InstanceProfileCredentialsProvider(const Aws::Client::ClientConfiguration& clientConfig, long refreshRateMs = REFRESH_THRESHOLD);
223+
215224
/**
216225
* Retrieves the credentials if found, otherwise returns empty credential set.
217226
*/

src/aws-cpp-sdk-core/include/aws/core/client/ClientConfiguration.h

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,22 @@ namespace Aws
440440
*/
441441
ResponseChecksumValidation responseChecksumValidation = ResponseChecksumValidation::WHEN_SUPPORTED;
442442
} checksumConfig;
443-
443+
444+
/**
445+
* IMDS configuration settings
446+
*/
447+
struct {
448+
/**
449+
* Number of total attempts to make when retrieving data from IMDS. Default 1.
450+
*/
451+
long metadata_service_num_attempts = 1;
452+
453+
/**
454+
* Timeout in seconds when retrieving data from IMDS. Default 1.
455+
*/
456+
long metadata_service_timeout = 1;
457+
} imdsConfig;
458+
444459
/**
445460
* A helper function to read config value from env variable or aws profile config
446461
*/
@@ -458,7 +473,7 @@ namespace Aws
458473
* Configuration that is specifically used for the windows http client
459474
*/
460475
struct WinHTTPOptions {
461-
/**
476+
/**`
462477
* Sets the windows http client to use WINHTTP_NO_CLIENT_CERT_CONTEXT when connecting
463478
* to a service, specifically only useful when disabling ssl verification and using
464479
* a different type of authentication.
@@ -492,6 +507,21 @@ namespace Aws
492507
* AWS profile name to use for credentials.
493508
*/
494509
Aws::String profile;
510+
511+
/**
512+
* IMDS configuration settings
513+
*/
514+
struct {
515+
/**
516+
* Number of total attempts to make when retrieving data from IMDS. Default 1.
517+
*/
518+
long metadataServiceNumAttempts = 1;
519+
520+
/**
521+
* Timeout in seconds when retrieving data from IMDS. Default 1.
522+
*/
523+
long metadataServiceTimeout = 1;
524+
} imdsConfig;
495525
}credentialProviderConfig;
496526
};
497527

src/aws-cpp-sdk-core/include/aws/core/config/EC2InstanceProfileConfigLoader.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ namespace Aws
1919
class EC2MetadataClient;
2020
}
2121

22+
namespace Client
23+
{
24+
struct ClientConfiguration;
25+
}
26+
2227
namespace Config
2328
{
2429
static const char* const INSTANCE_PROFILE_KEY = "InstanceProfile";
@@ -33,7 +38,12 @@ namespace Aws
3338
* If client is nullptr, the default EC2MetadataClient will be created.
3439
*/
3540
EC2InstanceProfileConfigLoader(const std::shared_ptr<Aws::Internal::EC2MetadataClient>& = nullptr);
36-
41+
42+
/**
43+
* Creates EC2MetadataClient using the provided ClientConfiguration.
44+
*/
45+
EC2InstanceProfileConfigLoader(const Aws::Client::ClientConfiguration& clientConfig);
46+
3747
virtual ~EC2InstanceProfileConfigLoader() = default;
3848

3949
protected:

src/aws-cpp-sdk-core/source/auth/AWSCredentialsProvider.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <aws/core/auth/AWSCredentialsProvider.h>
88

99
#include <aws/core/config/AWSProfileConfigLoader.h>
10+
#include <aws/core/client/ClientConfiguration.h>
1011
#include <aws/core/platform/Environment.h>
1112
#include <aws/core/platform/FileSystem.h>
1213
#include <aws/core/platform/OSVersionInfo.h>
@@ -242,6 +243,12 @@ InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider(const std
242243
AWS_LOGSTREAM_INFO(INSTANCE_LOG_TAG, "Creating Instance with injected EC2MetadataClient and refresh rate " << refreshRateMs);
243244
}
244245

246+
InstanceProfileCredentialsProvider::InstanceProfileCredentialsProvider(const Aws::Client::ClientConfiguration& clientConfig, long refreshRateMs) :
247+
m_ec2MetadataConfigLoader(Aws::MakeShared<Aws::Config::EC2InstanceProfileConfigLoader>(INSTANCE_LOG_TAG, clientConfig)),
248+
m_loadFrequencyMs(refreshRateMs)
249+
{
250+
AWS_LOGSTREAM_INFO(INSTANCE_LOG_TAG, "Creating Instance with IMDS timeout: " << clientConfig.credentialProviderConfig.imdsConfig.metadataServiceTimeout << "s, attempts: " << clientConfig.credentialProviderConfig.imdsConfig.metadataServiceNumAttempts);
251+
}
245252

246253
AWSCredentials InstanceProfileCredentialsProvider::GetAWSCredentials()
247254
{

src/aws-cpp-sdk-core/source/client/ClientConfiguration.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ static const char* DISABLE_IMDSV1_CONFIG_VAR = "AWS_EC2_METADATA_V1_DISABLED";
4141
static const char* DISABLE_IMDSV1_ENV_VAR = "ec2_metadata_v1_disabled";
4242
static const char* AWS_ACCOUNT_ID_ENDPOINT_MODE_ENVIRONMENT_VARIABLE = "AWS_ACCOUNT_ID_ENDPOINT_MODE";
4343
static const char* AWS_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_FILE_OPTION = "account_id_endpoint_mode";
44+
static const char* AWS_METADATA_SERVICE_TIMEOUT_ENV_VAR = "AWS_METADATA_SERVICE_TIMEOUT";
45+
static const char* AWS_METADATA_SERVICE_TIMEOUT_CONFIG_VAR = "metadata_service_timeout";
46+
static const char* AWS_METADATA_SERVICE_NUM_ATTEMPTS_ENV_VAR = "AWS_METADATA_SERVICE_NUM_ATTEMPTS";
47+
static const char* AWS_METADATA_SERVICE_NUM_ATTEMPTS_CONFIG_VAR = "metadata_service_num_attempts";
4448

4549
using RequestChecksumConfigurationEnumMapping = std::pair<const char*, RequestChecksumCalculation>;
4650
static const std::array<RequestChecksumConfigurationEnumMapping, 2> REQUEST_CHECKSUM_CONFIG_MAPPING = {{
@@ -288,6 +292,31 @@ void setConfigFromEnvOrProfile(ClientConfiguration &config)
288292
AWS_ACCOUNT_ID_ENDPOINT_MODE_CONFIG_FILE_OPTION,
289293
{"required", "disabled", "preferred"}, /* allowed values */
290294
"preferred" /* default value */);
295+
296+
// Load IMDS configuration from environment variables and config file
297+
Aws::String timeoutStr = ClientConfiguration::LoadConfigFromEnvOrProfile(AWS_METADATA_SERVICE_TIMEOUT_ENV_VAR,
298+
config.profileName,
299+
AWS_METADATA_SERVICE_TIMEOUT_CONFIG_VAR,
300+
{}, /* allowed values */
301+
"1" /* default value */);
302+
303+
// Load IMDS configuration from environment variables and config file
304+
Aws::String numAttemptsStr = ClientConfiguration::LoadConfigFromEnvOrProfile(AWS_METADATA_SERVICE_NUM_ATTEMPTS_ENV_VAR,
305+
config.profileName,
306+
AWS_METADATA_SERVICE_NUM_ATTEMPTS_CONFIG_VAR,
307+
{}, /* allowed values */
308+
"1" /* default value */);
309+
310+
// Parse and set IMDS num attempts
311+
long attempts = std::stol(numAttemptsStr.c_str());
312+
if (attempts >= 1) {
313+
config.credentialProviderConfig.imdsConfig.metadataServiceNumAttempts = attempts;
314+
}
315+
// Parse and set IMDS timeout
316+
long timeout = std::stol(timeoutStr.c_str());
317+
if (timeout >= 1) {
318+
config.credentialProviderConfig.imdsConfig.metadataServiceTimeout = timeout;
319+
}
291320
}
292321

293322
ClientConfiguration::ClientConfiguration()

src/aws-cpp-sdk-core/source/config/EC2InstanceProfileConfigLoader.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <aws/core/config/AWSProfileConfigLoader.h>
77
#include <aws/core/internal/AWSHttpResourceClient.h>
88
#include <aws/core/auth/AWSCredentialsProvider.h>
9+
#include <aws/core/client/ClientConfiguration.h>
910
#include <aws/core/utils/memory/stl/AWSList.h>
1011
#include <aws/core/utils/logging/LogMacros.h>
1112
#include <aws/core/utils/json/JsonSerializer.h>
@@ -37,6 +38,9 @@ namespace Aws
3738
m_ec2metadataClient = client;
3839
}
3940
}
41+
EC2InstanceProfileConfigLoader::EC2InstanceProfileConfigLoader(const Aws::Client::ClientConfiguration& clientConfig)
42+
: m_ec2metadataClient(Aws::MakeShared<Aws::Internal::EC2MetadataClient>(EC2_INSTANCE_PROFILE_LOG_TAG, clientConfig))
43+
{}
4044

4145
bool EC2InstanceProfileConfigLoader::LoadInternal()
4246
{

0 commit comments

Comments
 (0)