tag:crieit.net,2005:https://crieit.net/tags/GnuPG/feed 「GnuPG」の記事 - Crieit Crieitでタグ「GnuPG」に投稿された最近の記事 2019-12-21T15:38:21+09:00 https://crieit.net/tags/GnuPG/feed tag:crieit.net,2005:PublicArticle/15630 2019-12-21T15:38:21+09:00 2019-12-21T15:38:21+09:00 https://crieit.net/posts/MinQ-7-Git MinQ開発日記 (7) 認証情報を含んだプロジェクトをGitで管理する <h1 id="認証情報を含んだプロジェクトをGitで管理する"><a href="#%E8%AA%8D%E8%A8%BC%E6%83%85%E5%A0%B1%E3%82%92%E5%90%AB%E3%82%93%E3%81%A0%E3%83%97%E3%83%AD%E3%82%B8%E3%82%A7%E3%82%AF%E3%83%88%E3%82%92Git%E3%81%A7%E7%AE%A1%E7%90%86%E3%81%99%E3%82%8B">認証情報を含んだプロジェクトをGitで管理する</a></h1> <h2 id="モチベーション"><a href="#%E3%83%A2%E3%83%81%E3%83%99%E3%83%BC%E3%82%B7%E3%83%A7%E3%83%B3">モチベーション</a></h2> <p>GORMでデータベースへアクセスする場合ユーザー名やパスワードが必要でした. こうした認証情報をgitで管理してGitHubのようなリモート・リポジトリに上げてしまうのは例え非公開でも大変危険です. かといってこれをプロジェクト外部で管理すると, デプロイ時にめんどいうになりそうです. 後間違えコミットしてしまうということも有り得ます.</p> <h2 id="用語"><a href="#%E7%94%A8%E8%AA%9E">用語</a></h2> <h3 id="PGP (Pretty Good Privacy)"><a href="#PGP+%28Pretty+Good+Privacy%29">PGP (Pretty Good Privacy)</a></h3> <p>暗号化プログラムで, 公開暗号方式を使うようです.</p> <h3 id="GPG (GnuPG)"><a href="#GPG+%28GnuPG%29">GPG (GnuPG)</a></h3> <p>GNUによるオープンソースなPGPです.</p> <h3><a target="_blank" rel="nofollow noopener" href="https://www.gnupg.org/software/pinentry/index.html">pinentry</a></h3> <p>GnuPGがパスフレーズやPIN番号を読み取れるようにするプログラムのようです. なくても良さそうでは有ります.</p> <h3><a target="_blank" rel="nofollow noopener" href="https://linux.die.net/man/1/gpg-agent">gpg-agent</a></h3> <p>秘密鍵を管理してくれるデーモンのようです. ssh-agentと同じようなもののようです.</p> <blockquote> <p>gpg-agent is a daemon to manage secret (private) keys independently from any protocol. It is used as a backend for gpg and gpgsm as well as for a couple of other utilities.</p> </blockquote> <p><a target="_blank" rel="nofollow noopener" href="https://linux.die.net/man/1/gpg-agent">gpg-agent(1) - Linux man page</a></p> <blockquote> <p>If the agent process has the key, it provides it to gpg. If it doesn't, it attempts to load the encrypted key from your keyring, and prompts you for the key's passphrase. Once the agent has obtained the decrypted key, it passes it to the gpg process. In addition to GPG keys, Gpg-agent can similarly store SSH keys and provide them to SSH processes, like the ssh-agent program that comes with SSH.</p> </blockquote> <p><a target="_blank" rel="nofollow noopener" href="https://unix.stackexchange.com/a/188813/382631">How does GPG agent work?</a></p> <blockquote> <p>gpg-agent is mostly used as daemon to request and cache the password for the keychain.</p> </blockquote> <p><a target="_blank" rel="nofollow noopener" href="https://wiki.archlinux.org/index.php/GnuPG#gpg-agent">gpg-agent</a></p> <blockquote> <p>gpg-agent can be configured via the pinentry-program stanza to use a particular pinentry user interface when prompting the user for a passphrase.</p> </blockquote> <p><a target="_blank" rel="nofollow noopener" href="https://wiki.archlinux.org/index.php/GnuPG#pinentry">pinentry</a></p> <h3 id="用語の整理"><a href="#%E7%94%A8%E8%AA%9E%E3%81%AE%E6%95%B4%E7%90%86">用語の整理</a></h3> <div class="table-responsive"><table> <thead> <tr> <th>名前</th> <th>用途</th> </tr> </thead> <tbody> <tr> <td>GPG(GnuPG)</td> <td>暗号化や署名の生成を行うプログラム</td> </tr> <tr> <td>Pinetnry</td> <td>GPGにパスフレーズやPIN番号を安全に渡すプログラム</td> </tr> <tr> <td>gpg-agent</td> <td>GPGの代理として鍵を管理するプログラム?</td> </tr> </tbody> </table></div> <p>GPG本体とgpg-agentの違いというか役割分担がよく分かっていませんね😅</p> <h2 id="GnuPG"><a href="#GnuPG">GnuPG</a></h2> <h3 id="導入"><a href="#%E5%B0%8E%E5%85%A5">導入</a></h3> <p>brewを使うと簡単にできます</p> <pre><code class="bash">brew install gnupg gnupg2 pinentry-mac gpg-agent </code></pre> <p>gpg-agentにpinentry-programを指定します.</p> <pre><code class="bash">which pinentry-mac echo "pinentry-program /usr/local/bin/pinentry-mac" >>~/.gnupg/gpg-agent.conf cat ~/.gnupg/gpg-agent.conf </code></pre> <p>ところがgpg-agentは存在しません. GPG 2.1以降はgpg-agent付属して配布されているようです.</p> <p><a target="_blank" rel="nofollow noopener" href="https://stackoverflow.com/questions/52435650/gpg-agent-not-found-for-homebrew">gpg-agent not found for homebrew</a></p> <blockquote> <p>It expects that the reader is familiar with GnuPG version 2.0 and aware that GnuPG consists of gpg, gpgsm, and gpg-agent as its main components.</p> </blockquote> <p><a target="_blank" rel="nofollow noopener" href="https://www.gnupg.org/faq/whats-new-in-2.1.html">GnuPG</a></p> <p>これでGPGで鍵を生成するときにパスフレーズが求められるとpinentry-programであるpinentry-macが起動するはずです. が, どうも2.0以降はpinentry-programの設定が変わっているようで動きませんでした. Stackoverflowに質問しておきました.</p> <p><a target="_blank" rel="nofollow noopener" href="https://stackoverflow.com/questions/59433705/where-should-i-specify-a-pinentry-program-for-gnupg-2-0-and-later">Where should I specify a pinentry program for GnuPG 2.0 and later?</a></p> <p>とりあえずなしでいきます.</p> <h3 id="鍵の生成"><a href="#%E9%8D%B5%E3%81%AE%E7%94%9F%E6%88%90">鍵の生成</a></h3> <p>必要となる情報は以下です.</p> <ul> <li>名前</li> <li>e-mail</li> <li>パスフレーズ</li> </ul> <pre><code class="bash">gpg --generate-key </code></pre> <p>で用意した情報を入力しましょう.</p> <h2 id="git-crypt"><a href="#git-crypt">git-crypt</a></h2> <h3 id="導入"><a href="#%E5%B0%8E%E5%85%A5">導入</a></h3> <p>こちらもbrewでインストールします.</p> <pre><code class="bash">brew install git-crypt </code></pre> <h3 id="利用"><a href="#%E5%88%A9%E7%94%A8">利用</a></h3> <p><a target="_blank" rel="nofollow noopener" href="https://dev.classmethod.jp/tool/git/git-crypt/">git-crypt を使って秘密情報を版管理する</a>を参考にしてやってみましょう.</p> <h2 id="まとめ"><a href="#%E3%81%BE%E3%81%A8%E3%82%81">まとめ</a></h2> <p>これで公開鍵を知らない人は中身を見れなくなりました. よってGitHubにアップロードして問題ないと思われます. VPSに公開鍵をアップロードしておけばgit crypt unlockで復号化されます.</p> <h2 id="課題"><a href="#%E8%AA%B2%E9%A1%8C">課題</a></h2> <p>デプロイするときに面倒が増します. Gitのフックはpush時しか出来ないからです. のでGitHubはリモート・リポジトリと割り切ってデプロはAnsibleとか使った方が良いと思いました.</p> <p><a target="_blank" rel="nofollow noopener" href="https://satoshun.github.io/2015/02/ansible-go_deploy/">AnsibleでGoアプリをデプロイ</a></p> <h2 id="Reference"><a href="#Reference">Reference</a></h2> <p><a target="_blank" rel="nofollow noopener" href="https://www.digitalocean.com/community/tutorials/an-introduction-to-managing-secrets-safely-with-version-control-systems">An Introduction to Managing Secrets Safely with Version Control Systems</a><br /> <a target="_blank" rel="nofollow noopener" href="https://qiita.com/daisukeoda/items/c6b6c36009fa3409dc39">本番用の.envを外部に一切知られずに安全にgithubで保存する方法</a><br /> <a target="_blank" rel="nofollow noopener" href="https://dev.classmethod.jp/tool/git/git-crypt/">git-crypt を使って秘密情報を版管理する</a></p> ブレイン