AWSDefaultCredentialHandlerTest.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Console/Console.h>
  9. #include <AWSCoreInternalBus.h>
  10. #include <Credential/AWSDefaultCredentialHandler.h>
  11. #include <TestFramework/AWSCoreFixture.h>
  12. #include <AzCore/Utils/Utils.h>
  13. #include <aws/core/platform/Environment.h>
  14. using namespace AWSCore;
  15. static constexpr char AWSDEFAULTCREDENTIALHANDLERTEST_ALLOC_TAG[] = "AWSDefaultCredentialHandlerTest";
  16. static constexpr char AWS_EC2_METADATA_DISABLED[] = "AWS_EC2_METADATA_DISABLED";
  17. static constexpr const char* AWS_ACCESS_KEY = "AWSACCESSKEY";
  18. static constexpr const char* AWS_SECRET_KEY = "AWSSECRETKEY";
  19. class EnvironmentAWSCredentialsProviderMock
  20. : public Aws::Auth::EnvironmentAWSCredentialsProvider
  21. {
  22. public:
  23. MOCK_METHOD0(GetAWSCredentials, Aws::Auth::AWSCredentials());
  24. };
  25. class ProfileConfigFileAWSCredentialsProviderMock
  26. : public Aws::Auth::ProfileConfigFileAWSCredentialsProvider
  27. {
  28. public:
  29. MOCK_METHOD0(GetAWSCredentials, Aws::Auth::AWSCredentials());
  30. };
  31. class InstanceProfileCredentialsProviderMock
  32. : public Aws::Auth::InstanceProfileCredentialsProvider
  33. {
  34. public:
  35. MOCK_METHOD0(GetAWSCredentials, Aws::Auth::AWSCredentials());
  36. };
  37. class AWSDefaultCredentialHandlerMock
  38. : public AWSDefaultCredentialHandler
  39. {
  40. public:
  41. void SetupMocks(
  42. std::shared_ptr<EnvironmentAWSCredentialsProviderMock> environmentCredentialsProviderMock,
  43. std::shared_ptr<ProfileConfigFileAWSCredentialsProviderMock> profileCredentialsProviderMock,
  44. std::shared_ptr<InstanceProfileCredentialsProviderMock> instanceProfileCredentialsProviderMock)
  45. {
  46. SetEnvironmentCredentialsProvider(environmentCredentialsProviderMock);
  47. SetProfileCredentialsProvider(profileCredentialsProviderMock);
  48. SetInstanceProfileCredentialProvider(instanceProfileCredentialsProviderMock);
  49. }
  50. };
  51. class AWSDefaultCredentialHandlerTest
  52. : public AWSCoreFixture
  53. , public AWSCoreInternalRequestBus::Handler
  54. {
  55. public:
  56. AWSDefaultCredentialHandlerTest() = default;
  57. ~AWSDefaultCredentialHandlerTest() override = default;
  58. void SetUp() override
  59. {
  60. AWSCoreFixture::SetUpFixture();
  61. AWSCoreInternalRequestBus::Handler::BusConnect();
  62. m_environmentCredentialsProviderMock = Aws::MakeShared<EnvironmentAWSCredentialsProviderMock>(AWSDEFAULTCREDENTIALHANDLERTEST_ALLOC_TAG);
  63. m_profileCredentialsProviderMock = Aws::MakeShared<ProfileConfigFileAWSCredentialsProviderMock>(AWSDEFAULTCREDENTIALHANDLERTEST_ALLOC_TAG);
  64. m_instanceProfileCredentialsProviderMock = Aws::MakeShared<InstanceProfileCredentialsProviderMock>(AWSDEFAULTCREDENTIALHANDLERTEST_ALLOC_TAG);
  65. m_credentialHandler = AZStd::make_unique<AWSDefaultCredentialHandlerMock>();
  66. m_credentialHandler->ActivateHandler();
  67. m_credentialHandler->SetupMocks(m_environmentCredentialsProviderMock, m_profileCredentialsProviderMock, m_instanceProfileCredentialsProviderMock);
  68. }
  69. void TearDown() override
  70. {
  71. m_credentialHandler->DeactivateHandler();
  72. m_credentialHandler.reset();
  73. m_allowAWSMetadataCredentials = false;
  74. m_profileCredentialsProviderMock.reset();
  75. m_environmentCredentialsProviderMock.reset();
  76. m_instanceProfileCredentialsProviderMock.reset();
  77. AWSCoreInternalRequestBus::Handler::BusDisconnect();
  78. AWSCoreFixture::TearDownFixture();
  79. }
  80. // AWSCoreInternalRequestBus interface implementation
  81. AZStd::string GetProfileName() const override { return m_profileName; }
  82. AZStd::string GetResourceMappingConfigFilePath() const override { return ""; }
  83. bool IsAllowedAWSMetadataCredentials() const override { return m_allowAWSMetadataCredentials; }
  84. void ReloadConfiguration() override {}
  85. std::shared_ptr<EnvironmentAWSCredentialsProviderMock> m_environmentCredentialsProviderMock;
  86. std::shared_ptr<ProfileConfigFileAWSCredentialsProviderMock> m_profileCredentialsProviderMock;
  87. std::shared_ptr<InstanceProfileCredentialsProviderMock> m_instanceProfileCredentialsProviderMock;
  88. AZStd::unique_ptr<AWSDefaultCredentialHandlerMock> m_credentialHandler;
  89. AZStd::string m_profileName;
  90. bool m_allowAWSMetadataCredentials{ false };
  91. };
  92. TEST_F(AWSDefaultCredentialHandlerTest, GetCredentialsProvider_EnvironmentCredentialProviderReturnsNonEmptyCredentials_GetExpectedCredentialProvider)
  93. {
  94. Aws::Auth::AWSCredentials nonEmptyCredential(AWS_ACCESS_KEY, AWS_SECRET_KEY);
  95. EXPECT_CALL(*m_environmentCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(nonEmptyCredential));
  96. EXPECT_CALL(*m_profileCredentialsProviderMock, GetAWSCredentials()).Times(0);
  97. EXPECT_CALL(*m_instanceProfileCredentialsProviderMock, GetAWSCredentials()).Times(0);
  98. auto credentialProvider = m_credentialHandler->GetCredentialsProvider();
  99. EXPECT_TRUE(credentialProvider == m_environmentCredentialsProviderMock);
  100. }
  101. TEST_F(AWSDefaultCredentialHandlerTest, GetCredentialsProvider_ProfileCredentialProviderReturnsNonEmptyCredentials_GetExpectedCredentialProvider)
  102. {
  103. Aws::Auth::AWSCredentials emptyCredential;
  104. Aws::Auth::AWSCredentials nonEmptyCredential(AWS_ACCESS_KEY, AWS_SECRET_KEY);
  105. EXPECT_CALL(*m_environmentCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(emptyCredential));
  106. EXPECT_CALL(*m_profileCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(nonEmptyCredential));
  107. EXPECT_CALL(*m_instanceProfileCredentialsProviderMock, GetAWSCredentials()).Times(0);
  108. auto credentialProvider = m_credentialHandler->GetCredentialsProvider();
  109. EXPECT_TRUE(credentialProvider == m_profileCredentialsProviderMock);
  110. }
  111. TEST_F(AWSDefaultCredentialHandlerTest, GetCredentialsProvider_ProfileNameHasBeenChanged_GetDifferentCredentialProvider)
  112. {
  113. Aws::Auth::AWSCredentials emptyCredential;
  114. EXPECT_CALL(*m_environmentCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(emptyCredential));
  115. EXPECT_CALL(*m_profileCredentialsProviderMock, GetAWSCredentials()).Times(0);
  116. m_profileName = "dummyProfile";
  117. auto credentialProvider = m_credentialHandler->GetCredentialsProvider();
  118. EXPECT_TRUE(credentialProvider != m_profileCredentialsProviderMock);
  119. }
  120. TEST_F(AWSDefaultCredentialHandlerTest, GetCredentialsProvider_NoCredentialFoundInChain_GetNullPointer)
  121. {
  122. Aws::Auth::AWSCredentials emptyCredential;
  123. EXPECT_CALL(*m_environmentCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(emptyCredential));
  124. EXPECT_CALL(*m_profileCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(emptyCredential));
  125. EXPECT_CALL(*m_instanceProfileCredentialsProviderMock, GetAWSCredentials()).Times(0);
  126. auto credentialProvider = m_credentialHandler->GetCredentialsProvider();
  127. EXPECT_FALSE(credentialProvider);
  128. }
  129. TEST_F(AWSDefaultCredentialHandlerTest, GetCredentialHandlerOrder_Call_AlwaysGetExpectedValue)
  130. {
  131. auto actualOrder = m_credentialHandler->GetCredentialHandlerOrder();
  132. EXPECT_EQ(actualOrder, CredentialHandlerOrder::DEFAULT_CREDENTIAL_HANDLER);
  133. }
  134. TEST_F(AWSDefaultCredentialHandlerTest,
  135. GetCredentialsProvider_AllowAWSMetadataCredentials_InstanceProfileReturnsNonEmptyCredentials_GetExpectedCredentialProvider)
  136. {
  137. Aws::Auth::AWSCredentials emptyCredential;
  138. Aws::Auth::AWSCredentials nonEmptyCredential(AWS_ACCESS_KEY, AWS_SECRET_KEY);
  139. EXPECT_CALL(*m_environmentCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(emptyCredential));
  140. EXPECT_CALL(*m_profileCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(emptyCredential));
  141. EXPECT_CALL(*m_instanceProfileCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(nonEmptyCredential));
  142. m_allowAWSMetadataCredentials = true;
  143. auto credentialProvider = m_credentialHandler->GetCredentialsProvider();
  144. EXPECT_TRUE(credentialProvider == m_instanceProfileCredentialsProviderMock);
  145. }
  146. TEST_F(
  147. AWSDefaultCredentialHandlerTest,
  148. GetCredentialsProvider_AllowAWSMetadataCredentials_InstanceMetadataNonTrueValue_GetExpectedCredentialProvider)
  149. {
  150. // save current value so we can restore it after the test
  151. const auto currentEc2MetadataDisabledValue = Aws::Environment::GetEnv(AWS_EC2_METADATA_DISABLED);
  152. // set it
  153. const auto nonTrueValue = "thisValueIsNotTheWordTrue";
  154. AZ::Utils::SetEnv(AWS_EC2_METADATA_DISABLED, nonTrueValue, 1);
  155. Aws::Auth::AWSCredentials emptyCredential;
  156. Aws::Auth::AWSCredentials nonEmptyCredential(AWS_ACCESS_KEY, AWS_SECRET_KEY);
  157. EXPECT_CALL(*m_environmentCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(emptyCredential));
  158. EXPECT_CALL(*m_profileCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(emptyCredential));
  159. EXPECT_CALL(*m_instanceProfileCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(nonEmptyCredential));
  160. m_allowAWSMetadataCredentials = true;
  161. auto credentialProvider = m_credentialHandler->GetCredentialsProvider();
  162. EXPECT_TRUE(credentialProvider == m_instanceProfileCredentialsProviderMock);
  163. // restore previous value
  164. AZ::Utils::SetEnv(AWS_EC2_METADATA_DISABLED, currentEc2MetadataDisabledValue.c_str(), 1);
  165. }
  166. TEST_F(
  167. AWSDefaultCredentialHandlerTest,
  168. GetCredentialsProvider_AllowAWSMetadataCredentials_InstanceMetadataDisabled_GetDifferentCredentialProvider)
  169. {
  170. // save current value so we can restore it after the test
  171. const auto currentEc2MetadataDisabledValue = Aws::Environment::GetEnv(AWS_EC2_METADATA_DISABLED);
  172. // set it
  173. const auto caseInsensitiveTrue = "TruE";
  174. AZ::Utils::SetEnv(AWS_EC2_METADATA_DISABLED, caseInsensitiveTrue, 1);
  175. Aws::Auth::AWSCredentials emptyCredential;
  176. EXPECT_CALL(*m_environmentCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(emptyCredential));
  177. EXPECT_CALL(*m_profileCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(emptyCredential));
  178. EXPECT_CALL(*m_instanceProfileCredentialsProviderMock, GetAWSCredentials()).Times(0);
  179. m_allowAWSMetadataCredentials = true;
  180. auto credentialProvider = m_credentialHandler->GetCredentialsProvider();
  181. EXPECT_TRUE(credentialProvider != m_instanceProfileCredentialsProviderMock);
  182. // restore previous value
  183. AZ::Utils::SetEnv(AWS_EC2_METADATA_DISABLED, currentEc2MetadataDisabledValue.c_str(), 1);
  184. }
  185. TEST_F(AWSDefaultCredentialHandlerTest, GetCredentialsProvider_AllowAWSMetadataCredentials_NoCredentialFoundInChain_GetNullPointer)
  186. {
  187. Aws::Auth::AWSCredentials emptyCredential;
  188. EXPECT_CALL(*m_environmentCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(emptyCredential));
  189. EXPECT_CALL(*m_profileCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(emptyCredential));
  190. EXPECT_CALL(*m_instanceProfileCredentialsProviderMock, GetAWSCredentials()).Times(1).WillOnce(::testing::Return(emptyCredential));
  191. m_allowAWSMetadataCredentials = true;
  192. auto credentialProvider = m_credentialHandler->GetCredentialsProvider();
  193. EXPECT_FALSE(credentialProvider);
  194. }