AWSNativeSDKInit.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <AWSNativeSDKInit/AWSNativeSDKInit.h>
  9. #include <AzCore/Module/Environment.h>
  10. #include <AzCore/Utils/Utils.h>
  11. #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK)
  12. #include <AWSNativeSDKInit/AWSLogSystemInterface.h>
  13. #include <aws/core/Aws.h>
  14. #include <aws/core/platform/Environment.h>
  15. #include <aws/core/utils/logging/DefaultLogSystem.h>
  16. #endif
  17. namespace AWSNativeSDKInit
  18. {
  19. static constexpr char AWS_EC2_METADATA_DISABLED[] = "AWS_EC2_METADATA_DISABLED";
  20. namespace Platform
  21. {
  22. #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK)
  23. void CustomizeSDKOptions(Aws::SDKOptions& options);
  24. void CustomizeShutdown();
  25. void CopyCaCertBundle();
  26. #endif
  27. } // namespace Platform
  28. const char* const InitializationManager::initializationManagerTag = "AWSNativeSDKInitializer";
  29. AZ::EnvironmentVariable<InitializationManager> InitializationManager::s_initManager = nullptr;
  30. InitializationManager::InitializationManager()
  31. {
  32. InitializeAwsApiInternal();
  33. }
  34. InitializationManager::~InitializationManager()
  35. {
  36. ShutdownAwsApiInternal();
  37. }
  38. void InitializationManager::InitAwsApi()
  39. {
  40. s_initManager = AZ::Environment::CreateVariable<InitializationManager>(initializationManagerTag);
  41. Platform::CopyCaCertBundle();
  42. }
  43. void InitializationManager::Shutdown()
  44. {
  45. s_initManager = nullptr;
  46. }
  47. bool InitializationManager::IsInitialized()
  48. {
  49. return s_initManager.IsConstructed();
  50. }
  51. void InitializationManager::InitializeAwsApiInternal()
  52. {
  53. #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK)
  54. Aws::Utils::Logging::LogLevel logLevel;
  55. #if defined(AZ_DEBUG_BUILD) || defined(AZ_PROFILE_BUILD)
  56. logLevel = Aws::Utils::Logging::LogLevel::Warn;
  57. #else
  58. logLevel = Aws::Utils::Logging::LogLevel::Error;
  59. #endif
  60. m_awsSDKOptions.loggingOptions.logLevel = logLevel;
  61. m_awsSDKOptions.loggingOptions.logger_create_fn = [logLevel]()
  62. {
  63. return Aws::MakeShared<AWSLogSystemInterface>("AWS", logLevel);
  64. };
  65. m_awsSDKOptions.memoryManagementOptions.memoryManager = &m_memoryManager;
  66. Platform::CustomizeSDKOptions(m_awsSDKOptions);
  67. Aws::InitAPI(m_awsSDKOptions);
  68. #endif // #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK)
  69. }
  70. bool InitializationManager::PreventAwsEC2MetadataCalls(bool force)
  71. {
  72. bool prevented = false;
  73. #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK)
  74. // Helper function to prevent calls to the Amazon EC2 instance metadata service (IMDS), unless environment var has been
  75. // configured. AWS C++ SDK may reach out to EC2 IMDS for region, config or credentials, but unless code is running on EC2
  76. // compute such calls will fail and waste network resources. Note: AWS C++ SDK explicitly only checks if lower case version of
  77. // AWS_EC2_METADATA_DISABLED == "true", otherwise it will enable the EC2 metadata service calls.
  78. const auto ec2MetadataEnvVar = Aws::Environment::GetEnv(AWS_EC2_METADATA_DISABLED);
  79. if (ec2MetadataEnvVar.empty() || force)
  80. {
  81. prevented = true;
  82. AZ::Utils::SetEnv(AWS_EC2_METADATA_DISABLED, "true", true);
  83. }
  84. else if (Aws::Utils::StringUtils::ToLower(ec2MetadataEnvVar.c_str()) == "true")
  85. {
  86. prevented = true;
  87. }
  88. #endif // #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK)
  89. return prevented;
  90. }
  91. void InitializationManager::ShutdownAwsApiInternal()
  92. {
  93. #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK)
  94. Aws::ShutdownAPI(m_awsSDKOptions);
  95. Platform::CustomizeShutdown();
  96. #endif // #if defined(PLATFORM_SUPPORTS_AWS_NATIVE_SDK)
  97. }
  98. } // namespace AWSNativeSDKInit