WindowsでCodeCommitを使ってみた話

AWSのCodeCommitでは、gitリポジトリへの接続方法として httpsモードsshモード が用意されています。AWSではhttpsモードを推奨しています。httpsモードであれば、管理するのはIAMユーザのみとなり、ssh秘密鍵を管理しなくて良くなるので、こちらを推奨しているのだと思います。

結論を言ってしまうと、Windows利用者いる場合には sshモード を利用するのが無難だと思っています。

実際に、httpsモードを利用した場合どうなるか、以下に記載します。

環境

CodeCommitにgitリポジトリを作成

以下のコマンドにて、CodeCommitにgitリポジトリを作成します。

$ aws codecommit create-repository --repository-name Sample --repository-description "Sample Repository"

リポジトリにアクセスするIAMユーザの作成

上で作成したリポジトリにアクセスできるIAMユーザを作成していきます。

公式マニュアルはこちら。

AWS CLI 認証情報ヘルパーを使用して Windows 上で AWS CodeCommit リポジトリへの HTTPS 接続の設定手順 - AWS CodeCommit

IAMユーザを作成します。

$ aws iam create-user --user-name sample-git-user

作成したIAMユーザに、シークレットアクセスキーを付与します。

$ aws iam create-access-key --user-name sample-git-user

CodeCommitアクセス用IAMポリシーを付与します。今回はFullAccess権限を付与しています。

$ aws iam attach-user-policy --user-name sample-git-user --policy-arn arn:aws:iam::aws:policy/AWSCodeCommitFullAccess

作成したIAMユーザに、HTTPS Git認証を与えます。これを付与されたIAMユーザが、CodeCommitのGitリポジトリにアクセスできるようなります。

$ aws iam create-service-specific-credential --user-name sample-git-user --service-name codecommit.amazonaws.com

Client側AWS CLIで認証ヘルパーの設定

認証ヘルパーの設定を実施することで、IAMユーザの情報を基にCodeCommitのGitリポジトリへ認証できるようになります。

まずは、ローカルのAWS CLI認証情報に、CodeCommit用ユーザのprofileを作成します。

$ aws configure --profile codecommit-sample
AWS Access Key ID [None]: XXXXXXXXXXXX
AWS Secret Access Key [None]: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Default region name [None]: ap-northeast-1
Default output format [None]:

続いて、gitのコンフィグ情報に認証ヘルパーの設定を入れます。コマンドはこんな感じです。

$ git config --global credential.helper "!aws codecommit credential-helper --profile codecommit-sample $@"
$ git config --global credential.UseHttpPath true

以下のコマンドで設定内容を確認できます。

$ git config --global --edit
[credential]
        helper = !aws codecommit credential-helper --profile codecommit-sample $@
        UseHttpPath = true

git cloneの実行

設定完了したので、CodeCommitのgitリポジトリに対してgit cloneしてみます。

尚、当初gitのバージョンが version 2.19.1.windows.1 だったのですが、クローン時に以下fatalのメッセージが出力されていました。

$ git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/Sample
Cloning into 'Sample'...
fatal: NullReferenceException encountered.
    オブジェクト参照がオブジェクト インスタンスに設定されていません。
fatal: NullReferenceException encountered.
    オブジェクト参照がオブジェクト インスタンスに設定されていません。
warning: You appear to have cloned an empty repository.

StackOverFlowを見ると、Windowsのgitのバージョンを上げろ、とあったので、バージョンを 2.20 に上げてみた所、出力されなくなりました。

git - fatal: NullReferenceException encountered when interacting with remote - Stack Overflow

改めてgit commitして、pushしてみたところ、

$ echo test > test.txt

$ git add test.txt

$ git commit -m "CodeCommit test"
[master (root-commit) 34fa2a4] CodeCommit test
 1 file changed, 1 insertion(+)
 create mode 100644 test.txt

$ git push
Logon failed, use ctrl+c to cancel basic credential prompt.
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 225 bytes | 225.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/Sample
 * [new branch]      master -> master

pushする度に、以下のポップアップが表示されることになります。

f:id:goodbyegangster:20181227025753p:plain

回避策はないのかなとマニュアルを読んでいると、以下の記述がありました。一部Windowsだと互換性の関係で出ちゃうから、キャンセルを押して、と。そんな。pushする度にキャンセル押すことになるの。。。

On some versions of Windows, you might see a pop-up dialog box asking for your user name and password. This is the built-in credential management system for Windows, but it is not compatible with the credential helper for AWS CodeCommit. Choose Cancel.

ssh接続の場合

ちなみにssh接続する場合、公開鍵と秘密鍵を用意してあげて、以下のコマンドで利用するIAMユーザに公開鍵を登録してあげます。sshでCodeCommitのgitリポジトリにアクセスできるようなります。

$ aws iam upload-ssh-public-key --user-name sample-git-user --ssh-public-key-body "ssh-rsa XXXX...."