📝

CodeCommit の GitPush および GitPull アクションについて

2025/03/01に公開

CodeCommit permissions reference - AWS CodeCommit

In CodeCommit, the GitPull IAM policy permissions apply to any Git client command where data is retrieved from CodeCommit, including git fetch, git clone, and so on. Similarly, the GitPush IAM policy permissions apply to any Git client command where data is sent to CodeCommit. For example, if the GitPush IAM policy permission is set to Allow, a user can push the deletion of a branch using the Git protocol. That push is unaffected by any permissions applied to the DeleteBranch operation for that IAM user. The DeleteBranch permission applies to actions performed with the console, the AWS CLI, the SDKs, and the API, but not the Git protocol.

上記挙動を確認してみました。

  • IAM ポリシーで GitPull を禁止した状態で git clone
  • IAM ポリシーで DeleteBranch を禁止した状態でブランチ削除

なお、CodeCommit リポジトリは作成済みです。

IAM ポリシーで GitPull を禁止した状態で git clone

AdministratorAccess 権限が付与されている IAM ロールに以下のポリシーをアタッチしました。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Deny",
            "Action": "codecommit:GitPull",
            "Resource": "*"
        }
    ]
}

この状態で git clone コマンドを実行してもエラーになることを確認できました。

$ git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test
Cloning into 'test'...
fatal: unable to access 'https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test/': The requested URL returned error: 403
xxx:~/environment

CloudTrail にも AccessDenied の記録が残っていました。

{
  "eventSource": "codecommit.amazonaws.com",
  "eventName": "GitPull",
  "userAgent": "git/2.47.1",
  "errorCode": "AccessDenied",
  "errorMessage": "User: arn:aws:sts::012345678901:assumed-role/xxx/xxx is not authorized to perform: codecommit:GitPull on resource: arn:aws:codecommit:ap-northeast-1:012345678901:test with an explicit deny in an identity-based policy"
}

以上より、IAM ポリシーで GitPull を禁止した状態では git clone を実行できないことを確認できました。

IAM ポリシーで DeleteBranch を禁止した状態でブランチ削除

AdministratorAccess 権限が付与されている IAM ロールに以下のポリシーをアタッチしました。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Deny",
            "Action": "codecommit:DeleteBranch",
            "Resource": "*"
        }
    ]
}

コンソール上で dev ブランチを作成しておきます。

AWS CLI の delete-branch コマンドでは AccessDeniedException が発生することを確認します。

$ aws codecommit delete-branch --repository-name test --branch-name dev

An error occurred (AccessDeniedException) when calling the DeleteBranch operation: User: arn:aws:sts::012345678901:assumed-role/xxx/xxx is not authorized to perform: codecommit:DeleteBranch on resource: arn:aws:codecommit:ap-northeast-1:012345678901:test with an explicit deny in an identity-based policy

続いて、Git コマンドからブランチを削除してみます。

# クローン
$ git clone https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test
$ cd test

# ブランチ確認
$ git branch
* dev

# リモートブランチ削除
$ git push origin --delete dev
To https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/test
 - [deleted]         dev

CodeCommit コンソールを確認すると、dev ブランチが削除されていることを確認できました。

以上より、IAM ポリシーで DeleteBranch を禁止した状態でも Git クライアントからはブランチを削除できることを確認できました。

まとめ

CodeCommit の GitPush および GitPull アクションについて紹介しました。
どなたかの参考になれば幸いです。

参考資料

Discussion