BuiltInParameters.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. * SPDX-License-Identifier: Apache-2.0.
  4. */
  5. #include <aws/core/endpoint/BuiltInParameters.h>
  6. #include <aws/core/utils/logging/LogMacros.h>
  7. static const char ENDPOINT_BUILTIN_LOG_TAG[] = "EndpointBuiltInParameters";
  8. namespace Aws
  9. {
  10. namespace Endpoint
  11. {
  12. void BuiltInParameters::OverrideEndpoint(const Aws::String& endpoint, const Aws::Http::Scheme& scheme)
  13. {
  14. static const char* SDK_ENDPOINT = "Endpoint";
  15. if (endpoint.compare(0, 7, "http://") == 0 || endpoint.compare(0, 8, "https://") == 0)
  16. {
  17. SetStringParameter(SDK_ENDPOINT, endpoint);
  18. }
  19. else
  20. {
  21. SetStringParameter(SDK_ENDPOINT, Aws::String(Aws::Http::SchemeMapper::ToString(scheme)) + "://" + endpoint);
  22. }
  23. }
  24. bool StringEndsWith(const Aws::String& str, const Aws::String& suffix)
  25. {
  26. if (suffix.size() > str.size())
  27. return false;
  28. return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
  29. }
  30. void BuiltInParameters::SetFromClientConfiguration(const Client::ClientConfiguration& config)
  31. {
  32. bool forceFIPS = false;
  33. static const char* AWS_REGION = "Region";
  34. if (!config.region.empty()) {
  35. static const char* FIPS_PREFIX = "fips-";
  36. static const char* FIPS_SUFFIX = "-fips";
  37. if (config.region.rfind(FIPS_PREFIX, 0) == 0) {
  38. // Backward compatibility layer for code hacking previous SDK version
  39. Aws::String regionOverride = config.region.substr(sizeof(FIPS_PREFIX) - 1);
  40. forceFIPS = true;
  41. SetStringParameter(AWS_REGION, regionOverride);
  42. } else if (StringEndsWith(config.region, FIPS_SUFFIX)) {
  43. Aws::String regionOverride = config.region.substr(0, config.region.size() - sizeof(FIPS_SUFFIX) - 1);
  44. forceFIPS = true;
  45. SetStringParameter(AWS_REGION, regionOverride);
  46. } else {
  47. SetStringParameter(AWS_REGION, config.region);
  48. }
  49. }
  50. static const char* AWS_USE_FIPS = "UseFIPS";
  51. SetBooleanParameter(AWS_USE_FIPS, config.useFIPS || forceFIPS);
  52. static const char* AWS_USE_DUAL_STACK = "UseDualStack";
  53. SetBooleanParameter(AWS_USE_DUAL_STACK, config.useDualStack);
  54. if (!config.endpointOverride.empty()) {
  55. OverrideEndpoint(config.endpointOverride, config.scheme);
  56. if (config.region.empty()) {
  57. AWS_LOGSTREAM_WARN(ENDPOINT_BUILTIN_LOG_TAG,
  58. "Endpoint is overridden but region is not set. "
  59. "Region is required my many endpoint rule sets to resolve the endpoint. "
  60. "And it is required to compute an aws signature.");
  61. SetStringParameter(AWS_REGION, "region-not-set"); // dummy endpoint resolution parameter
  62. }
  63. }
  64. }
  65. void BuiltInParameters::SetFromClientConfiguration(const Client::GenericClientConfiguration<false>& config)
  66. {
  67. return SetFromClientConfiguration(static_cast<const Client::ClientConfiguration&>(config));
  68. }
  69. void BuiltInParameters::SetFromClientConfiguration(const Client::GenericClientConfiguration<true>& config)
  70. {
  71. SetFromClientConfiguration(static_cast<const Client::ClientConfiguration&>(config));
  72. }
  73. const BuiltInParameters::EndpointParameter& BuiltInParameters::GetParameter(const Aws::String& name) const
  74. {
  75. const auto foundIt = std::find_if(m_params.begin(), m_params.end(),
  76. [name](const BuiltInParameters::EndpointParameter& item)
  77. {
  78. return item.GetName() == name;
  79. });
  80. if (foundIt != m_params.end())
  81. {
  82. return *foundIt;
  83. }
  84. else
  85. {
  86. static const BuiltInParameters::EndpointParameter BUILTIN_NOT_FOUND_PARAMETER("PARAMETER_NOT_SET", false, EndpointParameter::ParameterOrigin::CLIENT_CONTEXT);
  87. return BUILTIN_NOT_FOUND_PARAMETER;
  88. }
  89. }
  90. void BuiltInParameters::SetParameter(EndpointParameter param)
  91. {
  92. const auto foundIt = std::find_if(m_params.begin(), m_params.end(),
  93. [param](const BuiltInParameters::EndpointParameter& item)
  94. {
  95. return item.GetName() == param.GetName();
  96. });
  97. if (foundIt != m_params.end())
  98. {
  99. m_params.erase(foundIt);
  100. }
  101. m_params.emplace_back(std::move(param));
  102. }
  103. void BuiltInParameters::SetStringParameter(Aws::String name, Aws::String value)
  104. {
  105. return SetParameter(EndpointParameter(std::move(name), std::move(value), EndpointParameter::ParameterOrigin::BUILT_IN));
  106. }
  107. void BuiltInParameters::SetBooleanParameter(Aws::String name, bool value)
  108. {
  109. return SetParameter(EndpointParameter(std::move(name), value, EndpointParameter::ParameterOrigin::BUILT_IN));
  110. }
  111. const Aws::Vector<BuiltInParameters::EndpointParameter>& BuiltInParameters::GetAllParameters() const
  112. {
  113. return m_params;
  114. }
  115. } // namespace Endpoint
  116. } // namespace Aws