Files
SIA/scripts/gitea_keys.sh
2025-03-24 10:53:51 +00:00

191 lines
5.7 KiB
Bash
Executable File

#!/bin/bash
set -e
# Check if required environment variables are set
if [ -z "$SIA_GITEA_API_KEY" ]; then
echo "Error: SIA_GITEA_API_KEY environment variable is not set"
exit 1
fi
if [ -z "$SIA_REPO_URL" ]; then
echo "Error: SIA_REPO_URL environment variable is not set"
exit 1
fi
# Install dependencies
apt-get update
apt-get install -y jq openssh-client git
# Extract host, user and port from SIA_REPO_URL
if [[ "$SIA_REPO_URL" == ssh://* ]]; then
# Handle ssh:// format
URL_PATH=${SIA_REPO_URL#ssh://}
# Get user if specified
if [[ "$URL_PATH" == *"@"* ]]; then
SSH_USER=${URL_PATH%%@*}
URL_PATH=${URL_PATH#*@}
else
SSH_USER="git"
fi
# Extract domain and port
if [[ "$URL_PATH" == *":"* ]]; then
# Format with port: domain:port/path
SSH_HOST=${URL_PATH%%:*}
PORT_PATH=${URL_PATH#*:}
SSH_PORT=${PORT_PATH%%/*}
else
# Format without port: domain/path
SSH_HOST=${URL_PATH%%/*}
SSH_PORT=22
fi
else
# Handle git@ format (git@github.com:user/repo.git)
if [[ "$SIA_REPO_URL" == *"@"* ]]; then
SSH_USER=${SIA_REPO_URL%%@*}
URL_PATH=${SIA_REPO_URL#*@}
if [[ "$URL_PATH" == *":"* ]]; then
SSH_HOST=${URL_PATH%%:*}
SSH_PORT=22 # Standard format doesn't specify port
else
SSH_HOST=${URL_PATH%%/*}
SSH_PORT=22
fi
else
SSH_USER="git"
SSH_HOST=${SIA_REPO_URL%%/*}
SSH_PORT=22
fi
fi
echo "Using SSH configuration:"
echo " Host: $SSH_HOST"
echo " User: $SSH_USER"
echo " Port: $SSH_PORT"
# Create SSH directory if it doesn't exist
mkdir -p ~/.ssh
chmod 700 ~/.ssh
# Generate SSH key if it doesn't exist
if [ ! -f ~/.ssh/sia_repo ]; then
echo "Generating new SSH key..."
ssh-keygen -t ed25519 -f ~/.ssh/sia_repo -N ""
fi
# Ensure proper permissions on SSH keys
chmod 600 ~/.ssh/sia_repo
chmod 644 ~/.ssh/sia_repo.pub
# Get the public key
SSH_PUBLIC_KEY=$(cat ~/.ssh/sia_repo.pub)
# Add host key to known_hosts to avoid verification prompt
echo "Adding host key to known_hosts..."
ssh-keyscan -t rsa -p $SSH_PORT $SSH_HOST >> ~/.ssh/known_hosts
chmod 600 ~/.ssh/known_hosts
# Clear any existing connections in SSH config
if [ -f ~/.ssh/config ]; then
rm -f ~/.ssh/config
fi
# Check if a key with title "sia" already exists and delete it
echo "Checking for existing SSH key in Gitea..."
RESPONSE=$(curl -s -X GET "https://$SSH_HOST/api/v1/user/keys" \
-H "accept: application/json" \
-H "Authorization: token $SIA_GITEA_API_KEY")
# Debug: Print the response to help diagnose issues
echo "API Response: $RESPONSE"
# Check if response is valid JSON
if echo "$RESPONSE" | jq empty 2>/dev/null; then
echo "Response is valid JSON"
# Check if response is an array
if [ "$(echo "$RESPONSE" | jq 'if type=="array" then "array" else "not_array" end')" = '"array"' ]; then
echo "Response is an array, proceeding with key check"
KEY_ID=$(echo "$RESPONSE" | jq -r '.[] | select(.title=="sia") | .id')
if [ ! -z "$KEY_ID" ] && [ "$KEY_ID" != "null" ]; then
echo "Found existing key with ID $KEY_ID, deleting..."
curl -X DELETE "https://$SSH_HOST/api/v1/user/keys/$KEY_ID" \
-H "accept: application/json" \
-H "Authorization: token $SIA_GITEA_API_KEY"
else
echo "No existing key with title 'sia' found"
fi
else
echo "Response is not an array, skipping key check"
fi
else
echo "Invalid JSON response from API, skipping key check"
fi
# Add the SSH key to Gitea using the API
echo "Adding SSH key to Gitea using API..."
ADD_RESPONSE=$(curl -s -X POST "https://$SSH_HOST/api/v1/user/keys" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-H "Authorization: token $SIA_GITEA_API_KEY" \
-d "{\"title\":\"sia\", \"key\":\"$SSH_PUBLIC_KEY\"}")
# Debug: Print the response
echo "Add key response: $ADD_RESPONSE"
# Check if the response indicates success
if echo "$ADD_RESPONSE" | jq -e '.id' > /dev/null 2>&1; then
echo "SSH key added successfully!"
else
echo "Failed to add SSH key. Please check your API token and try again."
exit 1
fi
# Create SSH config file to specify which key to use
echo "Configuring SSH to use the correct key for repository host..."
cat > ~/.ssh/config << EOL
Host $SSH_HOST
User $SSH_USER
Port $SSH_PORT
IdentityFile ~/.ssh/sia_repo
PreferredAuthentications publickey
StrictHostKeyChecking no
EOL
chmod 600 ~/.ssh/config
# Stop any running SSH agents to start fresh
pkill ssh-agent || true
# Start SSH agent and add the key
eval $(ssh-agent -s)
ssh-add ~/.ssh/sia_repo
# Set up the Git SSH command to force use of the config file
export GIT_SSH_COMMAND="ssh -F ~/.ssh/config"
# Test the SSH connection
echo "Testing SSH connection... (this might fail if the server rejects the connection)"
ssh -F ~/.ssh/config -T -p $SSH_PORT $SSH_USER@$SSH_HOST || true
# Ensure git is configured with a name and email
if ! git config --global user.email > /dev/null; then
echo "Configuring default git user email..."
git config --global user.email "niels.geens@gmail.com"
fi
if ! git config --global user.name > /dev/null; then
echo "Configuring default git user name..."
git config --global user.name "Niels Geens"
fi
# Set the GIT_SSH_COMMAND environment variable persistently
echo 'export GIT_SSH_COMMAND="ssh -F ~/.ssh/config"' >> ~/.bashrc
# Add a helper function to modify bootstrap.sh behavior without changing it
echo 'alias git="GIT_SSH_COMMAND=\"ssh -F ~/.ssh/config\" git"' >> ~/.bashrc
echo "SSH key setup completed. The environment has been configured for git operations."