概要
AWS CLIをインストールし、AWSの認証情報を設定している状態で Git for Windows をつかってHTTPS接続で CodeCommit へアクセスしようとしたらうまくいかずにハマった。
前提
- CodeCommitのリポジトリは作成済み
- CodeCommitのアクセス権限を持つIAMユーザも作成済み
- ローカルPCへAWS CLIをインストール済み、defaultプロファイルも設定済み
AWS公式ドキュメント
まずは公式ドキュメントを読んでみる。結論から言うと自分の場合はこれだけでは足りなかった。
- AWS CLI 認証情報ヘルパーを使用して Windows 上で AWS CodeCommit リポジトリへの HTTPS 接続の設定手順
- Git for Windows: Git for Windows をインストールしたが、リポジトリへのアクセスが拒否される (403)
Git for Windows のインストール
こちらからインストーラをダウンロードして実行。
インストール手順については以下の記事も参考になる。※ただし今回は一部のオプションを変更しているので注意。
インストール手順
バージョンアップのたびにインストール時のオプションが頻繁に変わっているような・・・。とりあえず今回はGit-2.15.1.2-64-bit
のバージョンで以下の通りインストール。
1. Select Components
使いたいコンポーネントのみチェック。自分の場合は
・Git Bash Here
・Git GUI Here
のみ。
2. Choosing the default editor used by Git
好みの問題の気がするので何でもいい。自分の場合は Use the Nano editor by default
を選択。
3. Adjusting your PATH environment
AWS公式ドキュメントに従い、Use Git from the Windows Command Prompt
を選択。
4. Choosing the SSH executable
Use OpenSSH
を選択。
5. Choosing HTTPS transport backend
Use the OpenSSL library
を選択。
6. Configuring the line ending conversions
改行コードを勝手に変更されたくないため Checkout as-is, commit as-is
を選択。
7. Configuring the terminal emulator to use with Git Bash
Use MinTTY(the default terminal of MSYS2)
を選択。
8. Configuring extra options
AWS公式ドキュメントに従い、
・Enable Git Credential Manager
はチェックをはずす。
・Enable file system caching
のみチェックする。
AWS CLI のプロファイル設定
default
のプロファイルが設定済みで、このプロファイルで CodeCommit へアクセスするなら追加の設定は不要。default
プロファイルの Default region name
と CodeCommit のリージョンが異なる場合や別のIAMユーザを使いたい場合は CodeCommit 用のプロファイルを作成する。
例)CodeCommit用のプロファイル名が「hogehoge」の場合
> aws configure --profile hogehoge
AWS Access Key ID [None]: XXXXXXXXXXXXXXXX
AWS Secret Access Key [None]: XXXXXXXXXXXXXXXX
Default region name [None]: us-east-1
Default output format [None]:
Credential Helperの設定(失敗)
AWS公式ドキュメントを参考に。
git config --global credential.helper '!aws codecommit credential-helper --profile hogehoge $@'
git config --global credential.UseHttpPath true
しかしここまでの設定だとうまくいかなかった。ユーザIDとパスワードを求められてしまう。
AWS Tools for Windows PowerShell
こちらから AWS Tools for Windows PowerShell をダウンロードインストールする。
CodeCommitの認証を通すだけなら、インストール時の Custom Setup 画面で AWS CodeCommit Credential Helper
のみを選択すればOK。
Credential Helperの設定(成功)
先程インストールした AWS CodeCommit Credential Helper
を使う。コマンドプロンプトで以下を実行。
cd %PROGRAMFILES(X86)%\AWS Tools\CodeCommit
git-credential-AWSS4.exe -p hogehoge
結果、CredentialHelper として以下が設定された模様。
$ git config --list
...(略)...
credential.helper=!'C:\Users\xxxxxxxx\AppData\Roaming\GitCredStore\git-credential-AWSSV4.exe' --profile=hogehoge
credential.usehttppath=true
これでようやく認証が通るようになった。