Files
2023-06-18 12:31:48 +03:00

62 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# getOrCreateSshKeys.sh
# Set default values for variables
IDLE_TIME=${IDLE_TIME:-86400}
LOCALUSER=${LOCALUSER:-swsuser}
LOCALGROUP=${LOCALGROUP:-swsgroup}
export GIT_SSH_COMMAND="ssh -i $HOME/.ssh/id_ed25519"
getOrCreateSshKeys() {
#user is root
if [ "$USER" = "root" ]
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
chmod 666 ~/.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
}