【问题标题】:Get Azure Security Center alerts via python SDK通过 python SDK 获取 Azure 安全中心警报
【发布时间】:2021-02-16 22:50:20
【问题描述】:
【问题讨论】:
-
我已经更新了我的代码。如果有帮助,请您在guide 之后帮助将其标记为答案?谢谢。
标签:
python
azure
azure-sdk-python
securitycenter
【解决方案1】:
我只能提供一个粗略的参考。
安装包azure-mgmt-security后,应使用包中的List方法,源代码为here。
这里是关于如何认证的doc。
这里是doc关于如何获取tenantId / client_id / key。
这是我的代码:
from azure.mgmt.security import SecurityCenter
from azure.common.credentials import ServicePrincipalCredentials
subscription_id = "xxxx"
# Tenant ID for your Azure subscription
TENANT_ID = '<Your tenant ID>'
# Your service principal App ID
CLIENT = '<Your service principal ID>'
# Your service principal password
KEY = '<Your service principal password>'
credentials = ServicePrincipalCredentials(
client_id = CLIENT,
secret = KEY,
tenant = TENANT_ID
)
client = SecurityCenter(credentials=credentials,subscription_id=subscription_id,asc_location="centralus")
client.alerts.list()
另外,您可以在 python 中将List Alerts api 与 http 请求一起使用。
【解决方案2】:
截至 2021 年 2 月的今天,Microsoft 再次更改了凭据的实例化方式。这是当前的:
from azure.identity import DefaultAzureCredential
# Acquire a credential object for the app identity. When running in the cloud,
# DefaultAzureCredential uses the app's managed identity (MSI) or user-assigned service principal.
# When run locally, DefaultAzureCredential relies on environment variables named
# AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, and AZURE_TENANT_ID.
credential = DefaultAzureCredential()
而且它还改变了SecurityCenter签名,credentials参数被重命名为credential,没有“s”。
完整文档here。