Skip to content

Commit 689e862

Browse files
committed
Support pod identity
1 parent fb3e21c commit 689e862

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

msal/managed_identity.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,9 @@ def _obtain_token_on_azure_vm(http_client, managed_identity, resource):
448448
}
449449
_adjust_param(params, managed_identity)
450450
resp = http_client.get(
451-
"http://169.254.169.254/metadata/identity/oauth2/token",
451+
os.getenv(
452+
"AZURE_POD_IDENTITY_AUTHORITY_HOST", "http://169.254.169.254"
453+
).strip("/") + "/metadata/identity/oauth2/token",
452454
params=params,
453455
headers={"Metadata": "true"},
454456
)

tests/test_mi.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,29 @@ def _test_happy_path(self, app, mocked_http, expires_in, resource="R"):
121121

122122
class VmTestCase(ClientTestCase):
123123

124-
def test_happy_path(self):
124+
def _test_happy_path(self) -> callable:
125125
expires_in = 7890 # We test a bigger than 7200 value here
126126
with patch.object(self.app._http_client, "get", return_value=MinimalResponse(
127127
status_code=200,
128128
text='{"access_token": "AT", "expires_in": "%s", "resource": "R"}' % expires_in,
129129
)) as mocked_method:
130-
self._test_happy_path(self.app, mocked_method, expires_in)
130+
super(VmTestCase, self)._test_happy_path(self.app, mocked_method, expires_in)
131+
return mocked_method
132+
133+
def test_happy_path_of_vm(self):
134+
self._test_happy_path().assert_called_with(
135+
'http://169.254.169.254/metadata/identity/oauth2/token',
136+
params={'api-version': '2018-02-01', 'resource': 'R'},
137+
headers={'Metadata': 'true'},
138+
)
139+
140+
@patch.dict(os.environ, {"AZURE_POD_IDENTITY_AUTHORITY_HOST": "http://localhost:1234//"})
141+
def test_happy_path_of_pod_identity(self):
142+
self._test_happy_path().assert_called_with(
143+
'http://localhost:1234/metadata/identity/oauth2/token',
144+
params={'api-version': '2018-02-01', 'resource': 'R'},
145+
headers={'Metadata': 'true'},
146+
)
131147

132148
def test_vm_error_should_be_returned_as_is(self):
133149
raw_error = '{"raw": "error format is undefined"}'

0 commit comments

Comments
 (0)