Azure Python SDK: 获取角色分配类型(可用 vs 活动)

huangapple go评论166阅读模式
英文:

Azure Python SDK: Get Role Assignment Type (eligible vs active)

问题

使用Azure Python SDK,对于每个分配给资源的角色分配,我想获取分配类型,即合格分配或活动分配。

RoleAssignment Class不提供此信息。

在Azure门户中,转到PIM -> Azure资源 -> (选择资源)-> 分配,我可以看到“合格分配”和“活动分配”选项卡:

Azure Python SDK: 获取角色分配类型(可用 vs 活动)

我已经搜索了所有相关的Azure Python SDK服务,但找不到一个可以提供分配类型(合格 vs 活动)的方法。

备选方案:如果Azure Python SDK没有提供解决方案,是否提供了一个API端点来获取此类信息?

英文:

Using the Azure Python SDK, for each role assignment to a resource, I want to get the assignment type i.e. eligible assignment or active assignment.

The RoleAssignment Class does not provide this information.

Using the Azure Portal, going to PIM -> Azure resources -> (Selecting a resource) -> Assignments, I get a tab "Eligible assignments" and "Active assignment":

Azure Python SDK: 获取角色分配类型(可用 vs 活动)

I searched through all relevant Azure Python SDK services but could not find one, that provides me the type of an assignment (eligible vs active).

Alternative: If there is no solution provided with Azure Python SDK, is there an API endpoint provided that kind of information?

答案1

得分: 1

你需要使用两个单独的API端点来获取Azure资源的合格和活动角色分配。

合格的角色分配:

GET https://management.azure.com/{scope}/providers/Microsoft.Authorization/roleEligibilityScheduleInstances?api-version=2020-10-01

活动角色分配:

GET https://management.azure.com/{scope}/providers/Microsoft.Authorization/roleAssignmentScheduleInstances?api-version=2020-10-01

我有一个存储帐户,下面是合格角色分配:

Azure Python SDK: 获取角色分配类型(可用 vs 活动)

要获取此存储帐户的合格角色分配,我运行了以下Python代码,并成功获得结果:

  1. from azure.identity import ClientSecretCredential
  2. import requests
  3. # 用实际值替换以下值
  4. tenant_id = "tenantID"
  5. client_id = "appID"
  6. client_secret = "secret"
  7. # 用实际URL替换以下URL
  8. url = "https://management.azure.com/subscriptions/<subId>/resourceGroups/<rg_name>/providers/Microsoft.Storage/storageAccounts/sristorageacc11/providers/Microsoft.Authorization/roleEligibilityScheduleInstances?api-version=2020-10-01"
  9. # 创建一个ClientSecretCredential实例
  10. credential = ClientSecretCredential(
  11. tenant_id=tenant_id,
  12. client_id=client_id,
  13. client_secret=client_secret
  14. )
  15. # 获取Azure管理API的访问令牌
  16. token = credential.get_token("https://management.azure.com/.default")
  17. headers = {
  18. "Authorization": "Bearer " + token.token,
  19. "Content-Type": "application/json"
  20. }
  21. response = requests.get(url, headers=headers)
  22. if response.status_code == 200:
  23. data = response.json()
  24. for item in data["value"]:
  25. principal_display_name = item["properties"]["expandedProperties"]["principal"]["displayName"]
  26. role_definition_display_name = item["properties"]["expandedProperties"]["roleDefinition"]["displayName"]
  27. principal_type = item["properties"]["expandedProperties"]["principal"]["type"]
  28. print("Principal Display Name:", principal_display_name)
  29. print("Principal Type:", principal_type)
  30. print("Role Definition Display Name:", role_definition_display_name)
  31. print("-" * 50) # 用于清晰分隔的线
  32. else:
  33. print("Request failed with status code:", response.status_code)
  34. print("Response content:", response.content)

响应:

Azure Python SDK: 获取角色分配类型(可用 vs 活动)

类似地,我有以下活动角色分配的存储帐户:

Azure Python SDK: 获取角色分配类型(可用 vs 活动)

要获取此存储帐户的活动角色分配,我运行了以下Python代码,通过更改URL成功获得结果:

  1. from azure.identity import ClientSecretCredential
  2. import requests
  3. # 用实际值替换以下值
  4. tenant_id = "tenantID"
  5. client_id = "appID"
  6. client_secret = "secret"
  7. # 用实际URL替换以下URL
  8. url = "https://management.azure.com/subscriptions/<subId>/resourceGroups/<rg_name>/providers/Microsoft.Storage/storageAccounts/sristorageacc11/providers/Microsoft.Authorization/roleAssignmentScheduleInstances?api-version=2020-10-01"
  9. # 创建一个ClientSecretCredential实例
  10. credential = ClientSecretCredential(
  11. tenant_id=tenant_id,
  12. client_id=client_id,
  13. client_secret=client_secret
  14. )
  15. # 获取Azure管理API的访问令牌
  16. token = credential.get_token("https://management.azure.com/.default")
  17. headers = {
  18. "Authorization": "Bearer " + token.token,
  19. "Content-Type": "application/json"
  20. }
  21. response = requests.get(url, headers=headers)
  22. if response.status_code == 200:
  23. data = response.json()
  24. for item in data["value"]:
  25. principal_display_name = item["properties"]["expandedProperties"]["principal"]["displayName"]
  26. role_definition_display_name = item["properties"]["expandedProperties"]["roleDefinition"]["displayName"]
  27. principal_type = item["properties"]["expandedProperties"]["principal"]["type"]
  28. print("Principal Display Name:", principal_display_name)
  29. print("Principal Type:", principal_type)
  30. print("Role Definition Display Name:", role_definition_display_name)
  31. print("-" * 50) # 用于清晰分隔的线
  32. else:
  33. print("Request failed with status code:", response.status_code)
  34. print("Response content:", response.content)

