Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/botocore/configprovider.py: 50%
326 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:51 +0000
1# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License"). You
4# may not use this file except in compliance with the License. A copy of
5# the License is located at
6#
7# http://aws.amazon.com/apache2.0/
8#
9# or in the "license" file accompanying this file. This file is
10# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11# ANY KIND, either express or implied. See the License for the specific
12# language governing permissions and limitations under the License.
13"""This module contains the interface for controlling how configuration
14is loaded.
15"""
16import copy
17import logging
18import os
20from botocore import utils
21from botocore.exceptions import InvalidConfigError
23logger = logging.getLogger(__name__)
26#: A default dictionary that maps the logical names for session variables
27#: to the specific environment variables and configuration file names
28#: that contain the values for these variables.
29#: When creating a new Session object, you can pass in your own dictionary
30#: to remap the logical names or to add new logical names. You can then
31#: get the current value for these variables by using the
32#: ``get_config_variable`` method of the :class:`botocore.session.Session`
33#: class.
34#: These form the keys of the dictionary. The values in the dictionary
35#: are tuples of (<config_name>, <environment variable>, <default value>,
36#: <conversion func>).
37#: The conversion func is a function that takes the configuration value
38#: as an argument and returns the converted value. If this value is
39#: None, then the configuration value is returned unmodified. This
40#: conversion function can be used to type convert config values to
41#: values other than the default values of strings.
42#: The ``profile`` and ``config_file`` variables should always have a
43#: None value for the first entry in the tuple because it doesn't make
44#: sense to look inside the config file for the location of the config
45#: file or for the default profile to use.
46#: The ``config_name`` is the name to look for in the configuration file,
47#: the ``env var`` is the OS environment variable (``os.environ``) to
48#: use, and ``default_value`` is the value to use if no value is otherwise
49#: found.
50#: NOTE: Fixing the spelling of this variable would be a breaking change.
51#: Please leave as is.
52BOTOCORE_DEFAUT_SESSION_VARIABLES = {
53 # logical: config_file, env_var, default_value, conversion_func
54 'profile': (None, ['AWS_DEFAULT_PROFILE', 'AWS_PROFILE'], None, None),
55 'region': ('region', 'AWS_DEFAULT_REGION', None, None),
56 'data_path': ('data_path', 'AWS_DATA_PATH', None, None),
57 'config_file': (None, 'AWS_CONFIG_FILE', '~/.aws/config', None),
58 'ca_bundle': ('ca_bundle', 'AWS_CA_BUNDLE', None, None),
59 'api_versions': ('api_versions', None, {}, None),
60 # This is the shared credentials file amongst sdks.
61 'credentials_file': (
62 None,
63 'AWS_SHARED_CREDENTIALS_FILE',
64 '~/.aws/credentials',
65 None,
66 ),
67 # These variables only exist in the config file.
68 # This is the number of seconds until we time out a request to
69 # the instance metadata service.
70 'metadata_service_timeout': (
71 'metadata_service_timeout',
72 'AWS_METADATA_SERVICE_TIMEOUT',
73 1,
74 int,
75 ),
76 # This is the number of request attempts we make until we give
77 # up trying to retrieve data from the instance metadata service.
78 'metadata_service_num_attempts': (
79 'metadata_service_num_attempts',
80 'AWS_METADATA_SERVICE_NUM_ATTEMPTS',
81 1,
82 int,
83 ),
84 'ec2_metadata_service_endpoint': (
85 'ec2_metadata_service_endpoint',
86 'AWS_EC2_METADATA_SERVICE_ENDPOINT',
87 None,
88 None,
89 ),
90 'ec2_metadata_service_endpoint_mode': (
91 'ec2_metadata_service_endpoint_mode',
92 'AWS_EC2_METADATA_SERVICE_ENDPOINT_MODE',
93 None,
94 None,
95 ),
96 'ec2_metadata_v1_disabled': (
97 'ec2_metadata_v1_disabled',
98 'AWS_EC2_METADATA_V1_DISABLED',
99 False,
100 utils.ensure_boolean,
101 ),
102 'imds_use_ipv6': (
103 'imds_use_ipv6',
104 'AWS_IMDS_USE_IPV6',
105 False,
106 utils.ensure_boolean,
107 ),
108 'use_dualstack_endpoint': (
109 'use_dualstack_endpoint',
110 'AWS_USE_DUALSTACK_ENDPOINT',
111 None,
112 utils.ensure_boolean,
113 ),
114 'use_fips_endpoint': (
115 'use_fips_endpoint',
116 'AWS_USE_FIPS_ENDPOINT',
117 None,
118 utils.ensure_boolean,
119 ),
120 'ignore_configured_endpoint_urls': (
121 'ignore_configured_endpoint_urls',
122 'AWS_IGNORE_CONFIGURED_ENDPOINT_URLS',
123 None,
124 utils.ensure_boolean,
125 ),
126 'parameter_validation': ('parameter_validation', None, True, None),
127 # Client side monitoring configurations.
128 # Note: These configurations are considered internal to botocore.
129 # Do not use them until publicly documented.
130 'csm_enabled': (
131 'csm_enabled',
132 'AWS_CSM_ENABLED',
133 False,
134 utils.ensure_boolean,
135 ),
136 'csm_host': ('csm_host', 'AWS_CSM_HOST', '127.0.0.1', None),
137 'csm_port': ('csm_port', 'AWS_CSM_PORT', 31000, int),
138 'csm_client_id': ('csm_client_id', 'AWS_CSM_CLIENT_ID', '', None),
139 # Endpoint discovery configuration
140 'endpoint_discovery_enabled': (
141 'endpoint_discovery_enabled',
142 'AWS_ENDPOINT_DISCOVERY_ENABLED',
143 'auto',
144 None,
145 ),
146 'sts_regional_endpoints': (
147 'sts_regional_endpoints',
148 'AWS_STS_REGIONAL_ENDPOINTS',
149 'legacy',
150 None,
151 ),
152 'retry_mode': ('retry_mode', 'AWS_RETRY_MODE', 'legacy', None),
153 'defaults_mode': ('defaults_mode', 'AWS_DEFAULTS_MODE', 'legacy', None),
154 # We can't have a default here for v1 because we need to defer to
155 # whatever the defaults are in _retry.json.
156 'max_attempts': ('max_attempts', 'AWS_MAX_ATTEMPTS', None, int),
157 'user_agent_appid': ('sdk_ua_app_id', 'AWS_SDK_UA_APP_ID', None, None),
158 'request_min_compression_size_bytes': (
159 'request_min_compression_size_bytes',
160 'AWS_REQUEST_MIN_COMPRESSION_SIZE_BYTES',
161 10240,
162 None,
163 ),
164 'disable_request_compression': (
165 'disable_request_compression',
166 'AWS_DISABLE_REQUEST_COMPRESSION',
167 False,
168 utils.ensure_boolean,
169 ),
170}
171# A mapping for the s3 specific configuration vars. These are the configuration
172# vars that typically go in the s3 section of the config file. This mapping
173# follows the same schema as the previous session variable mapping.
174DEFAULT_S3_CONFIG_VARS = {
175 'addressing_style': (('s3', 'addressing_style'), None, None, None),
176 'use_accelerate_endpoint': (
177 ('s3', 'use_accelerate_endpoint'),
178 None,
179 None,
180 utils.ensure_boolean,
181 ),
182 'use_dualstack_endpoint': (
183 ('s3', 'use_dualstack_endpoint'),
184 None,
185 None,
186 utils.ensure_boolean,
187 ),
188 'payload_signing_enabled': (
189 ('s3', 'payload_signing_enabled'),
190 None,
191 None,
192 utils.ensure_boolean,
193 ),
194 'use_arn_region': (
195 ['s3_use_arn_region', ('s3', 'use_arn_region')],
196 'AWS_S3_USE_ARN_REGION',
197 None,
198 utils.ensure_boolean,
199 ),
200 'us_east_1_regional_endpoint': (
201 [
202 's3_us_east_1_regional_endpoint',
203 ('s3', 'us_east_1_regional_endpoint'),
204 ],
205 'AWS_S3_US_EAST_1_REGIONAL_ENDPOINT',
206 None,
207 None,
208 ),
209 's3_disable_multiregion_access_points': (
210 ('s3', 's3_disable_multiregion_access_points'),
211 'AWS_S3_DISABLE_MULTIREGION_ACCESS_POINTS',
212 None,
213 utils.ensure_boolean,
214 ),
215}
216# A mapping for the proxy specific configuration vars. These are
217# used to configure how botocore interacts with proxy setups while
218# sending requests.
219DEFAULT_PROXIES_CONFIG_VARS = {
220 'proxy_ca_bundle': ('proxy_ca_bundle', None, None, None),
221 'proxy_client_cert': ('proxy_client_cert', None, None, None),
222 'proxy_use_forwarding_for_https': (
223 'proxy_use_forwarding_for_https',
224 None,
225 None,
226 utils.normalize_boolean,
227 ),
228}
231def create_botocore_default_config_mapping(session):
232 chain_builder = ConfigChainFactory(session=session)
233 config_mapping = _create_config_chain_mapping(
234 chain_builder, BOTOCORE_DEFAUT_SESSION_VARIABLES
235 )
236 config_mapping['s3'] = SectionConfigProvider(
237 's3',
238 session,
239 _create_config_chain_mapping(chain_builder, DEFAULT_S3_CONFIG_VARS),
240 )
241 config_mapping['proxies_config'] = SectionConfigProvider(
242 'proxies_config',
243 session,
244 _create_config_chain_mapping(
245 chain_builder, DEFAULT_PROXIES_CONFIG_VARS
246 ),
247 )
248 return config_mapping
251def _create_config_chain_mapping(chain_builder, config_variables):
252 mapping = {}
253 for logical_name, config in config_variables.items():
254 mapping[logical_name] = chain_builder.create_config_chain(
255 instance_name=logical_name,
256 env_var_names=config[1],
257 config_property_names=config[0],
258 default=config[2],
259 conversion_func=config[3],
260 )
261 return mapping
264class DefaultConfigResolver:
265 def __init__(self, default_config_data):
266 self._base_default_config = default_config_data['base']
267 self._modes = default_config_data['modes']
268 self._resolved_default_configurations = {}
270 def _resolve_default_values_by_mode(self, mode):
271 default_config = self._base_default_config.copy()
272 modifications = self._modes.get(mode)
274 for config_var in modifications:
275 default_value = default_config[config_var]
276 modification_dict = modifications[config_var]
277 modification = list(modification_dict.keys())[0]
278 modification_value = modification_dict[modification]
279 if modification == 'multiply':
280 default_value *= modification_value
281 elif modification == 'add':
282 default_value += modification_value
283 elif modification == 'override':
284 default_value = modification_value
285 default_config[config_var] = default_value
286 return default_config
288 def get_default_modes(self):
289 default_modes = ['legacy', 'auto']
290 default_modes.extend(self._modes.keys())
291 return default_modes
293 def get_default_config_values(self, mode):
294 if mode not in self._resolved_default_configurations:
295 defaults = self._resolve_default_values_by_mode(mode)
296 self._resolved_default_configurations[mode] = defaults
297 return self._resolved_default_configurations[mode]
300class ConfigChainFactory:
301 """Factory class to create our most common configuration chain case.
303 This is a convenience class to construct configuration chains that follow
304 our most common pattern. This is to prevent ordering them incorrectly,
305 and to make the config chain construction more readable.
306 """
308 def __init__(self, session, environ=None):
309 """Initialize a ConfigChainFactory.
311 :type session: :class:`botocore.session.Session`
312 :param session: This is the session that should be used to look up
313 values from the config file.
315 :type environ: dict
316 :param environ: A mapping to use for environment variables. If this
317 is not provided it will default to use os.environ.
318 """
319 self._session = session
320 if environ is None:
321 environ = os.environ
322 self._environ = environ
324 def create_config_chain(
325 self,
326 instance_name=None,
327 env_var_names=None,
328 config_property_names=None,
329 default=None,
330 conversion_func=None,
331 ):
332 """Build a config chain following the standard botocore pattern.
334 In botocore most of our config chains follow the the precendence:
335 session_instance_variables, environment, config_file, default_value.
337 This is a convenience function for creating a chain that follow
338 that precendence.
340 :type instance_name: str
341 :param instance_name: This indicates what session instance variable
342 corresponds to this config value. If it is None it will not be
343 added to the chain.
345 :type env_var_names: str or list of str or None
346 :param env_var_names: One or more environment variable names to
347 search for this value. They are searched in order. If it is None
348 it will not be added to the chain.
350 :type config_property_names: str/tuple or list of str/tuple or None
351 :param config_property_names: One of more strings or tuples
352 representing the name of the key in the config file for this
353 config option. They are searched in order. If it is None it will
354 not be added to the chain.
356 :type default: Any
357 :param default: Any constant value to be returned.
359 :type conversion_func: None or callable
360 :param conversion_func: If this value is None then it has no effect on
361 the return type. Otherwise, it is treated as a function that will
362 conversion_func our provided type.
364 :rvalue: ConfigChain
365 :returns: A ConfigChain that resolves in the order env_var_names ->
366 config_property_name -> default. Any values that were none are
367 omitted form the chain.
368 """
369 providers = []
370 if instance_name is not None:
371 providers.append(
372 InstanceVarProvider(
373 instance_var=instance_name, session=self._session
374 )
375 )
376 if env_var_names is not None:
377 providers.extend(self._get_env_providers(env_var_names))
378 if config_property_names is not None:
379 providers.extend(
380 self._get_scoped_config_providers(config_property_names)
381 )
382 if default is not None:
383 providers.append(ConstantProvider(value=default))
385 return ChainProvider(
386 providers=providers,
387 conversion_func=conversion_func,
388 )
390 def _get_env_providers(self, env_var_names):
391 env_var_providers = []
392 if not isinstance(env_var_names, list):
393 env_var_names = [env_var_names]
394 for env_var_name in env_var_names:
395 env_var_providers.append(
396 EnvironmentProvider(name=env_var_name, env=self._environ)
397 )
398 return env_var_providers
400 def _get_scoped_config_providers(self, config_property_names):
401 scoped_config_providers = []
402 if not isinstance(config_property_names, list):
403 config_property_names = [config_property_names]
404 for config_property_name in config_property_names:
405 scoped_config_providers.append(
406 ScopedConfigProvider(
407 config_var_name=config_property_name,
408 session=self._session,
409 )
410 )
411 return scoped_config_providers
414class ConfigValueStore:
415 """The ConfigValueStore object stores configuration values."""
417 def __init__(self, mapping=None):
418 """Initialize a ConfigValueStore.
420 :type mapping: dict
421 :param mapping: The mapping parameter is a map of string to a subclass
422 of BaseProvider. When a config variable is asked for via the
423 get_config_variable method, the corresponding provider will be
424 invoked to load the value.
425 """
426 self._overrides = {}
427 self._mapping = {}
428 if mapping is not None:
429 for logical_name, provider in mapping.items():
430 self.set_config_provider(logical_name, provider)
432 def __deepcopy__(self, memo):
433 config_store = ConfigValueStore(copy.deepcopy(self._mapping, memo))
434 for logical_name, override_value in self._overrides.items():
435 config_store.set_config_variable(logical_name, override_value)
437 return config_store
439 def __copy__(self):
440 config_store = ConfigValueStore(copy.copy(self._mapping))
441 for logical_name, override_value in self._overrides.items():
442 config_store.set_config_variable(logical_name, override_value)
444 return config_store
446 def get_config_variable(self, logical_name):
447 """
448 Retrieve the value associeated with the specified logical_name
449 from the corresponding provider. If no value is found None will
450 be returned.
452 :type logical_name: str
453 :param logical_name: The logical name of the session variable
454 you want to retrieve. This name will be mapped to the
455 appropriate environment variable name for this session as
456 well as the appropriate config file entry.
458 :returns: value of variable or None if not defined.
459 """
460 if logical_name in self._overrides:
461 return self._overrides[logical_name]
462 if logical_name not in self._mapping:
463 return None
464 provider = self._mapping[logical_name]
465 return provider.provide()
467 def get_config_provider(self, logical_name):
468 """
469 Retrieve the provider associated with the specified logical_name.
470 If no provider is found None will be returned.
472 :type logical_name: str
473 :param logical_name: The logical name of the session variable
474 you want to retrieve. This name will be mapped to the
475 appropriate environment variable name for this session as
476 well as the appropriate config file entry.
478 :returns: configuration provider or None if not defined.
479 """
480 if (
481 logical_name in self._overrides
482 or logical_name not in self._mapping
483 ):
484 return None
485 provider = self._mapping[logical_name]
486 return provider
488 def set_config_variable(self, logical_name, value):
489 """Set a configuration variable to a specific value.
491 By using this method, you can override the normal lookup
492 process used in ``get_config_variable`` by explicitly setting
493 a value. Subsequent calls to ``get_config_variable`` will
494 use the ``value``. This gives you per-session specific
495 configuration values.
497 ::
498 >>> # Assume logical name 'foo' maps to env var 'FOO'
499 >>> os.environ['FOO'] = 'myvalue'
500 >>> s.get_config_variable('foo')
501 'myvalue'
502 >>> s.set_config_variable('foo', 'othervalue')
503 >>> s.get_config_variable('foo')
504 'othervalue'
506 :type logical_name: str
507 :param logical_name: The logical name of the session variable
508 you want to set. These are the keys in ``SESSION_VARIABLES``.
510 :param value: The value to associate with the config variable.
511 """
512 self._overrides[logical_name] = value
514 def clear_config_variable(self, logical_name):
515 """Remove an override config variable from the session.
517 :type logical_name: str
518 :param logical_name: The name of the parameter to clear the override
519 value from.
520 """
521 self._overrides.pop(logical_name, None)
523 def set_config_provider(self, logical_name, provider):
524 """Set the provider for a config value.
526 This provides control over how a particular configuration value is
527 loaded. This replaces the provider for ``logical_name`` with the new
528 ``provider``.
530 :type logical_name: str
531 :param logical_name: The name of the config value to change the config
532 provider for.
534 :type provider: :class:`botocore.configprovider.BaseProvider`
535 :param provider: The new provider that should be responsible for
536 providing a value for the config named ``logical_name``.
537 """
538 self._mapping[logical_name] = provider
541class SmartDefaultsConfigStoreFactory:
542 def __init__(self, default_config_resolver, imds_region_provider):
543 self._default_config_resolver = default_config_resolver
544 self._imds_region_provider = imds_region_provider
545 # Initializing _instance_metadata_region as None so we
546 # can fetch region in a lazy fashion only when needed.
547 self._instance_metadata_region = None
549 def merge_smart_defaults(self, config_store, mode, region_name):
550 if mode == 'auto':
551 mode = self.resolve_auto_mode(region_name)
552 default_configs = (
553 self._default_config_resolver.get_default_config_values(mode)
554 )
555 for config_var in default_configs:
556 config_value = default_configs[config_var]
557 method = getattr(self, f'_set_{config_var}', None)
558 if method:
559 method(config_store, config_value)
561 def resolve_auto_mode(self, region_name):
562 current_region = None
563 if os.environ.get('AWS_EXECUTION_ENV'):
564 default_region = os.environ.get('AWS_DEFAULT_REGION')
565 current_region = os.environ.get('AWS_REGION', default_region)
566 if not current_region:
567 if self._instance_metadata_region:
568 current_region = self._instance_metadata_region
569 else:
570 try:
571 current_region = self._imds_region_provider.provide()
572 self._instance_metadata_region = current_region
573 except Exception:
574 pass
576 if current_region:
577 if region_name == current_region:
578 return 'in-region'
579 else:
580 return 'cross-region'
581 return 'standard'
583 def _update_provider(self, config_store, variable, value):
584 original_provider = config_store.get_config_provider(variable)
585 default_provider = ConstantProvider(value)
586 if isinstance(original_provider, ChainProvider):
587 chain_provider_copy = copy.deepcopy(original_provider)
588 chain_provider_copy.set_default_provider(default_provider)
589 default_provider = chain_provider_copy
590 elif isinstance(original_provider, BaseProvider):
591 default_provider = ChainProvider(
592 providers=[original_provider, default_provider]
593 )
594 config_store.set_config_provider(variable, default_provider)
596 def _update_section_provider(
597 self, config_store, section_name, variable, value
598 ):
599 section_provider_copy = copy.deepcopy(
600 config_store.get_config_provider(section_name)
601 )
602 section_provider_copy.set_default_provider(
603 variable, ConstantProvider(value)
604 )
605 config_store.set_config_provider(section_name, section_provider_copy)
607 def _set_retryMode(self, config_store, value):
608 self._update_provider(config_store, 'retry_mode', value)
610 def _set_stsRegionalEndpoints(self, config_store, value):
611 self._update_provider(config_store, 'sts_regional_endpoints', value)
613 def _set_s3UsEast1RegionalEndpoints(self, config_store, value):
614 self._update_section_provider(
615 config_store, 's3', 'us_east_1_regional_endpoint', value
616 )
618 def _set_connectTimeoutInMillis(self, config_store, value):
619 self._update_provider(config_store, 'connect_timeout', value / 1000)
622class BaseProvider:
623 """Base class for configuration value providers.
625 A configuration provider has some method of providing a configuration
626 value.
627 """
629 def provide(self):
630 """Provide a config value."""
631 raise NotImplementedError('provide')
634class ChainProvider(BaseProvider):
635 """This provider wraps one or more other providers.
637 Each provider in the chain is called, the first one returning a non-None
638 value is then returned.
639 """
641 def __init__(self, providers=None, conversion_func=None):
642 """Initalize a ChainProvider.
644 :type providers: list
645 :param providers: The initial list of providers to check for values
646 when invoked.
648 :type conversion_func: None or callable
649 :param conversion_func: If this value is None then it has no affect on
650 the return type. Otherwise, it is treated as a function that will
651 transform provided value.
652 """
653 if providers is None:
654 providers = []
655 self._providers = providers
656 self._conversion_func = conversion_func
658 def __deepcopy__(self, memo):
659 return ChainProvider(
660 copy.deepcopy(self._providers, memo), self._conversion_func
661 )
663 def provide(self):
664 """Provide the value from the first provider to return non-None.
666 Each provider in the chain has its provide method called. The first
667 one in the chain to return a non-None value is the returned from the
668 ChainProvider. When no non-None value is found, None is returned.
669 """
670 for provider in self._providers:
671 value = provider.provide()
672 if value is not None:
673 return self._convert_type(value)
674 return None
676 def set_default_provider(self, default_provider):
677 if self._providers and isinstance(
678 self._providers[-1], ConstantProvider
679 ):
680 self._providers[-1] = default_provider
681 else:
682 self._providers.append(default_provider)
684 num_of_constants = sum(
685 isinstance(provider, ConstantProvider)
686 for provider in self._providers
687 )
688 if num_of_constants > 1:
689 logger.info(
690 'ChainProvider object contains multiple '
691 'instances of ConstantProvider objects'
692 )
694 def _convert_type(self, value):
695 if self._conversion_func is not None:
696 return self._conversion_func(value)
697 return value
699 def __repr__(self):
700 return '[%s]' % ', '.join([str(p) for p in self._providers])
703class InstanceVarProvider(BaseProvider):
704 """This class loads config values from the session instance vars."""
706 def __init__(self, instance_var, session):
707 """Initialize InstanceVarProvider.
709 :type instance_var: str
710 :param instance_var: The instance variable to load from the session.
712 :type session: :class:`botocore.session.Session`
713 :param session: The botocore session to get the loaded configuration
714 file variables from.
715 """
716 self._instance_var = instance_var
717 self._session = session
719 def __deepcopy__(self, memo):
720 return InstanceVarProvider(
721 copy.deepcopy(self._instance_var, memo), self._session
722 )
724 def provide(self):
725 """Provide a config value from the session instance vars."""
726 instance_vars = self._session.instance_variables()
727 value = instance_vars.get(self._instance_var)
728 return value
730 def __repr__(self):
731 return 'InstanceVarProvider(instance_var={}, session={})'.format(
732 self._instance_var,
733 self._session,
734 )
737class ScopedConfigProvider(BaseProvider):
738 def __init__(self, config_var_name, session):
739 """Initialize ScopedConfigProvider.
741 :type config_var_name: str or tuple
742 :param config_var_name: The name of the config variable to load from
743 the configuration file. If the value is a tuple, it must only
744 consist of two items, where the first item represents the section
745 and the second item represents the config var name in the section.
747 :type session: :class:`botocore.session.Session`
748 :param session: The botocore session to get the loaded configuration
749 file variables from.
750 """
751 self._config_var_name = config_var_name
752 self._session = session
754 def __deepcopy__(self, memo):
755 return ScopedConfigProvider(
756 copy.deepcopy(self._config_var_name, memo), self._session
757 )
759 def provide(self):
760 """Provide a value from a config file property."""
761 scoped_config = self._session.get_scoped_config()
762 if isinstance(self._config_var_name, tuple):
763 section_config = scoped_config.get(self._config_var_name[0])
764 if not isinstance(section_config, dict):
765 return None
766 return section_config.get(self._config_var_name[1])
767 return scoped_config.get(self._config_var_name)
769 def __repr__(self):
770 return 'ScopedConfigProvider(config_var_name={}, session={})'.format(
771 self._config_var_name,
772 self._session,
773 )
776class EnvironmentProvider(BaseProvider):
777 """This class loads config values from environment variables."""
779 def __init__(self, name, env):
780 """Initialize with the keys in the dictionary to check.
782 :type name: str
783 :param name: The key with that name will be loaded and returned.
785 :type env: dict
786 :param env: Environment variables dictionary to get variables from.
787 """
788 self._name = name
789 self._env = env
791 def __deepcopy__(self, memo):
792 return EnvironmentProvider(
793 copy.deepcopy(self._name, memo), copy.deepcopy(self._env, memo)
794 )
796 def provide(self):
797 """Provide a config value from a source dictionary."""
798 if self._name in self._env:
799 return self._env[self._name]
800 return None
802 def __repr__(self):
803 return f'EnvironmentProvider(name={self._name}, env={self._env})'
806class SectionConfigProvider(BaseProvider):
807 """Provides a dictionary from a section in the scoped config
809 This is useful for retrieving scoped config variables (i.e. s3) that have
810 their own set of config variables and resolving logic.
811 """
813 def __init__(self, section_name, session, override_providers=None):
814 self._section_name = section_name
815 self._session = session
816 self._scoped_config_provider = ScopedConfigProvider(
817 self._section_name, self._session
818 )
819 self._override_providers = override_providers
820 if self._override_providers is None:
821 self._override_providers = {}
823 def __deepcopy__(self, memo):
824 return SectionConfigProvider(
825 copy.deepcopy(self._section_name, memo),
826 self._session,
827 copy.deepcopy(self._override_providers, memo),
828 )
830 def provide(self):
831 section_config = self._scoped_config_provider.provide()
832 if section_config and not isinstance(section_config, dict):
833 logger.debug(
834 "The %s config key is not a dictionary type, "
835 "ignoring its value of: %s",
836 self._section_name,
837 section_config,
838 )
839 return None
840 for section_config_var, provider in self._override_providers.items():
841 provider_val = provider.provide()
842 if provider_val is not None:
843 if section_config is None:
844 section_config = {}
845 section_config[section_config_var] = provider_val
846 return section_config
848 def set_default_provider(self, key, default_provider):
849 provider = self._override_providers.get(key)
850 if isinstance(provider, ChainProvider):
851 provider.set_default_provider(default_provider)
852 return
853 elif isinstance(provider, BaseProvider):
854 default_provider = ChainProvider(
855 providers=[provider, default_provider]
856 )
857 self._override_providers[key] = default_provider
859 def __repr__(self):
860 return (
861 f'SectionConfigProvider(section_name={self._section_name}, '
862 f'session={self._session}, '
863 f'override_providers={self._override_providers})'
864 )
867class ConstantProvider(BaseProvider):
868 """This provider provides a constant value."""
870 def __init__(self, value):
871 self._value = value
873 def __deepcopy__(self, memo):
874 return ConstantProvider(copy.deepcopy(self._value, memo))
876 def provide(self):
877 """Provide the constant value given during initialization."""
878 return self._value
880 def __repr__(self):
881 return 'ConstantProvider(value=%s)' % self._value
884class ConfiguredEndpointProvider(BaseProvider):
885 """Lookup an endpoint URL from environment variable or shared config file.
887 NOTE: This class is considered private and is subject to abrupt breaking
888 changes or removal without prior announcement. Please do not use it
889 directly.
890 """
892 _ENDPOINT_URL_LOOKUP_ORDER = [
893 'environment_service',
894 'environment_global',
895 'config_service',
896 'config_global',
897 ]
899 def __init__(
900 self,
901 full_config,
902 scoped_config,
903 client_name,
904 environ=None,
905 ):
906 """Initialize a ConfiguredEndpointProviderChain.
908 :type full_config: dict
909 :param full_config: This is the dict representing the full
910 configuration file.
912 :type scoped_config: dict
913 :param scoped_config: This is the dict representing the configuration
914 for the current profile for the session.
916 :type client_name: str
917 :param client_name: The name used to instantiate a client using
918 botocore.session.Session.create_client.
920 :type environ: dict
921 :param environ: A mapping to use for environment variables. If this
922 is not provided it will default to use os.environ.
923 """
924 self._full_config = full_config
925 self._scoped_config = scoped_config
926 self._client_name = client_name
927 self._transformed_service_id = self._get_snake_case_service_id(
928 self._client_name
929 )
930 if environ is None:
931 environ = os.environ
932 self._environ = environ
934 def provide(self):
935 """Lookup the configured endpoint URL.
937 The order is:
939 1. The value provided by a service-specific environment variable.
940 2. The value provided by the global endpoint environment variable
941 (AWS_ENDPOINT_URL).
942 3. The value provided by a service-specific parameter from a services
943 definition section in the shared configuration file.
944 4. The value provided by the global parameter from a services
945 definition section in the shared configuration file.
946 """
947 for location in self._ENDPOINT_URL_LOOKUP_ORDER:
948 logger.debug(
949 'Looking for endpoint for %s via: %s',
950 self._client_name,
951 location,
952 )
954 endpoint_url = getattr(self, f'_get_endpoint_url_{location}')()
956 if endpoint_url:
957 logger.info(
958 'Found endpoint for %s via: %s.',
959 self._client_name,
960 location,
961 )
962 return endpoint_url
964 logger.debug('No configured endpoint found.')
965 return None
967 def _get_snake_case_service_id(self, client_name):
968 # Get the service ID without loading the service data file, accounting
969 # for any aliases and standardizing the names with hyphens.
970 client_name = utils.SERVICE_NAME_ALIASES.get(client_name, client_name)
971 hyphenized_service_id = (
972 utils.CLIENT_NAME_TO_HYPHENIZED_SERVICE_ID_OVERRIDES.get(
973 client_name, client_name
974 )
975 )
976 return hyphenized_service_id.replace('-', '_')
978 def _get_service_env_var_name(self):
979 transformed_service_id_env = self._transformed_service_id.upper()
980 return f'AWS_ENDPOINT_URL_{transformed_service_id_env}'
982 def _get_services_config(self):
983 if 'services' not in self._scoped_config:
984 return {}
986 section_name = self._scoped_config['services']
987 services_section = self._full_config.get('services', {}).get(
988 section_name
989 )
991 if not services_section:
992 error_msg = (
993 f'The profile is configured to use the services '
994 f'section but the "{section_name}" services '
995 f'configuration does not exist.'
996 )
997 raise InvalidConfigError(error_msg=error_msg)
999 return services_section
1001 def _get_endpoint_url_config_service(self):
1002 snakecase_service_id = self._transformed_service_id.lower()
1003 return (
1004 self._get_services_config()
1005 .get(snakecase_service_id, {})
1006 .get('endpoint_url')
1007 )
1009 def _get_endpoint_url_config_global(self):
1010 return self._scoped_config.get('endpoint_url')
1012 def _get_endpoint_url_environment_service(self):
1013 return EnvironmentProvider(
1014 name=self._get_service_env_var_name(), env=self._environ
1015 ).provide()
1017 def _get_endpoint_url_environment_global(self):
1018 return EnvironmentProvider(
1019 name='AWS_ENDPOINT_URL', env=self._environ
1020 ).provide()