使用 blackbox 加密 git 裡的檔案 — Part1

Luyo
Luyo
Jul 21, 2017 · 5 min read

Github 這類的雲端服務雖然很方便,但畢竟是雲端,把帳號密碼或是 API 的 key pair 放上去仍然是有一定的風險,所以針對 repository 內特定的檔案做加密有時候是必要的。

調查了一下,找到了兩個看起來合用的套件:git-secretblackbox,實際使用後發現 git-secret 目前的版本 v0.2.2 在 CentOS 7.3 及 Mac OS X El Capitan (10.11.3) 上執行 git secret reveal 指令都沒有反應,似乎是有 bug,而 blackbox 用起來則沒有什麼問題,所以就選用了 blackbox。

安裝 Blackbox

以下假設 username 叫做 “xxx”,所以如果指令裡面有遇到 xxx 的都請自動換成你真實的 username。

CentOS 用的套件管理工具為 rpm,依官方 github 說明:

The RPM way: Check out the repo and make an RPM via make packages-rpm; now you can distribute the RPM via local methods.

先把 repository clone 下來

$ cd
$ git clone https://github.com/StackExchange/blackbox.git
$ cd blackbox

但接下來執行 make packages-rpm 卻會跳出 "fpm:command not found" 的錯誤訊息。fpm 是個未知的套件,要怎麼安裝?回 blackbox 的頁面往下看到:

The Debian/Ubuntu way: Check out the repo and install fpm. Now you can make a DEB make packages-deb that can be distributed via local methods.

點擊 fpm 連結,找到 fpm 套件的安裝方法

$ sudo yum -y install ruby-devel gcc make rpm-build rubygems
(略)
$ sudo gem install --no-ri --no-rdoc fpm
(略)
$ fpm --version
1.8.1

安裝好 fpm 之後回頭安裝 blackbox:

$ make packages-rpm
(略)
/home/xxx/rpmbuild-stack_blackbox/stack_blackbox-1.0-404.noarch.rpm

這次就沒有錯誤訊息了,而最後一行就是這個 rpm 套件被生成後的路徑,故執行:

$ sudo rpm -ivh /home/xxx/rpmbuild-stack_blackbox/stack_blackbox-1.0-404.noarch.rpm
Preparing... ################################# [100%]
Updating / installing...
1:stack_blackbox-0:1.0-404 ################################# [100%]

這樣就安裝完成了。

等等,怎麼還是沒有 blackbox_ 開頭的等指令集可以使用?研究一下 Makefile 可以發現,系統產生了一個 /usr/blackbox/的資料夾以及 /etc/profile.d/usrblackbox.sh 檔案,後者會將 /usr/blackbox/bin 加入 $PATH,也就是每個 user 都預設載入 /usr/blackbox/bin/ 底下的指令。只是安裝的 script 並沒有幫我們自動重新載入現在這個 shell session 的 $PATH,所以只要重新登入或是執行 source ~/.bashrc 就可以了。

產生 GPG Key

因為 balckbox 是基於 GPG 加密方式來運作的,所以每個 user 欲使用前都必須先以自己的 email 當做帳號去產生 GPG Key。

$ gpg --gen-key

下指令後會進入互動介面,前幾個問題都直接按 enter 用預設值即可;接下來填入個人姓名、Email 及暱稱,即可產生 gpg key。生成的設定及相關檔案會在 ~/.gnupg 底下,沒事不要去動它。

在 repository 中初始化 blackbox

以下假設 username 叫做 “xxx”,用來產生 gpg key 的 email 叫做 “xxx@ooo.com”。

進入到 repository 目錄之中,執行以下指令

$ blackbox_initialize
Enable blackbox for this git repo? (yes/no)

輸入 yes,會顯示

VCS_TYPE: git
grep: keyrings/live/.gitattributes: No such file or directory
NEXT STEP: You need to manually check these in:
git commit -m'INITIALIZE BLACKBOX' keyrings /home/xxx/blackbox/.gitignore

這時候 blackbox 會幫你生成 keyrings/ 及 .gitignore,接著就照他的指示下指令:

$ git commit -m'INITIALIZE BLACKBOX' keyrings /home/xxx/blackbox/.gitignore

這樣就完成 blackbox 在這個 repository 中的初始化了。


到這邊我們才總算完成了基礎建設,下一篇文章將記錄如何設定 blackbox 的管理者以及如何加解密檔案。

verybuy-dev

)

Luyo

Written by

Luyo

Founder, Developer of VeryBuy — https://www.verybuy.cc

verybuy-dev

VeryBuy 研發手札

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade