A Git credential helper that retrieves credentials from Bitwarden Secrets Manager using the bws CLI.
This helper allows you to store your Git credentials (like personal access tokens or username/password) securely in Bitwarden Secrets Manager and have Git automatically fetch them when needed.
- Git invokes the helper script when credentials are required.
- The script receives the
getcommand from Git. - It uses
bws secret get <secret_id>to fetch the secret data from Bitwarden Secrets Manager. - It uses
jqto parse the fetched JSON and extract the nested JSON string using the provided username (optional) and password key arguments as the JSON key paths. - It outputs the credentials to Git in the required format (
username=...andpassword=...).
Before using this credential helper, ensure you have the following installed and configured:
bws: Bitwarden Secrets Manager CLI.- Ensure
bwsis configured to access your secrets. You likely need to set theBWS_ACCESS_TOKENenvironment variable.
- Ensure
jq: A command-line JSON processor.
Executing the following command will download the script and make it executable:
curl -s https://raw.githubusercontent.com/tdharris/git-credential-bws/main/installer.sh | bash -s-
Download the
git-credential-bwsscript and place it in a directory in yourPATH(e.g.,/usr/local/bin). -
Make the script executable:
chmod +x /path/to/git-credential-bws
To use the helper, you need to configure Git to use it as a credential helper. You can do this globally or for specific domains or even repositories. The configuration is done in your Git configuration file (~/.gitconfig).
-
Prepare your Bitwarden Secret:
- Create a secret in Bitwarden Secrets Manager.
- The value of this secret must be a JSON string containing key-value pairs for your credentials.
- Example Secret Value:
{ "GIT_USER": "my-gitlab-username", // Optional "GITLAB_TOKEN": "glpat-yyyyyyyyyyyyyyyyyyyyyyyyyyyy" } - Note the Secret ID (e.g.,
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
-
Configure Git:
-
Use
git configto tell Git to usegit-credential-bws. You must provide the Secret ID (-s) and the key within the secret's JSON value that holds the password/token (-p). Optionally, provide the key for the username (-u). -
The command format is:
!git-credential-bws -s <secret_id> -p <password_key> [-u <username_key>]
- The
!tells Git to treat the command as a shell command. - Ensure
git-credential-bwsis executable and in yourPATH, or use the full path to the script.
- The
-
Examples (using the secret above):
-
Configure for GitHub (using PAT only):
# Uses the secret ID and the 'GITHUB_OAUTH_TOKEN' key from the JSON git config --global credential.https://github.com.helper \ '!git-credential-bws -s YOUR_SECRET_ID -p GITHUB_OAUTH_TOKEN'
-
Configure for GitLab (using PAT only):
# Uses the secret ID and the 'GITLAB_TOKEN' key from the JSON git config --global credential.https://gitlab.com.helper \ '!git-credential-bws -s YOUR_SECRET_ID -p GITLAB_TOKEN'
-
Configure for a service requiring username and password:
# Uses the secret ID, 'GIT_USER' key for username, 'GITHUB_OAUTH_TOKEN' key for password git config --global credential.https://mygitserver.com.helper \ '!git-credential-bws -s YOUR_SECRET_ID -u GIT_USER -p GITHUB_OAUTH_TOKEN'
-
Global fallback (less common, use per-host if possible):
# Uses the secret ID and 'GITHUB_OAUTH_TOKEN' key globally (use with caution) git config --global credential.helper \ '!git-credential-bws -s YOUR_SECRET_ID -p GITHUB_OAUTH_TOKEN'
-
-
Replace
YOUR_SECRET_IDwith your actual Bitwarden Secret ID.
-
If you encounter any issues, you can simulate how Git interacts with the credential helper by running the command manually:
echo "host=domain.com"
echo "protocol=https"
echo "" | git-credential-bws -s <secret_id> -p <JSON_ATTRIBUTE_FOR_PASSWORD> getFor example, to test the GitHub configuration:
echo "host=github.com"
echo "protocol=https"
echo "" | git-credential-bws -s <secret_id> -p GITHUB_OAUTH_TOKEN getNote: Replace <secret_id> with the actual ID of your Bitwarden secret.
{ "GIT_USER": "my-github-username", // Optional "GITHUB_OAUTH_TOKEN": "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxx" }