LoginSignup
42
36

More than 5 years have passed since last update.

Code Commitのgitリポジトリにsshとhttpsでアクセスしてみる

Posted at

Code Commitが使えるようになったのでsshとhttpsでアクセスしてみた時のメモ

環境

  • MacOS X
  • リポジトリは作成済み(GUIで簡単にできます。AWS CLIでもできるようです)
  • AWS CLIはインストール済み

HTTPSを使ってアクセスする

Set Up the AWS CodeCommit Credential Helper for HTTPS Connections

AWS CLIのアップグレード

AWS CLIのバージョン情報を確認します。

$aws --version

1.7.38より古い場合には以下コマンドでアップグレードを行います。

$sudo pip install --upgrade awscli
$aws --version
aws-cli/1.7.38 Python/2.7.6 Darwin/14.4.0

設定を行う

AWS CLIでCodeCommitを使う時のアクセスキーなどを設定します。aws configure --profile CodeCommitProfileというコマンドでCodeCommit用のプロファイルを作成します。2015 7/13現在、tokyoリージョンでは使えないのでDefault region nameではus-east-1を選択してください。Access Key ID、Secret Access Keyは接続したいIAMユーザーの情報を設定してください。

$aws configure --profile CodeCommitProfile

AWS Access Key ID [None]: 
AWS Secret Access Key [None]: 
Default region name [None]: us-east-1 
Default output format [None]: 

上記コマンドで ~/.aws/credentials にCodeCommit用のプロファイル情報が追記されます。

次にGitを利用する際の認証で先ほど作ったプロファイル情報を利用するために以下のコマンドを実行します。

$git config --global credential.helper '!aws --profile CodeCommitProfile codecommit credential-helper $@'
$git config --global credential.UseHttpPath true

上記を実行によって ~/.gitconfig に以下が追記されているかと思います。

~/.gitconfig
[credential]    
    helper = !aws --profile CodeCommitProfile codecommit credential-helper $@
    UseHttpPath = true

使ってみる

準備が出来たので実際にgit cloneしてみます。リポジトリ名は自分のものに適宜変更してください。

$git clone https://git-codecommit.us-east-1.amazonaws.com/v1/repos/AWSTest
Cloning into 'AWSTest'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.

あとは普通に使えます。

$cd AWSTest
$touch README
$git add README
$git commit -m "first commit"
$git push origin master

SSHを使ってアクセスする

Set Up AWS CodeCommit for SSH Connections

SSH公開鍵の確認、作成

SSH公開鍵があるか確認します。

$cd ~/.ssh
$ls
authorized_keys2  id_rsa       known_hosts
config            id_rsa.pub

id_rsaid_rsa.pub のような組み合わせがあれば既に存在するものを使ってもいいですし、CodeCommit用に別に作っても良いと思います。

存在しないなどの場合にはssh-keygenコマンドで作成してください。

公開鍵のアップロード

作成した公開鍵をIAMと紐付けます。
公開鍵のアップロードにはAWS CLIを使って以下のようなコマンドを実行します。
file://id_rsa.pub は先ほど作成した公開鍵のパス、 --user-name では関連付けしたいIAMのユーザー名を入力しください。

$cd ~/.ssh/
$aws iam upload-ssh-public-key --ssh-public-key-body file://id_rsa.pub --user-name HogeFugaUser
{
    "SSHPublicKey": {
        "UserName": "HogeFugaUser",
        "Status": "Active",
        "SSHPublicKeyBody": "・・・",
        "UploadDate": "2015-07-12T13:05:40.244Z",
        "Fingerprint": "・・・・",
        "SSHPublicKeyId": "・・・・"
    }
}

アップロードが成功すると上記ような標準出力が表示されるので SSHPublicKeyId をメモしておきます。

CodeCommitサーバーのRSA公開鍵のフィンガープリントの登録

以下のコマンドで ~/.ssh/known_hosts にcode commit(US)のRSA公開鍵のフィンガープリントを登録します。他のリージョンができた場合、別途そちらの情報を追記する必要があるかと思います。

$echo "git-codecommit.us-east-1.amazonaws.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCdut7aOM5Zh16OJ+GOP75O7x5oyHKAiA1ieuySetj/hAq4VrAuZV5R2TypZJcKBaripOtTc/Sr0FOU4YvxUla40PPH8N1lbDp6Pnc4BexKsrt2kz++TqIKx5FHmUQV3mit16kxRwHey3dv030+qXBDo3WPQjm2+JLoq0XcadpnCAMCd3ChaBnDRM+51GZbuEFilpZsxUchUzl0gseC+shYOBd7TqxTlIhj/56d/YF1kq7RMZYrwBnyYdVhpLeUJCeYjyx/O6FPSezNTLiinz5jjioWZATgn+G8feL/hIsk8g+7JoIcb2muUlymdxs+8l2lS+8MXqT0q9ohT+Knhb2j" >> ~/.ssh/known_hosts

ssh configの設定

code commitのホストにssh接続する場合にどのユーザーでどの秘密鍵を使うかを指定します。
指定は ~/.ssh/config に以下の内容を追記します。なお、 hogefuga には公開鍵アップロード後に返却されたSSHPublicKeyIdを指定し、~/.ssh/id_rsa ではアップロードした公開鍵と対応する秘密鍵のパスを指定してください。

$vi ~/.ssh/config
~/.ssh/config
Host git-codecommit.*.amazonaws.com
  User hogefuga
    IdentityFile ~/.ssh/id_rsa

接続確認

以下のコマンドでssh接続確認してみます。

$ssh git-codecommit.us-east-1.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-1.amazonaws.com closed by remote host.
Connection to git-codecommit.us-east-1.amazonaws.com closed.

上記のようにsuccessfullyと出力されれば成功です。

使ってみる

git cloneする時のスキームをsshに変更してうまくできればOKです

$git clone ssh://git-codecommit.us-east-1.amazonaws.com/v1/repos/AWSTest
$cd AWSTest
$git log 
42
36
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
42
36