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