Skip to content

Commit 869a89d

Browse files
authored
[Identity] Loosen imds unreachable error check (#33928)
Signed-off-by: Paul Van Eck <[email protected]>
1 parent 0539665 commit 869a89d

File tree

4 files changed

+9
-6
lines changed

4 files changed

+9
-6
lines changed

sdk/identity/azure-identity/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Bugs Fixed
1010

1111
- Fixed the bug that `ClientAssertionCredential` constructor fails if kwargs are provided. ([#33673](https://github.com/Azure/azure-sdk-for-python/issues/33673))
12+
- `ManagedIdentityCredential` is more lenient with the error message it matches when falling through to the next credential in the chain in the case that Docker Desktop returns a 403 response when attempting to access the IMDS endpoint. ([#33928](https://github.com/Azure/azure-sdk-for-python/pull/33928))
1213

1314
### Other Changes
1415

sdk/identity/azure-identity/azure/identity/_credentials/imds.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ def _check_forbidden_response(ex: HttpResponseError) -> None:
4343
"""Special case handling for Docker Desktop.
4444
4545
Docker Desktop proxies all HTTP traffic, and if the IMDS endpoint is unreachable, it
46-
responds with a 403 with a message that contains "A socket operation was attempted to an unreachable network".
46+
responds with a 403 with a message that contains "unreachable".
4747
4848
:param ~azure.core.exceptions.HttpResponseError ex: The exception raised by the request
4949
:raises ~azure.core.exceptions.CredentialUnavailableError: When the IMDS endpoint is unreachable
5050
"""
5151
if ex.status_code == 403:
52-
if ex.message and "A socket operation was attempted to an unreachable network" in ex.message:
52+
if ex.message and "unreachable" in ex.message:
5353
error_message = f"ManagedIdentityCredential authentication unavailable. Error: {ex.message}"
5454
raise CredentialUnavailableError(message=error_message) from ex
5555

sdk/identity/azure-identity/tests/test_imds_credential.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ def send(request, **kwargs):
6565
assert error_message in ex.value.message
6666

6767

68-
def test_imds_request_failure_docker_desktop():
68+
@pytest.mark.parametrize("error_ending", ("network", "host", "foo"))
69+
def test_imds_request_failure_docker_desktop(error_ending):
6970
"""The credential should raise CredentialUnavailableError when a 403 with a specific message is received"""
7071

7172
error_message = (
7273
"connecting to 169.254.169.254:80: connecting to 169.254.169.254:80: dial tcp 169.254.169.254:80: "
73-
"connectex: A socket operation was attempted to an unreachable network." # cspell:disable-line
74+
f"connectex: A socket operation was attempted to an unreachable {error_ending}." # cspell:disable-line
7475
)
7576
probe = mock_response(status_code=403, json_payload={"error": error_message})
7677
transport = mock.Mock(send=mock.Mock(return_value=probe))

sdk/identity/azure-identity/tests/test_imds_credential_async.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,13 @@ async def send(request, **kwargs):
9898
assert error_message in ex.value.message
9999

100100

101-
async def test_imds_request_failure_docker_desktop():
101+
@pytest.mark.parametrize("error_ending", ("network", "host", "foo"))
102+
async def test_imds_request_failure_docker_desktop(error_ending):
102103
"""The credential should raise CredentialUnavailableError when a 403 with a specific message is received"""
103104

104105
error_message = (
105106
"connecting to 169.254.169.254:80: connecting to 169.254.169.254:80: dial tcp 169.254.169.254:80: "
106-
"connectex: A socket operation was attempted to an unreachable network." # cspell:disable-line
107+
f"connectex: A socket operation was attempted to an unreachable {error_ending}." # cspell:disable-line
107108
)
108109
probe = mock_response(status_code=403, json_payload={"error": error_message})
109110
transport = mock.Mock(send=mock.Mock(return_value=get_completed_future(probe)))

0 commit comments

Comments
 (0)