Created
September 18, 2019 02:14
-
-
Save TAKEDA-Takashi/d60141a79f18717ca734c4d7e3a6317e to your computer and use it in GitHub Desktop.
指定したAWSプロファイルのアクセスキー をローテーションするスクリプト(Windows版)。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# AWS CLIおよびAWS Tools for PowerShellで使用しているアクセスキーをローテーションする。 | |
# 新しいアクセスキーを発行し、片方もしくは両方のクレデンシャルファイルを更新します。 | |
# クレデンシャルファイルは同一フォルダーにバックアップされるため、意図しない動作をした場合は手動で復元してください。 | |
# | |
# Usage: | |
# > aws-iam-user-key-rotation.ps1 [profile] | |
# | |
$profileName = $args[0] | |
if ($profileName -eq $null) { | |
$profileName = "default" | |
} | |
$awsCliInstalled = $false | |
$awsPowerShellInstalled = $false | |
# AWS CLIのインストールチェック | |
try { | |
aws --version > $null | |
$awsCliInstalled = $true | |
echo "AWS CLIが見つかりました。" | |
$Env:AWS_DEFAULT_PROFILE = $profileName | |
$Env:AWS_DEFAULT_OUTPUT = "json" | |
} catch { | |
# do nothing | |
} | |
# AWS Tools for PowerShellのインストールチェック | |
if (Get-Module -ListAvailable -Name AWSPowerShell) { | |
echo "AWS Tools for PowerShellが見つかりました。" | |
$awsPowerShellInstalled = $true | |
Set-AWSCredential -ProfileName $profileName | |
} | |
# CoreおよびAWS Tools IdentityManagementのインストールチェック | |
if (!$awsPowerShellInstalled) { | |
# どちらもPowerShell 6以上が必要 | |
if ($PSVersionTable.PSVersion.Major -ge 6) { | |
if (Get-Module -ListAvailable -Name AWSPowerShell.NetCore) { | |
echo "AWS Tools for PowerShell Coreが見つかりました。" | |
$awsPowerShellInstalled = $true | |
Import-Module AWSPowerShell.NetCore | |
Set-AWSCredential -ProfileName $profileName | |
} elseif (Get-Module -ListAvailable -Name AWS.Tools.IdentityManagement) { | |
echo "AWS Tools IdentityManagementが見つかりました。" | |
$awsPowerShellInstalled = $true | |
Import-Module ("Common","IdentityManagement" | %{ "AWS.Tools.$_" }) | |
Set-AWSCredential -ProfileName $profileName | |
} | |
} else { | |
$Host.UI.WriteErrorLine("PowerShellのバージョンが6より小さいため、CoreおよびAWS.Tools.*はチェックしません。") | |
} | |
} | |
if (!($awsCliInstalled -or $awsPowerShellInstalled)) { | |
$Host.UI.WriteErrorLine("AWS CLIおよびAWS Tools for PowerShellが見つかりませんでした。") | |
exit 1 | |
} | |
echo "${profileName} プロファイルのアクセスキーをローテーションします。" | |
# | |
# main | |
# | |
$ErrorActionPreference = "Stop" | |
if ($awsPowerShellInstalled) { | |
$user = Get-IAMUser | |
} else { | |
$user = aws iam get-user | ConvertFrom-Json | |
$user = $user.User | |
} | |
echo "対象のIAMユーザーは $($user.UserName) です。" | |
if ($awsPowerShellInstalled) { | |
$currentAccessKey = Get-IAMAccessKey | |
} else { | |
$currentAccessKey = aws iam list-access-keys | ConvertFrom-Json | |
$currentAccessKey = $currentAccessKey.AccessKeyMetadata | |
} | |
# AWSのアクセスキーは一度に2個までしか作成できないため先にチェックしておく | |
if ($currentAccessKey.Count -eq 2) { | |
$Host.UI.WriteErrorLine("アクセスキーが2個存在しています。使用していないアクセスキーを削除してから再実行してください。") | |
exit 1 | |
} | |
if ($awsPowerShellInstalled) { | |
cp $Env:HOMEPATH\AppData\Local\AWSToolkit\RegisteredAccounts.json $Env:HOMEPATH\AppData\Local\AWSToolkit\RegisteredAccounts.json.bak | |
} | |
if ($awsCliInstalled) { | |
cp $Env:HOMEPATH\.aws\credentials $Env:HOMEPATH\.aws\credentials.bak | |
} | |
if ($awsPowerShellInstalled) { | |
$newAccessKey = New-IAMAccessKey -UserName $user.UserName | |
} else { | |
$newAccessKey = aws iam create-access-key --user-name $user.UserName | ConvertFrom-Json | |
$newAccessKey = $newAccessKey.AccessKey | |
} | |
echo "新規アクセスキーを作成しました。" | |
if ($awsPowerShellInstalled) { | |
Set-AWSCredential -AccessKey $newAccessKey.AccessKeyId -SecretKey $newAccessKey.SecretAccessKey -StoreAs $profileName | |
} | |
if ($awsCliInstalled) { | |
aws configure set aws_access_key_id $newAccessKey.AccessKeyId | |
aws configure set aws_secret_access_key $newAccessKey.SecretAccessKey | |
} | |
echo "クレデンシャルファイルを更新しました。" | |
echo "新しいアクセスキーで試行します。" | |
if ($awsPowerShellInstalled) { | |
$updateAccessKeys = Get-IAMAccessKey | |
} else { | |
echo "10秒待機します..." | |
Start-Sleep -s 10 | |
$updateAccessKeys = aws iam list-access-keys | ConvertFrom-Json | |
$updateAccessKeys = $updateAccessKeys.AccessKeyMetadata | |
} | |
if ($updateAccessKeys.Count -ne 2 ) { | |
$Host.UI.WriteErrorLine("アクセスキーが2個存在していません。何かおかしいようです。") | |
exit 1 | |
} | |
if ($awsPowerShellInstalled) { | |
Remove-IAMAccessKey -UserName $currentAccessKey.UserName -AccessKeyId $currentAccessKey.AccessKeyId -Force | |
} else { | |
aws iam delete-access-key --user-name $currentAccessKey.UserName --access-key-id $currentAccessKey.AccessKeyId | |
} | |
echo "古いアクセスキーを削除しました。" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment