概要
AWSユーザに割り当てられたポリシー(権限)一覧をpowershellでCSV出力するスクリプトを紹介します。
スクリプト概要
AWSでのIAMポリシーの割り当てにはいくつかパターンがありますが以下のようにそれぞれ取得コマンドが異なります。
- ①ユーザにグループを割り当て(グループには管理ポリシーを付与) →aws iam list-attached-group-policies
- ②ユーザにグループを割り当て(グループにはインラインポリシーを付与) →aws iam list-group-policies
- ③ユーザに直接管理ポリシーを割り当て →aws iam list-attached-user-policies
- ④ユーザに直接インラインポリシーを割り当て →aws iam list-user-policies
本スクリプトでは上記コマンドから得られるユーザに割り当てられているポリシー一覧を./output/summary.csvに出力します。
プログラムのループ構造
aws iam list-users
aws iam list-groups-for-user
①aws iam list-attached-group-policies
②aws iam list-group-policies
③aws iam list-attached-user-policies
④aws iam list-user-policies
erDiagram
list-users ||--o{ list-groups-for-user : user
list-users ||--o{ list-attached-user-policies: user
list-groups-for-user ||--o{ list-attached-group-policies : group
list-groups-for-user ||--o{ list-group-policies : group
list-users ||--o{ list-user-policies : user
スクリプト実行の前提
- AWS CLIの認証情報(アクセスキー、シークレットキー)を設定済みであること。(未設定の場合aws configure 等で設定する)
- 実行ユーザにIAMのLIST権限が付与されていること
スクリプト
Function copycsvObject($user_data) {
$csv = New-Object PSCustomObject
$user_data.psobject.properties | ForEach-Object {
$csv | Add-Member -MemberType $_.MemberType -Name $_.Name -Value $_.Value
}
return $csv
}
#出力ディレクトリ作成
new-item output -type directory -f | out-null
#既存ファイル削除
remove-item .\output\* -Force
#出力ファイル
$all_csv = @()
#ユーザ情報取得
$users = aws iam list-users | ConvertFrom-Json
$users.users | convertto-csv -NoTypeInformation | out-file -Encoding default -FilePath ./output/list-users.csv
("User count:" + $users.users.count) | out-host
#グループ情報取得
$groups = aws iam list-groups | ConvertFrom-Json
$groups.Groups | convertto-csv -NoTypeInformation | out-file -Encoding default -FilePath ./output/list-groups.csv
("group count:" + $groups.Groups.count) | out-host
#ポリシー情報取得
$policies = aws iam list-policies | ConvertFrom-Json
$policies.policies | convertto-csv -NoTypeInformation | out-file -Encoding default -FilePath ./output/list-policies.csv
("policy count:" + $policies.policies.count) | out-host
# ユーザに紐づくポリシーを取得
$users.users | ForEach-Object {
$user = $_
("username:" + $user.userName )| out-host
$user_data = New-Object PSCustomObject | Select-Object -Property UserName, UserName_Arn, UserName_CreateDate, GroupName, Group_Arn, PolicyName, Policy_Arn
$user_data.UserName = $user.userName
$user_data.UserName_Arn = $user.Arn
$user_data.UserName_CreateDate = $user.CreateDate
$gfu = aws iam list-groups-for-user --user-name $user.UserName | ConvertFrom-Json
(" group count:" + $gfu.groups.count) | out-host
$gfu.groups | convertto-csv -NoTypeInformation | out-file -Encoding default -FilePath ./output/list-groups-for-user.csv -append
$gfu.groups | ForEach-Object {
$g = $_
#グループポリシー
$gp = aws iam list-group-policies --group-name $g.GroupName | ConvertFrom-Json
(" group policy count:" + $gp.PolicyNames.count) | out-host
# $gp.PolicyNames | convertto-csv -NoTypeInformation | out-file -Encoding default -FilePath ./output/list-group-policies.csv -append
$gp.PolicyNames | ForEach-Object {
$p = $_
$group_policy = copycsvObject($user_data)
$group_policy.PolicyName = $p
$group_policy.GroupName = $g.GroupName
$group_policy.Group_Arn = $g.Arn
$all_csv += $group_policy
}
#グループにアタッチされたポリシー
$agp = aws iam list-attached-group-policies --group-name $g.GroupName | ConvertFrom-Json
(" atattched group policy count:" + $agp.AttachedPolicies.count) | out-host
$agp.AttachedPolicies | ConvertTo-Csv -NoTypeInformation | out-file -Encoding default -FilePath ./output/list-attached-group-policies.csv -append
$agp.AttachedPolicies | ForEach-Object {
$p = $_
$group_policy = copycsvObject($user_data)
$group_policy.PolicyName = $p.PolicyName
$group_policy.Policy_arn = $p.PolicyArn
$group_policy.GroupName = $g.GroupName
$group_policy.Group_Arn = $g.Arn
$all_csv += $group_policy
}
#グループに割り当てポリシーがない場合
if ($gp.PolicyNames.count -eq 0 `
-and $agp.AttachedPolicies.count -eq 0) {
$user_group = copycsvObject($user_data)
$user_group.GroupName = $g.GroupName
$user_group.Group_Arn = $g.Arn
$all_csv += $user_group
}
}
#ユーザアタッチポリシー
$aup = aws iam list-attached-user-policies --user-name $user.UserName | ConvertFrom-Json
(" atattched user policy count:" + $aup.AttachedPolicies.count) | out-host
$aup.AttachedPolicies | convertto-csv -NoTypeInformation | out-file -Encoding default -FilePath ./output/list-attached-user-policies.csv -append
$aup.AttachedPolicies | ForEach-Object {
$p = $_
$user_policy = copycsvObject($user_data)
$user_policy.PolicyName = $p.PolicyName
$user_policy.Policy_Arn = $p.PolicyArn
$all_csv += $user_policy
}
#ユーザポリシー
$up = aws iam list-user-policies --user-name $user.UserName | ConvertFrom-Json
(" user policy count:" + $up.PolicyNames.count) | out-host
# $up.PolicyNames | convertto-csv -NoTypeInformation | out-file -Encoding default -FilePath ./output/list-user-policies.csv -append
$up.PolicyNames | ForEach-Object {
$p = $_
$user_policy = copycsvObject($user_data)
$user_policy.PolicyName = $p
$all_csv += $user_policy
}
#ユーザにグループ、割り当てポリシー共にない場合
if ($gfu.groups.count -eq 0 `
-and $aup.AttachedPolicies.count -eq 0 `
-and $up.PolicyNames.count -eq 0) {
$all_csv += $user_data
}
}
$all_csv | convertto-csv -NoTypeInformation | out-file -Encoding default -FilePath ./output/summary.csv -append