Azure 日志分析 API 读取

Azure 日志分析 API 读取

我正在尝试从我的应用程序获取对 Azure Log Analytics 的读取访问权限并执行了以下步骤:

  1. 在 AD 门户的“应用程序注册”下注册应用程序
  2. 在“身份验证”选项卡下添加平台:Web;重定向 URI:http://localhost/auth
  3. 已请求并授予此应用程序 API 权限以读取日志分析数据:日志分析 API:读取的数据:类型应用程序:状态已授予

然后使用此代码尝试读取:

SECRET="XXXXXXXXXXX"
CLIENT="e7207353-ee8d-4bcc-9580-bfaaf2c0da7e"
URI="http://localhost/auth"
RESOURCE="management.azure.com"
TARGET="https://$RESOURCE/subscriptions/XXXXXXX/resourceGroups/myRG01/providers/Microsoft.OperationalInsights/workspaces/law-01/api/query?api-version=1"

# (1) Obtain token
RESP=$(curl --silent -H "Content-Type: application/x-www-form-urlencoded" -X POST \
            -d "grant_type=client_credentials&client_id=${CLIENT}&resource=https://${RESOURCE}&client_secret=${SECRET}&redirect_uri=${URI}" \
         https://login.microsoftonline.com/...orgTenantID.../oauth2/token )

TOKEN=$(echo "$RESP" | jq -r .access_token)

# (2) Call Log Analytics API
curl --silent -X POST \
   -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
   -d '{"query": "AzureActivity | limit 10"}' $TARGET | jq

但是在成功获取令牌后,调用 Log Analytics 时出现“AuthorizationFailed”错误:

{
  "error": {
    "code": "AuthorizationFailed",
    "message": "The client '02531282-409c-4752-8b10-4f995ceaac5d' with object id '02531282-409c-4752-8b10-4f995ceaac5d' does not have authorization to perform action 'microsoft.operationalinsights/workspaces/query/read' over scope '/subscriptions/XXXXXXX/resourceGroups/myRG01/providers/Microsoft.OperationalInsights/workspaces/law-01/api/query' or the scope is invalid. If access was recently granted, please refresh your credentials."
  }
}

我哪里错了?几天前才授予访问权限,所以希望传播中的任何延迟都过去了。无论如何,“刷新您的凭据”对于这种情况意味着什么?还有其他吗?

感谢您的帮助。谢谢。

=== 发布答案更新 === 使用 API 访问 loganalytics 数据,如下所述 -https://learn.microsoft.com/en-us/azure/azure-monitor/logs/api/request-format因此上述代码的变量应该是:

RESOURCE="api.loganalytics.io"
TARGET="https://$RESOURCE/v1/workspaces/...LAW_ID.../query"

答案1

您需要授予您的服务主体(应用程序注册)对日志分析工作区的 Azure RBAC 权限,以便它能够读取数据,这与您可能通过 Azure AD 授予的任何权限是分开的。

相关内容