ConfigAndCredentialsCacheManager.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/core/config/ConfigAndCredentialsCacheManager.h>
  6. #include <aws/core/auth/AWSCredentialsProvider.h>
  7. #include <aws/core/utils/memory/stl/AWSList.h>
  8. #include <aws/core/utils/json/JsonSerializer.h>
  9. #include <fstream>
  10. namespace Aws
  11. {
  12. namespace Config
  13. {
  14. using namespace Aws::Utils;
  15. using namespace Aws::Auth;
  16. #ifdef _MSC_VER
  17. // VS2015 compiler's bug, warning s_CoreErrorsMapper: symbol will be dynamically initialized (implementation limitation)
  18. AWS_SUPPRESS_WARNING(4592,
  19. static ConfigAndCredentialsCacheManager* s_configManager(nullptr);
  20. )
  21. #else
  22. static ConfigAndCredentialsCacheManager* s_configManager(nullptr);
  23. #endif
  24. static const char CONFIG_CREDENTIALS_CACHE_MANAGER_TAG[] = "ConfigAndCredentialsCacheManager";
  25. ConfigAndCredentialsCacheManager::ConfigAndCredentialsCacheManager() :
  26. m_credentialsFileLoader(Aws::Auth::ProfileConfigFileAWSCredentialsProvider::GetCredentialsProfileFilename()),
  27. m_configFileLoader(Aws::Auth::GetConfigProfileFilename(), true/*use profile prefix*/)
  28. {
  29. ReloadCredentialsFile();
  30. ReloadConfigFile();
  31. }
  32. void ConfigAndCredentialsCacheManager::ReloadConfigFile()
  33. {
  34. Aws::Utils::Threading::WriterLockGuard guard(m_configLock);
  35. m_configFileLoader.SetFileName(Aws::Auth::GetConfigProfileFilename());
  36. m_configFileLoader.Load();
  37. }
  38. void ConfigAndCredentialsCacheManager::ReloadCredentialsFile()
  39. {
  40. Aws::Utils::Threading::WriterLockGuard guard(m_credentialsLock);
  41. m_credentialsFileLoader.SetFileName(Aws::Auth::ProfileConfigFileAWSCredentialsProvider::GetCredentialsProfileFilename());
  42. m_credentialsFileLoader.Load();
  43. }
  44. bool ConfigAndCredentialsCacheManager::HasConfigProfile(const Aws::String& profileName) const
  45. {
  46. Aws::Utils::Threading::ReaderLockGuard guard(m_configLock);
  47. return (m_configFileLoader.GetProfiles().count(profileName) == 1);
  48. }
  49. Aws::Config::Profile ConfigAndCredentialsCacheManager::GetConfigProfile(const Aws::String& profileName) const
  50. {
  51. Aws::Utils::Threading::ReaderLockGuard guard(m_configLock);
  52. const auto& profiles = m_configFileLoader.GetProfiles();
  53. const auto &iter = profiles.find(profileName);
  54. if (iter == profiles.end())
  55. {
  56. return {};
  57. }
  58. return iter->second;
  59. }
  60. Aws::Map<Aws::String, Aws::Config::Profile> ConfigAndCredentialsCacheManager::GetConfigProfiles() const
  61. {
  62. Aws::Utils::Threading::ReaderLockGuard guard(m_configLock);
  63. return m_configFileLoader.GetProfiles();
  64. }
  65. Aws::String ConfigAndCredentialsCacheManager::GetConfig(const Aws::String& profileName, const Aws::String& key) const
  66. {
  67. Aws::Utils::Threading::ReaderLockGuard guard(m_configLock);
  68. const auto& profiles = m_configFileLoader.GetProfiles();
  69. const auto &iter = profiles.find(profileName);
  70. if (iter == profiles.end())
  71. {
  72. return {};
  73. }
  74. return iter->second.GetValue(key);
  75. }
  76. bool ConfigAndCredentialsCacheManager::HasCredentialsProfile(const Aws::String& profileName) const
  77. {
  78. Aws::Utils::Threading::ReaderLockGuard guard(m_credentialsLock);
  79. return (m_credentialsFileLoader.GetProfiles().count(profileName) == 1);
  80. }
  81. Aws::Config::Profile ConfigAndCredentialsCacheManager::GetCredentialsProfile(const Aws::String& profileName) const
  82. {
  83. Aws::Utils::Threading::ReaderLockGuard guard(m_credentialsLock);
  84. const auto &profiles = m_credentialsFileLoader.GetProfiles();
  85. const auto &iter = profiles.find(profileName);
  86. if (iter == profiles.end())
  87. {
  88. return {};
  89. }
  90. return iter->second;
  91. }
  92. Aws::Map<Aws::String, Aws::Config::Profile> ConfigAndCredentialsCacheManager::GetCredentialsProfiles() const
  93. {
  94. Aws::Utils::Threading::ReaderLockGuard guard(m_credentialsLock);
  95. return m_credentialsFileLoader.GetProfiles();
  96. }
  97. Aws::Auth::AWSCredentials ConfigAndCredentialsCacheManager::GetCredentials(const Aws::String& profileName) const
  98. {
  99. Aws::Utils::Threading::ReaderLockGuard guard(m_credentialsLock);
  100. const auto& profiles = m_credentialsFileLoader.GetProfiles();
  101. const auto &iter = profiles.find(profileName);
  102. if (iter == profiles.end())
  103. {
  104. return {};
  105. }
  106. return iter->second.GetCredentials();
  107. }
  108. void InitConfigAndCredentialsCacheManager()
  109. {
  110. if (s_configManager)
  111. {
  112. return;
  113. }
  114. s_configManager = Aws::New<ConfigAndCredentialsCacheManager>(CONFIG_CREDENTIALS_CACHE_MANAGER_TAG);
  115. }
  116. void CleanupConfigAndCredentialsCacheManager()
  117. {
  118. Aws::Delete(s_configManager);
  119. s_configManager = nullptr;
  120. }
  121. void ReloadCachedConfigFile()
  122. {
  123. assert(s_configManager);
  124. s_configManager->ReloadConfigFile();
  125. }
  126. void ReloadCachedCredentialsFile()
  127. {
  128. assert(s_configManager);
  129. s_configManager->ReloadCredentialsFile();
  130. }
  131. bool HasCachedConfigProfile(const Aws::String& profileName)
  132. {
  133. assert(s_configManager);
  134. return s_configManager->HasConfigProfile(profileName);
  135. }
  136. Aws::Config::Profile GetCachedConfigProfile(const Aws::String& profileName)
  137. {
  138. assert(s_configManager);
  139. return s_configManager->GetConfigProfile(profileName);
  140. }
  141. Aws::Map<Aws::String, Aws::Config::Profile> GetCachedConfigProfiles()
  142. {
  143. assert(s_configManager);
  144. return s_configManager->GetConfigProfiles();
  145. }
  146. Aws::String GetCachedConfigValue(const Aws::String &profileName, const Aws::String &key)
  147. {
  148. assert(s_configManager);
  149. return s_configManager->GetConfig(profileName, key);
  150. }
  151. Aws::String GetCachedConfigValue(const Aws::String &key)
  152. {
  153. assert(s_configManager);
  154. return s_configManager->GetConfig(Aws::Auth::GetConfigProfileName(), key);
  155. }
  156. bool HasCachedCredentialsProfile(const Aws::String& profileName)
  157. {
  158. assert(s_configManager);
  159. return s_configManager->HasCredentialsProfile(profileName);
  160. }
  161. Aws::Config::Profile GetCachedCredentialsProfile(const Aws::String &profileName)
  162. {
  163. assert(s_configManager);
  164. return s_configManager->GetCredentialsProfile(profileName);
  165. }
  166. Aws::Map<Aws::String, Aws::Config::Profile> GetCachedCredentialsProfiles()
  167. {
  168. assert(s_configManager);
  169. return s_configManager->GetCredentialsProfiles();
  170. }
  171. Aws::Auth::AWSCredentials GetCachedCredentials(const Aws::String &profileName)
  172. {
  173. assert(s_configManager);
  174. return s_configManager->GetCredentials(profileName);
  175. }
  176. } // Config namespace
  177. } // Aws namespace