LoginSignup
0
0

More than 1 year has passed since last update.

git-secretsをgit initやgit cloneした時に自動で有効にして、AWSアクセスキーの漏洩を防ぐ

Last updated at Posted at 2023-03-28

git secretsのインストール

取り敢えずaptで書きますが、macの場合はhomebrewなど適宜変更してください。

$ sudo apt install git-secrets
(中略:インストール系のログ)
$ cd dir/my-repo #ご自身の既にあるgitのリポジトリに移動
$ git secrets --install
✓ Installed commit-msg hook to .git/hooks/commit-msg
✓ Installed pre-commit hook to .git/hooks/pre-commit
✓ Installed prepare-commit-msg hook to .git/hooks/prepare-commit-msg
$ git secrets --register-aws #AWS向けのcommit検査設定の導入
OK

これで、既存のリポジトリに導入が出来ました。

試してみる

試しに、適当なファイル名の新規ファイルを作ってみます

hoge.txt
AWSAccessKeyId=AKIAXXXXXXXXXXXXXXXX
aws_secret_access_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

尚、AWSの場合アクセスキーは20桁、シークレットアクセスキーは40桁固定になっており、桁数を変えると機能しなくなります。

$ git add hoge.txt
$ git commit -m "test"
hoge.txt:1:AWSAccessKeyId=AKIAXXXXXXXXXXXXXXXX
hoge.txt:2:aws_secret_access_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

[ERROR] Matched one or more prohibited patterns

Possible mitigations:
- Mark false positives as allowed using: git config --add secrets.allowed ...
- Mark false positives as allowed by adding regular expressions to .gitallowed at repository's root directory
- List your configured patterns: git config --get-all secrets.patterns
- List your configured allowed patterns: git config --get-all secrets.allowed
- List your configured allowed patterns in .gitallowed at repository's root directory
- Use --no-verify if this is a one-time false positive

すると、こんな感じで怒られるので、無事に導入出来ていることがわかります。

全てのリポジトリで同じチェックを働かせたい

上記だけだと、特定のリポジトリにしか検査が走らないので、全てのリポジトリでAWS向けのcommit検査設定の導入出来るようにします

$ git secrets --register-aws --global
OK

既にあるリポジトリで、実際に動作するか確認します。

$ cd dir/other-my-repo #ご自身の既にある別gitのリポジトリに移動
$ git secrets --install #installはしないと動かない
✓ Installed commit-msg hook to .git/hooks/commit-msg
✓ Installed pre-commit hook to .git/hooks/pre-commit
✓ Installed prepare-commit-msg hook to .git/hooks/prepare-commit-msg
$ cp ~/dir/my-repo/hoge.txt .
$ git add hoge.txt
$ git commit -m "test"
hoge.txt:1:AWSAccessKeyId=AKIAXXXXXXXXXXXXXXXX
hoge.txt:2:aws_secret_access_key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

[ERROR] Matched one or more prohibited patterns

Possible mitigations:
- Mark false positives as allowed using: git config --add secrets.allowed ...
- Mark false positives as allowed by adding regular expressions to .gitallowed at repository's root directory
- List your configured patterns: git config --get-all secrets.patterns
- List your configured allowed patterns: git config --get-all secrets.allowed
- List your configured allowed patterns in .gitallowed at repository's root directory
- Use --no-verify if this is a one-time false positive

無事怒られることを確認出来ました。

今後新しくinitしたりcloneしたりする際に、自動で有効にする

上記までの問題点として、今後新しくinitしたりcloneしたりするときに、毎回git secrets --installしないと行けないことです。忘れてしまって、いつもの調子でやったら検査してくれずに漏洩させてしまった…という事故が容易に想像できます。

ソレを防ぐために、初回で自動で起動するようにgitのデフォルトフックを設定します。
尚、以下は一度もデフォルトフックを設定していない人向けです。既に設定している人は、設定を上書きしてしまうので注意してください。既に設定している方は、該当3ファイルのgit-secretsと思われる箇所をコピペしたりなどして対応してください。

$ mkdir -p ~/.git-template/hooks
$ cd dir/my-repo #ご自身の既にあるgitのリポジトリに移動
$ cd .git/hooks/
$ cp commit-msg pre-commit prepare-commit-msg ~/.git-template/hooks/
$ git config --global init.templatedir '~/.git-template'

特にここでは確認は記載しませんが、一応initしたものやcloneしたものでクレデンシャルが弾かれるか確認してください。

0
0
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
0
0