Skip to content

Instantly share code, notes, and snippets.

@centum
Created June 20, 2020 19:45
Show Gist options
  • Save centum/caf4656ceda5ae44ea758fbba8ec4bb0 to your computer and use it in GitHub Desktop.
Save centum/caf4656ceda5ae44ea758fbba8ec4bb0 to your computer and use it in GitHub Desktop.
Multiple repositories github deployment ssh keys
# Github deploy keys
export GIT_SSH_COMMAND=~/.ssh/git-keys/repo-ssh-custom-key.sh
export GIT_SSH_COMMAND_DEBUGLOG=~/.ssh/git-keys/repo-ssh-custom-key.log
#!/bin/bash
# Based on https://gist.github.com/vhermecz/4e2ae9468f2ff7532bf3f8155ac95c74
# Script to use custom ssh keys for various git repositories
# Run without arguments to get usage info.
#
# How it works:
# When used with SSH, git sends the path to the repository in the SSH command.
# @see: https://github.com/git/git/blob/e870325/connect.c#L1268
# We extract this info and search for a key with the name.
# Based on the source, this seems to be used format since v2.0 at least.
# @see: https://github.com/git/git/commit/a2036d7
# ssh-keygen -t ed25519 -f ~/.ssh/git-keys/jettonbox-jb-land-templates-ua
if [[ $# -eq 0 ]]; then
echo "Usage"
echo "Set script as GIT_SSH_COMMAND"
echo "Add SSH keys for git repositories under ~/.ssh/git-keys/ folder."
echo "File name format:"
echo " For the repository git@github.com:github/practice.git"
echo " Put the private key into the file github-practice"
echo " (Note: slash converted to dash in path, no extension)"
echo ""
echo "Uses ssh by default, use GIT_SSH_COMMAND_REALSSH envvar to override."
echo "For debugging set log output in envvar GIT_SSH_COMMAND_DEBUGLOG."
exit 1
fi
function debuglog() {
[ ! -z "$GIT_SSH_COMMAND_DEBUGLOG" ] && (echo `date +%FT%T` "$@") >> $GIT_SSH_COMMAND_DEBUGLOG
return 0
}
for CMD_BUF in "$@"; do :; done
debuglog "Value of cmd.buf is: '$CMD_BUF'"
# @source: https://superuser.com/a/1142939/277157
declare -a "array=($( echo "$CMD_BUF" | sed 's/[][`~!@#$%^&*():;<>.,?/\|{}=+-]/\\&/g' ))"
for CMD_PATH in "${array[@]}"; do :; done
CMD_PATH=$(echo "$CMD_PATH" | sed 's/\\//g')
IDENTITY=
if [[ $CMD_PATH == *.git ]] ;
then
REPOKEY=$(echo "$CMD_PATH" | sed 's/\.git//g' | sed 's/\//-/g')
KEYFILE=$(echo ~/.ssh/git-keys/$REPOKEY)
if [[ -f "$KEYFILE" ]]
then
debuglog "Key '$KEYFILE' exists"
IDENTITY=$(echo "-i $KEYFILE")
else
debuglog "Key '$KEYFILE' is missing"
fi
else
debuglog "No repo name detected. Skipping"
fi
SSH=${GIT_SSH_COMMAND_REALSSH:-ssh}
set -- $SSH $IDENTITY "$@"
debuglog "Calling with '$@'"
"$@"
#!/bin/bash
if [[ $# -eq 0 ]]; then
echo "Usage"
echo "ssh-keygen git@github.com:github/practice.git"
exit 1
fi
CMD_PATH=$(echo "$1" | sed 's/\\//g')
if [[ $CMD_PATH == *.git ]] ;
then
REPOKEY=$(echo "$CMD_PATH" | cut -d: -f2 | sed 's/\.git//g' | sed 's/\//-/g')
KEYFILE=$(echo ~/.ssh/git-keys/$REPOKEY)
if [[ -f "$KEYFILE" ]]
then
echo "Key '$KEYFILE' already exists"
exit 3
fi
else
echo "No repo name detected. Skipping"
exit 2
fi
set -- ssh-keygen -t ed25519 -f $KEYFILE
echo "Calling with '$@'"
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment