forked from simon0191/custom-ssh-key-buildpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile
executable file
·44 lines (33 loc) · 1.12 KB
/
compile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env bash
# bin/compile <build-dir> <cache-dir> <env-dir>
# Generates an SSH config file for connections if a config var exists.
ENV_DIR=$3
if [[ -f $ENV_DIR/CUSTOM_SSH_KEY && -f $ENV_DIR/CUSTOM_SSH_KEY_HOSTS ]]; then
echo "" >&1
# Ensure we have an ssh folder
if [ ! -d ~/.ssh ]; then
mkdir -p ~/.ssh
chmod 700 ~/.ssh
fi
# Load the private key into custom_key file.
base64 --decode $ENV_DIR/CUSTOM_SSH_KEY > ~/.ssh/custom_key
# Change the permissions on the file to
# be read-only for this user.
chmod 400 ~/.ssh/custom_key
# Split $CUSTOM_SSH_KEY_HOSTS
IFS=',' ;for element in `cat $ENV_DIR/CUSTOM_SSH_KEY_HOSTS`;
do
echo -e "Host $element\n"\
" IdentityFile ~/.ssh/custom_key\n"\
" IdentitiesOnly yes\n"\
" UserKnownHostsFile=/dev/null\n"\
" StrictHostKeyChecking no"\
>> ~/.ssh/config
ssh-keyscan -H -t rsa $element >> ~/.ssh/known_hosts
ssh-keyscan -H -t rsa $element
done
# Add the key to ssh agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/custom_key
echo "-----> Successfully added custom SSH key"
fi