The Will Will Web

記載著 Will 在網路世界的學習心得與技術分享

介紹好用工具:Git Credential Manager for Windows (記憶 Git 常用密碼)

我在兩年前曾經分享過【透過 HTTP 與 HTTPS 連接 Git 儲存庫時如何記憶常用密碼】文章,當時該篇文章所提到的 Windows Credential Store for Git 專案已經不再維護了,取而代之的則是由微軟官方支援Git Credential Manager for Windows 版本。本篇文章主要用來介紹這個工具的使用方式與注意事項。

Git Credential Manager for Windows 簡介

這套由微軟官方支援Git Credential Manager for Windows 版本,相較於 Git for Windows 內建的 Credential Storage 功能,這套工具除了增加支援任何 HTTP/HTTPS 的 單因素驗證 (single-factor authentication) 之外,還更加支援 多因素驗證 (multi-factor authentication) 的 Visual Studio Team Services 與 GitHub 等平台。其功能特色如下:

  • 支援更安全的密碼保存機制,將密碼儲存在 Windows 作業系統內建的 Windows Credential Store 儲存區中 (認證管理員)。
    ※ 你可以參考下圖開啟認證管理員:
  • 支援 Visual Studio Team Services 的 多因素驗證 (Multi-factor authentication)
  • 支援 GitHub 的 雙因素驗證 (二階段驗證) (相關說明: Two-factor authentication (2FA) )
  • 支援 Visual Studio Team Services 與 GitHub 提供的 個人存取金鑰 (Personal Access Token) 的建立與使用
  • 支援基於 Azure AD 下的 Visual Studio Team Services 非互動模式 (Non-interactive mode)
  • 提供額外的選項可設定建置代理程式 (Optional settings for build agent optimization) (VSTS)

※ 關於 Git Credential Manager for Windows 的運作原理,請參見 How the Git Credential Managers works 文章說明。

 

安裝 Git Credential Manager for Windows

最新版的下載網址為:
https://github.com/Microsoft/Git-Credential-Manager-for-Windows/releases/latest

直接下載 Setup.exe 進行安裝即可:

 

安裝完成後,建議開啟命令提示字元視窗,並執行以下命令,查詢 Git for Windows 的認證管理員是否有被正確設定:

  • git config --global credential.helper

正確的回應值應該為 manager 才對喔!像我公司的電腦,在安裝完 Git Credential Manager for Windows 之後,這個 credential.helper 選項並沒有正確被設定,導致密碼都無法被正常儲存。

如果你沒有正確設定好的話也沒關係,直接輸入以下指令就可以設定成功:

  • git config --global credential.helper manager

 

使用 Git Credential Manager for Windows

正常來說,只要把 Git Credential Manager for Windows 裝好,甚麼都不用設定就會自動生效!

無論你用 TortoiseGit 或命令提示字元下的 Git.exe 命令列工具,只要第一試圖跟遠端儲存庫連線且需要密碼時,他都會自動跳出帳戶密碼提示,而當你輸入完帳號密碼後,該組帳號密碼就會被儲存到系統的 Windows Credential Store 儲存區中 (認證管理員)。

以下我們以 GitHub 為例,先 clone 儲存庫,然後執行 git push 命令,此時會跳出讓你輸入帳號、密碼登入:

登入成功後,若你從認證管理員查詢剛剛的認證紀錄,你會發現有一筆 git:https://github.com 的認證被記憶下來了,但有趣的地方是登入的使用者名稱並不是我輸入時打的帳號,而是一個奇特的 Personal Access Token 帳號!

如果你這時跑去 GitHub 查看 Personal access tokens 頁面 ( https://github.com/settings/tokens ),就會發現這裡被自動新增了一筆 Personal access tokens 金鑰,這是你剛剛在輸入認證時 Git Credential Manager for Windows 幫你呼叫 GitHub API 自動建立的!

這個意思也就是說,你剛剛儲存在 Windows Credential Store 儲存區的認證資訊,並不是儲存你真正在 GitHub 的帳號密碼,而是更安全的 個人存取金鑰 (Personal access tokens) ,你隨時都可以從線上刪除此金鑰,因此安全性大增!

 

設定建置代理程式 (Build agents)

當你使用 Visual Studio Team Services 的建置代理程式自動化建置專案時,可能會遇到無法進行身分認證的問題,這個問題主要是因為建置代理程式通常無法進行任何互動式的操作,所以任何需要互動的認證過程 (例如開啟 Microsoft Account 的登入視窗),都會無法進行而導致建置過程卡住!這個問題當然也會發生在使用 Jenkins CI 或其他連續性整合工具在自動化建置的時候,因此,你必須執行以下命令,用以關閉任何互動式的操作:

  • git config --global credential.interactive never

如果要關閉每次登入 Visual Studio Team Services 時都會自動跳出 Microsoft Account 與 Azure AD 的選擇視窗,你可以執行以下命令,指名只要用 Azure AD 進行驗證:

  • git config --global credential.authority Azure

如果要關閉不必要的服務帳戶驗證 (service account credential validation),可以輸入以下命令進行設定:

  • git config --global credential.validate false

值得一提的是,上述三個選項設定 (credential.interactive, credential.authority, credential.validate) 都不是 Git for Windows 內建的,而是 Git Credential Manager for Windows 擴充出來的設定。

 

相關連結

留言評論