LoginSignup
19
24

More than 3 years have passed since last update.

Windowsの開発環境からAWS CodeCommitのリポジトリに接続するメモ

Last updated at Posted at 2019-08-07

概要

CodeCommitに作成したリポジトリへWindows環境のIDEおよびGit for Windowsから接続する方法について調べたメモです。
Java開発を意識したのでIDEはEclipseとIntelliJ IDEAで検証しました。

確認した開発ツールと接続方法の組み合わせは以下の通りです。

  • Git for Windowsを利用する場合
    • HTTPS接続 + Git credential helper
    • SSH接続
  • Eclipseを利用する場合
    • HTTPS接続 + Eclipse Secure Storage
    • HTTPS接続 + AWS Toolkit for Eclipse
  • Intellij IDEAを利用する場合
    • HTTPS接続 + Git credential helper
    • HTTPS接続 + IntelliJ IDEA Passwords
    • SSH接続

簡単なまとめ

  • EclipseとGit for Windowsを併用する場合はHTTPS接続がお勧めです。
    • ただしHTTPS Git 認証情報はEclipseとGit for Windowsで別々の管理になります。
    • AWS Toolkit for EclipseはなくてもCodeCommitのリポジトリを操作できます。(2019/08時点で大きなアドバンテージはないように思いました)
  • IntelliJ IDEAとGit for Windowsを併用する場合はHTTPS/SSH接続どちら可能ですがHTTPS接続がお勧めです。
    • HTTPS Git 認証情報はGit credential helper (Windowsの資格情報マネージャ―)で一括管理できます。
    • IntelliJ IDEAにもAWS Toolkitがありますが試していません。(2019/03にリリースされました)

環境

  • Windows 10 Professional 1903
  • Eclipse 2019-06
    • AWS Toolkit for Eclipse 2.0
  • IntelliJ IDEA 2019.2
  • Git for Windows 2.20.0

参考

AWS側の準備

CodeCommitに接続できるIAMユーザを作成します。この記事ではCodeCommitに接続できる権限をグループに付与し、そのグループにIAMユーザを所属させるという形にしました。
図のアクセスキーの作成は特権ユーザで行い、AWS CodeCommit の SSH キーおよびAWS CodeCommit の HTTPS Git 認証情報の設定は個々のユーザで行うという想定で権限設定を行いました。
iam_auth_setting_01.png

グループの作成

developersというグループを作成し、以下のポリシーをアタッチします。

ポリシー 説明
AWSCodeCommitFullAccess Provides full access to AWS CodeCommit via the AWS Management Console.
IAMReadOnlyAccess Provides read only access to IAM via the AWS Management Console.
IAMUserSSHKeys Provides the ability for an IAM user to manage their own SSH keys.

IAMユーザの作成

検証用のcodecommit_test_userというCodeCommitのリポジトリを操作するユーザを作成します。
ユーザ作成後にアクセスキーを作成しdevelopersグループに追加します。

CodeCommitの認証情報設定

ここからは作成したIAMユーザで操作を行います。作成したIAMユーザでAWS Management Consoleにログインし、IAMのユーザ画面の認証情報タブを開きます。(初回ログイン時はパスワードの変更を求められます。)

SSH接続用の認証情報の作成

Windows側でGit for WindowsのGit Bashを立ち上げssh-keygenコマンドで鍵ファイルを作成します。

$ ssh-keygen

作成した秘密鍵ファイル(private key)はユーザーホームディレクトリ/.sshに配置し、公開鍵ファイル(public key)はAWS CodeCommit の SSH キーセクションからアップロードします。
iam_auth_setting_02.png

SSH パブリックキーのアップロードボタンを押すと、公開鍵の内容をアップロードする画面が表示されるので、公開鍵の内容を貼り付けてアップロードします。
iam_auth_setting_03.png

アップロードするとSSH キー IDが発行されます。SSH キー IDは.ssh/configファイルを編集するときに必要になります。

HTTPS接続用の認証情報の作成

AWS CodeCommit の HTTPS Git 認証情報セクションにある生成ボタンをクリックします。説明文にあるとおり認証情報は2つまで登録できます。
iam_auth_setting_04.png

生成されたユーザーとマスクされたパスワードが画面に表示されます。この画面にある証明書のダウンロードボタンをクリックして証明書ファイルをダウンロードして保管します。
iam_auth_setting_05.png

認証情報の設定結果

SSHとHTTPSの両方を検証するため最終的に図のような結果になりましたが、使用する開発ツールや接続方法によって設定の必要がないものもあります。たとえば、HTTPS接続だけで済むのであればSSH接続用の設定は不要です。
iam_auth_setting_06.png

リポジトリの作成

引き続き同じユーザで、検証で使うcodecommit-test-repoというリポジトリを作成します。
CodeCommitのリポジトリ画面でリポジトリの作成ボタンをクリックします。
create_repo_01.png

