Update SSH keys on gitea
This commit is contained in:
@@ -44,7 +44,7 @@ alias \
|
|||||||
ll="ls -l" \
|
ll="ls -l" \
|
||||||
la="ls -al" \
|
la="ls -al" \
|
||||||
ag="alias | grep" \
|
ag="alias | grep" \
|
||||||
enp="enpasscli -vault="$HOME/Documents/Enpass/Vaults/Primary" -sort show" \
|
enp="enpasscli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort show" \
|
||||||
dgit="git --git-dir ~/.config/dotfiles/.git --work-tree=$HOME"
|
dgit="git --git-dir ~/.config/dotfiles/.git --work-tree=$HOME"
|
||||||
|
|
||||||
# Check if main exists and use instead of master
|
# Check if main exists and use instead of master
|
||||||
|
|||||||
@@ -6,3 +6,10 @@ read -n 1 -r -s -p $'Press enter when you have synced enpass...\n'
|
|||||||
|
|
||||||
export MASTERPW=$(dialog --no-cancel --passwordbox "Enter Enpass master password." 10 60 3>&1 1>&2 2>&3 3>&1)
|
export MASTERPW=$(dialog --no-cancel --passwordbox "Enter Enpass master password." 10 60 3>&1 1>&2 2>&3 3>&1)
|
||||||
|
|
||||||
|
CHECKPW=$(enpasscli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort list)
|
||||||
|
|
||||||
|
while [[ "$CHECKPW" == *level=fatal* ]]; do
|
||||||
|
export MASTERPW=$(dialog --no-cancel --passwordbox "That password didn't work, please try again" 10 60 3>&1 1>&2 2>&3 3>&1)
|
||||||
|
CHECKPW=$(enpasscli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort list)
|
||||||
|
done
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +1,55 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Extract the login details from enpass
|
||||||
LABS=$(enpasscli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort show "Scarif: Labs" 2>&1)
|
LABS=$(enpasscli -vault="$HOME/Documents/Enpass/Vaults/primary" -sort show "Scarif: Labs" 2>&1)
|
||||||
LABSUN=$(echo "$LABS" | grep -Po "(?<=login: )\w+")
|
LABSUN=$(echo "$LABS" | grep -Po "(?<=login: )\w+")
|
||||||
LABSPW=$(echo "$LABS" | grep -Po "(?<=pass : ).+(?=\")")
|
LABSPW=$(echo "$LABS" | grep -Po "(?<=pass : ).+(?=\")")
|
||||||
|
|
||||||
SSHPATH="$HOME/.ssh/id_ed25519"
|
SSHPATH="$HOME/.ssh/id_ed25519" # The path to the SSH key file
|
||||||
|
TITLE="$USER@$(cat /etc/hostname)" # The title for the SSH key
|
||||||
|
|
||||||
ssh-keygen -t ed25519 -f "$SSHPATH" -N "" -q
|
# Generate the SSH key if it does not exist
|
||||||
|
if [ ! -f $SSHPATH ] && ssh-keygen -t ed25519 -f "$SSHPATH" -N "" -q
|
||||||
|
|
||||||
|
# A method to generate the parameters for creating an SSH key on gitea
|
||||||
generate_post_data() {
|
generate_post_data() {
|
||||||
cat <<EOF
|
cat <<EOF
|
||||||
{
|
{
|
||||||
"key": "$(cat "$SSHPATH.pub")",
|
"key": "$(cat "$SSHPATH.pub")",
|
||||||
"read_only": false,
|
"read_only": false,
|
||||||
"title": "$USER@$(cat /etc/hostname)"
|
"title": "$TITLE"
|
||||||
}
|
}
|
||||||
EOF
|
EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
CREDENTIALS="$LABSUN:$LABSPW"
|
CREDENTIALS="$LABSUN:$LABSPW" # The credentials to pass to the API
|
||||||
|
KEYS_URL="https://$CREDENTIALS@labs.scarif.space/api/v1/users/keys"
|
||||||
|
|
||||||
KEYS=$(curl -X GET -s --url "https://$CREDENTIALS@labs.scarif.space/api/v1/users/$LABSUN/keys")
|
# Get all the existing keys
|
||||||
|
KEYS=$(curl -X GET -s --url "$KEYS_URL")
|
||||||
|
|
||||||
echo $KEYS | jq '.[].title'
|
KEY_EXISTS=$(echo $KEYS | jq --arg KEY "$(cat "$SSHPATH.pub")" 'contains({ key: $KEY })')
|
||||||
|
|
||||||
curl --request POST \
|
[[ $KEY_EXISTS == true ]] && return
|
||||||
|
|
||||||
|
# Extract the ids of the keys with the same title as this machine
|
||||||
|
IDS=$(echo $KEYS | jq --arg TITLE "$TITLE" 'map(select(.title == $TITLE))[].id')
|
||||||
|
|
||||||
|
# Loop through the keys and remove them from gitea to be replaced by the new one
|
||||||
|
if [ ! -z "$IDS" ]; do
|
||||||
|
for ID in IDS; do
|
||||||
|
curl -X DELETE \
|
||||||
|
-s \
|
||||||
|
-H "Accept: application/json" \
|
||||||
|
-H "Content-Type:application/json" \
|
||||||
|
--url "$KEYS_URL/$ID"
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Save the new key in gitea
|
||||||
|
curl -X POST \
|
||||||
-s \
|
-s \
|
||||||
-H "Accept: application/json" \
|
-H "Accept: application/json" \
|
||||||
-H "Content-Type:application/json" \
|
-H "Content-Type:application/json" \
|
||||||
--data "$(generate_post_data)" \
|
--data "$(generate_post_data)" \
|
||||||
--url "https://$CREDENTIALS@labs.scarif.space/api/v1/users/$LABSUN/keys"
|
--url "$KEYS_URL"
|
||||||
|
|||||||
Reference in New Issue
Block a user