git-secretsとは
git-secretsはGitリポジトリにコミットするときに、秘密情報を公開しないようにするOSSです。git-secretsは事前に公開しない秘密情報のパターンを定義し、その情報をgitのpre-commitの機能を用いてコミットしないようにしています。
インストールや使い方は以下の記事が詳しいのでそちらをどうぞ
クラウド破産しないように git-secrets を使う
検出パターンの定義は正規表現を用いて行いますが、設定をミスした場合どのように削除するのか公式に書いてなかったので備忘録として残します。(コミットミスのエラーメッセージにはある)
検出パターンの削除方法
git-secretsで定義した検出パターンは.git/config
もしくは~/.gitconfig
に書き込まれます。
そのためその行を削除すれば消えます。
$ git secrets --add 'passwd\s*=\s*".+"'
$ git secrets --add --global 'passwd\s*=\s*".+"'
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[secrets]
patterns = passwd\\s*=\\s*\".+\"
root@kali:~/Documents/git-secrets-test#
$ cat ~/.gitconfig
[user]
email = ********
name = ******
[secrets]
patterns = passwd\\s*=\\s*\".+\"
configから削除したらコミットできるかの確認
passwd.txtファイルを作成し、コミットして失敗するのを確認
$ echo 'passwd="P@ssw0rd"' > passwd.txt
$ git add passwd.txt
$ git commit -m "test commit"
passwd.txt:1:passwd="P@ssw0rd"
[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
.git/configや~/.gitconfigから削除して消えるのを確認
以下はすでにファイル編集済み
$ cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[secrets]
$ cat ~/.gitconfig
[user]
email = *****
name = *****
[secrets]
$ git commit -m "test commit" # git addはすでにしているのでコミットだけ
[master 16e54f1] test commit
1 file changed, 1 insertion(+), 1 deletion(-)
configからパターンを消したらコミットできることを確認
無心でawsの設定を使うだけであれば問題ないと思うが、自分で設定したパターンを使う人の参考になれば