リポジトリ名を入力して作成ボタンをクリックします。
create_repo_02.png

リポジトリの作成が完了すると"接続のステップ"という画面が表示されます。この画面の下のほうにファイルを追加する欄があるので、ここでREADME.mdというファイルを追加しコミットしました。(この操作は必須ではありません。)
図はファイル追加直後の状態です。
create_repo_03.png

次にブランチメニューを開きブランチの作成ボタンをクリックして、developというブランチを作成します。
create_repo_04.png
create_repo_05.png

図はブランチを作成した直後の状態です。masterがデフォルトブランチになっているのでdevelopへ変更します。
create_repo_06.png

次に設定メニューを開きデフォルトブランチをdevelopに変更します。
create_repo_07.png

最後にURLのクローンメニューからSSHとHTTPS用のURLを控えておきます。

接続 URL
SSH ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/codecommit-test-repo
HTTPS https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/codecommit-test-repo

Git for Windows

HTTPSで接続する

Git credential helperの設定

Git credential helperでユーザ名・パスワードをキャッシュしておくと、接続のたびに入力しなくて済むようになります。
以下のコマンドで現在の設定を確認します。managerと出力されれば設定されていることになります。(managerはWindows用のcredential helperです。)

$ git config --global credential.helper
manager

以下のコマンドで設定します。(.gitconfigを直接編集することでもかまいません。)

$ git config --global credential.helper manager

設定を取り消す場合は以下のコマンドを実行します。

$ git config --global --unset credential.helper

※確認していませんが、Git for Windowsのインストール時にEnable Git Credential Managerにチェックを付けておく必要があるようです。
git_for_windows_install.png

リポジトリをクローンする

$ git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/codecommit-test-repo
Cloning into 'codecommit-test-repo'...
remote: Counting objects: 10, done.
Unpacking objects: 100% (10/10), done.

Git credential helperが有効になっている場合、キャッシュされていなければ接続時に図のダイアログが表示されます。
windows_git_credential_manager_01.png

ここで登録したユーザ名・パスワードは、実際にはWindowsの資格情報マネージャが管理しています。
コントロールパネルユーザアカウント資格情報マネージャーWindows 資格情報の管理でアクセスできます。
windows_資格情報マネージャ_01.png
図の枠線が今回登録した資格情報です。削除したい場合は削除をクリックします。
windows_資格情報マネージャ_02.png

SSHで接続する

.ssh/configの編集

SSHのconfigファイルを編集します。
Userには上記の「SSH接続用の設定情報の作成」で発行されたSSH キー IDを指定、IdentityFileには作成した秘密鍵ファイルを指定します。

Host git-codecommit.*.amazonaws.com
  User <SSH キー ID>
  IdentityFile "~/.ssh/<秘密鍵ファイル>"

テスト

Git Bashからsshで接続を行い、You have successfully authenticated over SSH.というメッセージを確認します。

$ ssh git-codecommit.us-east-2.amazonaws.com
// ...省略

You have successfully authenticated over SSH. You can use Git to interact with AWS CodeCommit. Interactive shells are not supported.Connection to git-codecommit.us-east-2.amazonaws.com closed by remote host.
Connection to git-codecommit.us-east-2.amazonaws.com closed.

リポジトリをクローンする

$ git clone ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/codecommit-test-repo
Cloning into 'codecommit-test-repo'...
remote: Counting objects: 3, done.
Receiving objects: 100% (3/3), 248 bytes | 248.00 KiB/s, done.

developブランチをcheckoutします。

$ cd codecommit-test-repo
$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

$ git checkout develop
Switched to a new branch 'develop'
Branch 'develop' set up to track remote branch 'develop' from 'origin'.

README.mdを編集してcommit & push

$ git add README.md

$ git commit -m "2nd commit via Git for Windows"
[develop 2b1d436] 2nd commit via Git for Windows
 1 file changed, 5 insertions(+), 1 deletion(-)

$ git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 323 bytes | 161.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/codecommit-test-repo
   5a394b3..2b1d436  develop -> develop

Eclipse

EclipseにはAWS Toolkit for Eclipseというプラグインがあり、これをインストールすることでEclipse上からさまざまな操作(EC2、S3、SQS、DynamoDB、CodeCommit、CloudFront、SNS)が行えるようになるようです。
EclipseからCodeCommitのリポジトリを操作するのにAWS Toolkit for Eclipseは必須ではありませんが、このプラグインを使う場合の操作を確認するために使用しない場合と使用した場合の2通りを試しました。

HTTPSで接続する

AWS Toolkit for Eclipseを使わない場合

