Try   HackMD

Git-Secret 紀錄

在 Linux 安裝 git secret

  • 安裝指令:
    • apt-get update
    • apt-get install git-secret
  • 驗證:
    • git secret --version
  • 如果遇到以下錯誤
root@3c7e31ff011f:/# apt-get install git-secret Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package git-secret

可使用deb package做法:
You can find the deb repository here. Pre-requirements: make sure you have installed apt-transport-https

  1. echo "deb https://dl.bintray.com/sobolevn/deb git-secret main" | tee -a /etc/apt/sources.list
  2. wget -qO - https://api.bintray.com/users/sobolevn/keys/gpg/public.key | apt-key add -
  3. apt-get update && apt-get install git-secret

參考: https://git-secret.io/installation

在 macOS 安裝 git secret

  • 安裝指令:
    • brew install git-secret
  • 驗證方式同上

在 Windows 安裝 git secret

  • 待補

介紹 git secret

  • 將敏感檔案(ex: .env) 經由加密後,推送到 git

加密&解密的方式

  • 由工具 GPG 產生一組公、私鑰
  • 公鑰用於加密,而私鑰作為唯一解密的鑰匙
    • 衍生解釋: 公鑰被盜取沒關係,但私鑰被盜取就完了

git secret 使用情境

假設要開發一個新的專案

Step1: 初始化

  • git init
  • git secret init
    • 輸入 ls -al
      • Image Not Showing Possible Reasons
        • The image file may be corrupted
        • The server hosting the image is unavailable
        • The image path is incorrect
        • The image format is not supported
        Learn More →
    • 發現有一個 .gitsecret 隱藏的檔案,代表成功init

Step2: 產生金鑰(公&私)

  • gpg --gen-key
    • 會要求輸入:
      • Name
      • Email
      • Image Not Showing Possible Reasons
        • The image file may be corrupted
        • The server hosting the image is unavailable
        • The image path is incorrect
        • The image format is not supported
        Learn More →
    • 接著會要求輸入短密碼(Passphrase)
      • Image Not Showing Possible Reasons
        • The image file may be corrupted
        • The server hosting the image is unavailable
        • The image path is incorrect
        • The image format is not supported
        Learn More →
    • 驗證:
      • 輸入:
        • gpg -k:查看公鑰名單

          • Image Not Showing Possible Reasons
            • The image file may be corrupted
            • The server hosting the image is unavailable
            • The image path is incorrect
            • The image format is not supported
            Learn More →
        • gpg -K:查看私鑰名單

          • Image Not Showing Possible Reasons
            • The image file may be corrupted
            • The server hosting the image is unavailable
            • The image path is incorrect
            • The image format is not supported
            Learn More →

Step3: 匯出OR匯入公鑰

  • 匯出公鑰:
    • gpg -a --export ${用戶的 id} > ${輸出的文件名}
      • ex: gpg -a --export DemoUser > Demo_Public.key
    • 此時,當前位置會有 Demo_Public.key 檔案
  • 匯出私鑰:
    • gpg -a --export ${用戶的 id} > ${輸出的文件名}
      • ex: gpg -a --export-secret-key DemoUser > Demo_private.key
    • 此時,當前位置會有 Demo_Public.key 檔案
  • 匯入公/私鑰:
    • gpg --import ${公鑰檔案名稱}
      • ex: gpg --import Demo_Public.key
    • 此時,輸入gpg -k 查看公鑰列表,會有 Demo_Public.key 公鑰的 主人名稱 & Email
  • 補充:
    • 匯出公私鑰是為了備份用,以防當前環境掛掉。
    • 記得要把匯出後的鑰匙檔,放在安全的位置。

Step4: 加入擁有解密權限人員

  • git secret tell ${UserName} / ${UserEmail}

    • ex: git secret tell DemoUser
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
  • 驗證:

    • git secret whoknows
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
  • 說明:

    • 加入 DemoUser 後,當輸入加密指令,會以 DemoUser 的公鑰加密。

Step5: 加入要加密的檔案名單

  • 先新增一個 .env 檔案
    • touch .env
  • 將.env 加入即將加密的名單
    • git secret add ${檔案名稱}
      • ex: git secret add .env
    • 報錯:
      Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →
      • 還未ignore .env 所以不能執行
    • 解法: 將 .env 加入於 .gitignore 裡
      • Image Not Showing Possible Reasons
        • The image file may be corrupted
        • The server hosting the image is unavailable
        • The image path is incorrect
        • The image format is not supported
        Learn More →

Step6: 將加密檔案名單,進行加密

  • git secret hide
    • Image Not Showing Possible Reasons
      • The image file may be corrupted
      • The server hosting the image is unavailable
      • The image path is incorrect
      • The image format is not supported
      Learn More →

Step7: 解密

  • git secret reveal
    • 第一次解密會要求輸入短密碼(passphrase)
    • 輸入正確便會解密

git secret 常用指令

  • git secret init
  • git secret add ${file_name}
  • git secret hide
  • git secret reveal
  • git secret whoknows
  • git secret killperson <emails>

gpg 常用指令

  • 生成 gpg 金鑰

    • gpg --gen-key
  • 看本機金鑰(私鑰)的列表

    • gpg --list-secret-keys
  • 看本機金鑰(公鑰)的列表

    • gpg --list-keys
  • 刪除金鑰(私鑰)

    • gpg --delete-secret-keys 《KeyID》
  • 刪除金鑰(公鑰)

    • gpg --delete-keys 《KeyID》
  • 導出私鑰

    • gpg -a --export-secret-keys <用戶的 id> > < 輸出的文件名 >
    • 輸出私鑰,需要當時創建的密碼
  • 導出公鑰

    • gpg -a --export < 用戶的 id> > < 輸出的文件名 >
  • 導入金鑰

    • gpg --import <file>
  • gpg 的 GUI 軟體

    • GPG Keychain