Skip to content

Commit

Permalink
Ensure all botocore errors are caught during session creation
Browse files Browse the repository at this point in the history
Adds a catch for the base botocore.exception.BotoCoreError to the utils
that create boto3 client/resource connections, so individual modules no
longer need to wrap their module.client() calls in a try/except.
  • Loading branch information
hakbailey committed Jun 27, 2024
1 parent 91fe72b commit 3364c78
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
4 changes: 3 additions & 1 deletion plugins/module_utils/botocore.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def boto3_conn(module, conn_type=None, resource=None, region=None, endpoint=None
ValueError,
botocore.exceptions.ProfileNotFound, botocore.exceptions.PartialCredentialsError,
botocore.exceptions.NoCredentialsError, botocore.exceptions.ConfigParseError,
botocore.exceptions.NoRegionError
botocore.exceptions.NoRegionError, and finally the base botocore.exceptions.BotoCoreError
"""
try:
return _boto3_conn(conn_type=conn_type, resource=resource, region=region, endpoint=endpoint, **params)
Expand All @@ -108,6 +108,8 @@ def boto3_conn(module, conn_type=None, resource=None, region=None, endpoint=None
msg=f"The {module._name} module requires a region and none was found in configuration, "
"environment variables or module parameters",
)
except botocore.exceptions.BotoCoreError as e:
module.fail_json(msg=f"Couldn't connect to AWS: {to_native(e)}")


def _merge_botocore_config(config_a, config_b):
Expand Down
4 changes: 3 additions & 1 deletion plugins/plugin_utils/botocore.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def boto3_conn(plugin, conn_type=None, resource=None, region=None, endpoint=None
ValueError,
botocore.exceptions.ProfileNotFound, botocore.exceptions.PartialCredentialsError,
botocore.exceptions.NoCredentialsError, botocore.exceptions.ConfigParseError,
botocore.exceptions.NoRegionError
botocore.exceptions.NoRegionError, and finally the base botocore.BotoCoreError
"""

try:
Expand All @@ -47,6 +47,8 @@ def boto3_conn(plugin, conn_type=None, resource=None, region=None, endpoint=None
plugin.fail_aws(
"A region is required and none was found in configuration, environment variables or module parameters"
)
except botocore.exceptions.BotoCoreError as e:
plugin.fail_aws(to_native(e))


def get_aws_connection_info(plugin):
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/module_utils/botocore/test_boto3_conn.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ def test_boto3_conn_success(monkeypatch, aws_module, botocore_utils):
botocore.exceptions.NoRegionError(),
"The sentinel.MODULE_NAME module requires a region and none was found",
),
(
botocore.exceptions.UnknownServiceError(
service_name=sentinel.SERVICE_ERROR_NAME, known_service_names=sentinel.SERVICE_ERROR_KNOWN
),
None,
),
(botocore.exceptions.BotoCoreError(), None),
],
)
def test_boto3_conn_exception(monkeypatch, aws_module, botocore_utils, failure, custom_error):
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/plugin_utils/botocore/test_boto3_conn_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ def test_boto3_conn_success_plugin(monkeypatch, aws_plugin, botocore_utils):
(botocore.exceptions.NoCredentialsError(), None),
(botocore.exceptions.ConfigParseError(path=sentinel.PARSE_ERROR), None),
(botocore.exceptions.NoRegionError(), "The sentinel.PLUGIN_NAME plugin requires a region"),
(
botocore.exceptions.UnknownServiceError(
service_name=sentinel.SERVICE_ERROR_NAME, known_service_names=sentinel.SERVICE_ERROR_KNOWN
),
None,
),
(botocore.exceptions.BotoCoreError(), None),
],
)
def test_boto3_conn_exception_plugin(monkeypatch, aws_plugin, botocore_utils, failure, custom_error):
Expand Down Expand Up @@ -113,6 +120,13 @@ def test_boto3_conn_exception_plugin(monkeypatch, aws_plugin, botocore_utils, fa
botocore.exceptions.NoRegionError(),
"A region is required and none was found",
),
(
botocore.exceptions.UnknownServiceError(
service_name=sentinel.SERVICE_ERROR_NAME, known_service_names=sentinel.SERVICE_ERROR_KNOWN
),
None,
),
(botocore.exceptions.BotoCoreError(), None),
],
)
def test_boto3_conn_exception_no_plugin_name(monkeypatch, aws_plugin, botocore_utils, failure, custom_error):
Expand Down

0 comments on commit 3364c78

Please sign in to comment.