Skip to content

Commit addc839

Browse files
authored
Ability to specify tenant_id (Azure#25207)
* Update azure_cli.py Updates the Azure CLI SDK To allow to create credential objects that are connected to tenants. This is EXTREMELY useful when you work with multiple tenants. * Update azure_cli.py * Update azure_cli.py * Update azure_cli.py * Update test_cli_credential.py * Update azure_cli.py * Update azure_cli.py * Update azure_cli.py * Update azure_cli.py * Update azure_cli.py * Update azure_cli.py
1 parent 4649826 commit addc839

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ class AzureCliCredential(object):
3636
3737
This requires previously logging in to Azure via "az login", and will use the CLI's currently logged in identity.
3838
"""
39+
def __init__(self, tenant_id: str = ""):
40+
object.__init__(self)
41+
42+
self.tenant_id = tenant_id
3943

4044
def __enter__(self):
4145
return self
@@ -67,7 +71,8 @@ def get_token(self, *scopes, **kwargs): # pylint: disable=no-self-use
6771

6872
resource = _scopes_to_resource(*scopes)
6973
command = COMMAND_LINE.format(resource)
70-
tenant = resolve_tenant("", **kwargs)
74+
tenant = resolve_tenant(default_tenant= self.tenant_id, **kwargs)
75+
7176
if tenant:
7277
command += " --tenant " + tenant
7378
output = _run_command(command)

sdk/identity/azure-identity/azure/identity/aio/_credentials/azure_cli.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class AzureCliCredential(AsyncContextManager):
3232
3333
This requires previously logging in to Azure via "az login", and will use the CLI's currently logged in identity.
3434
"""
35+
def __init__(self, tenant_id: str = ""):
36+
AsyncContextManager.__init__(self)
37+
38+
self.tenant_id = tenant_id
3539

3640
@log_get_token_async
3741
async def get_token(self, *scopes: str, **kwargs: "Any") -> "AccessToken":
@@ -55,7 +59,8 @@ async def get_token(self, *scopes: str, **kwargs: "Any") -> "AccessToken":
5559

5660
resource = _scopes_to_resource(*scopes)
5761
command = COMMAND_LINE.format(resource)
58-
tenant = resolve_tenant("", **kwargs)
62+
tenant = resolve_tenant(default_tenant= self.tenant_id, **kwargs)
63+
5964
if tenant:
6065
command += " --tenant " + tenant
6166
output = await _run_command(command)

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,36 @@ def test_timeout():
152152
AzureCliCredential().get_token("scope")
153153

154154

155+
def test_multitenant_authentication_class():
156+
default_tenant = "first-tenant"
157+
first_token = "***"
158+
second_tenant = "second-tenant"
159+
second_token = first_token * 2
160+
161+
def fake_check_output(command_line, **_):
162+
match = re.search("--tenant (.*)", command_line[-1])
163+
tenant = match.groups()[0] if match else default_tenant
164+
assert tenant in (default_tenant, second_tenant), 'unexpected tenant "{}"'.format(tenant)
165+
return json.dumps(
166+
{
167+
"expiresOn": datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"),
168+
"accessToken": first_token if tenant == default_tenant else second_token,
169+
"subscription": "some-guid",
170+
"tenant": tenant,
171+
"tokenType": "Bearer",
172+
}
173+
)
174+
175+
with mock.patch(CHECK_OUTPUT, fake_check_output):
176+
token = AzureCliCredential().get_token("scope")
177+
assert token.token == first_token
178+
179+
token = AzureCliCredential(tenant_id= default_tenant).get_token("scope")
180+
assert token.token == first_token
181+
182+
token = AzureCliCredential(tenant_id= second_tenant).get_token("scope")
183+
assert token.token == second_token
184+
155185
def test_multitenant_authentication():
156186
default_tenant = "first-tenant"
157187
first_token = "***"

0 commit comments

Comments
 (0)