他のGitホスティングサービスと同じ手順になります。
LocationのURIにHTTPS接続用のURLを入力、AuthenticationのUser/PasswordはAWS Management Consoleで生成したHTTPS Git 認証情報を入力します。
Store in Secure Storeにチェックを入れるとEclipseが資格情報を管理してくれるので次回から入力は不要になります。
※EclipseはGit credential helperをサポートしていないようです。
eclipse_https_clone_not_use_toolkit_01.png

Branch SelectionではそのままNextボタンをクリックします。
eclipse_https_clone_not_use_toolkit_02.png

Local Destinationでローカルのディレクトリを指定しFinishボタンをクリックします。
eclipse_https_clone_not_use_toolkit_03.png

初めてSecure Storageを使う場合、パスワードリカバリー用のヒントを設定するか確認されます。
eclipse_https_clone_not_use_toolkit_04.png

Yesをクリックすると図のようにパスワードリカバリー用のヒントを設定します。
eclipse_https_clone_not_use_toolkit_05.png

GitパースペクティブのGit Repositoriesは図のようになっています。あとは他のGitホスティングサービスと同様の操作になります。
eclipse_https_clone_not_use_toolkit_06.png

資格情報をクリアするには

メニューバーのWindows(W)PreferencesGeneralSecuritySecure StorageContentsタブを開きます。
個別の要素を削除することはできずDeleteボタンをすべて削除されるので注意が必要です。
eclipse_secure_storage.png

AWS Toolkit for Eclipseを使う場合

※インストールは補足欄を参照してください。

AWS Toolkit for Eclipseインストール後の再起動時にアクセスキーとシークレットアクセスキーを入力する画面が表示されますが、いったんCancelボタンを押してキャンセルします。
aws_tool_kit_market_place_04.png

Eclipseが起動したらメニューバーからWindowPreferencesAWS Toolkitを選択、入力画面が表示されます。
最初に緑色の十字アイコン(Configure Regional Account)をクリックします。リージョンを選択する画面で"Asia Pacific (Tokyo)"を選択します。
aws_tool_kit_market_place_04_5.png

Add profileボタンをクリックしてプロファイルを追加します。ここではProfile Nameにユーザ名を入力しました。続いてアクセスキーとシークレットアクセスキーの入力しApplyボタンを押します。
aws_tool_kit_market_place_05_b.png

入力したアクセスキーとシークレットアクセスキーはAWS credentials fileに保存されます。リロードするか確認されるのでYesをクリックします。
aws_tool_kit_market_place_06.png
※AWS credentials fileはユーザーホームディレクトリの.aws/credentialsにありキーは平文で保存されます。
※このファイルはAWS CLIからも参照されます。

AWS Toolkitの1つ下の階層にあるAWS CodeCommitを選択します。HTTPS Git 認証情報で作成したユーザ名とパスワードを入力しApply and Closeボタンを押して画面を閉じます。
aws_tool_kit_market_place_07.png
※ここで登録した認証情報はEclipseのSecure Storageにも保存されます。

AWS Managementパースペクティブを開く

EclipseにAWS Managementパースペクティブが追加されています。図はパースペクティブを開いた直後の状態です。
aws_tool_kit_market_place_08.png

ビューの国旗のアイコンをクリックしてリージョンをAsia Pacific (Tokyo)へ変更します。
aws_tool_kit_market_place_09.png

AWS ExplorerのAWS CodeCommitをクリックすると作成されているリポジトリが表示されます。
aws_tool_kit_market_place_10.png

リポジトリをクローンする

リポジトリ名を右クリックしてメニューからClone Repositoryを選択します。
aws_tool_kit_market_place_11.png

図のようにエラー画面が表示されます(2019/08現在)が、その後の操作には影響がないようなのでこのまま進めます。
aws_tool_kit_market_place_12.png

Nextボタンをクリックします。
aws_tool_kit_market_place_13.png

ローカルのディレクトリを指定してFinishボタンをクリックします。
aws_tool_kit_market_place_14.png

Gitパースペクティブを開く

Gitパースペクティブを開くとGit Repositoriesにcloneしたリポジトリが追加され、ワークスペースにもチェックアウトされています。
aws_tool_kit_market_place_17.png

SSHで接続する(できない)

2019/08現在、EclipseでCodeCommitのリポジトリにSSH接続する方法は見つかりませんでした。

IntelliJ IDEA

HTTPSで接続する

Clone Repository画面でURLにHTTPS接続用のURLを入力しTestボタンをクリックすると、ユーザ名・パスワードを入力するか、credential helperを利用するか選択するダイアログが表示されます。
intellij_https_clone_01.png

ユーザ名・パスワードを入力する

※Git credential helperが有効な場合は、そちらの認証画面が先に表示されます。

Enter credentialsをチェックしUsernameおよびPasswordにHTTPS接続用のユーザ名・パスワードを入力しLog Inボタンをクリックします。
Rememberにチェックすると入力した資格情報はIntellij IDEAが管理するファイルに保存されるので次からの入力は不要になります。
intellij_https_clone_04.png

