|
| 1 | +/** |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0. |
| 4 | + */ |
| 5 | + |
| 6 | +#include <aws/core/Aws.h> |
| 7 | +#include <aws/core/auth/AWSCredentialsProviderChain.h> |
| 8 | +#include <aws/core/platform/Environment.h> |
| 9 | +#include <aws/core/utils/FileSystemUtils.h> |
| 10 | +#include <aws/testing/AwsTestHelpers.h> |
| 11 | +#include <aws/testing/platform/PlatformTesting.h> |
| 12 | +#include <gtest/gtest.h> |
| 13 | + |
| 14 | +using namespace Aws; |
| 15 | +using namespace Aws::Auth; |
| 16 | +using namespace Aws::Environment; |
| 17 | +using namespace Aws::Utils; |
| 18 | + |
| 19 | +/** |
| 20 | + * Integration tests for DefaultAWSCredentialsProviderChain |
| 21 | + * These tests use real credentials from the environment (e.g., via ada) |
| 22 | + */ |
| 23 | +class DefaultCredentialsProviderChainIntegrationTest : public testing::Test |
| 24 | +{ |
| 25 | +protected: |
| 26 | + DefaultCredentialsProviderChainIntegrationTest() |
| 27 | + { |
| 28 | + m_options.loggingOptions.logLevel = Aws::Utils::Logging::LogLevel::Debug; |
| 29 | + InitAPI(m_options); |
| 30 | + } |
| 31 | + |
| 32 | + ~DefaultCredentialsProviderChainIntegrationTest() |
| 33 | + { |
| 34 | + ShutdownAPI(m_options); |
| 35 | + } |
| 36 | + |
| 37 | + SDKOptions m_options; |
| 38 | +}; |
| 39 | + |
| 40 | +TEST_F(DefaultCredentialsProviderChainIntegrationTest, ChainResolvesCredentialsFromEnvironment) |
| 41 | +{ |
| 42 | + // This test assumes AWS credentials are set via environment (e.g., ada) |
| 43 | + DefaultAWSCredentialsProviderChain chain; |
| 44 | + auto creds = chain.GetAWSCredentials(); |
| 45 | + |
| 46 | + // Should get valid credentials from environment |
| 47 | + EXPECT_FALSE(creds.IsEmpty()); |
| 48 | + EXPECT_FALSE(creds.GetAWSAccessKeyId().empty()); |
| 49 | + EXPECT_FALSE(creds.GetAWSSecretKey().empty()); |
| 50 | +} |
| 51 | + |
| 52 | +TEST_F(DefaultCredentialsProviderChainIntegrationTest, ChainCachesCredentials) |
| 53 | +{ |
| 54 | + DefaultAWSCredentialsProviderChain chain; |
| 55 | + |
| 56 | + // First call |
| 57 | + auto creds1 = chain.GetAWSCredentials(); |
| 58 | + EXPECT_FALSE(creds1.IsEmpty()); |
| 59 | + |
| 60 | + // Second call should return same cached credentials |
| 61 | + auto creds2 = chain.GetAWSCredentials(); |
| 62 | + EXPECT_FALSE(creds2.IsEmpty()); |
| 63 | + EXPECT_EQ(creds1.GetAWSAccessKeyId(), creds2.GetAWSAccessKeyId()); |
| 64 | + EXPECT_EQ(creds1.GetAWSSecretKey(), creds2.GetAWSSecretKey()); |
| 65 | +} |
| 66 | + |
| 67 | +TEST_F(DefaultCredentialsProviderChainIntegrationTest, ChainWithCustomProfile) |
| 68 | +{ |
| 69 | + // Create temporary credentials file with custom profile |
| 70 | + TempFile credsFile{std::ios_base::out | std::ios_base::trunc}; |
| 71 | + credsFile << "[custom-test-profile]\n"; |
| 72 | + credsFile << "aws_access_key_id = AKIAIOSFODNN7EXAMPLE\n"; |
| 73 | + credsFile << "aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY\n"; |
| 74 | + credsFile.close(); |
| 75 | + |
| 76 | + // Create temporary config file |
| 77 | + TempFile configFile{std::ios_base::out | std::ios_base::trunc}; |
| 78 | + configFile << "[profile custom-test-profile]\n"; |
| 79 | + configFile << "region = us-west-2\n"; |
| 80 | + configFile.close(); |
| 81 | + |
| 82 | + const EnvironmentRAII environmentRAII{{ |
| 83 | + {"AWS_SHARED_CREDENTIALS_FILE", credsFile.GetFileName()}, |
| 84 | + {"AWS_CONFIG_FILE", configFile.GetFileName()}, |
| 85 | + {"AWS_ACCESS_KEY_ID", ""}, |
| 86 | + {"AWS_SECRET_ACCESS_KEY", ""}, |
| 87 | + }}; |
| 88 | + |
| 89 | + Aws::Config::ReloadCachedConfigFile(); |
| 90 | + |
| 91 | + Client::ClientConfiguration::CredentialProviderConfiguration config; |
| 92 | + config.profile = "custom-test-profile"; |
| 93 | + DefaultAWSCredentialsProviderChain chain(config); |
| 94 | + |
| 95 | + auto creds = chain.GetAWSCredentials(); |
| 96 | + |
| 97 | + EXPECT_STREQ("AKIAIOSFODNN7EXAMPLE", creds.GetAWSAccessKeyId().c_str()); |
| 98 | + EXPECT_STREQ("wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", creds.GetAWSSecretKey().c_str()); |
| 99 | +} |
| 100 | + |
| 101 | +TEST_F(DefaultCredentialsProviderChainIntegrationTest, ChainWithProcessCredentials) |
| 102 | +{ |
| 103 | + // Create temporary config file with credential_process |
| 104 | + TempFile configFile{std::ios_base::out | std::ios_base::trunc}; |
| 105 | + configFile << "[default]\n"; |
| 106 | + configFile << "credential_process = echo '{\"Version\": 1, \"AccessKeyId\": \"AKIAPROCESSEXAMPLE\", \"SecretAccessKey\": \"ProcessSecretKeyExample\"}'\n"; |
| 107 | + configFile.close(); |
| 108 | + |
| 109 | + // Create empty credentials file to override ~/.aws/credentials |
| 110 | + TempFile credsFile{std::ios_base::out | std::ios_base::trunc}; |
| 111 | + credsFile << "[default]\n"; |
| 112 | + credsFile.close(); |
| 113 | + |
| 114 | + const EnvironmentRAII environmentRAII{{ |
| 115 | + {"AWS_CONFIG_FILE", configFile.GetFileName()}, |
| 116 | + {"AWS_SHARED_CREDENTIALS_FILE", credsFile.GetFileName()}, |
| 117 | + {"AWS_ACCESS_KEY_ID", ""}, |
| 118 | + {"AWS_SECRET_ACCESS_KEY", ""}, |
| 119 | + {"AWS_SESSION_TOKEN", ""}, |
| 120 | + {"AWS_EC2_METADATA_DISABLED", "true"}, |
| 121 | + }}; |
| 122 | + |
| 123 | + Aws::Config::ReloadCachedConfigFile(); |
| 124 | + |
| 125 | + DefaultAWSCredentialsProviderChain chain; |
| 126 | + auto creds = chain.GetAWSCredentials(); |
| 127 | + |
| 128 | + EXPECT_STREQ("AKIAPROCESSEXAMPLE", creds.GetAWSAccessKeyId().c_str()); |
| 129 | + EXPECT_STREQ("ProcessSecretKeyExample", creds.GetAWSSecretKey().c_str()); |
| 130 | +} |
| 131 | + |
| 132 | +TEST_F(DefaultCredentialsProviderChainIntegrationTest, ChainPrecedenceEnvironmentOverProfile) |
| 133 | +{ |
| 134 | + // Create temporary credentials file |
| 135 | + TempFile credsFile{std::ios_base::out | std::ios_base::trunc}; |
| 136 | + credsFile << "[default]\n"; |
| 137 | + credsFile << "aws_access_key_id = AKIAPROFILEEXAMPLE\n"; |
| 138 | + credsFile << "aws_secret_access_key = ProfileSecretKeyExample\n"; |
| 139 | + credsFile.close(); |
| 140 | + |
| 141 | + const EnvironmentRAII environmentRAII{{ |
| 142 | + {"AWS_SHARED_CREDENTIALS_FILE", credsFile.GetFileName()}, |
| 143 | + {"AWS_ACCESS_KEY_ID", "AKIAENVEXAMPLE"}, |
| 144 | + {"AWS_SECRET_ACCESS_KEY", "EnvSecretKeyExample"}, |
| 145 | + }}; |
| 146 | + |
| 147 | + Aws::Config::ReloadCachedConfigFile(); |
| 148 | + |
| 149 | + DefaultAWSCredentialsProviderChain chain; |
| 150 | + auto creds = chain.GetAWSCredentials(); |
| 151 | + |
| 152 | + // Environment should take precedence |
| 153 | + EXPECT_STREQ("AKIAENVEXAMPLE", creds.GetAWSAccessKeyId().c_str()); |
| 154 | + EXPECT_STREQ("EnvSecretKeyExample", creds.GetAWSSecretKey().c_str()); |
| 155 | +} |
| 156 | + |
| 157 | +TEST_F(DefaultCredentialsProviderChainIntegrationTest, ChainHandlesSessionToken) |
| 158 | +{ |
| 159 | + // Create temporary credentials file with session token |
| 160 | + TempFile credsFile{std::ios_base::out | std::ios_base::trunc}; |
| 161 | + credsFile << "[default]\n"; |
| 162 | + credsFile << "aws_access_key_id = ASIATEMPORARYEXAMPLE\n"; |
| 163 | + credsFile << "aws_secret_access_key = TempSecretKeyExample\n"; |
| 164 | + credsFile << "aws_session_token = TempSessionTokenExample123\n"; |
| 165 | + credsFile.close(); |
| 166 | + |
| 167 | + const EnvironmentRAII environmentRAII{{ |
| 168 | + {"AWS_SHARED_CREDENTIALS_FILE", credsFile.GetFileName()}, |
| 169 | + {"AWS_ACCESS_KEY_ID", ""}, |
| 170 | + {"AWS_SECRET_ACCESS_KEY", ""}, |
| 171 | + {"AWS_EC2_METADATA_DISABLED", "true"}, |
| 172 | + }}; |
| 173 | + |
| 174 | + Aws::Config::ReloadCachedConfigFile(); |
| 175 | + |
| 176 | + DefaultAWSCredentialsProviderChain chain; |
| 177 | + auto creds = chain.GetAWSCredentials(); |
| 178 | + |
| 179 | + EXPECT_STREQ("ASIATEMPORARYEXAMPLE", creds.GetAWSAccessKeyId().c_str()); |
| 180 | + EXPECT_STREQ("TempSecretKeyExample", creds.GetAWSSecretKey().c_str()); |
| 181 | + EXPECT_STREQ("TempSessionTokenExample123", creds.GetSessionToken().c_str()); |
| 182 | +} |
| 183 | + |
| 184 | +TEST_F(DefaultCredentialsProviderChainIntegrationTest, ChainReturnsEmptyWhenNoCredentialsAvailable) |
| 185 | +{ |
| 186 | + const EnvironmentRAII environmentRAII{{ |
| 187 | + {"AWS_ACCESS_KEY_ID", ""}, |
| 188 | + {"AWS_SECRET_ACCESS_KEY", ""}, |
| 189 | + {"AWS_SHARED_CREDENTIALS_FILE", "/nonexistent/credentials"}, |
| 190 | + {"AWS_CONFIG_FILE", "/nonexistent/config"}, |
| 191 | + {"AWS_EC2_METADATA_DISABLED", "true"}, |
| 192 | + }}; |
| 193 | + |
| 194 | + Aws::Config::ReloadCachedConfigFile(); |
| 195 | + |
| 196 | + DefaultAWSCredentialsProviderChain chain; |
| 197 | + auto creds = chain.GetAWSCredentials(); |
| 198 | + |
| 199 | + EXPECT_TRUE(creds.IsEmpty()); |
| 200 | +} |
0 commit comments