【问题标题】:How create/delete secrets of Azure service principal by using another service principal with REST API or Python SDK?如何通过 REST API 或 Python SDK 使用另一个服务主体来创建/删除 Azure 服务主体的机密?
【发布时间】:2021-10-28 11:45:08
【问题描述】:

我有 2 个应用注册(2 个服务主体)。 首先,我将其用作拥有令牌的凭据。 我需要从我的 Python 脚本中创建和删除第二个服务主体的机密。 不幸的是,我在文档中没有找到这样的例子。 我该怎么做?

【问题讨论】:

  • 完成)谢谢你1次)
  • 不! @Diggy,很高兴能帮上忙..

标签: python azure azure-rest-api service-principal


【解决方案1】:

您可以使用以下代码满足您的要求:

添加 Client_Secret:

from azure.identity import ClientSecretCredential
from msgraph.core import GraphClient 
import json
clientid= "Serviceprincipal1"
clientsecret = "secret"
tenantid = "tenantId"
credentials=ClientSecretCredential(tenant_id=tenantid,client_id=clientid,client_secret=clientsecret) 
graph_client = GraphClient(credential=credentials)

#get details of another service principal by providing the object id of the application
app = graph_client.get('/applications/serviceprincipal2objectid')
print(app.json())
#add new client sceret to that ad app
body={
    "passwordCredential": {
    "displayName": "NewPaasswordCreatedfromPythonSDK"
  }
}
addpass=graph_client.post('/applications/serviceprincipal2objectid/addPassword',json=json.dumps(body))
print("HTTP_request_Response:",addpass.status_code)

输出:

删除 Client_Secret:

#remove a client secret for that ad app
body= {
    "keyId": "1636f0ce-1b8c-46a0-a580-d0df086b91c7"## keyid of the key added earlier
    }
removepass=graph_client.post('/applications/serviceprincipal2objectid/removePassword',json=body)
print("HTTP_request_Response:",removepass.status_code)

输出:

注意:MSGRAPH-core python sdk 仅在预览版中使用,您必须使用pip install msgraph-core 进行安装

【讨论】:

  • 非常感谢你的好榜样!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-31
  • 1970-01-01
  • 2020-10-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多