接続できれば画面のように"Connection successful"と表示されるので、そのままCloneボタンをクリックします。
intellij_https_clone_05.png

資格情報をクリアするには

SettingsAppearance & BehaviorSystem SettingsPasswordsを選択。
Database欄の右端にある歯車アイコンをクリックしClearを選択します。
intellij_https_clone_06.png

credential helperを利用する

Use credentials helperをチェックしLog Inボタンをクリックします。
intellij_https_clone_02.png

接続できれば画面の通り"Connection successful"と表示されるので、そのままCloneボタンをクリックします。
intellij_https_clone_03.png

SSHで接続する

Git for WindowsでSSH接続が可能で、且つGitクライアントにGit for Windowsが設定されていれば(デフォルト)そのまま利用できます。
intellij_git.png

補足

ローカルリポジトリをリモートへpushする方法

Git for Windowsでローカルリポジトリをリモートへpushする方法の作業手順です。

リモートリポジトリの作成

AWS Management Consoleでリモートリポジトリを作成します。
リポジトリ名はdemo-aws-repo、接続方法はHTTPSにしました。

接続 URL
HTTPS https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/demo-aws-repo

ローカルのプロジェクトをgit化

Git Bashで以下のコマンドを実行します。

$ git init
$ git add .
$ git commit -m "first commit"

リモートへプッシュ

$ git remote add origin https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/demo-aws-repo
$ git remote -v
origin  https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/demo-aws-repo (fetch)
origin  https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/demo-aws-repo (push)
$ git push -u origin master

Enumerating objects: 26, done.
Counting objects: 100% (26/26), done.
Delta compression using up to 8 threads
Compressing objects: 100% (17/17), done.
Writing objects: 100% (26/26), 49.68 KiB | 2.61 MiB/s, done.
Total 26 (delta 0), reused 0 (delta 0)
To https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/demo-aws-repo
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

AWS Toolkit for Eclipseのインストール

マーケットプレースからAWS Toolkit for Eclipseをインストールします。
aws_tool_kit_market_place_01.png

AWS CodeCommit Pluginに追加のチェックを入れてConfirmボタンをクリックします。
aws_tool_kit_market_place_02.png

ライセンスに同意してFinishボタンをクリックします。Eclipseを再起動します。
aws_tool_kit_market_place_03.png

AWS CLIのインストール

インストールするには管理者権限が必要です。

aws_cli_install_01.png

aws_cli_install_02.png

aws_cli_install_03.png

aws_cli_install_04.png

aws_cli_install_05.png

バージョンの確認

> aws --version
aws-cli/1.16.210 Python/3.6.0 Windows/10 botocore/1.12.200

help

> aws help

helpはサービスやコマンドの引数に指定します。

> aws configure help
      ^^^^^^^^^
       |
       +--- サービス
> aws configure list help
      ^^^^^^^^^ ^^^^
       |         |
       |         +--- コマンド
       |
       +--- サービス

configure

リージョンをap-northeast-1に設定する例

> aws --profile codecommit_test_user configure set region ap-northeast-1

リージョンを取得する例

> aws --profile codecommit_test_user configure get region
ap-northeast-1
> aws --profile codecommit_test_user configure list
      Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile     codecommit_test_user           manual    --profile
access_key     ****************LFHV shared-credentials-file
secret_key     ****************t0OD shared-credentials-file
    region           ap-northeast-1      config-file    ~/.aws/config

codecommit

リポジトリの一覧を取得する例

> aws --profile codecommit_test_user codecommit list-repositories
{
    "repositories": [
        {
            "repositoryName": "codecommit-test-repo",
            "repositoryId": "d035e9da-1ab3-4f5f-8d25-8f79a0ae781b"
        }
    ]
}

AWS Toolkit for Intellij

Git - Credential Storage

credential helperにwincredを使う記事を見かけますが、Git for Windows 2.xではwincredは非推奨のようです。

$ git config --global credential.helper wincred

7.14 Git のさまざまなツール - 認証情報の保存

Windows を使っているなら、“wincred” という補助ツールがあります。 “osxkeychain” と同じような仕組み(Windows Credential Store)で、重要な情報を管理します。

と翻訳されていますが、英語サイトを見ると当該の箇所は

If you’re using Windows, you can install a helper called “Git Credential Manager for Windows.” This is similar to the “osxkeychain” helper described above, but uses the Windows Credential Store to control sensitive information. It can be found at https://github.com/Microsoft/Git-Credential-Manager-for-Windows.

と書かれています。

Git for Windows 2.xはmanager (Git Credential Manager for Windows (GCM))がデフォルトです。

$ git config --global credential.helper manager
19
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
24