【问题标题】:How to get Azure Activity Log Summary with alerts/powershell/cli?如何使用警报/powershell/cli 获取 Azure 活动日志摘要?
【发布时间】:2021-08-08 23:47:37
【问题描述】:

我目前正在尝试监控在我们的订阅示例中发生的任何 RBAC 更改:John.Doe 将 Sue.Jones 添加为 Resource Group rg-test 的读者。有没有什么可以实现我正在尝试使用 powershell/cli/rest 的东西。根据我的尝试和研究,事实并非如此。

查看活动日志,对于 Write RoleAssignments 操作,摘要包含我需要的所有输出,但是在使用 powershell/cli 时,您无法获得分配的角色或分配给谁。在总结中,您会得到:

操作名称

编写角色分配

时间戳

星期三(东部夏令时间)

事件发起者:John.Doe

与“Sue.Jones”共享消息。

角色:读者

范围资源组:'rg-test'

使用你得到的 powershell/cli/alerts

活动日志警报 alert-iamtesting 时间 2021 年 5 月 19 日 15:29 UTC 类别 行政 操作名称 Microsoft.Authorization/roleAssignments/write

相关 ID 0000000-000000000-000000000

级别信息

资源 ID /subscriptions/0000000-000000000-000000000/resourceGroups/rg-test/providers/Microsoft.Authorization/roleAssignments/0000000-000000000-000000000

来电者 John.Doe

属性 {"statusCode":"Created","serviceRequestId":"0000000-000000000-000000000","eventCategory":"Administrative","entity":"/subscriptions/0000000-000000000-000000000/resourceGroups/rg -test/providers/Microsoft.Authorization/roleAssignments/00000000000000000

【问题讨论】:

    标签: azure powershell azure-powershell azureportal azure-rbac


    【解决方案1】:

    当您在 Azure 门户中查看活动日志时,它会调用 3 个 API 端点。

    第一个是Activity Logs - List

    GET https://management.azure.com/subscriptions/{subscription id}/providers/microsoft.insights/eventtypes/management/values?api-version=2017-03-01-preview&$filter=eventTimestamp ge '2021-05-19T19:52:43Z' and eventTimestamp le '2021-05-20T01:52:43Z' and eventChannels eq 'Admin, Operation' and resourceGroupName eq '{resource group name}' and operations eq 'Microsoft.Authorization/roleAssignments/write' and levels eq 'Critical,Error,Warning,Informational'
    

    返回调用者operationNameeventTimestampresourceGroup对象id目标用户角色的 RoleDefinitionId

    第二个是获取目标用户:

    GET https://graph.windows.net/hanxia.onmicrosoft.com/users/{user object id}?api-version=1.6
    

    最后一个是获取角色:

    GET https://management.azure.com/subscriptions/{subscription id}/providers/Microsoft.Authorization/roleDefinitions/{RoleDefinitionId}?api-version=2015-07-01
    

    因此您可以获得所需的所有信息。

    当我们使用 Azure CLI 时,我们应该选择az monitor activity-log list但是它只相当于上面的第一个调用。我们得到一个名为 resourceId 的属性,它是 roleAssignment id。

    所以我们仍然需要获取带有 id 的 roleAssignment。这是一个简单的脚本。

    # list the activity logs of resource group "AllenTestRG01" in the past 4 hours
    $logs = az monitor activity-log list -g AllenTestRG01 --offset 4h | ConvertFrom-Json
    
    # I assume that the first $logs[0] is the log you are tracking. (you should implement your logic here to find the log you need)
    $logs[0].resourceId
    
    # list the role assignments for resource group "AllenTestRG01"
    $assignments = az role assignment list -g AllenTestRG01 | ConvertFrom-Json
    
    # do a match
    foreach ($assignment in $assignments){
        if ($assignment.id -eq $a[0].resourceId) {
            Write-Host "assigned user: " $assignment.principalName  " role: " 
    $assignment.roleDefinitionName
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-05-25
      • 2022-11-02
      • 2019-03-17
      • 1970-01-01
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      • 2023-02-22
      • 2021-11-14
      相关资源
      最近更新 更多