|
| 1 | +#!/bin/bash |
| 2 | +# Automatically configure Git to use SSH signing with forwarded SSH keys |
| 3 | +set -euo pipefail |
| 4 | + |
| 5 | +echo "Setting up Git SSH signing..." |
| 6 | + |
| 7 | +# Check if SSH agent forwarding is working |
| 8 | +if ! ssh-add -l &>/dev/null; then |
| 9 | + echo "ERROR: No SSH keys found in agent. SSH agent forwarding is not set up correctly." |
| 10 | + echo "SSH signing will not work without SSH agent forwarding." |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | + |
| 14 | +# Get the first SSH key from the agent |
| 15 | +SSH_KEY=$(ssh-add -L | head -n 1) |
| 16 | +if [ -z "$SSH_KEY" ]; then |
| 17 | + echo "ERROR: No SSH keys found in agent. SSH signing will not work." |
| 18 | + exit 1 |
| 19 | +fi |
| 20 | + |
| 21 | +# Extract the key type and key content |
| 22 | +KEY_TYPE=$(echo "$SSH_KEY" | awk '{print $1}') |
| 23 | +KEY_CONTENT=$(echo "$SSH_KEY" | awk '{print $2}') |
| 24 | + |
| 25 | +# Check if Git user settings are available |
| 26 | +if [[ -z "${GIT_USER_NAME:-}" || -z "${GIT_USER_EMAIL:-}" ]]; then |
| 27 | + echo "WARNING: Git user settings (GIT_USER_NAME and/or GIT_USER_EMAIL) are not set." |
| 28 | + echo "Git commit signing will not be configured." |
| 29 | + echo "If you need Git commit signing, add these variables to your environment file." |
| 30 | + exit 0 |
| 31 | +fi |
| 32 | + |
| 33 | +# Set Git user name from environment variable |
| 34 | +echo "Setting Git user.name: $GIT_USER_NAME" |
| 35 | +git config --global user.name "$GIT_USER_NAME" |
| 36 | + |
| 37 | +# Set Git user email from environment variable |
| 38 | +echo "Setting Git user.email: $GIT_USER_EMAIL" |
| 39 | +git config --global user.email "$GIT_USER_EMAIL" |
| 40 | + |
| 41 | +# Create the .ssh directory if it doesn't exist |
| 42 | +mkdir -p ~/.ssh |
| 43 | +chmod 700 ~/.ssh |
| 44 | + |
| 45 | +# Create or update the allowed signers file |
| 46 | +echo "Updating allowed signers file..." |
| 47 | +ALLOWED_SIGNERS_FILE=~/.ssh/allowed_signers |
| 48 | +SIGNER_LINE="$GIT_USER_EMAIL $KEY_TYPE $KEY_CONTENT" |
| 49 | + |
| 50 | +# Create the file if it doesn't exist |
| 51 | +if [ ! -f "$ALLOWED_SIGNERS_FILE" ]; then |
| 52 | + echo "$SIGNER_LINE" > "$ALLOWED_SIGNERS_FILE" |
| 53 | + echo "Created new allowed signers file." |
| 54 | +else |
| 55 | + # Check if the key is already in the file |
| 56 | + if ! grep -q "$KEY_CONTENT" "$ALLOWED_SIGNERS_FILE"; then |
| 57 | + # Append the key if it's not already there |
| 58 | + echo "$SIGNER_LINE" >> "$ALLOWED_SIGNERS_FILE" |
| 59 | + echo "Added new key to allowed signers file." |
| 60 | + else |
| 61 | + echo "Key already exists in allowed signers file." |
| 62 | + fi |
| 63 | +fi |
| 64 | + |
| 65 | +chmod 600 "$ALLOWED_SIGNERS_FILE" |
| 66 | + |
| 67 | +# Configure Git to use SSH signing |
| 68 | +echo "Configuring Git to use SSH signing..." |
| 69 | +git config --global gpg.format ssh |
| 70 | +git config --global user.signingkey "key::$KEY_TYPE $KEY_CONTENT" |
| 71 | +git config --global gpg.ssh.allowedSignersFile ~/.ssh/allowed_signers |
| 72 | +git config --global commit.gpgsign true |
| 73 | + |
| 74 | +echo "Git SSH signing setup complete!" |
| 75 | +echo "Your commits will now be automatically signed using your SSH key." |
| 76 | +echo "Make sure this key is added to GitHub as a signing key in your account settings." |
0 commit comments