Implement auto-loading of SSH keys

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2017-08-08 11:56:36 +02:00
parent af54670666
commit 20e7c554fc
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
3 changed files with 57 additions and 1 deletions

50
bin/git-loadkey Executable file
View file

@ -0,0 +1,50 @@
#!/usr/bin/env python
# Usage: git loadkey
#
# Configuration format:
# [
# {
# "match": ".*",
# "keyname": "...",
# }
# ]
import sys
import re
import subprocess
import os.path
import json
CONFIG = os.path.expanduser('~/.config/git-setmail.json')
def main():
try:
repos = [x.split()[1] for x in subprocess.check_output(['git', 'remote', '-v']).split('\n') if len(x.strip()) > 0]
except:
print 'Could not find origin, not loading key.'
return 0
if os.path.isfile(CONFIG):
combinations = json.loads(open(CONFIG).read())
else:
print 'Config ({}) found.'.format(CONFIG)
return 1
required_keys = []
for repo in sorted(set(repos)):
for combi in combinations:
if re.search(combi['match'], repo):
required_keys.append(combi['keyname'])
break
for key in sorted(set(required_keys)):
print 'Loading key "{}"...'.format(key)
subprocess.check_call(['vault-sshadd', key])
return 0
if __name__ == "__main__":
exit(main())

View file

@ -12,10 +12,13 @@ function step {
### Determine what to push
step "Collect refs to push to origin"
step "Getting current branch"
CURRENT_BRANCH=$(git branch --list | awk '/^\*/{print $2}')
step "Loading required key to push"
git loadkey
step "Execute pre-push auto-hook"
git autohook pre-push

View file

@ -10,6 +10,9 @@ function step {
echo -e ${STEP_COLOR}[$(date +%H:%M:%S)] $1${NO_COLOR}
}
step "Loading required keys to pull"
git loadkey
step "Fetching data from remote..."
for remote in $(git remote -v | awk '{print $1}' | sort | uniq); do
step "+++ Remote: '${remote}'"