Skip to content

Instantly share code, notes, and snippets.

@ruslanguns
Forked from bepcyc/add_secret_to_bitwarden.sh
Created January 15, 2023 20:34
Show Gist options
  • Save ruslanguns/0c60fbcca5dc9b6ff4e0bb7d218eb99b to your computer and use it in GitHub Desktop.
Save ruslanguns/0c60fbcca5dc9b6ff4e0bb7d218eb99b to your computer and use it in GitHub Desktop.
How to add secret content (e.g. a private key) to a bitwarden and how to restore it back. Chezmoi template included.
SECRET_NAME=id_rsa
SECRET_PATH=~/.ssh/id_rsa
# store the secret content as an item in bitwarden
echo "{\"organizationId\":null,\"folderId\":null,\"type\":2,\"name\":\"${SECRET_NAME}\",\"notes\":\"$(base64 -w 0 ${SECRET_PATH})\",\"favorite\":false,\"fields\":[],\"login\":null,\"secureNote\":{\"type\":0},\"card\":null,\"identity\":null}" | bw encode | bw create item
bw sync # optional
# retrieve the secret
# assuming a single search result
bw list items --search id_rsa | jq -r '.[0].notes' | base64 -d > ${SECRET_PATH}
# in case you're using chezmoi here's a template that will retrieve that secret automatically
#$cat $(chezmoi source-path ${SECRET_PATH})
#{{ (bitwarden "item" "id_rsa").notes | b64dec }}
chezmoi execute-template "`cat $(chezmoi source-path ${SECRET_PATH})`"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment