Files
dotfiles/.local/bin/setup/setupssh

75 lines
2.3 KiB
Plaintext
Raw Normal View History

2021-04-11 17:21:20 +01:00
#!/bin/bash
2021-04-16 12:26:33 +01:00
# Extract the login details from enpass
2021-04-16 00:29:01 +01:00
LABS=$(enpasscli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort show "Scarif: Labs" 2>&1)
2021-04-11 17:21:20 +01:00
LABSUN=$(echo "$LABS" | grep -Po "(?<=login: )\w+")
LABSPW=$(echo "$LABS" | grep -Po "(?<=pass : ).+(?=\")")
2021-04-16 12:26:33 +01:00
SSHPATH="$HOME/.ssh/id_ed25519" # The path to the SSH key file
TITLE="$USER@$(cat /etc/hostname)" # The title for the SSH key
2021-04-11 17:21:20 +01:00
2021-04-16 12:26:33 +01:00
# Generate the SSH key if it does not exist
2021-05-01 13:32:52 +01:00
[ ! -f $SSHPATH ] && ssh-keygen -t ed25519 -f "$SSHPATH" -N "" -q
KEY=$(cat "$SSHPATH.pub")
2021-04-11 17:21:20 +01:00
2021-04-16 12:26:33 +01:00
# A method to generate the parameters for creating an SSH key on gitea
2021-04-11 17:21:20 +01:00
generate_post_data() {
cat <<EOF
{
2021-05-01 13:32:52 +01:00
"key": "$KEY",
2021-04-11 17:21:20 +01:00
"read_only": false,
2021-04-16 12:26:33 +01:00
"title": "$TITLE"
2021-04-11 17:21:20 +01:00
}
EOF
}
2021-04-16 12:26:33 +01:00
CREDENTIALS="$LABSUN:$LABSPW" # The credentials to pass to the API
2021-05-01 13:32:52 +01:00
KEYS_URL="https://$CREDENTIALS@labs.scarif.space/api/v1/user/keys"
2021-04-16 00:29:01 +01:00
2021-04-16 12:26:33 +01:00
# Get all the existing keys
KEYS=$(curl -X GET -s --url "$KEYS_URL")
2021-04-16 00:29:01 +01:00
2021-05-01 13:32:52 +01:00
KEY_EXISTS=$(echo $KEYS | jq --arg TITLE "$TITLE" 'map(.title)|contains([$TITLE])')
2021-04-11 17:21:20 +01:00
2021-05-01 13:42:00 +01:00
if [[ $KEY_EXISTS == true ]]; then
2021-04-16 14:12:06 +01:00
# Extract the ids of the keys with the same title as this machine
2021-05-01 13:42:00 +01:00
IDS=$(echo "$KEYS" | jq --arg TITLE "$TITLE" 'map(select(.title == $TITLE))[].id')
2021-04-16 14:12:06 +01:00
# Loop through the keys and remove them from gitea to be replaced by the new one
if [ ! -z "$IDS" ]; then
2021-05-01 13:42:00 +01:00
for ID in $IDS; do
echo "Deleting key with ID $ID"
2021-04-16 14:12:06 +01:00
curl -X DELETE \
-s \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
--url "$KEYS_URL/$ID"
done
fi
2021-04-16 12:26:33 +01:00
fi
2021-05-01 13:42:00 +01:00
# Save the new key in gitea
curl -X POST \
-s \
-H "Accept: application/json" \
-H "Content-Type:application/json" \
--data "$(generate_post_data)" \
--url "$KEYS_URL"
2021-04-16 14:12:06 +01:00
HTTP_REPLACE="s/https:\/\/labs\.scarif\.space\//git@labs.scarif.space:/"
for dir in $(ls "$HOME/.local/src"); 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
2021-05-01 13:32:52 +01: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"
2021-04-16 14:12:06 +01:00