代码之家  ›  专栏  ›  技术社区  ›  4c74356b41

如何获取当前上下文(登录实体)objectId

  •  0
  • 4c74356b41  · 技术社区  · 7 年前

    graphrbac_client = GraphRbacManagementClient(
        credentials = ServicePrincipalCredentials(
            client_id = CLIENT,
            secret = KEY,
            tenant = TENANT_ID,
            resource = "https://graph.windows.net"
        ),
        TENANT_ID
    )
    for sp in graphrbac_client.service_principals.list():
      if sp.app_id == graphrbac_client.config.credentials.id:
        print('found it')
    

    它可以工作,但对应用程序要求太多权限(我只设法让它与Directory一起工作。ReadAll不与Directory一起工作 Application.ReadWrite.All ,出于某种原因)。我找到的所有方法似乎都需要预先了解objectId。。。这正是我想要找回的。

    使用 this

    def resolve_service_principal(identifier):
        """Get an object_id from a client_id.
        """
        graphrbac_credentials = ServicePrincipalCredentials(
            client_id=os.environ['AZURE_CLIENT_ID'],
            secret=os.environ['AZURE_CLIENT_SECRET'],
            tenant=os.environ['AZURE_TENANT_ID'],
            resource="https://graph.windows.net"
        )
        graphrbac_client = GraphRbacManagementClient(
            graphrbac_credentials,
            os.environ['AZURE_TENANT_ID']
        )
    
        result = list(graphrbac_client.service_principals.list(filter="servicePrincipalNames/any(c:c eq '{}')".format(identifier)))
        if result:
            return result[0].object_id
        raise RuntimeError("Unable to get object_id from client_id")
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Laurent Mazuel    7 年前

    您至少需要包0.50.0,并且 signed_in_user.get

    user = graphrbac_client.signed_in_user.get()
    assert user.mail_nickname.startswith("admin")
    

    https://docs.microsoft.com/en-us/python/api/azure-graphrbac/azure.graphrbac.operations.signedinuseroperations?view=azure-python#get-custom-headers-none--raw-false----operation-config-

    (我在微软的SDK团队工作)

    编辑:似乎这只对用户有效,然后我会尝试:

        objects = graphrbac_client.objects.get_objects_by_object_ids({
            'object_ids': [CLIENT],
            'types': ['ServicePrincipal']
        })