Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/botocore/configprovider.py: 50%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

327 statements  

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} 

208# A mapping for the s3 specific configuration vars. These are the configuration 

209# vars that typically go in the s3 section of the config file. This mapping 

210# follows the same schema as the previous session variable mapping. 

211DEFAULT_S3_CONFIG_VARS = { 

212 'addressing_style': (('s3', 'addressing_style'), None, None, None), 

213 'use_accelerate_endpoint': ( 

214 ('s3', 'use_accelerate_endpoint'), 

215 None, 

216 None, 

217 utils.ensure_boolean, 

218 ), 

219 'use_dualstack_endpoint': ( 

220 ('s3', 'use_dualstack_endpoint'), 

221 None, 

222 None, 

223 utils.ensure_boolean, 

224 ), 

225 'payload_signing_enabled': ( 

226 ('s3', 'payload_signing_enabled'), 

227 None, 

228 None, 

229 utils.ensure_boolean, 

230 ), 

231 'use_arn_region': ( 

232 ['s3_use_arn_region', ('s3', 'use_arn_region')], 

233 'AWS_S3_USE_ARN_REGION', 

234 None, 

235 utils.ensure_boolean, 

236 ), 

237 'us_east_1_regional_endpoint': ( 

238 [ 

239 's3_us_east_1_regional_endpoint', 

240 ('s3', 'us_east_1_regional_endpoint'), 

241 ], 

242 'AWS_S3_US_EAST_1_REGIONAL_ENDPOINT', 

243 None, 

244 None, 

245 ), 

246 's3_disable_multiregion_access_points': ( 

247 ('s3', 's3_disable_multiregion_access_points'), 

248 'AWS_S3_DISABLE_MULTIREGION_ACCESS_POINTS', 

249 None, 

250 utils.ensure_boolean, 

251 ), 

252} 

253# A mapping for the proxy specific configuration vars. These are 

254# used to configure how botocore interacts with proxy setups while 

255# sending requests. 

256DEFAULT_PROXIES_CONFIG_VARS = { 

257 'proxy_ca_bundle': ('proxy_ca_bundle', None, None, None), 

258 'proxy_client_cert': ('proxy_client_cert', None, None, None), 

259 'proxy_use_forwarding_for_https': ( 

260 'proxy_use_forwarding_for_https', 

261 None, 

262 None, 

263 utils.normalize_boolean, 

264 ), 

265} 

266 

267 

268def create_botocore_default_config_mapping(session): 

269 chain_builder = ConfigChainFactory(session=session) 

270 config_mapping = _create_config_chain_mapping( 

271 chain_builder, BOTOCORE_DEFAUT_SESSION_VARIABLES 

272 ) 

273 config_mapping['s3'] = SectionConfigProvider( 

274 's3', 

275 session, 

276 _create_config_chain_mapping(chain_builder, DEFAULT_S3_CONFIG_VARS), 

277 ) 

278 config_mapping['proxies_config'] = SectionConfigProvider( 

279 'proxies_config', 

280 session, 

281 _create_config_chain_mapping( 

282 chain_builder, DEFAULT_PROXIES_CONFIG_VARS 

283 ), 

284 ) 

285 return config_mapping 

286 

287 

288def _create_config_chain_mapping(chain_builder, config_variables): 

289 mapping = {} 

290 for logical_name, config in config_variables.items(): 

291 mapping[logical_name] = chain_builder.create_config_chain( 

292 instance_name=logical_name, 

293 env_var_names=config[1], 

294 config_property_names=config[0], 

295 default=config[2], 

296 conversion_func=config[3], 

297 ) 

298 return mapping 

299 

300 

301class DefaultConfigResolver: 

302 def __init__(self, default_config_data): 

303 self._base_default_config = default_config_data['base'] 

304 self._modes = default_config_data['modes'] 

305 self._resolved_default_configurations = {} 

306 

307 def _resolve_default_values_by_mode(self, mode): 

308 default_config = self._base_default_config.copy() 

309 modifications = self._modes.get(mode) 

310 

311 for config_var in modifications: 

312 default_value = default_config[config_var] 

313 modification_dict = modifications[config_var] 

314 modification = list(modification_dict.keys())[0] 

315 modification_value = modification_dict[modification] 

