Files
dotfiles/.local/bin/station-install/ssh.sh

91 lines
2.8 KiB
Bash
Raw Normal View History

2026-03-01 23:15:53 +00:00
#! /bin/bash
2026-03-01 00:09:35 +00:00
2026-03-14 16:07:57 +00:00
echo "-------------------------------------"
echo "SSH Setup Script"
echo "-------------------------------------"
2026-03-01 00:09:35 +00:00
2026-03-14 16:07:57 +00:00
echo "Extracting login details from enpass"
2026-03-14 19:15:38 +00:00
LABS=$(enpass-cli -vault="$HOME/.local/share/Enpass/Enpass/Vaults/primary" -json -sort show "Scarif: Labs")
LABSUN=$(echo "$LABS" | jq -r '.[].login')
LABSPW=$(echo "$LABS" | jq -r '.[].password')
2026-03-01 00:09:35 +00:00
SSHPATH="$HOME/.ssh/id_ed25519" # The path to the SSH key file
TITLE="$USER@$(cat /etc/hostname)" # The title for the SSH key
2026-03-14 16:07:57 +00:00
echo "~~~~~~~~"
echo "Generating the SSH key if it does not exist"
2026-03-01 00:09:35 +00:00
[ ! -f $SSHPATH ] && ssh-keygen -t ed25519 -f "$SSHPATH" -N "" -q
KEY=$(cat "$SSHPATH.pub")
# A method to generate the parameters for creating an SSH key on gitea
generate_post_data() {
cat <<EOF
{
"key": "$KEY",
"read_only": false,
"title": "$TITLE"
}
EOF
}
CREDENTIALS="$LABSUN:$LABSPW" # The credentials to pass to the API
KEYS_URL="https://$CREDENTIALS@labs.scarif.space/api/v1/user/keys"
# Get all the existing keys
KEYS=$(curl -X GET -s -S --url "$KEYS_URL")
2026-03-01 00:25:15 +00:00
KEY_EXISTS=$(echo $KEYS | jq --arg TITLE "$TITLE" 'map(.title|ascii_downcase)|contains([$TITLE|ascii_downcase])')
2026-03-01 00:09:35 +00:00
2026-03-14 16:07:57 +00:00
echo "~~~~~~~~"
echo "Checking if the key exists on the server"
2026-03-01 00:09:35 +00:00
if [[ $KEY_EXISTS == true ]]; then
2026-03-01 00:25:15 +00:00
echo "Found keys with the same title as this machine."
2026-03-01 00:09:35 +00:00
# Extract the ids of the keys with the same title as this machine
2026-03-14 19:15:38 +00:00
MATCH=$(echo "$KEYS" | jq --arg TITLE "$TITLE" 'map(select(.title|ascii_downcase == ($TITLE|ascii_downcase)))[]')
2026-03-01 00:09:35 +00:00
2026-03-14 19:15:38 +00:00
if [[ -n "$MATCH" ]]; then
ID=$(echo "$MATCH" | jq -r '.id')
EXISTING_KEY=$(echo "$MATCH" | jq -r '.key')
if [[ "$KEY" == "$EXISTING_KEY" ]]; then
echo "Key already exists and is correct. Nothing to do."
exit 0;
else
echo "Key exists but differs. Deleting existing key ID $ID"
2026-03-01 00:09:35 +00:00
curl -X DELETE \
-s -S \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
2026-03-14 19:15:38 +00:00
--url "$KEYS_URL/$ID"
fi
2026-03-01 00:09:35 +00:00
fi
fi
2026-03-14 16:07:57 +00:00
echo "~~~~~~~~"
2026-03-01 00:25:15 +00:00
echo "Saving the new key"
2026-03-01 00:09:35 +00:00
curl -X POST \
-s -S \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
--data "$(generate_post_data)" \
2026-03-01 00:25:15 +00:00
--url "$KEYS_URL"
2026-03-01 00:09:35 +00:00
HTTP_REPLACE="s/https:\/\/labs\.scarif\.space\//git@labs.scarif.space:/"
2026-03-01 11:41:14 +00:00
for dir in "$HOME/.local/nixos"; do
dir="$HOME/.local/src/$dir"
if [ -d $dir ]; then
cd "$dir"
SSH_URL=$(git remote get-url origin | sed "$HTTP_REPLACE")
git remote set-url origin "$SSH_URL"
fi
done
2026-03-01 00:09:35 +00:00
2026-03-14 16:07:57 +00:00
echo "~~~~~~~~"
2026-03-01 00:25:15 +00:00
echo "Replacing remote URL to use SSH key"
2026-03-01 00:09:35 +00:00
DOTFILES_SSH_URL=$(git --git-dir "$HOME/.config/dotfiles/.git" --work-tree="$HOME" remote get-url origin | sed "$HTTP_REPLACE")
git --git-dir "$HOME/.config/dotfiles/.git" --work-tree="$HOME" remote set-url origin "$DOTFILES_SSH_URL"
2026-03-14 19:15:38 +00:00