1+ /* *
2+ * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+ * SPDX-License-Identifier: Apache-2.0.
4+ */
5+
6+ #pragma once
7+
8+ #include < aws/core/Core_EXPORTS.h>
9+ #include < aws/core/utils/memory/stl/AWSString.h>
10+ #include < aws/core/config/AWSProfileConfig.h>
11+ #include < aws/core/config/ConfigAndCredentialsCacheManager.h>
12+ #include < aws/core/platform/Environment.h>
13+ #include < aws/core/utils/StringUtils.h>
14+ #include < aws/core/utils/logging/LogMacros.h>
15+
16+ namespace Aws
17+ {
18+ namespace Config
19+ {
20+ /* *
21+ * Resolver that sources endpoints and sets them on endpoint providers.
22+ */
23+ class AWS_CORE_API EndpointResolver
24+ {
25+ public:
26+ /* *
27+ * Sources endpoint and sets it on the endpoint provider.
28+ * This should be called after InitBuiltInParameters but before ResolveEndpoint.
29+ *
30+ * @param serviceId Service identifier (e.g., "s3", "Elastic Beanstalk")
31+ * @param profileName Profile name for shared config lookup
32+ * @param endpointProvider Endpoint provider to configure
33+ */
34+ template <typename EndpointProviderT>
35+ static void EndpointSource (const Aws::String& serviceId,
36+ const Aws::String& profileName,
37+ EndpointProviderT& endpointProvider)
38+ {
39+ const Aws::String serviceKey = ToEnvSuffix (serviceId);
40+ static const char * LOG_TAG = " EndpointResolver" ;
41+
42+ // 1) Check ignore flag from environment variable
43+ Aws::String ignoreEnv = Aws::Environment::GetEnv (" AWS_IGNORE_CONFIGURED_ENDPOINT_URLS" );
44+ if (!ignoreEnv.empty () && Utils::StringUtils::ConvertToBool (ignoreEnv.c_str ())) {
45+ AWS_LOGSTREAM_DEBUG (LOG_TAG, " Configured endpoints ignored due to AWS_IGNORE_CONFIGURED_ENDPOINT_URLS=true" );
46+ return ;
47+ }
48+
49+ // 1b) Check ignore flag from profile (early check)
50+ if (!profileName.empty () && HasCachedConfigProfile (profileName)) {
51+ Profile profile = GetCachedConfigProfile (profileName);
52+ const Aws::String ignoreVal = profile.GetValue (" ignore_configured_endpoint_urls" );
53+ if (!ignoreVal.empty () && Utils::StringUtils::ConvertToBool (ignoreVal.c_str ())) {
54+ AWS_LOGSTREAM_DEBUG (LOG_TAG, " Configured endpoints ignored due to ignore_configured_endpoint_urls=true in profile: " << profileName);
55+ return ;
56+ }
57+ }
58+
59+ // 2) Service-specific environment variable
60+ {
61+ Aws::String service = " AWS_ENDPOINT_URL_" ;
62+ service += serviceKey;
63+ Aws::String fromEnv = Aws::Environment::GetEnv (service.c_str ());
64+ if (!fromEnv.empty ()) {
65+ AWS_LOGSTREAM_DEBUG (LOG_TAG, " Resolved configured endpoint from service-specific environment variable: " << service);
66+ AWS_LOGSTREAM_TRACE (LOG_TAG, " Configured endpoint URL: " << fromEnv);
67+ endpointProvider.OverrideEndpoint (fromEnv);
68+ return ;
69+ }
70+ }
71+
72+ // 3) Global environment variable
73+ {
74+ Aws::String fromEnv = Aws::Environment::GetEnv (" AWS_ENDPOINT_URL" );
75+ if (!fromEnv.empty ()) {
76+ AWS_LOGSTREAM_DEBUG (LOG_TAG, " Resolved configured endpoint from global environment variable: AWS_ENDPOINT_URL" );
77+ AWS_LOGSTREAM_TRACE (LOG_TAG, " Configured endpoint URL: " << fromEnv);
78+ endpointProvider.OverrideEndpoint (fromEnv);
79+ return ;
80+ }
81+ }
82+
83+ // Skip profile resolution no profile available
84+ if (profileName.empty () || !HasCachedConfigProfile (profileName)) {
85+ AWS_LOGSTREAM_DEBUG (LOG_TAG, " No configured endpoint found - no profile available or profile not cached" );
86+ return ;
87+ }
88+
89+ Profile profile = GetCachedConfigProfile (profileName);
90+
91+ // 4) Service-specific endpoint from shared config profile
92+ const auto & endpoints = profile.GetServices ().GetEndpoints ();
93+ auto it = endpoints.find (serviceKey);
94+ if (it != endpoints.end () && !it->second .empty ()) {
95+ AWS_LOGSTREAM_DEBUG (LOG_TAG, " Resolved configured endpoint from service-specific profile setting for service: " << serviceKey << " in profile: " << profileName);
96+ AWS_LOGSTREAM_TRACE (LOG_TAG, " Configured endpoint URL: " << it->second );
97+ endpointProvider.OverrideEndpoint (it->second );
98+ return ;
99+ }
100+
101+ // 5) Global profile endpoint
102+ auto endpoint = profile.GetGlobalEndpointUrl ();
103+ if (!endpoint.empty ()) {
104+ AWS_LOGSTREAM_DEBUG (LOG_TAG, " Resolved configured endpoint from global profile setting in profile: " << profileName);
105+ AWS_LOGSTREAM_TRACE (LOG_TAG, " Configured endpoint URL: " << endpoint);
106+ endpointProvider.OverrideEndpoint (endpoint);
107+ return ;
108+ }
109+
110+ AWS_LOGSTREAM_DEBUG (LOG_TAG, " No configured endpoint found for service: " << serviceId);
111+ }
112+
113+ private:
114+ /* *
115+ * Convert service ID to environment variable suffix format
116+ */
117+ static Aws::String ToEnvSuffix (const Aws::String& serviceId);
118+ };
119+
120+ } // namespace Config
121+ } // namespace Aws
0 commit comments