响应:

Azure Python SDK: 获取角色分配类型(可用 vs 活动)

英文:

You need to use two separate API endpoints to get eligible and active role assignments of Azure resources.

Eligible role assignments:

  1. GET https://management.azure.com/{scope}/providers/Microsoft.Authorization/roleEligibilityScheduleInstances?api-version=2020-10-01

Active Role assignments:

  1. GET https://management.azure.com/{scope}/providers/Microsoft.Authorization/roleAssignmentScheduleInstances?api-version=2020-10-01

I have one storage account with below Eligible role assignments:

Azure Python SDK: 获取角色分配类型(可用 vs 活动)

To get eligible role assignments of this storage account, I ran below python code and got results successfully:

  1. from azure.identity import ClientSecretCredential
  2. import requests
  3. # Replace with your actual values
  4. tenant_id = &quot;tenantID&quot;
  5. client_id = &quot;appID&quot;
  6. client_secret = &quot;secret&quot;
  7. # Replace with your actual URL
  8. url = &quot;https://management.azure.com/subscriptions/&lt;subId&gt;/resourceGroups/&lt;rg_name&gt;/providers/Microsoft.Storage/storageAccounts/sristorageacc11/providers/Microsoft.Authorization/roleEligibilityScheduleInstances?api-version=2020-10-01&quot;
  9. # Create a ClientSecretCredential instance
  10. credential = ClientSecretCredential(
  11. tenant_id=tenant_id,
  12. client_id=client_id,
  13. client_secret=client_secret
  14. )
  15. # Get the access token for the Azure Management API
  16. token = credential.get_token(&quot;https://management.azure.com/.default&quot;)
  17. headers = {
  18. &quot;Authorization&quot;: &quot;Bearer &quot; + token.token,
  19. &quot;Content-Type&quot;: &quot;application/json&quot;
  20. }
  21. response = requests.get(url, headers=headers)
  22. if response.status_code == 200:
  23. data = response.json()
  24. for item in data[&quot;value&quot;]:
  25. principal_display_name = item[&quot;properties&quot;][&quot;expandedProperties&quot;][&quot;principal&quot;][&quot;displayName&quot;]
  26. role_definition_display_name = item[&quot;properties&quot;][&quot;expandedProperties&quot;][&quot;roleDefinition&quot;][&quot;displayName&quot;]
  27. principal_type = item[&quot;properties&quot;][&quot;expandedProperties&quot;][&quot;principal&quot;][&quot;type&quot;]
  28. print(&quot;Principal Display Name:&quot;, principal_display_name)
  29. print(&quot;Principal Type:&quot;, principal_type)
  30. print(&quot;Role Definition Display Name:&quot;, role_definition_display_name)
  31. print(&quot;-&quot; * 50) # Separating lines for clarity
  32. else:
  33. print(&quot;Request failed with status code:&quot;, response.status_code)
  34. print(&quot;Response content:&quot;, response.content)

Response:

Azure Python SDK: 获取角色分配类型(可用 vs 活动)

Similarly, I have below Active role assignments for that storage account:

Azure Python SDK: 获取角色分配类型(可用 vs 活动)

To get active role assignments of this storage account, I ran below python code by changing URL and got results successfully:

  1. from azure.identity import ClientSecretCredential
  2. import requests
  3. # Replace with your actual values
  4. tenant_id = &quot;tenantID&quot;
  5. client_id = &quot;appID&quot;
  6. client_secret = &quot;secret&quot;
  7. # Replace with your actual URL
  8. url = &quot;https://management.azure.com/subscriptions/&lt;subId&gt;/resourceGroups/&lt;rg_name&gt;/providers/Microsoft.Storage/storageAccounts/sristorageacc11/providers/Microsoft.Authorization/roleAssignmentScheduleInstances?api-version=2020-10-01&quot;
  9. # Create a ClientSecretCredential instance
  10. credential = ClientSecretCredential(
  11. tenant_id=tenant_id,
  12. client_id=client_id,
  13. client_secret=client_secret
  14. )
  15. # Get the access token for the Azure Management API
  16. token = credential.get_token(&quot;https://management.azure.com/.default&quot;)
  17. headers = {
  18. &quot;Authorization&quot;: &quot;Bearer &quot; + token.token,
  19. &quot;Content-Type&quot;: &quot;application/json&quot;
  20. }
  21. response = requests.get(url, headers=headers)
  22. if response.status_code == 200:
  23. data = response.json()
  24. for item in data[&quot;value&quot;]:
  25. principal_display_name = item[&quot;properties&quot;][&quot;expandedProperties&quot;][&quot;principal&quot;][&quot;displayName&quot;]
  26. role_definition_display_name = item[&quot;properties&quot;][&quot;expandedProperties&quot;][&quot;roleDefinition&quot;][&quot;displayName&quot;]
  27. principal_type = item[&quot;properties&quot;][&quot;expandedProperties&quot;][&quot;principal&quot;][&quot;type&quot;]
  28. print(&quot;Principal Display Name:&quot;, principal_display_name)
  29. print(&quot;Principal Type:&quot;, principal_type)
  30. print(&quot;Role Definition Display Name:&quot;, role_definition_display_name)
  31. print(&quot;-&quot; * 50) # Separating lines for clarity
  32. else:
  33. print(&quot;Request failed with status code:&quot;, response.status_code)
  34. print(&quot;Response content:&quot;, response.content)

Response:

Azure Python SDK: 获取角色分配类型(可用 vs 活动)

huangapple
  • 本文由 发表于 2023年8月10日 23:47:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定