2019-12-21に投稿

MinQ開発日記 (7) 認証情報を含んだプロジェクトをGitで管理する

認証情報を含んだプロジェクトをGitで管理する

モチベーション

GORMでデータベースへアクセスする場合ユーザー名やパスワードが必要でした. こうした認証情報をgitで管理してGitHubのようなリモート・リポジトリに上げてしまうのは例え非公開でも大変危険です. かといってこれをプロジェクト外部で管理すると, デプロイ時にめんどいうになりそうです. 後間違えコミットしてしまうということも有り得ます.

用語

PGP (Pretty Good Privacy)

暗号化プログラムで, 公開暗号方式を使うようです.

GPG (GnuPG)

GNUによるオープンソースなPGPです.

pinentry

GnuPGがパスフレーズやPIN番号を読み取れるようにするプログラムのようです. なくても良さそうでは有ります.

gpg-agent

秘密鍵を管理してくれるデーモンのようです. ssh-agentと同じようなもののようです.

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.

gpg-agent(1) - Linux man page

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.

How does GPG agent work?

gpg-agent is mostly used as daemon to request and cache the password for the keychain.

gpg-agent

gpg-agent can be configured via the pinentry-program stanza to use a particular pinentry user interface when prompting the user for a passphrase.

pinentry

用語の整理

名前 用途
GPG(GnuPG) 暗号化や署名の生成を行うプログラム
Pinetnry GPGにパスフレーズやPIN番号を安全に渡すプログラム
gpg-agent GPGの代理として鍵を管理するプログラム?

GPG本体とgpg-agentの違いというか役割分担がよく分かっていませんね😅

GnuPG

導入

brewを使うと簡単にできます

brew install gnupg gnupg2 pinentry-mac gpg-agent

gpg-agentにpinentry-programを指定します.

which pinentry-mac
echo "pinentry-program /usr/local/bin/pinentry-mac" >>~/.gnupg/gpg-agent.conf
cat ~/.gnupg/gpg-agent.conf

ところがgpg-agentは存在しません. GPG 2.1以降はgpg-agent付属して配布されているようです.

gpg-agent not found for homebrew

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.

GnuPG

これでGPGで鍵を生成するときにパスフレーズが求められるとpinentry-programであるpinentry-macが起動するはずです. が, どうも2.0以降はpinentry-programの設定が変わっているようで動きませんでした. Stackoverflowに質問しておきました.

Where should I specify a pinentry program for GnuPG 2.0 and later?

とりあえずなしでいきます.

鍵の生成

必要となる情報は以下です.

  • 名前
  • e-mail
  • パスフレーズ
gpg --generate-key

で用意した情報を入力しましょう.

git-crypt

導入

こちらもbrewでインストールします.

brew install git-crypt

利用

git-crypt を使って秘密情報を版管理するを参考にしてやってみましょう.

まとめ

これで公開鍵を知らない人は中身を見れなくなりました. よってGitHubにアップロードして問題ないと思われます. VPSに公開鍵をアップロードしておけばgit crypt unlockで復号化されます.

課題

デプロイするときに面倒が増します. Gitのフックはpush時しか出来ないからです. のでGitHubはリモート・リポジトリと割り切ってデプロはAnsibleとか使った方が良いと思いました.

AnsibleでGoアプリをデプロイ

Reference

An Introduction to Managing Secrets Safely with Version Control Systems
本番用の.envを外部に一切知られずに安全にgithubで保存する方法
git-crypt を使って秘密情報を版管理する

ツイッターでシェア
みんなに共有、忘れないようにメモ

ブレイン

Androidアプリ開発者を目指しています. 興味あることリスト: https://t.co/ew3bb6grdJ Github: https://t.co/9btqysHqWr Qiita: https://t.co/ZVRhjouauX

Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。

また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!

有料記事を販売できるようになりました!

こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?

コメント