Azure Activity 日志
Azure Activity Log
我想监视谁更改了 rbac 分配,我创建了 powershell 脚本用于从 Azure Activity 日志收集数据。我使用了下面的一段代码。使用此解决方案,我可以获得以下项目:
来电者 - 进行角色分配更改的用户,
时间戳,
资源名称 - 已提供此资源分配更改,
操作类型 - 写入或删除
在 Azure 门户的 Activity 日志面板中,在摘要门户(消息:与 "user info" 共享)中,我可以看到已被授予 permissions/assignment资源,但是使用我的 powershell 脚本我无法获取此信息,有什么方法可以获取此信息吗?
Get-AzureRmLog -StartTime (Get-Date).AddDays(-7) |
Where-Object {$_.Authorization.Action -like
'Microsoft.Authorization/roleAssignments/*'} |
Select-Object @{N="Caller";E={$_.Caller}},
@{N="Resource";E={$_.Authorization.Scope}},
@{N="Action";E={Split-Path $_.Authorization.action -leaf}},
EventTimestamp
脚本输出:
Caller : username@xxx.com
Resource :/subscriptions/xxxx/resourceGroups/Powershell/providers/Microsoft.Compute/virtualMachines/xx/providers/Microsoft.Authorization/roleAssignments/xxxx
Action : write
EventTimestamp : 8/29/2019 10:12:31 AM
这对你有用吗?
Get-AzureRmLog -StartTime (Get-Date).AddDays(-7) |
Where-Object {$_.Authorization.Action -like 'Microsoft.Authorization/roleAssignments/*'} |
Select-Object @{N="Caller";E={$_.Caller}},
@{N="Resource";E={$_.Authorization.Scope}},
@{N="Action";E={Split-Path $_.Authorization.action -leaf}},
@{N="Name";E={$_.Claims.Content.name}},
EventTimestamp
我的输出:
Caller : username@domain.com
Resource : /subscriptions/xxxx/resourceGroups/xxxx/providers/Microsoft.Authorization/roleAssignments/xxxx
Action : write
Name : John Doe
EventTimestamp : 30.08.2019 12.05.52
注意:我使用了 Get-AzLog。不确定 Get-AzLog 和 Get-AzureRmLog 之间是否有任何区别。
相当肯定这不会通过此 cmdlet 公开。我什至没有在角色分配中看到此信息。所以不确定你到底是什么意思。
使用 Az PowerShell cmdlet 当前不支持您获取分配了 RBAC 角色的用户名的要求 Get-AzLog or Get-AzureRmLog。
但是,我们可以通过为 Activity Logs - List and Az PowerShell cmdlet Get-AzureADUser.
利用 Azure REST API 来满足您的要求
以这种方式,因为我们依赖于 Azure REST API Activity Logs - List(但看起来你想要 PowerShell 方式来完成要求)所以在 PowerShell 中调用 REST API如下所示。
$request = "https://management.azure.com/subscriptions/{subscriptionId}/providers/microsoft.insights/eventtypes/management/values?api-version=2015-04-01&`$filter={$filter}"
$auth = "eyxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$authHeader = @{
'Content-Type'='application/json'
'Accept'='application/json'
'Authorization'= "Bearer $auth"
}
$Output = Invoke-RestMethod -Uri $request -Headers $authHeader -Method GET -Body $Body
$ActivityLogsFinalOutput = $Output.Value
开发您的 PowerShell 代码以从 Azure REST API for Activity Logs - List 调用的输出中获取 "PrincipalId"(在 "properties" 下)。获取到的"PrincipalId"就是你最终要获取的用户的ObjectID
现在利用 Az PowerShell cmdlet Get-AzureADUser 并让您的命令如下所示。
(Get-AzureADUser -ObjectID "<PrincipalID>").DisplayName
希望对您有所帮助!!干杯!!
更新:
请找到 PowerShell 获取需要在上述 REST API 调用中使用的身份验证令牌(即 $auth)的方法。
$ClientID = "<ClientID>" #ApplicationID
$ClientSecret = "<ClientSecret>" #key from Application
$tennantid = "<TennantID>"
$TokenEndpoint = {https://login.windows.net/{0}/oauth2/token} -f $tennantid
$ARMResource = "https://management.core.windows.net/";
$Body1 = @{
'resource'= $ARMResource
'client_id' = $ClientID
'grant_type' = 'client_credentials'
'client_secret' = $ClientSecret
}
$params = @{
ContentType = 'application/x-www-form-urlencoded'
Headers = @{'accept'='application/json'}
Body = $Body1
Method = 'Post'
URI = $TokenEndpoint
}
$token = Invoke-RestMethod @params
$token | select access_token, @{L='Expires';E={[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($_.expires_on))}} | fl *
我也看到 this 新方法,但我没有机会对此进行测试。如果有兴趣,您也可以尝试这个或使用上述方法。
更新2:
$ActivityLogsFinalOutput| %{
if(($_.properties.responseBody) -like "*principalId*"){
$SplittedPrincipalID = $_.properties.responseBody -split "PrincipalID"
$SplittedComma = $SplittedPrincipalID[1] -split ","
$SplittedDoubleQuote = $SplittedComma[0] -split "`""
$PrincipalID = $SplittedDoubleQuote[2]
#Continue code for getting Azure AD User using above fetched $PrincipalID
#...
#...
}
}
我想监视谁更改了 rbac 分配,我创建了 powershell 脚本用于从 Azure Activity 日志收集数据。我使用了下面的一段代码。使用此解决方案,我可以获得以下项目: 来电者 - 进行角色分配更改的用户, 时间戳, 资源名称 - 已提供此资源分配更改, 操作类型 - 写入或删除
在 Azure 门户的 Activity 日志面板中,在摘要门户(消息:与 "user info" 共享)中,我可以看到已被授予 permissions/assignment资源,但是使用我的 powershell 脚本我无法获取此信息,有什么方法可以获取此信息吗?
Get-AzureRmLog -StartTime (Get-Date).AddDays(-7) |
Where-Object {$_.Authorization.Action -like
'Microsoft.Authorization/roleAssignments/*'} |
Select-Object @{N="Caller";E={$_.Caller}},
@{N="Resource";E={$_.Authorization.Scope}},
@{N="Action";E={Split-Path $_.Authorization.action -leaf}},
EventTimestamp
脚本输出:
Caller : username@xxx.com
Resource :/subscriptions/xxxx/resourceGroups/Powershell/providers/Microsoft.Compute/virtualMachines/xx/providers/Microsoft.Authorization/roleAssignments/xxxx
Action : write
EventTimestamp : 8/29/2019 10:12:31 AM
这对你有用吗?
Get-AzureRmLog -StartTime (Get-Date).AddDays(-7) |
Where-Object {$_.Authorization.Action -like 'Microsoft.Authorization/roleAssignments/*'} |
Select-Object @{N="Caller";E={$_.Caller}},
@{N="Resource";E={$_.Authorization.Scope}},
@{N="Action";E={Split-Path $_.Authorization.action -leaf}},
@{N="Name";E={$_.Claims.Content.name}},
EventTimestamp
我的输出:
Caller : username@domain.com
Resource : /subscriptions/xxxx/resourceGroups/xxxx/providers/Microsoft.Authorization/roleAssignments/xxxx
Action : write
Name : John Doe
EventTimestamp : 30.08.2019 12.05.52
注意:我使用了 Get-AzLog。不确定 Get-AzLog 和 Get-AzureRmLog 之间是否有任何区别。
相当肯定这不会通过此 cmdlet 公开。我什至没有在角色分配中看到此信息。所以不确定你到底是什么意思。
使用 Az PowerShell cmdlet 当前不支持您获取分配了 RBAC 角色的用户名的要求 Get-AzLog or Get-AzureRmLog。
但是,我们可以通过为 Activity Logs - List and Az PowerShell cmdlet Get-AzureADUser.
利用 Azure REST API 来满足您的要求以这种方式,因为我们依赖于 Azure REST API Activity Logs - List(但看起来你想要 PowerShell 方式来完成要求)所以在 PowerShell 中调用 REST API如下所示。
$request = "https://management.azure.com/subscriptions/{subscriptionId}/providers/microsoft.insights/eventtypes/management/values?api-version=2015-04-01&`$filter={$filter}"
$auth = "eyxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$authHeader = @{
'Content-Type'='application/json'
'Accept'='application/json'
'Authorization'= "Bearer $auth"
}
$Output = Invoke-RestMethod -Uri $request -Headers $authHeader -Method GET -Body $Body
$ActivityLogsFinalOutput = $Output.Value
开发您的 PowerShell 代码以从 Azure REST API for Activity Logs - List 调用的输出中获取 "PrincipalId"(在 "properties" 下)。获取到的"PrincipalId"就是你最终要获取的用户的ObjectID
现在利用 Az PowerShell cmdlet Get-AzureADUser 并让您的命令如下所示。
(Get-AzureADUser -ObjectID "<PrincipalID>").DisplayName
希望对您有所帮助!!干杯!!
更新:
请找到 PowerShell 获取需要在上述 REST API 调用中使用的身份验证令牌(即 $auth)的方法。
$ClientID = "<ClientID>" #ApplicationID
$ClientSecret = "<ClientSecret>" #key from Application
$tennantid = "<TennantID>"
$TokenEndpoint = {https://login.windows.net/{0}/oauth2/token} -f $tennantid
$ARMResource = "https://management.core.windows.net/";
$Body1 = @{
'resource'= $ARMResource
'client_id' = $ClientID
'grant_type' = 'client_credentials'
'client_secret' = $ClientSecret
}
$params = @{
ContentType = 'application/x-www-form-urlencoded'
Headers = @{'accept'='application/json'}
Body = $Body1
Method = 'Post'
URI = $TokenEndpoint
}
$token = Invoke-RestMethod @params
$token | select access_token, @{L='Expires';E={[timezone]::CurrentTimeZone.ToLocalTime(([datetime]'1/1/1970').AddSeconds($_.expires_on))}} | fl *
我也看到 this 新方法,但我没有机会对此进行测试。如果有兴趣,您也可以尝试这个或使用上述方法。
更新2:
$ActivityLogsFinalOutput| %{
if(($_.properties.responseBody) -like "*principalId*"){
$SplittedPrincipalID = $_.properties.responseBody -split "PrincipalID"
$SplittedComma = $SplittedPrincipalID[1] -split ","
$SplittedDoubleQuote = $SplittedComma[0] -split "`""
$PrincipalID = $SplittedDoubleQuote[2]
#Continue code for getting Azure AD User using above fetched $PrincipalID
#...
#...
}
}