Azure / Azure/azure-rest-api-specs

Returns none when code is authenticated with the service principal

Open
#21,920 10 comments 0 reactions 0 assignees View on GitHub
customer-reported needs-team-attention question Service Attention
Dominant language
TypeSpec
Stars
3.1k
Forks
6k
Avg merge
2d 22h
Merged PRs (30d)
444

Description

**Describe the bug**

We are trying to run code using the azure python SDK and are getting different results when we are authenticated with a service principal instead of a user.
When the below code is run with regular username pass authentication, the last line of the code, sub[‘domain’], will return the list of domains in the tenant. When the same code is run when authenticated as a service principal, sub[‘domain’] returns ‘None’ even though there are domains values that exist for the tenant. This makes me think there is some role or api permission that a service principal might not be granted by default.

**To Reproduce**

Steps to reproduce the behavior:
The code snippet was enclosed below. The below code works with username, password authentication, whereas it is returning none when authenticating with service principal.

**Expected behavior**

domains[sub['tenant_id']][0] should return the list of domains in the tenant

**Screenshots**

import functools
import logging
from typing import Any, Dict, List
from azure import identity # type: ignore
from azure.mgmt import resource, storage, web
from azure.storage import blob
from azure.identity import ClientSecretCredential
from azure.mgmt.compute import ComputeManagementClient

creds = identity.AzureCliCredential()
client = resource.SubscriptionClient(credential=creds)

subs = { s.subscription_id: s.as_dict() for s in client.subscriptions.list()}

**update subscription info with tenant domains**
domains = {t.tenant_id: t.domains for t in client.tenants.list()}

for sub in subs.values():
sub['domain'] = domains[sub['tenant_id']][0]

**Additional context**

Q1) Is the service principal being used is in the same tenant, with the same permissions, as the user account that was used to successfully list domains? I believe it is yes, please confirm this.
A) Yes

Q2) Is the service principal authentication being attempted with the AzureCliCredential, as shown in your example code?
A) Yes

Contributor guide

Open the contributing guide

Research direction

Start by reviewing the AzureCliCredential setup and the SubscriptionClient entry points in the report, especially subscriptions.list() and tenants.list(). Compare the results for user and service-principal authentication, then verify whether the tenant-domain response is expected for the service principal and document the required permissions or corrected behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, python
Domain
api, cloud
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.