Skip to content

Commit cc6bf2a

Browse files
authored
[Identity] Fix live ACI test (#40434)
Signed-off-by: Paul Van Eck <[email protected]>
1 parent eae4b41 commit cc6bf2a

File tree

2 files changed

+78
-45
lines changed

2 files changed

+78
-45
lines changed

sdk/identity/azure-identity/tests/integration/azure-kubernetes-service/app.py

Lines changed: 75 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,65 @@
44
# ------------------------------------
55
import os
66
import asyncio
7+
import argparse
78

89
from azure.identity import ManagedIdentityCredential
910
from azure.identity.aio import ManagedIdentityCredential as AsyncManagedIdentityCredential
1011
from azure.storage.blob import BlobServiceClient
1112
from azure.storage.blob.aio import BlobServiceClient as AsyncBlobServiceClient
1213

1314

14-
def run_sync():
15-
credential = ManagedIdentityCredential()
15+
def run_sync(identity_type="system"):
16+
"""Run synchronous authentication using the specified identity type.
17+
18+
:param str identity_type: The type of managed identity to use ("system" or "user")
19+
"""
20+
if identity_type == "user" and os.environ.get("IDENTITY_USER_DEFINED_IDENTITY_CLIENT_ID"):
21+
credential = ManagedIdentityCredential(client_id=os.environ.get("IDENTITY_USER_DEFINED_IDENTITY_CLIENT_ID"))
22+
storage_name = os.environ.get("IDENTITY_STORAGE_NAME_USER_ASSIGNED", os.environ.get("IDENTITY_STORAGE_NAME"))
23+
else:
24+
# Default to system-assigned identity
25+
credential = ManagedIdentityCredential()
26+
storage_name = os.environ.get("IDENTITY_STORAGE_NAME")
27+
28+
if not storage_name:
29+
print("Storage account name not found in environment variables")
30+
return False
1631

1732
client = BlobServiceClient(
18-
account_url=f"https://{os.environ['IDENTITY_STORAGE_NAME']}.blob.core.windows.net",
33+
account_url=f"https://{storage_name}.blob.core.windows.net",
1934
credential=credential,
2035
)
2136

2237
containers = client.list_containers()
2338
for container in containers:
2439
print(container["name"])
2540

26-
if os.environ.get("IDENTITY_USER_DEFINED_IDENTITY_CLIENT_ID") and os.environ.get(
27-
"IDENTITY_STORAGE_NAME_USER_ASSIGNED"
28-
):
29-
credential = ManagedIdentityCredential(client_id=os.environ.get("IDENTITY_USER_DEFINED_IDENTITY_CLIENT_ID"))
30-
31-
client = BlobServiceClient(
32-
account_url=f"https://{os.environ['IDENTITY_STORAGE_NAME_USER_ASSIGNED']}.blob.core.windows.net",
33-
credential=credential,
34-
)
41+
print(f"Successfully acquired token with ManagedIdentityCredential (identity_type={identity_type})")
42+
return True
3543

36-
containers = client.list_containers()
37-
for container in containers:
38-
print(container["name"])
3944

40-
print(f"Successfully acquired token with ManagedIdentityCredential")
45+
async def run_async(identity_type="system"):
46+
"""Run asynchronous authentication using the specified identity type.
4147
48+
:param str identity_type: The type of managed identity to use ("system" or "user")
49+
"""
50+
if identity_type == "user" and os.environ.get("IDENTITY_USER_DEFINED_IDENTITY_CLIENT_ID"):
51+
credential = AsyncManagedIdentityCredential(
52+
client_id=os.environ.get("IDENTITY_USER_DEFINED_IDENTITY_CLIENT_ID")
53+
)
54+
storage_name = os.environ.get("IDENTITY_STORAGE_NAME_USER_ASSIGNED", os.environ.get("IDENTITY_STORAGE_NAME"))
55+
else:
56+
# Default to system-assigned identity
57+
credential = AsyncManagedIdentityCredential()
58+
storage_name = os.environ.get("IDENTITY_STORAGE_NAME")
4259

43-
async def run_async():
44-
credential = AsyncManagedIdentityCredential()
60+
if not storage_name:
61+
print("Storage account name not found in environment variables")
62+
return False
4563

4664
client = AsyncBlobServiceClient(
47-
account_url=f"https://{os.environ['IDENTITY_STORAGE_NAME']}.blob.core.windows.net",
65+
account_url=f"https://{storage_name}.blob.core.windows.net",
4866
credential=credential,
4967
)
5068

@@ -54,29 +72,44 @@ async def run_async():
5472
await client.close()
5573
await credential.close()
5674

57-
if os.environ.get("IDENTITY_USER_DEFINED_IDENTITY_CLIENT_ID") and os.environ.get(
58-
"IDENTITY_STORAGE_NAME_USER_ASSIGNED"
59-
):
60-
credential = AsyncManagedIdentityCredential(
61-
client_id=os.environ.get("IDENTITY_USER_DEFINED_IDENTITY_CLIENT_ID")
62-
)
63-
64-
client = AsyncBlobServiceClient(
65-
account_url=f"https://{os.environ['IDENTITY_STORAGE_NAME_USER_ASSIGNED']}.blob.core.windows.net",
66-
credential=credential,
67-
)
68-
69-
async for container in client.list_containers():
70-
print(container["name"])
71-
72-
await client.close()
73-
await credential.close()
74-
75-
print("Successfully acquired token with async ManagedIdentityCredential")
75+
print(f"Successfully acquired token with async ManagedIdentityCredential (identity_type={identity_type})")
76+
return True
7677

7778

7879
if __name__ == "__main__":
79-
run_sync()
80-
asyncio.run(run_async())
81-
82-
print("Passed!")
80+
parser = argparse.ArgumentParser(description="Test managed identity authentication in AKS")
81+
parser.add_argument(
82+
"--identity-type",
83+
choices=["system", "user", "both"],
84+
default="both",
85+
help="Type of managed identity to use (system, user, or both)",
86+
)
87+
args = parser.parse_args()
88+
89+
success = True
90+
91+
if args.identity_type in ["system", "both"]:
92+
print("Testing with system-assigned managed identity:")
93+
if not run_sync("system"):
94+
success = False
95+
96+
if args.identity_type in ["user", "both"] and os.environ.get("IDENTITY_USER_DEFINED_IDENTITY_CLIENT_ID"):
97+
print("\nTesting with user-assigned managed identity:")
98+
if not run_sync("user"):
99+
success = False
100+
101+
if args.identity_type in ["system", "both"]:
102+
print("\nTesting with async system-assigned managed identity:")
103+
if not asyncio.run(run_async("system")):
104+
success = False
105+
106+
if args.identity_type in ["user", "both"] and os.environ.get("IDENTITY_USER_DEFINED_IDENTITY_CLIENT_ID"):
107+
print("\nTesting with async user-assigned managed identity:")
108+
if not asyncio.run(run_async("user")):
109+
success = False
110+
111+
if success:
112+
print("\nPassed!")
113+
else:
114+
print("\nFailed!")
115+
exit(1)

sdk/identity/azure-identity/tests/integration/test_azure_container_instance.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def test_azure_container_instance(self):
2323

2424
# Using "script" as a workaround for "az container exec" requiring a tty.
2525
# https://github.com/Azure/azure-cli/issues/17530
26-
command = (
27-
f"{az_path} container exec -g {resource_group} -n {container_instance_name} --exec-command 'python /app.py'"
28-
)
26+
# Note: Currently only testing user-assigned identity as system-assigned identity doesn't seem to work if both a user-assigned
27+
# identity and system-assigned identity are assigned to the container instance.
28+
command = f"{az_path} container exec -g {resource_group} -n {container_instance_name} --exec-command 'python /app.py --identity-type user'"
2929
output = run_command(
3030
[
3131
"script",

0 commit comments

Comments
 (0)