automate/010_generate-ssh.sh

108 lines
2.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# FIXME: Rather just creating these files in tmp move the actions into the loop blow.
LOCAL_USER_EMAIL=cyteen@ring-zero.co.uk
cat > /tmp/generate-ssh-key.sh << EOF
ssh-keygen -t rsa -b 4096 -N '' -C "${LOCAL_USER_EMAIL}" -f ${HOME}/.ssh/id_rsa
ssh-keygen -t rsa -b 4096 -N '' -C "${LOCAL_USER_EMAIL}" -f ${HOME}/.ssh/github_rsa
ssh-keygen -t rsa -b 4096 -N '' -C "${LOCAL_USER_EMAIL}" -f ${HOME}/.ssh/devuan_rsa
ssh-keygen -t rsa -b 4096 -N '' -C "${LOCAL_USER_EMAIL}" -f ${HOME}/.ssh/debian_rsa
ssh-keygen -t rsa -b 4096 -N '' -C "${LOCAL_USER_EMAIL}" -f ${HOME}/.ssh/gitlab_rsa
EOF
cat > /tmp/ssh-key-add.sh << EOF
eval "$(ssh-agent -s)"
ssh-add ${HOME}/.ssh/id_rsa
ssh-add ${HOME}/.ssh/github_rsa
ssh-add ${HOME}/.ssh/devuan_rsa
ssh-add ${HOME}/.ssh/debian_rsa
ssh-add ${HOME}/.ssh/gitlab_rsa
EOF
cat > /tmp/ssh-key-permissions.sh << EOF
chmod 700 ${HOME}/.ssh
chmod 644 ${HOME}/.ssh/authorized_keys
chmod 644 ${HOME}/.ssh/known_hosts
chmod 644 ${HOME}/.ssh/config
chmod 600 ${HOME}/.ssh/id_rsa
chmod 644 ${HOME}/.ssh/id_rsa.pub
chmod 600 ${HOME}/.ssh/gitlab_rsa
chmod 644 ${HOME}/.ssh/gitlab_rsa.pub
chmod 600 ${HOME}/.ssh/github_rsa
chmod 644 ${HOME}/.ssh/github_rsa.pub
chmod 600 ${HOME}/.ssh/devuan_rsa
chmod 644 ${HOME}/.ssh/devuan_rsa.pub
chmod 600 ${HOME}/.ssh/debian_rsa
chmod 644 ${HOME}/.ssh/debian_rsa.pub
EOF
## Set preferred key for each
# local gitlab
cat <<EOF | sudo tee -a ${HOME}/.ssh/config >/dev/null
Host git.ring-zero.co.uk
User git
Preferredauthentications publickey
IdentityFile ${HOME}/.ssh/gitlab_rsa
EOF
# gitlab
cat <<EOF | sudo tee -a ${HOME}/.ssh/config >/dev/null
Host gitlab.com
User git
Preferredauthentications publickey
IdentityFile ${HOME}/.ssh/gitlab_rsa
EOF
# github
cat <<EOF | sudo tee -a ${HOME}/.ssh/config >/dev/null
Host github.com
User git
Preferredauthentications publickey
IdentityFile ${HOME}/.ssh/github_rsa
EOF
# github
cat <<EOF | sudo tee -a ${HOME}/.ssh/config >/dev/null
Host git.devuan.org
User git
Preferredauthentications publickey
IdentityFile ${HOME}/.ssh/devuan_rsa
EOF
# debian
cat <<EOF | sudo tee -a ${HOME}/.ssh/config >/dev/null
Host salsa.debian.org
User git
Preferredauthentications publickey
IdentityFile ${HOME}/.ssh/debian_rsa
EOF
DEST=${1:-/etc/skel}
cat > ${DEST}/.zprofile <<'EOF'
if [ -f ${DEST}/.ssh/id_rsa.pub ]; then
chmod 644 ${DEST}/.ssh/id_rsa.pub
else
echo "Generating ssh keys."
ssh-keygen -q -t rsa -N '' -f ${HOME}/.ssh/id_rsa 2>/dev/null <<< y >/dev/null
chmod 644 ${DEST}/.ssh/id_rsa.pub
fi
EOF
chmod +x ${DEST}/.zprofile
# System keys
ssh-keygen -A
# Root user keys
ssh-keygen -q -t rsa -N '' -f ${DEST}/.ssh/id_rsa 2>/dev/null <<< y >/dev/null
# Do this for any server you want to connect to without interaction.
# ssh-keyscan git.ring-zero.co.uk >> ${HOME}/.ssh/known_hosts