316 if modification == 'multiply': 

317 default_value *= modification_value 

318 elif modification == 'add': 

319 default_value += modification_value 

320 elif modification == 'override': 

321 default_value = modification_value 

322 default_config[config_var] = default_value 

323 return default_config 

324 

325 def get_default_modes(self): 

326 default_modes = ['legacy', 'auto'] 

327 default_modes.extend(self._modes.keys()) 

328 return default_modes 

329 

330 def get_default_config_values(self, mode): 

331 if mode not in self._resolved_default_configurations: 

332 defaults = self._resolve_default_values_by_mode(mode) 

333 self._resolved_default_configurations[mode] = defaults 

334 return self._resolved_default_configurations[mode] 

335 

336 

337class ConfigChainFactory: 

338 """Factory class to create our most common configuration chain case. 

339 

340 This is a convenience class to construct configuration chains that follow 

341 our most common pattern. This is to prevent ordering them incorrectly, 

342 and to make the config chain construction more readable. 

343 """ 

344 

345 def __init__(self, session, environ=None): 

346 """Initialize a ConfigChainFactory. 

347 

348 :type session: :class:`botocore.session.Session` 

349 :param session: This is the session that should be used to look up 

350 values from the config file. 

351 

352 :type environ: dict 

353 :param environ: A mapping to use for environment variables. If this 

354 is not provided it will default to use os.environ. 

355 """ 

356 self._session = session 

357 if environ is None: 

358 environ = os.environ 

359 self._environ = environ 

360 

361 def create_config_chain( 

362 self, 

363 instance_name=None, 

364 env_var_names=None, 

365 config_property_names=None, 

366 default=None, 

367 conversion_func=None, 

368 ): 

369 """Build a config chain following the standard botocore pattern. 

370 

371 In botocore most of our config chains follow the the precendence: 

372 session_instance_variables, environment, config_file, default_value. 

373 

374 This is a convenience function for creating a chain that follow 

375 that precendence. 

376 

377 :type instance_name: str 

378 :param instance_name: This indicates what session instance variable 

379 corresponds to this config value. If it is None it will not be 

380 added to the chain. 

381 

382 :type env_var_names: str or list of str or None 

383 :param env_var_names: One or more environment variable names to 

384 search for this value. They are searched in order. If it is None 

385 it will not be added to the chain. 

386 

387 :type config_property_names: str/tuple or list of str/tuple or None 

388 :param config_property_names: One of more strings or tuples 

389 representing the name of the key in the config file for this 

390 config option. They are searched in order. If it is None it will 

391 not be added to the chain. 

392 

393 :type default: Any 

394 :param default: Any constant value to be returned. 

395 

396 :type conversion_func: None or callable 

397 :param conversion_func: If this value is None then it has no effect on 

398 the return type. Otherwise, it is treated as a function that will 

399 conversion_func our provided type. 

400 

401 :rvalue: ConfigChain 

402 :returns: A ConfigChain that resolves in the order env_var_names -> 

403 config_property_name -> default. Any values that were none are 

404 omitted form the chain. 

405 """ 

406 providers = [] 

407 if instance_name is not None: 

408 providers.append( 

409 InstanceVarProvider( 

410 instance_var=instance_name, session=self._session 

411 ) 

412 ) 

413 if env_var_names is not None: 

414 providers.extend(self._get_env_providers(env_var_names)) 

415 if config_property_names is not None: 

416 providers.extend( 

417 self._get_scoped_config_providers(config_property_names) 

418 ) 

419 if default is not None: 

420 providers.append(ConstantProvider(value=default)) 

421 

422 return ChainProvider( 

423 providers=providers, 

424 conversion_func=conversion_func, 

425 ) 

426 

427 def _get_env_providers(self, env_var_names): 

428 env_var_providers = [] 

429 if not isinstance(env_var_names, list): 

430 env_var_names = [env_var_names] 

431 for env_var_name in env_var_names: 

432 env_var_providers.append( 

433 EnvironmentProvider(name=env_var_name, env=self._environ) 

434 ) 

435 return env_var_providers 

436 

437 def _get_scoped_config_providers(self, config_property_names): 

438 scoped_config_providers = [] 

439 if not isinstance(config_property_names, list): 

