Programming Self-Study Notebook

勉強したことを忘れないように! 思い出せるように!!

「git-secrets」のはじめかた(Windows編)

f:id:overworker:20201107214512p:plain:h150

git-secretsとは

  • gitのcommit/commit message等の中に事前設定した公開すべきではない情報が含まれていた場合に、そのcommitを却下(リジェクト)する。
  • コミットを却下するので、秘密情報を共有リポジトリ上に公開するリスクが低減される。

git-secretsの導入方法(Windows編)

手順1.git-secretsをzipでダウンロードする

GitHubgit-secretsのページからzipでダウンロードする

f:id:overworker:20201107221155p:plain:h250

手順2.適当な場所に展開する

  • どこでもOKです

手順3.インストールする

  • .ps1というファイルをコマンド実行する際のツールはWindows PowerShellというツールです。
  • コマンドプロンプト上でコマンド実行してもエラーになります。

手順3-1.Windows PowerShellを起動する

f:id:overworker:20201107222211p:plain:h250

手順3-2.install.ps1を実行する

  • Windows PowerShell上のカレントディレクトリを1-2で展開したディレクトリに移動してください。
    • ディレクトリ内にinstall.ps1というファイルが入っていることを確認してください。
  • ./install.ps1を実行する。
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

新しいクロスプラットフォームの PowerShell をお試しください https://aka.ms/pscore6

PS C:\Users\Test> cd C:\Users\Test\Documents\Program\git
PS C:\Users\Test\Documents\Program\git> cd .\git-secrets\
PS C:\Users\Test\Documents\Program\git\git-secrets> ./install.ps1
./install.ps1 : このシステムではスクリプトの実行が無効になっているため、ファイル C:\Users\Test\Documents\Program\git
\Santana\other\git-secrets\install.ps1 を読み込むことができません。詳細については、「about_Execution_Policies」(https:/
/go.microsoft.com/fwlink/?LinkID=135170) を参照してください。
発生場所 行:1 文字:1
+ ./install.ps1
+ ~~~~~~~~~~~~~
    + CategoryInfo          : セキュリティ エラー: (: ) []、PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess

→ 上記エラーが発生しなかった場合は、1-3-5まで飛ばして下さい。

手順3-3.install.ps1でのエラーへの対応方法
 →コマンド実行に対するセキュリティレベルの変更

  • 上記エラーの原因は、PowerShell のスクリプトの実行が実行ポリシーによって許可されていないからです。

  • 今回は、実行ポリシーをRestrictedからRemoteSignedに変更したいと思います。

  • まずは、変更前の実行ポリシーを確認します。

PS C:\Users\Test\Documents\Program\git\git-secrets> Get-ExecutionPolicy
Restricted
  • 次に、実行ポリシーを変更します。
PS C:\Users\Test\Documents\Program\git\git-secrets> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process

実行ポリシーの変更
実行ポリシーは、信頼されていないスクリプトからの保護に役立ちます。実行ポリシーを変更すると、about
_Execution_Policies のヘルプ トピック (https://go.microsoft.com/fwlink/?LinkID=135170)
で説明されているセキュリティ上の危険にさらされる可能性があります。実行ポリシーを変更しますか?
[Y] はい(Y)  [A] すべて続行(A)  [N] いいえ(N)  [L] すべて無視(L)  [S] 中断(S)  [?] ヘルプ
(既定値は "N"):Y
  • 変更後の実行ポリシーを確認します。
PS C:\Users\Test\Documents\Program\git\git-secrets> Get-ExecutionPolicy
RemoteSigned

手順3-4.install.ps1を実行する(2回目)

PS C:\Users\Test\Documents\Program\git\git-secrets> ./install.ps1
Checking to see if installation directory already exists...
Creating installation directory.
Copying files.
Checking if directory already exists in Path...
Adding to path.
Adding to user session.
Done.

installが成功しました。

手順3-5.git securetsをグローバル登録しておく

PS C:\Users\Test\Documents\Program\git\git-secrets> git secrets --register-aws --global
OK

→ここまでが基本的な準備です。

手順4.ワークスペースgit securetsを適用する

  • ワークスペース.git ディレクトリがあるところでgit secrets --installを実行します
PS C:\Users\Test\Documents\Program\git\git-secrets> cd ..\workspace\
PS C:\Users\Test\Documents\Program\git\workspace> 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

手順5.gitのクライアントツールに適用する?

  • 私はSourceTreeというクライアントツールを使用しているのですが、以下の手続きを実施しないとエラーになってしまいました。
  • Windowsでは %USERPROFILE%\.git-secrets にあるgit-secretsgit-secrets.1をGitコマンドのあるところ(C:\Program Files\Git\cmd など)にコピーしてください。

上記で、設定は完了です。