-
Notifications
You must be signed in to change notification settings - Fork 0
TOPS-2500 - cap boto3 client timeouts & retries (env load hangs) #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kthhrv
wants to merge
4
commits into
master
Choose a base branch
from
kh/tops-2500-boto3-timeouts
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1a799d6
TOPS-2500 - cap boto3 client timeouts & retries (env load hangs)
kthhrv 5add2fe
TOPS-2500 - PR review: clarify retry count, pin behaviour, widen over…
kthhrv 46a6835
TOPS-2500 - PR review: make retries assertions robust to botocore in-…
kthhrv 64ad3bb
TOPS-2500 - PR review: clarify AWS_CLIENT_CONFIG is built at import
kthhrv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| """Shared botocore client configuration for envars' AWS calls. | ||
|
|
||
| Without an explicit ``Config``, ``boto3.client(...)`` inherits the botocore defaults: | ||
| ``connect_timeout=60s``, ``read_timeout=60s`` and legacy retries (up to 5 attempts). | ||
| A single stalled STS/KMS/SSM/CloudFormation endpoint can therefore hang for up to | ||
| ``5 x 60 = ~300s``, and a full resolve makes several such calls (STS for location | ||
| auto-detect, one KMS decrypt per secret, plus any ``parameter_store:`` / | ||
| ``cloudformation_export:`` lookups). These bounds turn a silent multi-minute hang into | ||
| a fast, legible failure while still tolerating a transient blip. | ||
|
|
||
| Override per-environment via the ``ENVARS_AWS_*`` variables if the defaults are too tight. | ||
| """ | ||
|
|
||
| import os | ||
|
|
||
| from botocore.config import Config | ||
|
|
||
|
|
||
| def _int_env(name: str, default: int) -> int: | ||
| """Reads a positive int from the environment, falling back to ``default``.""" | ||
| try: | ||
| value = int(os.environ[name]) | ||
| except (KeyError, ValueError): | ||
| return default | ||
| return value if value > 0 else default | ||
|
|
||
|
|
||
| def _build_config() -> Config: | ||
| """Builds the bounded AWS client config from the current ``ENVARS_AWS_*`` environment values. | ||
|
|
||
| Bounded so a stalled endpoint fails in seconds, not minutes. botocore treats | ||
| ``retries.max_attempts`` as the RETRY count, so ``max_attempts=2`` resolves to 3 total | ||
| attempts (1 initial + 2 retries, i.e. ``total_max_attempts=3``); with ``read_timeout=5s`` | ||
| the worst case is ~17s, versus botocore's default of ``read_timeout(60) x legacy 5 | ||
| attempts = ~300s``. A fully-unreachable endpoint fails the whole resolve regardless, so we | ||
| fail fast rather than wait it out. | ||
| """ | ||
| return Config( | ||
| connect_timeout=_int_env("ENVARS_AWS_CONNECT_TIMEOUT", 3), | ||
| read_timeout=_int_env("ENVARS_AWS_READ_TIMEOUT", 5), | ||
| retries={"max_attempts": _int_env("ENVARS_AWS_MAX_ATTEMPTS", 2), "mode": "standard"}, | ||
| ) | ||
|
|
||
|
|
||
| # Built once, at import. envars is a short-lived CLI, so reading ENVARS_AWS_* here (from the | ||
| # already-populated process environment) is equivalent to reading them per client — set any | ||
| # overrides in the environment before invoking envars, not after import. Every client is built | ||
| # from this shared instance; call _build_config() directly only if you need a dynamic re-read. | ||
| AWS_CLIENT_CONFIG = _build_config() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| """Tests for the shared bounded AWS client configuration (TOPS-2500).""" | ||
|
|
||
| from unittest.mock import MagicMock, patch | ||
|
|
||
| import boto3 | ||
| from botocore.exceptions import ReadTimeoutError | ||
|
|
||
| from src.envars import aws_config, cloud_utils | ||
| from src.envars.aws_cloudformation import CloudFormationExports | ||
| from src.envars.aws_kms import AWSKMSAgent | ||
| from src.envars.aws_ssm import SSMParameterStore | ||
|
|
||
|
|
||
| def test_default_config_is_bounded(): | ||
| """The shared config caps timeouts and retries well below botocore's defaults (60s/60s/5). | ||
|
|
||
| Asserts individual keys rather than full-dict equality on ``retries``: botocore rewrites | ||
| that dict in place when a client is built from the config (``max_attempts`` becomes | ||
| ``total_max_attempts``), so an equality assertion would be order- and version-fragile. | ||
| """ | ||
| cfg = aws_config.AWS_CLIENT_CONFIG | ||
| assert cfg.connect_timeout == 3 | ||
| assert cfg.read_timeout == 5 | ||
| assert cfg.retries["mode"] == "standard" | ||
|
|
||
|
|
||
| def test_default_makes_three_total_attempts(): | ||
| """max_attempts=2 resolves to 3 total HTTP attempts (1 initial + 2 retries). | ||
|
|
||
| botocore treats retries.max_attempts as the retry count, so total_max_attempts = N + 1 | ||
| (verified against botocore 1.39.4). Pinning the resolved value keeps the ~300s -> ~17s | ||
| worst case from silently regressing if that mapping ever changes, and makes the PR's | ||
| "3 total attempts" claim executable. Built from a fresh _build_config() so botocore's | ||
| in-place rewrite of retries doesn't leak into the shared AWS_CLIENT_CONFIG other tests read. | ||
| """ | ||
| client = boto3.client("sts", region_name="eu-west-1", config=aws_config._build_config()) | ||
| assert client.meta.config.retries["total_max_attempts"] == 3 | ||
|
|
||
|
|
||
| def test_env_overrides_flow_through(monkeypatch): | ||
| """All three ENVARS_AWS_* overrides wire through to the built config, not just read_timeout.""" | ||
| monkeypatch.setenv("ENVARS_AWS_CONNECT_TIMEOUT", "7") | ||
| monkeypatch.setenv("ENVARS_AWS_READ_TIMEOUT", "11") | ||
| monkeypatch.setenv("ENVARS_AWS_MAX_ATTEMPTS", "4") | ||
| client = boto3.client("sts", region_name="eu-west-1", config=aws_config._build_config()) | ||
| assert client.meta.config.connect_timeout == 7 | ||
| assert client.meta.config.read_timeout == 11 | ||
| assert client.meta.config.retries["total_max_attempts"] == 5 # input 4 -> 5 total (N+1) | ||
|
|
||
|
|
||
| def test_int_env_parses_positive_override(monkeypatch): | ||
| """_int_env returns a valid positive override from the environment.""" | ||
| monkeypatch.setenv("ENVARS_AWS_READ_TIMEOUT", "42") | ||
| assert aws_config._int_env("ENVARS_AWS_READ_TIMEOUT", 10) == 42 | ||
|
|
||
|
|
||
| def test_int_env_falls_back_when_unset_invalid_or_non_positive(monkeypatch): | ||
| """Unset, non-numeric, and zero/negative values all fall back to the default.""" | ||
| monkeypatch.delenv("ENVARS_AWS_READ_TIMEOUT", raising=False) | ||
| assert aws_config._int_env("ENVARS_AWS_READ_TIMEOUT", 10) == 10 # unset | ||
|
|
||
| monkeypatch.setenv("ENVARS_AWS_READ_TIMEOUT", "not-an-int") | ||
| assert aws_config._int_env("ENVARS_AWS_READ_TIMEOUT", 10) == 10 # invalid | ||
|
|
||
| monkeypatch.setenv("ENVARS_AWS_READ_TIMEOUT", "0") | ||
| assert aws_config._int_env("ENVARS_AWS_READ_TIMEOUT", 10) == 10 # non-positive | ||
|
|
||
|
|
||
| def test_kms_client_uses_bounded_config(): | ||
| """AWSKMSAgent constructs its client with the shared bounded config.""" | ||
| agent = AWSKMSAgent(region_name="eu-west-1") | ||
| assert agent.kms_client.meta.config.connect_timeout == 3 | ||
| assert agent.kms_client.meta.config.read_timeout == 5 | ||
|
|
||
|
|
||
| def test_ssm_client_uses_bounded_config(): | ||
| """SSMParameterStore constructs its client with the shared bounded config.""" | ||
| store = SSMParameterStore(region_name="eu-west-1") | ||
| assert store.client.meta.config.read_timeout == 5 | ||
|
|
||
|
|
||
| def test_cloudformation_client_uses_bounded_config(): | ||
| """CloudFormationExports constructs its client with the shared bounded config.""" | ||
| exports = CloudFormationExports(region_name="eu-west-1") | ||
| assert exports.client.meta.config.read_timeout == 5 | ||
|
|
||
|
|
||
| def test_get_aws_account_id_returns_none_on_timeout(): | ||
| """A stalled STS endpoint degrades to None instead of raising ReadTimeoutError. | ||
|
|
||
| Regression for the DSS-3428 crash: get_aws_account_id only caught NoCredentialsError, | ||
| so a stalled endpoint surfaced as an uncaught traceback out of `envars exec`. | ||
| """ | ||
| stalled = MagicMock() | ||
| stalled.get_caller_identity.side_effect = ReadTimeoutError(endpoint_url="https://sts.eu-west-1.amazonaws.com/") | ||
| with patch.object(boto3, "client", return_value=stalled): | ||
| assert cloud_utils.get_aws_account_id() is None | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.