440 config_property_names = [config_property_names] 

441 for config_property_name in config_property_names: 

442 scoped_config_providers.append( 

443 ScopedConfigProvider( 

444 config_var_name=config_property_name, 

445 session=self._session, 

446 ) 

447 ) 

448 return scoped_config_providers 

449 

450 

451class ConfigValueStore: 

452 """The ConfigValueStore object stores configuration values.""" 

453 

454 def __init__(self, mapping=None): 

455 """Initialize a ConfigValueStore. 

456 

457 :type mapping: dict 

458 :param mapping: The mapping parameter is a map of string to a subclass 

459 of BaseProvider. When a config variable is asked for via the 

460 get_config_variable method, the corresponding provider will be 

461 invoked to load the value. 

462 """ 

463 self._overrides = {} 

464 self._mapping = {} 

465 if mapping is not None: 

466 for logical_name, provider in mapping.items(): 

467 self.set_config_provider(logical_name, provider) 

468 

469 def __deepcopy__(self, memo): 

470 config_store = ConfigValueStore(copy.deepcopy(self._mapping, memo)) 

471 for logical_name, override_value in self._overrides.items(): 

472 config_store.set_config_variable(logical_name, override_value) 

473 

474 return config_store 

475 

476 def __copy__(self): 

477 config_store = ConfigValueStore(copy.copy(self._mapping)) 

478 for logical_name, override_value in self._overrides.items(): 

479 config_store.set_config_variable(logical_name, override_value) 

480 

481 return config_store 

482 

483 def get_config_variable(self, logical_name): 

484 """ 

485 Retrieve the value associated with the specified logical_name 

486 from the corresponding provider. If no value is found None will 

487 be returned. 

488 

489 :type logical_name: str 

490 :param logical_name: The logical name of the session variable 

491 you want to retrieve. This name will be mapped to the 

492 appropriate environment variable name for this session as 

493 well as the appropriate config file entry. 

494 

495 :returns: value of variable or None if not defined. 

496 """ 

497 if logical_name in self._overrides: 

498 return self._overrides[logical_name] 

499 if logical_name not in self._mapping: 

500 return None 

501 provider = self._mapping[logical_name] 

502 return provider.provide() 

503 

504 def get_config_provider(self, logical_name): 

505 """ 

506 Retrieve the provider associated with the specified logical_name. 

507 If no provider is found None will be returned. 

508 

509 :type logical_name: str 

510 :param logical_name: The logical name of the session variable 

511 you want to retrieve. This name will be mapped to the 

512 appropriate environment variable name for this session as 

513 well as the appropriate config file entry. 

514 

515 :returns: configuration provider or None if not defined. 

516 """ 

517 if ( 

518 logical_name in self._overrides 

519 or logical_name not in self._mapping 

520 ): 

521 return None 

522 provider = self._mapping[logical_name] 

523 return provider 

524 

525 def set_config_variable(self, logical_name, value): 

526 """Set a configuration variable to a specific value. 

527 

528 By using this method, you can override the normal lookup 

529 process used in ``get_config_variable`` by explicitly setting 

530 a value. Subsequent calls to ``get_config_variable`` will 

531 use the ``value``. This gives you per-session specific 

532 configuration values. 

533 

534 :: 

535 >>> # Assume logical name 'foo' maps to env var 'FOO' 

536 >>> os.environ['FOO'] = 'myvalue' 

537 >>> s.get_config_variable('foo') 

538 'myvalue' 

539 >>> s.set_config_variable('foo', 'othervalue') 

540 >>> s.get_config_variable('foo') 

541 'othervalue' 

542 

543 :type logical_name: str 

544 :param logical_name: The logical name of the session variable 

545 you want to set. These are the keys in ``SESSION_VARIABLES``. 

546 

547 :param value: The value to associate with the config variable. 

548 """ 

549 self._overrides[logical_name] = value 

550 

551 def clear_config_variable(self, logical_name): 

552 """Remove an override config variable from the session. 

553 

554 :type logical_name: str 

555 :param logical_name: The name of the parameter to clear the override 

556 value from. 

557 """ 

558 self._overrides.pop(logical_name, None) 

559 

560 def set_config_provider(self, logical_name, provider): 

