我正在使用以下方法获取订阅的安全分数Microsoft Azure 安全中心 (ASC) 管理客户端库。库中的所有操作都表明
您不应该直接实例化此类,而应创建一个 Client 实例,该实例将为您创建该类并将其附加为属性。
因此,我正在创建一个安全中心客户具有以下规格:
SecurityCenter(credentials, subscription_id, asc_location, base_url=None)
然而,在我看来,这是获得asc_location
正确的信息是使用 SecurityCenter 客户端来获取它......The spec与上面引用的内容相同,You should not instantiate...
。因此,我无法创建客户端,因为我需要 ASC 位置才能创建客户端,并且我需要创建客户端才能获取 ASC 位置。
文档提到
ASC 存储订阅数据的位置。可以从获取位置检索
在 Python SDK 文档中谷歌搜索并搜索这个“获取位置”没有给我任何结果(除了 REST API)。我错过了什么吗?我们是否应该像这样对位置进行硬编码这个帖子 or 这个 GitHub 问题来自 SDK 存储库?
作为官方 API 参考列出位置表示:
特定订阅的负责 ASC 的位置(主页
地区)。对于每个订阅,只有一个负责位置。
它不会改变,所以如果你已经知道的值,你可以硬编码这个值asc_location
您的订阅。
但每个订阅可能有不同的 asc_location 值(我的 2 个 Azure 订阅有不同的 asc_location 值)。
因此,如果您有很多 Azure 订阅,您只需查询即可asc_location
通过 API(据我所知,这是我能找到的唯一方法)然后使用SDK获取安全分数,尝试以下代码:
from azure.mgmt.security import SecurityCenter
from azure.identity import ClientSecretCredential
import requests
from requests.api import head, request
TENANT_ID = ''
CLIENT = ''
KEY = ''
subscription_id= ''
getLocationsURL = "https://management.azure.com/subscriptions/"+subscription_id+"/providers/Microsoft.Security/locations?api-version=2015-06-01-preview"
credentials = ClientSecretCredential(
client_id = CLIENT,
client_secret = KEY,
tenant_id = TENANT_ID
)
#request for asc_location for a subscription
azure_access_token = credentials.get_token('https://management.azure.com/.default')
r = requests.get(getLocationsURL,headers={"Authorization":"Bearer " + azure_access_token.token}).json()
location = r['value'][0]['name']
print("location:" + location)
client = SecurityCenter(credentials, subscription_id, asc_location=location)
for score in client.secure_scores.list():
print(score)
Result:
![enter image description here](https://i.stack.imgur.com/iVlYY.png)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)