gpt4 book ai didi

azure - 如何使用alerts/powershell/cli获取Azure事件日志摘要?

转载 作者:行者123 更新时间:2023-12-03 03:49:04 24 4
gpt4 key购买 nike

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

查看事件日志,对于 Write RoleAssignments 操作,摘要包含我需要的所有输出,但使用 powershell/cli 时,您无法获取分配了什么角色或分配给谁。在摘要中,您得到:

操作名称

编写角色分配

时间戳

周三(东部夏令时间)

事件发起者:John.Doe

与“Sue.Jones”共享消息。

角色:读者

范围资源组:“rg-test”

使用 powershell/cli/alerts 你得到

事件日志警报警报-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":"已创建","serviceRequestId":"0000000-000000000-000000000","eventCategory":"管理","entity":"/subscriptions/0000000-000000000-000000000/resourceGroups/rg -test/providers/Microsoft.Authorization/roleAssignments/00000000000000000

最佳答案

当您在 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'

它返回调用者操作名称事件时间戳资源组对象 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
}
}

关于azure - 如何使用alerts/powershell/cli获取Azure事件日志摘要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67608245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com