561 """Set the provider for a config value. 

562 

563 This provides control over how a particular configuration value is 

564 loaded. This replaces the provider for ``logical_name`` with the new 

565 ``provider``. 

566 

567 :type logical_name: str 

568 :param logical_name: The name of the config value to change the config 

569 provider for. 

570 

571 :type provider: :class:`botocore.configprovider.BaseProvider` 

572 :param provider: The new provider that should be responsible for 

573 providing a value for the config named ``logical_name``. 

574 """ 

575 self._mapping[logical_name] = provider 

576 

577 

578class SmartDefaultsConfigStoreFactory: 

579 def __init__(self, default_config_resolver, imds_region_provider): 

580 self._default_config_resolver = default_config_resolver 

581 self._imds_region_provider = imds_region_provider 

582 # Initializing _instance_metadata_region as None so we 

583 # can fetch region in a lazy fashion only when needed. 

584 self._instance_metadata_region = None 

585 

586 def merge_smart_defaults(self, config_store, mode, region_name): 

587 if mode == 'auto': 

588 mode = self.resolve_auto_mode(region_name) 

589 default_configs = ( 

590 self._default_config_resolver.get_default_config_values(mode) 

591 ) 

592 for config_var in default_configs: 

593 config_value = default_configs[config_var] 

594 method = getattr(self, f'_set_{config_var}', None) 

595 if method: 

596 method(config_store, config_value) 

597 

598 def resolve_auto_mode(self, region_name): 

599 current_region = None 

600 if os.environ.get('AWS_EXECUTION_ENV'): 

601 default_region = os.environ.get('AWS_DEFAULT_REGION') 

602 current_region = os.environ.get('AWS_REGION', default_region) 

603 if not current_region: 

604 if self._instance_metadata_region: 

605 current_region = self._instance_metadata_region 

606 else: 

607 try: 

608 current_region = self._imds_region_provider.provide() 

609 self._instance_metadata_region = current_region 

610 except Exception: 

611 pass 

612 

613 if current_region: 

614 if region_name == current_region: 

615 return 'in-region' 

616 else: 

617 return 'cross-region' 

618 return 'standard' 

619 

620 def _update_provider(self, config_store, variable, value): 

621 original_provider = config_store.get_config_provider(variable) 

622 default_provider = ConstantProvider(value) 

623 if isinstance(original_provider, ChainProvider): 

624 chain_provider_copy = copy.deepcopy(original_provider) 

625 chain_provider_copy.set_default_provider(default_provider) 

626 default_provider = chain_provider_copy 

627 elif isinstance(original_provider, BaseProvider): 

628 default_provider = ChainProvider( 

629 providers=[original_provider, default_provider] 

630 ) 

631 config_store.set_config_provider(variable, default_provider) 

632 

633 def _update_section_provider( 

634 self, config_store, section_name, variable, value 

635 ): 

636 section_provider_copy = copy.deepcopy( 

637 config_store.get_config_provider(section_name) 

638 ) 

639 section_provider_copy.set_default_provider( 

640 variable, ConstantProvider(value) 

641 ) 

642 config_store.set_config_provider(section_name, section_provider_copy) 

643 

644 def _set_retryMode(self, config_store, value): 

645 self._update_provider(config_store, 'retry_mode', value) 

646 

647 def _set_stsRegionalEndpoints(self, config_store, value): 

648 self._update_provider(config_store, 'sts_regional_endpoints', value) 

649 

650 def _set_s3UsEast1RegionalEndpoints(self, config_store, value): 

651 self._update_section_provider( 

652 config_store, 's3', 'us_east_1_regional_endpoint', value 

653 ) 

654 

655 def _set_connectTimeoutInMillis(self, config_store, value): 

656 self._update_provider(config_store, 'connect_timeout', value / 1000) 

657 

658 

659class BaseProvider: 

660 """Base class for configuration value providers. 

661 

662 A configuration provider has some method of providing a configuration 

663 value. 

664 """ 

665 

666 def provide(self): 

667 """Provide a config value.""" 

668 raise NotImplementedError('provide') 

669 

670 

671class ChainProvider(BaseProvider): 

672 """This provider wraps one or more other providers. 

673 

674 Each provider in the chain is called, the first one returning a non-None 

675 value is then returned. 

676 """ 

