Skip to content
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

Fixed ambiguous error message raised when resolving region from IMDS #9073

Merged
merged 1 commit into from
Nov 13, 2024
Merged
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
5 changes: 5 additions & 0 deletions .changes/next-release/bugfix-IMDS-70100.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "bugfix",
"category": "IMDS",
"description": "Fixed ambiguous error raised when failing to resolve a region from IMDS."
}
15 changes: 12 additions & 3 deletions awscli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from botocore.useragent import UserAgentComponent
from botocore.utils import resolve_imds_endpoint_mode
from botocore.utils import IMDSFetcher
from botocore.utils import BadIMDSRequestError
from botocore.configprovider import BaseProvider

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -171,9 +172,17 @@ def retrieve_region(self):
region = self._get_region()
return region
except self._RETRIES_EXCEEDED_ERROR_CLS:
logger.debug("Max number of attempts exceeded (%s) when "
"attempting to retrieve data from metadata service.",
self._num_attempts)
logger.debug(
"Max number of attempts exceeded (%s) when "
"attempting to retrieve data from metadata service.",
self._num_attempts
)
except BadIMDSRequestError as e:
logger.debug(
"Failed to retrieve a region from IMDS. "
"Region detection may not be supported from this endpoint: "
"%s", e.request.url
)
return None

def _get_region(self):
Expand Down
17 changes: 15 additions & 2 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ def add_get_region_imds_response(self, region=None):
region = self._region
self.add_imds_response(body=region.encode('utf-8'))

def add_imds_token_response(self):
self.add_imds_response(status_code=200, body=b'token')
def add_imds_token_response(self, status_code=200, body=b'token'):
self.add_imds_response(status_code=status_code, body=body)

def add_imds_connection_error(self, exception):
self._imds_responses.append(exception)
Expand Down Expand Up @@ -461,6 +461,19 @@ def test_exhaust_retries_on_region_request(self):
num_attempts=1).retrieve_region()
self.assertEqual(result, None)

def test_400_response_returns_none(self):
# Response from endpoint providing a 400 during
# token resolution implies the (likely third-party)
# endpoint may not support IMDS v2.
#
# We treat this as terminal because retries will
# unnecessarily delay failure for a incompatible endpoint.
self.add_imds_token_response(body=b"Bad Request", status_code=400)
result = InstanceMetadataRegionFetcher(
num_attempts=2
).retrieve_region()
self.assertEqual(result, None)


class TestIMDSRegionProvider(BaseIMDSRegionTest):
def assert_does_provide_expected_value(self, fetcher_region=None,
Expand Down
Loading