|
6 | 6 | #include <aws/core/config/AWSProfileConfigLoader.h>
|
7 | 7 | #include <aws/core/utils/FileSystemUtils.h>
|
8 | 8 | #include <aws/core/utils/memory/stl/AWSStreamFwd.h>
|
9 |
| -#include <aws/testing/mocks/aws/auth/MockAWSHttpResourceClient.h> |
10 | 9 | #include <fstream>
|
11 | 10 |
|
12 | 11 | using namespace Aws::Utils;
|
@@ -167,44 +166,137 @@ TEST(AWSConfigFileProfileConfigLoaderTest, TestCredentialsFileCorrupted)
|
167 | 166 | ASSERT_EQ(0u, loader.GetProfiles().size());
|
168 | 167 | }
|
169 | 168 |
|
170 |
| -static const char* const ALLOCATION_TAG = "EC2InstanceProfileConfigLoaderTest"; |
| 169 | +bool WriteConfigFileWithSSO(Aws::OStream& stream, const Aws::String& profileName, const Aws::String& ssoSessionName) { |
| 170 | + const Aws::String configFileContent = |
| 171 | + R"([ default] |
| 172 | + aws_access_key_id = ACCESS_KEY_0 |
| 173 | +aws_secret_access_key = SECRET_KEY_0 |
171 | 174 |
|
172 |
| -TEST(EC2InstanceProfileConfigLoaderTest, TestSuccesfullyHitsService) |
| 175 | +
|
| 176 | +[profile custom-profile ] |
| 177 | +aws_access_key_id = ACCESS_KEY_1 |
| 178 | +aws_secret_access_key = SECRET_KEY_1 |
| 179 | +[profile )" + profileName + R"(] |
| 180 | +sso_session = )" + ssoSessionName + R"( |
| 181 | +
|
| 182 | +[sso-session )" + ssoSessionName + R"(] |
| 183 | +sso_region = us-east-1 |
| 184 | +sso_start_url = https://d-abc123.awsapps.com/start)"; |
| 185 | + |
| 186 | + stream << configFileContent; |
| 187 | + stream.flush(); |
| 188 | + |
| 189 | + return stream.good(); |
| 190 | +} |
| 191 | + |
| 192 | +TEST(AWSConfigFileProfileConfigLoaderTest, TestConfigWithSSOParsing) |
173 | 193 | {
|
174 |
| - std::shared_ptr<MockEC2MetadataClient> mockClient = Aws::MakeShared<MockEC2MetadataClient>(ALLOCATION_TAG); |
175 |
| - mockClient->SetCurrentRegionValue("us-east-1"); |
176 |
| - mockClient->SetMockedCredentialsValue("{ \"AccessKeyId\": \"goodAccessKey\", \"SecretAccessKey\": \"goodSecretKey\", \"Token\": \"goodToken\" }"); |
| 194 | + TempFile configFile(std::ios_base::out | std::ios_base::trunc); |
177 | 195 |
|
178 |
| - EC2InstanceProfileConfigLoader loader(mockClient); |
| 196 | + ASSERT_TRUE(configFile.good()); |
| 197 | + static const Aws::String SSO_AWS_PROFILE = "AwsSdkBearerIntegrationTest-profile"; // arbitrary |
| 198 | + Aws::String profileFileName = configFile.GetFileName().find_last_of(R"(/\)") == std::string::npos ? |
| 199 | + configFile.GetFileName() : configFile.GetFileName().substr(configFile.GetFileName().find_last_of(R"(/\)")); |
| 200 | + static const Aws::String SSO_SESSION_NAME = profileFileName + "-sso-session"; // arbitrary |
| 201 | + ASSERT_TRUE(WriteConfigFileWithSSO(configFile, SSO_AWS_PROFILE, SSO_SESSION_NAME)); |
| 202 | + |
| 203 | + AWSConfigFileProfileConfigLoader loader(configFile.GetFileName()); |
179 | 204 | ASSERT_TRUE(loader.Load());
|
180 |
| - ASSERT_EQ(1u, loader.GetProfiles().size()); |
181 | 205 | auto profiles = loader.GetProfiles();
|
182 |
| - ASSERT_NE(profiles.end(), profiles.find(Aws::Config::INSTANCE_PROFILE_KEY)); |
183 |
| - auto creds = profiles[Aws::Config::INSTANCE_PROFILE_KEY].GetCredentials(); |
184 |
| - ASSERT_STREQ("goodAccessKey", creds.GetAWSAccessKeyId().c_str()); |
185 |
| - ASSERT_STREQ("goodSecretKey", creds.GetAWSSecretKey().c_str()); |
186 |
| - ASSERT_STREQ("goodToken", creds.GetSessionToken().c_str()); |
187 |
| - ASSERT_STREQ("us-east-1", profiles[Aws::Config::INSTANCE_PROFILE_KEY].GetRegion().c_str()); |
| 206 | + |
| 207 | + ASSERT_EQ(3u, profiles.size()); |
| 208 | + ASSERT_NE(profiles.end(), profiles.find("default")); |
| 209 | + ASSERT_NE(profiles.end(), profiles.find("custom-profile")); |
| 210 | + ASSERT_NE(profiles.end(), profiles.find(SSO_AWS_PROFILE)); |
| 211 | + |
| 212 | + const auto ssoProfile = profiles.at(SSO_AWS_PROFILE); |
| 213 | + ASSERT_EQ(SSO_AWS_PROFILE, ssoProfile.GetName()); |
| 214 | + ASSERT_TRUE(ssoProfile.GetRegion().empty()); |
| 215 | + ASSERT_TRUE(ssoProfile.GetCredentials().GetAWSSecretKey().empty() && |
| 216 | + ssoProfile.GetCredentials().GetAWSAccessKeyId().empty() && |
| 217 | + ssoProfile.GetCredentials().GetSessionToken().empty()); |
| 218 | + ASSERT_TRUE(ssoProfile.GetSourceProfile().empty()); |
| 219 | + ASSERT_TRUE(ssoProfile.GetCredentialProcess().empty()); |
| 220 | + |
| 221 | + // Important: sso_session pointing to a sso-session section is a different entity than sso_* properties under [profile] section |
| 222 | + ASSERT_TRUE(ssoProfile.GetSsoStartUrl().empty() && |
| 223 | + ssoProfile.GetSsoRegion().empty() && |
| 224 | + ssoProfile.GetSsoAccountId().empty() && |
| 225 | + ssoProfile.GetSsoRoleName().empty()); |
| 226 | + |
| 227 | + ASSERT_TRUE(ssoProfile.GetDefaultsMode().empty()); |
| 228 | + |
| 229 | + // here is [sso-session] section name that is linked by [profile] by a property "sso_session=<SSO_SESSION_NAME>" under [profile] |
| 230 | + ASSERT_EQ(SSO_SESSION_NAME, ssoProfile.GetSsoSession().GetName()); |
| 231 | + ASSERT_EQ("us-east-1", ssoProfile.GetSsoSession().GetSsoRegion()); |
| 232 | + ASSERT_EQ("https://d-abc123.awsapps.com/start", ssoProfile.GetSsoSession().GetSsoStartUrl()); |
188 | 233 | }
|
189 | 234 |
|
190 |
| -TEST(EC2InstanceProfileConfigLoaderTest, TestFailsToHitService) |
| 235 | +TEST(AWSConfigFileProfileConfigLoaderTest, TestProfileDumping) |
191 | 236 | {
|
192 |
| - std::shared_ptr<MockEC2MetadataClient> mockClient = Aws::MakeShared<MockEC2MetadataClient>(ALLOCATION_TAG); |
193 |
| - mockClient->SetCurrentRegionValue(""); |
194 |
| - mockClient->SetMockedCredentialsValue(""); |
| 237 | + TempFile configFile(std::ios_base::out | std::ios_base::trunc); |
195 | 238 |
|
196 |
| - EC2InstanceProfileConfigLoader loader(mockClient); |
197 |
| - ASSERT_FALSE(loader.Load()); |
198 |
| - ASSERT_EQ(0u, loader.GetProfiles().size()); |
199 |
| -} |
| 239 | + ASSERT_TRUE(configFile.good()); |
| 240 | + static const Aws::String SSO_AWS_PROFILE = "AwsSdkBearerIntegrationTest-profile"; // arbitrary |
| 241 | + Aws::String profileFileName = configFile.GetFileName().find_last_of(R"(/\)") == std::string::npos ? |
| 242 | + configFile.GetFileName() : configFile.GetFileName().substr(configFile.GetFileName().find_last_of(R"(/\)")); |
| 243 | + static const Aws::String SSO_SESSION_NAME = profileFileName + "-sso-session"; // arbitrary |
| 244 | + ASSERT_TRUE(WriteConfigFileWithSSO(configFile, SSO_AWS_PROFILE, SSO_SESSION_NAME)); |
| 245 | + |
| 246 | + class TEST_HELPER_AWSConfigFileProfileConfigLoader : public AWSConfigFileProfileConfigLoader |
| 247 | + { |
| 248 | + public: |
| 249 | + TEST_HELPER_AWSConfigFileProfileConfigLoader(const Aws::String& fileName) |
| 250 | + : AWSConfigFileProfileConfigLoader(fileName, /*useProfilePrefix*/true) |
| 251 | + {} |
| 252 | + |
| 253 | + bool Public_PersistInternal(const Aws::Map<Aws::String, Aws::Config::Profile>& profiles) |
| 254 | + { |
| 255 | + return this->PersistInternal(profiles); |
| 256 | + } |
| 257 | + }; |
| 258 | + |
| 259 | + // Parse static test profile config |
| 260 | + AWSConfigFileProfileConfigLoader loader(configFile.GetFileName()); |
| 261 | + ASSERT_TRUE(loader.Load()); |
| 262 | + auto initiallyReadProfiles = loader.GetProfiles(); |
200 | 263 |
|
201 |
| -TEST(EC2InstanceProfileConfigLoaderTest, TestBadJsonInResponse) |
202 |
| -{ |
203 |
| - std::shared_ptr<MockEC2MetadataClient> mockClient = Aws::MakeShared<MockEC2MetadataClient>(ALLOCATION_TAG); |
204 |
| - mockClient->SetCurrentRegionValue("us-east-1"); |
205 |
| - mockClient->SetMockedCredentialsValue("{ \"AccessKeyId\": \"goodAccessKey\","); |
| 264 | + // Dump parsed test profile config |
| 265 | + TempFile dumpedConfigFile(std::ios_base::out | std::ios_base::trunc); |
| 266 | + ASSERT_TRUE(dumpedConfigFile.good()); |
| 267 | + TEST_HELPER_AWSConfigFileProfileConfigLoader dumper(dumpedConfigFile.GetFileName()); |
| 268 | + dumper.Public_PersistInternal(initiallyReadProfiles); |
206 | 269 |
|
207 |
| - EC2InstanceProfileConfigLoader loader(mockClient); |
208 |
| - ASSERT_FALSE(loader.Load()); |
209 |
| - ASSERT_EQ(0u, loader.GetProfiles().size()); |
| 270 | + // Parse dumped test profile config |
| 271 | + AWSConfigFileProfileConfigLoader loaderOfDumped(dumpedConfigFile.GetFileName()); |
| 272 | + ASSERT_TRUE(loaderOfDumped.Load()); |
| 273 | + auto profiles = loaderOfDumped.GetProfiles(); |
| 274 | + |
| 275 | + // Repeat validation from a previous test |
| 276 | + ASSERT_EQ(3u, profiles.size()); |
| 277 | + ASSERT_NE(profiles.end(), profiles.find("default")); |
| 278 | + ASSERT_NE(profiles.end(), profiles.find("custom-profile")); |
| 279 | + ASSERT_NE(profiles.end(), profiles.find(SSO_AWS_PROFILE)); |
| 280 | + |
| 281 | + const auto ssoProfile = profiles.at(SSO_AWS_PROFILE); |
| 282 | + ASSERT_EQ(SSO_AWS_PROFILE, ssoProfile.GetName()); |
| 283 | + ASSERT_TRUE(ssoProfile.GetRegion().empty()); |
| 284 | + ASSERT_TRUE(ssoProfile.GetCredentials().GetAWSSecretKey().empty() && |
| 285 | + ssoProfile.GetCredentials().GetAWSAccessKeyId().empty() && |
| 286 | + ssoProfile.GetCredentials().GetSessionToken().empty()); |
| 287 | + ASSERT_TRUE(ssoProfile.GetSourceProfile().empty()); |
| 288 | + ASSERT_TRUE(ssoProfile.GetCredentialProcess().empty()); |
| 289 | + |
| 290 | + // Important: sso_session pointing to a sso-session section is a different entity than sso_* properties under [profile] section |
| 291 | + ASSERT_TRUE(ssoProfile.GetSsoStartUrl().empty() && |
| 292 | + ssoProfile.GetSsoRegion().empty() && |
| 293 | + ssoProfile.GetSsoAccountId().empty() && |
| 294 | + ssoProfile.GetSsoRoleName().empty()); |
| 295 | + |
| 296 | + ASSERT_TRUE(ssoProfile.GetDefaultsMode().empty()); |
| 297 | + |
| 298 | + // here is [sso-session] section name that is linked by [profile] by a property "sso_session=<SSO_SESSION_NAME>" under [profile] |
| 299 | + ASSERT_EQ(SSO_SESSION_NAME, ssoProfile.GetSsoSession().GetName()); |
| 300 | + ASSERT_EQ("us-east-1", ssoProfile.GetSsoSession().GetSsoRegion()); |
| 301 | + ASSERT_EQ("https://d-abc123.awsapps.com/start", ssoProfile.GetSsoSession().GetSsoStartUrl()); |
210 | 302 | }
|
0 commit comments