python - 是否可以从 Azure Python SDK 获取 ASC 位置?

标签 python azure azure-sdk-python securitycenter

我正在使用 Microsoft Azure Security Center (ASC) Management Client Library 获取订阅的安全分数。库中的所有操作都表明

You should not instantiate directly this class, but create a Client instance that will create it for you and attach it as attribute.

因此,我正在创建一个 SecurityCenter具有以下规范的客户端:

SecurityCenter(credentials, subscription_id, asc_location, base_url=None)

但是,在我看来,正确获取 asc_location 信息的唯一方法是使用 SecurityCenter 客户端来获取它... The spec与上面引用的内容相同,你不应该实例化...。因此,我无法创建客户端,因为我需要 ASC 位置才能创建客户端,并且我需要创建客户端来获取 ASC 位置。

文档提到

The location where ASC stores the data of the subscription. can be retrieved from Get locations

在 Python SDK 文档中谷歌搜索并搜索此“获取位置”没有给我任何结果(除了 REST API)。我错过了什么吗?我们是否应该像 this SO post 那样对位置进行硬编码?或this GitHub issue来自 SDK 存储库?

最佳答案

作为官方 API 引用 list locations表示:

The location of the responsible ASC of the specific subscription (home region). For each subscription there is only one responsible location.

它不会改变,因此如果您已经知道订阅的 asc_location 值,则可以硬编码该值。

但每个订阅可能有不同的 asc_location 值(我的 2 个 Azure 订阅有不同的 asc_location 值)。 因此,如果您有大量 Azure 订阅,则只需通过 API 查询 asc_location (据我所知,这是我能找到的唯一方法)然后使用SDK获取Secure Score,尝试以下代码:

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)

结果: enter image description here enter image description here

关于python - 是否可以从 Azure Python SDK 获取 ASC 位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65619297/

相关文章:

azure - Azure YAML 管道中版本号的共享计数器

azure - 使用 python sdk 在 azure 中的 Linux 虚拟机中运行命令

python - 调用 PutObject 操作时的 PermanentRedirect

python - 使用 pyparsing Forward() 和多个规则来查找回文序列

AzureApp HTTP 错误 500.30 - ASP.NET Core 应用程序无法启动

Rust 中的 Azure Active Directory 身份验证

azure - 模块 'azure.common.credentials' 没有属性 'signed_session'

python - 使用 Python SDK 创建 Azure 容器时出现“HTTP header 格式不正确”错误

python - 如何在 CFG 中定义 null 或 lambda?

python - 如何在numpy数组的给定行中保留N个最小元素?