【问题标题】:Resolving principal id in Azure AD to User,Service将 Azure AD 中的主体 ID 解析为用户、服务
【发布时间】:2021-02-28 03:27:44
【问题描述】:

我正在尝试将主体 ID 列表解析为用户/服务名称等详细信息。我有以下代码-

from azure.common.credentials import ServicePrincipalCredentials
from azure.graphrbac import GraphRbacManagementClient

TENANT = 'something.onmicrosoft.com'
TENANT_ID = '...'
CLIENT_ID = '...'
SECRET = '...'
List_of_Principal_IDs= ['...','...']
credentials = ServicePrincipalCredentials(
    client_id=CLIENT_ID,
    secret=SECRET,
    tenant=TENANT_ID,
    resource="https://graph.windows.net"
)
client = GraphRbacManagementClient(credentials, TENANT)

我尝试按照stackoverflow pages 之一的建议进行操作,但遇到了错误(见下文)。任何有关我如何将这些主体 ID 解析为人类可理解格式的指导都将不胜感激。

users = client.users.list(
         filter=f"principal_id eq '{List_of_Principal_IDs[0]}'"
     )
test = users.next()

错误 -

azure.graphrbac.models.graph_error_py3.GraphErrorException:属性“principal_id”不作为声明的属性或扩展属性存在。

users = client.objects.get_objects_by_object_ids(List_of_Principal_IDs[0])
user = users.next()

错误 -

msrest.exceptions.SerializationError:无法构建模型:无法 反序列化为对象:类型,AttributeError:'str'对象没有 属性“get”,DeserializationError:无法反序列化为 object: type, AttributeError: 'str' object has no attribute 'get'

【问题讨论】:

    标签: python-3.x azure azure-active-directory azure-ad-graph-api azure-rbac


    【解决方案1】:

    azure.graphrbac.models.graph_error_py3.GraphErrorException: 属性 “principal_id”不作为声明的属性或扩展存在 属性。

    关于这个错误,principal_id 中不存在properties of users。如果我没有误解,principal_id 表示用户的Object ID。但是Object_id不支持filter,需要使用get方法而不是list方法。

    user = client.users.get(upn_or_object_id)
    

    msrest.exceptions.SerializationError:无法构建模型:无法 反序列化为对象:类型,AttributeError:'str'对象没有 属性“get”,DeserializationError:无法反序列化为 object: type, AttributeError: 'str' object has no attribute 'get'

    get_objects_by_object_ids 需要GetObjectsParameters 类的参数,而不仅仅是一个列表。

    objects = graphrbac_client.objects.get_objects_by_object_ids({
        'object_ids': [list of object ids],
        'types': [list of object types]
    })
    

    【讨论】:

    • 如果您搜索已删除对象的对象ID,则将返回的对象转换为列表时,第二种方法会失败。因为我通常一次做 1000 多个,所以单独做通常会很慢。有什么你能想到的变通方法?
    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 2017-08-29
    相关资源
    最近更新 更多