Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions packages/smithy-aws-core/src/smithy_aws_core/config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

import os
from pathlib import Path

from smithy_aws_core.config.file_parser import (
FileType,
parse_config_file,
standardize,
)
from smithy_aws_core.config.merged_config import MergedConfig

_DEFAULT_CONFIG_FILE = "~/.aws/config"
_DEFAULT_CREDENTIALS_FILE = "~/.aws/credentials"
_CONFIG_FILE_ENV_VAR = "AWS_CONFIG_FILE"
_CREDENTIALS_FILE_ENV_VAR = "AWS_SHARED_CREDENTIALS_FILE"


def _resolve_config_paths(
config_file_path: Path | None = None,
credentials_file_path: Path | None = None,
) -> tuple[Path, Path]:
"""Resolve the final config and credentials file paths.

Resolution order for each path:
1. Explicit argument (if provided)
2. Environment variable (AWS_CONFIG_FILE / AWS_SHARED_CREDENTIALS_FILE)
3. Default (~/.aws/config / ~/.aws/credentials)

The ~ is expanded to the user's home directory.

:param config_file_path: Override path for config file.
:param credentials_file_path: Override path for credentials file.
:returns: Tuple of (resolved_config_path, resolved_credentials_path).
"""
config_path = (
config_file_path
or Path(os.environ.get(_CONFIG_FILE_ENV_VAR, _DEFAULT_CONFIG_FILE))
).expanduser()

credentials_path = (
credentials_file_path
or Path(os.environ.get(_CREDENTIALS_FILE_ENV_VAR, _DEFAULT_CREDENTIALS_FILE))
).expanduser()

return config_path, credentials_path


async def load_config(
Comment thread
SamRemis marked this conversation as resolved.
config_file_path: Path | None = None,
credentials_file_path: Path | None = None,
) -> MergedConfig:
"""Load and merge AWS config and credentials files.

Parses both files, standardizes them, and returns a merged
MergedConfig ready for querying.

:param config_file_path: Override path for config file.
Defaults to AWS_CONFIG_FILE env var or ~/.aws/config.
:param credentials_file_path: Override path for credentials file.
Defaults to AWS_SHARED_CREDENTIALS_FILE env var or ~/.aws/credentials.
:returns: A MergedConfig with merged profiles from both files.
"""

config_path, credentials_path = _resolve_config_paths(
config_file_path, credentials_file_path
)

raw_config = await parse_config_file(str(config_path))
raw_credentials = await parse_config_file(str(credentials_path))

std_config = standardize(raw_config, FileType.CONFIG)
std_credentials = standardize(raw_credentials, FileType.CREDENTIALS)

return MergedConfig(std_config, std_credentials)
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0

"""Config related exceptions"""


class ConfigParseError(Exception):
"""Raised when a config file cannot be parsed due to invalid syntax."""
Loading
Loading