Files
staticzolaserver-docker/getOrCreateSshKeys.sh
T

68 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# getOrCreateSshKeys.sh
if [ -z ${LOCALUSER} ]
then
# user this container should work
LOCALUSER='swsuser'
fi
if [ -z ${LOCALGROUP} ]
then
# group this container should work
LOCALGROUP='swsgroup'
fi
getOrCreateSshKeys() {
#user is root
if [ $(id -u) -eq 0 ]
then
echo "enter root modus of getOrCreateSshKeys.sh"
if [ ! -d /home/${LOCALUSER}/.ssh ]
then
echo "creating .ssh directory"
mkdir /home/${LOCALUSER}/.ssh
chmod 700 .ssh
fi
ls -la /run/secrests/id_*
if [ -r /run/secrets/id_ed25519 ] && [ -r /run/secrets/id_ed25519.pub ]
then
echo "copy keys from docker secret to user directory .ssh"
cp /run/secrets/id_ed25519 /home/${LOCALUSER}/.ssh/id_ed25519
cp /run/secrets/id_ed25519.pub /home/${LOCALUSER}/.ssh/id_ed25519.pub
chmod 600 /home/${LOCALUSER}/.ssh/id_ed25519.pub
chown ${LOCALUSER}:${LOCALGROUP} /home/${LOCALUSER}/.ssh/id_ed25519
chown ${LOCALUSER}:${LOCALGROUP} /home/${LOCALUSER}/.ssh/id_ed25519.pub
ls -la /home/{$LOCALUSER}/.ssh/
fi
# user is not root
else
echo "enter user modus of getOrCreateSshKeys.sh"
if [ ! -d ~/.ssh ]
then
echo "creating .ssh directory"
mkdir ~/.ssh
chmod 700 .ssh
fi
if [ -r /run/secrets/id_ed25519 ] && [ -r /run/secrets/id_ed25519.pub ]
then
echo "copy keys from docker secret"
ls -la /run/secrets/id_ed*
cp /run/secrets/id_ed25519 ~/.ssh/id_ed25519
cp /run/secrets/id_ed25519.pub ~/.ssh/id_ed25519.pub
chown ${LOCALUSER}:${LOCALGROUP} ~/.ssh/id_ed25519
chown ${LOCALUSER}:${LOCALGROUP} ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/id_ed25519.pub
ls -la ~/.ssh/
else
echo "generating new keys, because docker secret keys missing"
ssh-keygen -q -t ed25519 -C "swsuser@serinty.net" -f ~/.ssh/id_ed25519
echo "use this public key for git repo auth:"
cat /home/swsuser/.ssh/id_ed25519.pub
fi
fi
}