677 

678 def __init__(self, providers=None, conversion_func=None): 

679 """Initalize a ChainProvider. 

680 

681 :type providers: list 

682 :param providers: The initial list of providers to check for values 

683 when invoked. 

684 

685 :type conversion_func: None or callable 

686 :param conversion_func: If this value is None then it has no affect on 

687 the return type. Otherwise, it is treated as a function that will 

688 transform provided value. 

689 """ 

690 if providers is None: 

691 providers = [] 

692 self._providers = providers 

693 self._conversion_func = conversion_func 

694 

695 def __deepcopy__(self, memo): 

696 return ChainProvider( 

697 copy.deepcopy(self._providers, memo), self._conversion_func 

698 ) 

699 

700 def provide(self): 

701 """Provide the value from the first provider to return non-None. 

702 

703 Each provider in the chain has its provide method called. The first 

704 one in the chain to return a non-None value is the returned from the 

705 ChainProvider. When no non-None value is found, None is returned. 

706 """ 

707 for provider in self._providers: 

708 value = provider.provide() 

709 if value is not None: 

710 return self._convert_type(value) 

711 return None 

712 

713 def set_default_provider(self, default_provider): 

714 if self._providers and isinstance( 

715 self._providers[-1], ConstantProvider 

716 ): 

717 self._providers[-1] = default_provider 

718 else: 

719 self._providers.append(default_provider) 

720 

721 num_of_constants = sum( 

722 isinstance(provider, ConstantProvider) 

723 for provider in self._providers 

724 ) 

725 if num_of_constants > 1: 

726 logger.info( 

727 'ChainProvider object contains multiple ' 

728 'instances of ConstantProvider objects' 

729 ) 

730 

731 def _convert_type(self, value): 

732 if self._conversion_func is not None: 

733 return self._conversion_func(value) 

734 return value 

735 

736 def __repr__(self): 

737 return '[{}]'.format(', '.join([str(p) for p in self._providers])) 

738 

739 

740class InstanceVarProvider(BaseProvider): 

741 """This class loads config values from the session instance vars.""" 

742 

743 def __init__(self, instance_var, session): 

744 """Initialize InstanceVarProvider. 

745 

746 :type instance_var: str 

747 :param instance_var: The instance variable to load from the session. 

748 

749 :type session: :class:`botocore.session.Session` 

750 :param session: The botocore session to get the loaded configuration 

751 file variables from. 

752 """ 

753 self._instance_var = instance_var 

754 self._session = session 

755 

756 def __deepcopy__(self, memo): 

757 return InstanceVarProvider( 

758 copy.deepcopy(self._instance_var, memo), self._session 

759 ) 

760 

761 def provide(self): 

762 """Provide a config value from the session instance vars.""" 

763 instance_vars = self._session.instance_variables() 

764 value = instance_vars.get(self._instance_var) 

765 return value 

766 

767 def __repr__(self): 

768 return f'InstanceVarProvider(instance_var={self._instance_var}, session={self._session})' 

769 

770 

771class ScopedConfigProvider(BaseProvider): 

772 def __init__(self, config_var_name, session): 

773 """Initialize ScopedConfigProvider. 

774 

775 :type config_var_name: str or tuple 

776 :param config_var_name: The name of the config variable to load from 

777 the configuration file. If the value is a tuple, it must only 

778 consist of two items, where the first item represents the section 

779 and the second item represents the config var name in the section. 

780 

781 :type session: :class:`botocore.session.Session` 

782 :param session: The botocore session to get the loaded configuration 

783 file variables from. 

784 """ 

785 self._config_var_name = config_var_name 

786 self._session = session 

787 

788 def __deepcopy__(self, memo): 

789 return ScopedConfigProvider( 

790 copy.deepcopy(self._config_var_name, memo), self._session 

791 ) 

792 

793 def provide(self): 

794 """Provide a value from a config file property.""" 

795 scoped_config = self._session.get_scoped_config() 

796 if isinstance(self._config_var_name, tuple): 

797 section_config = scoped_config.get(self._config_var_name[0]) 

798 if not isinstance(section_config, dict): 

799 return None 

800 return section_config.get(self._config_var_name[1]) 

