Last active
October 2, 2019 17:56
-
-
Save chris-moreton/559dd1c10d3de13f645e86458e9fa294 to your computer and use it in GitHub Desktop.
Backup CredHub Credentials to a Text File
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Outputs the commands needed to recreate the credentials. | |
# Works with value, user and ssh types. | |
# Can be easily updated to work with other types. | |
mapfile -t CREDS < <(credhub find -n c | grep "name:") | |
for CRED in "${CREDS[@]}" | |
do | |
CRED=${CRED/- name: /} | |
OUTPUT="$(credhub get -n ${CRED})" | |
if [[ $OUTPUT == *"type: ssh"* ]]; then | |
PUBLIC="$(credhub get -q -n ${CRED} -k public_key)" | |
PRIVATE="$(credhub get -q -n ${CRED} -k private_key)" | |
echo "credhub set -n ${CRED} --type ssh --public \"${PUBLIC}\" --private \"${PRIVATE}\"" | |
elif [[ $OUTPUT == *"type: user"* ]]; then | |
USERNAME="$(credhub get -q -n ${CRED} -k username)" | |
PASSWORD="$(credhub get -q -n ${CRED} -k password)" | |
echo "credhub set -n ${CRED} --type user --username \"${USERNAME}\" --password \"${PASSWORD}\"" | |
elif [[ $OUTPUT == *"type: value"* ]]; then | |
VALUE="$(credhub get -q -n ${CRED})" | |
echo "credhub set -n ${CRED} --type value --value \"${VALUE}\"" | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment