|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from smithy_aws_core.config.file_parser import ( |
| 8 | + FileType, |
| 9 | + parse_config_file, |
| 10 | + standardize, |
| 11 | +) |
| 12 | +from smithy_aws_core.config.merged_config import MergedConfig |
| 13 | + |
| 14 | +_DEFAULT_CONFIG_FILE = "~/.aws/config" |
| 15 | +_DEFAULT_CREDENTIALS_FILE = "~/.aws/credentials" |
| 16 | +_CONFIG_FILE_ENV_VAR = "AWS_CONFIG_FILE" |
| 17 | +_CREDENTIALS_FILE_ENV_VAR = "AWS_SHARED_CREDENTIALS_FILE" |
| 18 | + |
| 19 | + |
| 20 | +def _resolve_config_paths( |
| 21 | + config_file_path: Path | None = None, |
| 22 | + credentials_file_path: Path | None = None, |
| 23 | +) -> tuple[Path, Path]: |
| 24 | + """Resolve the final config and credentials file paths. |
| 25 | +
|
| 26 | + Resolution order for each path: |
| 27 | + 1. Explicit argument (if provided) |
| 28 | + 2. Environment variable (AWS_CONFIG_FILE / AWS_SHARED_CREDENTIALS_FILE) |
| 29 | + 3. Default (~/.aws/config / ~/.aws/credentials) |
| 30 | +
|
| 31 | + The ~ is expanded to the user's home directory. |
| 32 | +
|
| 33 | + :param config_file_path: Override path for config file. |
| 34 | + :param credentials_file_path: Override path for credentials file. |
| 35 | + :returns: Tuple of (resolved_config_path, resolved_credentials_path). |
| 36 | + """ |
| 37 | + config_path = ( |
| 38 | + config_file_path |
| 39 | + or Path(os.environ.get(_CONFIG_FILE_ENV_VAR, _DEFAULT_CONFIG_FILE)) |
| 40 | + ).expanduser() |
| 41 | + |
| 42 | + credentials_path = ( |
| 43 | + credentials_file_path |
| 44 | + or Path(os.environ.get(_CREDENTIALS_FILE_ENV_VAR, _DEFAULT_CREDENTIALS_FILE)) |
| 45 | + ).expanduser() |
| 46 | + |
| 47 | + return config_path, credentials_path |
| 48 | + |
| 49 | + |
| 50 | +async def load_config( |
| 51 | + config_file_path: Path | None = None, |
| 52 | + credentials_file_path: Path | None = None, |
| 53 | +) -> MergedConfig: |
| 54 | + """Load and merge AWS config and credentials files. |
| 55 | +
|
| 56 | + Parses both files, standardizes them, and returns a merged |
| 57 | + MergedConfig ready for querying. |
| 58 | +
|
| 59 | + :param config_file_path: Override path for config file. |
| 60 | + Defaults to AWS_CONFIG_FILE env var or ~/.aws/config. |
| 61 | + :param credentials_file_path: Override path for credentials file. |
| 62 | + Defaults to AWS_SHARED_CREDENTIALS_FILE env var or ~/.aws/credentials. |
| 63 | + :returns: A MergedConfig with merged profiles from both files. |
| 64 | + """ |
| 65 | + |
| 66 | + config_path, credentials_path = _resolve_config_paths( |
| 67 | + config_file_path, credentials_file_path |
| 68 | + ) |
| 69 | + |
| 70 | + raw_config = await parse_config_file(str(config_path)) |
| 71 | + raw_credentials = await parse_config_file(str(credentials_path)) |
| 72 | + |
| 73 | + std_config = standardize(raw_config, FileType.CONFIG) |
| 74 | + std_credentials = standardize(raw_credentials, FileType.CREDENTIALS) |
| 75 | + |
| 76 | + return MergedConfig(std_config, std_credentials) |
0 commit comments