801 return scoped_config.get(self._config_var_name) 

802 

803 def __repr__(self): 

804 return f'ScopedConfigProvider(config_var_name={self._config_var_name}, session={self._session})' 

805 

806 

807class EnvironmentProvider(BaseProvider): 

808 """This class loads config values from environment variables.""" 

809 

810 def __init__(self, name, env): 

811 """Initialize with the keys in the dictionary to check. 

812 

813 :type name: str 

814 :param name: The key with that name will be loaded and returned. 

815 

816 :type env: dict 

817 :param env: Environment variables dictionary to get variables from. 

818 """ 

819 self._name = name 

820 self._env = env 

821 

822 def __deepcopy__(self, memo): 

823 return EnvironmentProvider( 

824 copy.deepcopy(self._name, memo), copy.deepcopy(self._env, memo) 

825 ) 

826 

827 def provide(self): 

828 """Provide a config value from a source dictionary.""" 

829 if self._name in self._env: 

830 return self._env[self._name] 

831 return None 

832 

833 def __repr__(self): 

834 return f'EnvironmentProvider(name={self._name}, env={self._env})' 

835 

836 

837class SectionConfigProvider(BaseProvider): 

838 """Provides a dictionary from a section in the scoped config 

839 

840 This is useful for retrieving scoped config variables (i.e. s3) that have 

841 their own set of config variables and resolving logic. 

842 """ 

843 

844 def __init__(self, section_name, session, override_providers=None): 

845 self._section_name = section_name 

846 self._session = session 

847 self._scoped_config_provider = ScopedConfigProvider( 

848 self._section_name, self._session 

849 ) 

850 self._override_providers = override_providers 

851 if self._override_providers is None: 

852 self._override_providers = {} 

853 

854 def __deepcopy__(self, memo): 

855 return SectionConfigProvider( 

856 copy.deepcopy(self._section_name, memo), 

857 self._session, 

858 copy.deepcopy(self._override_providers, memo), 

859 ) 

860 

861 def provide(self): 

862 section_config = self._scoped_config_provider.provide() 

863 if section_config and not isinstance(section_config, dict): 

864 logger.debug( 

865 "The %s config key is not a dictionary type, " 

866 "ignoring its value of: %s", 

867 self._section_name, 

868 section_config, 

869 ) 

870 return None 

871 for section_config_var, provider in self._override_providers.items(): 

872 provider_val = provider.provide() 

873 if provider_val is not None: 

874 if section_config is None: 

875 section_config = {} 

876 section_config[section_config_var] = provider_val 

877 return section_config 

878 

879 def set_default_provider(self, key, default_provider): 

880 provider = self._override_providers.get(key) 

881 if isinstance(provider, ChainProvider): 

882 provider.set_default_provider(default_provider) 

883 return 

884 elif isinstance(provider, BaseProvider): 

885 default_provider = ChainProvider( 

886 providers=[provider, default_provider] 

887 ) 

888 self._override_providers[key] = default_provider 

889 

890 def __repr__(self): 

891 return ( 

892 f'SectionConfigProvider(section_name={self._section_name}, ' 

893 f'session={self._session}, ' 

894 f'override_providers={self._override_providers})' 

895 ) 

896 

897 

898class ConstantProvider(BaseProvider): 

899 """This provider provides a constant value.""" 

900 

901 def __init__(self, value): 

902 self._value = value 

903 

904 def __deepcopy__(self, memo): 

905 return ConstantProvider(copy.deepcopy(self._value, memo)) 

906 

907 def provide(self): 

908 """Provide the constant value given during initialization.""" 

909 return self._value 

910 

911 def __repr__(self): 

912 return f'ConstantProvider(value={self._value})' 

913 

914 

915class ConfiguredEndpointProvider(BaseProvider): 

916 """Lookup an endpoint URL from environment variable or shared config file. 

917 

918 NOTE: This class is considered private and is subject to abrupt breaking 

919 changes or removal without prior announcement. Please do not use it 

920 directly. 

921 """ 

922 

923 _ENDPOINT_URL_LOOKUP_ORDER = [ 

924 'environment_service', 

925 'environment_global', 

926 'config_service', 

927 'config_global', 

928 ] 

929 

