aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/aws-sdk-cpp/aws-cpp-sdk-core/source/endpoint/BuiltInParameters.cpp
blob: 43c3e2f0f94d46e979e4b4b9463432ef30ab5597 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/core/endpoint/BuiltInParameters.h>
#include <aws/core/utils/logging/LogMacros.h>

static const char ENDPOINT_BUILTIN_LOG_TAG[] = "EndpointBuiltInParameters";

namespace Aws
{
namespace Endpoint
{
    void BuiltInParameters::OverrideEndpoint(const Aws::String& endpoint, const Aws::Http::Scheme& scheme)
    {
        static const char* SDK_ENDPOINT = "Endpoint";

        if (endpoint.compare(0, 7, "http://") == 0 || endpoint.compare(0, 8, "https://") == 0)
        {
            SetStringParameter(SDK_ENDPOINT, endpoint);
        }
        else
        {
            SetStringParameter(SDK_ENDPOINT, Aws::String(Aws::Http::SchemeMapper::ToString(scheme)) + "://" + endpoint);
        }
    }

    bool StringEndsWith(const Aws::String& str, const Aws::String& suffix)
    {
        if (suffix.size() > str.size())
            return false;
        return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
    }

    void BuiltInParameters::SetFromClientConfiguration(const Client::ClientConfiguration& config)
    {
        bool forceFIPS = false;
        static const char* AWS_REGION = "Region";
        if (!config.region.empty()) {
            static const char* FIPS_PREFIX = "fips-";
            static const char* FIPS_SUFFIX = "-fips";
            if (config.region.rfind(FIPS_PREFIX, 0) == 0) {
                // Backward compatibility layer for code hacking previous SDK version
                Aws::String regionOverride = config.region.substr(sizeof(FIPS_PREFIX) - 1);
                forceFIPS = true;
                SetStringParameter(AWS_REGION, regionOverride);
            } else if (StringEndsWith(config.region, FIPS_SUFFIX)) {
                Aws::String regionOverride = config.region.substr(0, config.region.size() - sizeof(FIPS_SUFFIX) - 1);
                forceFIPS = true;
                SetStringParameter(AWS_REGION, regionOverride);
            } else {
                SetStringParameter(AWS_REGION, config.region);
            }
        }

        static const char* AWS_USE_FIPS = "UseFIPS";
        SetBooleanParameter(AWS_USE_FIPS, config.useFIPS || forceFIPS);

        static const char* AWS_USE_DUAL_STACK = "UseDualStack";
        SetBooleanParameter(AWS_USE_DUAL_STACK, config.useDualStack);

        if (!config.endpointOverride.empty()) {
            OverrideEndpoint(config.endpointOverride, config.scheme);

            if (config.region.empty()) {
                AWS_LOGSTREAM_WARN(ENDPOINT_BUILTIN_LOG_TAG,
                                   "Endpoint is overridden but region is not set. "
                                   "Region is required my many endpoint rule sets to resolve the endpoint. "
                                   "And it is required to compute an aws signature.");
                SetStringParameter(AWS_REGION, "region-not-set"); // dummy endpoint resolution parameter
            }
        }
    }

    void BuiltInParameters::SetFromClientConfiguration(const Client::GenericClientConfiguration<false>& config)
    {
        return SetFromClientConfiguration(static_cast<const Client::ClientConfiguration&>(config));
    }

    void BuiltInParameters::SetFromClientConfiguration(const Client::GenericClientConfiguration<true>& config)
    {
        SetFromClientConfiguration(static_cast<const Client::ClientConfiguration&>(config));
    }

    const BuiltInParameters::EndpointParameter& BuiltInParameters::GetParameter(const Aws::String& name) const
    {
        const auto foundIt = std::find_if(m_params.begin(), m_params.end(),
                                          [name](const BuiltInParameters::EndpointParameter& item)
                                          {
                                              return item.GetName() == name;
                                          });

        if (foundIt != m_params.end())
        {
            return *foundIt;
        }
        else
        {
            static const BuiltInParameters::EndpointParameter BUILTIN_NOT_FOUND_PARAMETER("PARAMETER_NOT_SET", false, EndpointParameter::ParameterOrigin::CLIENT_CONTEXT);
            return BUILTIN_NOT_FOUND_PARAMETER;
        }
    }

    void BuiltInParameters::SetParameter(EndpointParameter param)
    {
        const auto foundIt = std::find_if(m_params.begin(), m_params.end(),
                                          [param](const BuiltInParameters::EndpointParameter& item)
                                          {
                                              return item.GetName() == param.GetName();
                                          });

        if (foundIt != m_params.end())
        {
            m_params.erase(foundIt);
        }
        m_params.emplace_back(std::move(param));
    }

    void BuiltInParameters::SetStringParameter(Aws::String name, Aws::String value)
    {
        return SetParameter(EndpointParameter(std::move(name), std::move(value), EndpointParameter::ParameterOrigin::BUILT_IN));
    }

    void BuiltInParameters::SetBooleanParameter(Aws::String name, bool value)
    {
        return SetParameter(EndpointParameter(std::move(name), value, EndpointParameter::ParameterOrigin::BUILT_IN));
    }

    const Aws::Vector<BuiltInParameters::EndpointParameter>& BuiltInParameters::GetAllParameters() const
    {
        return m_params;
    }
} // namespace Endpoint
} // namespace Aws