forked from dockito/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for git https credentials
This implements dockito#21. It is a simple way to provide git credentials not only for ssh via keys, but also for https. In corporate settings with internet access through proxies, ssh is often not allowed and one is forced to use https. There are two ways to use this, either you provide a credential store and mount it when running `vault`. ``` docker run -p 172.17.0.1:14242:3000 -v $PWD/store:/vault/store vault ``` Alternatively you can also start the vault with an empty store and interactively add / remove credentials. ``` docker run -p 172.17.0.1:14242:3000 --name vault vault # the following prompts you for a username and password which are stored in the # running container docker exec -it vault credentials set github.com ``` The usage inside a `Dockerfile` is the same as for the ssh key. Simply prepend your `git clone` or other commands with `ONVAULT`. The changes are really fairly simple: - One route is added in `index.js` to download the file with the credentials. - ONVAULT is extended to download the additional file and configure git, as well as revert the changes after executing the passed command. - The `credential` helper script is added to the container. This is actually not essential, but more of a convenience such that you don't need to create the credential files manually. I did not yet update the README, but I can do that before merge if you are willing to include this addition. closes dockito#21
- Loading branch information
1 parent
743cf01
commit 8b7587a
Showing
6 changed files
with
181 additions
and
4 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
!index.js | ||
!ONVAULT | ||
!package.json | ||
!credentials |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#!/bin/sh | ||
|
||
# allow overriding location of credential store | ||
: ${CREDENTIALFILE:=/vault/store/git-credentials} | ||
|
||
usage () { | ||
echo "Usage: | ||
$(basename $0) set URL | ||
$(basename $0) remove URL | ||
$(basename $0) list | ||
$(basename $0) clear | ||
Examples: | ||
$(basename $0) set github.com | ||
$(basename $0) set gitlab.com/foo/bar | ||
$(basename $0) remove github.com | ||
" | ||
} | ||
|
||
# parse arguments | ||
while [ $# -ge 1 ]; do | ||
case "$1" in | ||
set|remove) | ||
if [ $# -eq 2 ]; then | ||
CMD="credentials_$1 $2" | ||
shift | ||
fi | ||
break | ||
;; | ||
list|clear) | ||
CMD="credentials_$1" | ||
break | ||
;; | ||
-h|--help) | ||
usage | ||
exit 0 | ||
;; | ||
*) | ||
usage | ||
exit 1 | ||
;; | ||
esac | ||
|
||
shift | ||
done | ||
|
||
if [ $# -ne 1 ] || [ -z "$CMD" ]; then | ||
usage | ||
exit 1 | ||
fi | ||
|
||
# helper to url-encode username and password using javascript | ||
# proper escaping for username & password, see: https://gist.github.com/pierrevalade/6025241 | ||
encodeurl () { | ||
node <<EOF | ||
var escape = function(str) { | ||
return encodeURIComponent(str).replace(/[!*()']/g, function(character) { | ||
return '%' + character.charCodeAt(0).toString(16); | ||
}); | ||
}; | ||
console.log(escape('$1')) | ||
EOF | ||
} | ||
|
||
mask_passwords () { | ||
# replace passwords with ****** and discard non-matching lines | ||
sed -n 's|^\(.*://.*:\)\(.*\)\(@.*\)$|\1******\3|p' | ||
} | ||
|
||
credentials_remove () { | ||
# remove leading https:// | ||
URL="$( echo "$1" | sed -e 's#^.*://##' )" | ||
|
||
# remove existing entry | ||
matching=`grep "@$URL\$" "$CREDENTIALFILE" 2>/dev/null` | ||
if [ -n "$matching" ]; then | ||
tempfile=`mktemp -t git-credentials-XXXXXX` | ||
grep -v "@$URL\$" "$CREDENTIALFILE" > "$tempfile" | ||
mv "$tempfile" "$CREDENTIALFILE" | ||
else | ||
echo "No credentials for '$URL'" | ||
exit 1 | ||
fi | ||
} | ||
|
||
credentials_set () { | ||
# remove leading https:// | ||
URL="$( echo "$1" | sed -e 's#^.*://##' )" | ||
|
||
# remove existing entry | ||
matching=`grep "@$URL\$" "$CREDENTIALFILE" 2>/dev/null` | ||
if [ -n "$matching" ]; then | ||
echo "Removing existing entries:" | ||
echo "$matching" | mask_passwords | ||
read -p "Continue? [yN]: " CONFIRM | ||
if ! [ "$CONFIRM" = "y" ]; then | ||
exit 1 | ||
fi | ||
credentials_remove $URL | ||
echo "" | ||
fi | ||
|
||
echo "Adding credentials for 'https://$URL'." | ||
read -p "Username: " USER | ||
read -p "Password: " -s PASSWORD | ||
echo "" | ||
|
||
USER=`encodeurl "$USER"` | ||
PASSWORD=`encodeurl "$PASSWORD"` | ||
|
||
echo "https://$USER:$PASSWORD@$URL" >> "$CREDENTIALFILE" | ||
chmod 600 "$CREDENTIALFILE" | ||
} | ||
|
||
credentials_list () { | ||
if [ -f "$CREDENTIALFILE" ]; then | ||
# mask passwords and sort by URL (which starts after the "@") | ||
cat "$CREDENTIALFILE" | mask_passwords | sort -k 2 -t "@" | ||
fi | ||
} | ||
|
||
credentials_clear () { | ||
echo "" > "$CREDENTIALFILE" | ||
} | ||
|
||
|
||
$CMD |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
"dependencies": { | ||
"express": "^4.12.3", | ||
"mime": "^1.3.4", | ||
"touch": "^1.0.0", | ||
"mkdirp": "^0.5.1" | ||
} | ||
} |