930 def __init__( 

931 self, 

932 full_config, 

933 scoped_config, 

934 client_name, 

935 environ=None, 

936 ): 

937 """Initialize a ConfiguredEndpointProviderChain. 

938 

939 :type full_config: dict 

940 :param full_config: This is the dict representing the full 

941 configuration file. 

942 

943 :type scoped_config: dict 

944 :param scoped_config: This is the dict representing the configuration 

945 for the current profile for the session. 

946 

947 :type client_name: str 

948 :param client_name: The name used to instantiate a client using 

949 botocore.session.Session.create_client. 

950 

951 :type environ: dict 

952 :param environ: A mapping to use for environment variables. If this 

953 is not provided it will default to use os.environ. 

954 """ 

955 self._full_config = full_config 

956 self._scoped_config = scoped_config 

957 self._client_name = client_name 

958 self._transformed_service_id = self._get_snake_case_service_id( 

959 self._client_name 

960 ) 

961 if environ is None: 

962 environ = os.environ 

963 self._environ = environ 

964 

965 def provide(self): 

966 """Lookup the configured endpoint URL. 

967 

968 The order is: 

969 

970 1. The value provided by a service-specific environment variable. 

971 2. The value provided by the global endpoint environment variable 

972 (AWS_ENDPOINT_URL). 

973 3. The value provided by a service-specific parameter from a services 

974 definition section in the shared configuration file. 

975 4. The value provided by the global parameter from a services 

976 definition section in the shared configuration file. 

977 """ 

978 for location in self._ENDPOINT_URL_LOOKUP_ORDER: 

979 logger.debug( 

980 'Looking for endpoint for %s via: %s', 

981 self._client_name, 

982 location, 

983 ) 

984 

985 endpoint_url = getattr(self, f'_get_endpoint_url_{location}')() 

986 

987 if endpoint_url: 

988 logger.info( 

989 'Found endpoint for %s via: %s.', 

990 self._client_name, 

991 location, 

992 ) 

993 return endpoint_url 

994 

995 logger.debug('No configured endpoint found.') 

996 return None 

997 

998 def _get_snake_case_service_id(self, client_name): 

999 # Get the service ID without loading the service data file, accounting 

1000 # for any aliases and standardizing the names with hyphens. 

1001 client_name = utils.SERVICE_NAME_ALIASES.get(client_name, client_name) 

1002 hyphenized_service_id = ( 

1003 utils.CLIENT_NAME_TO_HYPHENIZED_SERVICE_ID_OVERRIDES.get( 

1004 client_name, client_name 

1005 ) 

1006 ) 

1007 return hyphenized_service_id.replace('-', '_') 

1008 

1009 def _get_service_env_var_name(self): 

1010 transformed_service_id_env = self._transformed_service_id.upper() 

1011 return f'AWS_ENDPOINT_URL_{transformed_service_id_env}' 

1012 

1013 def _get_services_config(self): 

1014 if 'services' not in self._scoped_config: 

1015 return {} 

1016 

1017 section_name = self._scoped_config['services'] 

1018 services_section = self._full_config.get('services', {}).get( 

1019 section_name 

1020 ) 

1021 

1022 if not services_section: 

1023 error_msg = ( 

1024 f'The profile is configured to use the services ' 

1025 f'section but the "{section_name}" services ' 

1026 f'configuration does not exist.' 

1027 ) 

1028 raise InvalidConfigError(error_msg=error_msg) 

1029 

1030 return services_section 

1031 

1032 def _get_endpoint_url_config_service(self): 

1033 snakecase_service_id = self._transformed_service_id.lower() 

1034 return ( 

1035 self._get_services_config() 

1036 .get(snakecase_service_id, {}) 

1037 .get('endpoint_url') 

1038 ) 

1039 

1040 def _get_endpoint_url_config_global(self): 

1041 return self._scoped_config.get('endpoint_url') 

1042 

1043 def _get_endpoint_url_environment_service(self): 

1044 return EnvironmentProvider( 

1045 name=self._get_service_env_var_name(), env=self._environ 

1046 ).provide() 

1047 

1048 def _get_endpoint_url_environment_global(self): 

1049 return EnvironmentProvider( 

1050 name='AWS_ENDPOINT_URL', env=self._environ 

1051 ).provide()