Why static SSH keys in CI/CD are a silent credential‑leakage vector

Many teams treat a private SSH key as a “set‑and‑forget” secret: they generate a key pair, drop the private half into a GitHub repository, and let a cron job or a CI runner use it to push code to production servers. The convenience is undeniable, but the security cost is rarely examined. A compromised CI runner, a leaked repository history, or even an over‑eager teammate with read access instantly exposes the private key, granting unrestricted shell access to every host that trusts the corresponding public key. This article explains why that practice is a hidden liability and demonstrates a safer, credential‑rotation‑aware pipeline that still satisfies the need for rapid, automated deployment.

Overview of the insecure pattern

The classic insecure pattern looks like this:

# .github/workflows/deploy.yml
name: Deploy
on:
  push:
    branches: [ main ]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Install SSH client
        run: sudo apt-get update && sudo apt-get install -y openssh-client
      - name: Add static private key
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.STATIC_SSH_KEY }}" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan -H example.com >> ~/.ssh/known_hosts
      - name: Sync code
        run: rsync -avz -e "ssh -i ~/.ssh/id_rsa" ./ [email protected]:/var/www/app

The key is stored as a GitHub secret named STATIC_SSH_KEY. Once the secret is added, anyone who can read repository settings can retrieve the raw key via the API. Moreover, the key never expires; if an attacker extracts it, they retain access until the key is manually revoked.

Designing a rotation‑friendly, short‑lived credential flow

The safer alternative is to replace the static key with a short‑lived, per‑run SSH certificate issued by an internal Certificate Authority (CA). The CI job requests a certificate just before the deployment step, uses it, and discards it when the job ends. Because the certificate is valid only for a few minutes, even a leak has negligible impact.

# ca/ssh-ca.conf
# Simple OpenSSH CA configuration
# The CA private key lives on a hardened bastion host, never in the repo.
# Clients request a signed certificate via the sign_ssh_cert.sh script.

Below is a minimal sign_ssh_cert.sh script that runs on the bastion. It authenticates the request using a one‑time token stored in GitHub Actions secrets.

#!/usr/bin/env bash
# sign_ssh_cert.sh – issue a temporary SSH certificate

set -euo pipefail

# $TOKEN is a secret known only to the CI job and the bastion
if [[ "$1" != "$TOKEN" ]]; then
  echo "Invalid token"
  exit 1
fi

# The public key is passed via stdin
read -r PUBKEY

# Certificate validity: now + 5 minutes
VALIDITY="+5m"

# Issue the certificate
ssh-keygen -s /etc/ssh/ca_key -I "ci-${GITHUB_RUN_ID}" \
  -V "$(date -u +"%Y-%m-%dT%H:%M:%SZ")${VALIDITY}" \
  -n "ci-${GITHUB_RUN_ID}" - <<< "$PUBKEY"

The CI workflow now performs three steps: generate an ephemeral key pair, request a certificate from the CA, and use the certificate for the rsync operation. Because the private key is generated inside the runner and never persisted, there is nothing for an attacker to steal after the job completes.

Implementing the secure workflow in GitHub Actions

The following workflow replaces the insecure static key with the rotation‑friendly approach:

# .github/workflows/secure-deploy.yml
name: Secure Deploy
on:
  push:
    branches: [ main ]

jobs:
  secure-deploy:
    runs-on: ubuntu-latest
    env:
      CA_ENDPOINT: https://bastion.example.com/sign_ssh_cert.sh
      CA_TOKEN: ${{ secrets.CA_ONE_TIME_TOKEN }}
    steps:
      - uses: actions/checkout@v3

      - name: Generate temporary SSH key pair
        id: genkey
        run: |
          ssh-keygen -t ed25519 -f /tmp/ci_key -N "" -C "ci-${GITHUB_RUN_ID}"
          echo "pubkey=$(cat /tmp/ci_key.pub)" >> $GITHUB_OUTPUT
          echo "privkey=$(cat /tmp/ci_key)" >> $GITHUB_OUTPUT

      - name: Request short‑lived certificate
        id: cert
        run: |
          CERT=$(curl -s -X POST "$CA_ENDPOINT" \
            -H "Authorization: Bearer $CA_TOKEN" \
            --data-binary "${{ steps.genkey.outputs.pubkey }}")
          echo "$CERT" > /tmp/ci_key-cert.pub
          chmod 600 /tmp/ci_key*

      - name: Install SSH client & known hosts
        run: |
          sudo apt-get update && sudo apt-get install -y openssh-client
          mkdir -p ~/.ssh
          ssh-keyscan -H example.com >> ~/.ssh/known_hosts

      - name: Deploy with rsync using certificate
        env:
          SSH_PRIVKEY: ${{ steps.genkey.outputs.privkey }}
        run: |
          chmod 600 /tmp/ci_key
          rsync -avz -e "ssh -i /tmp/ci_key -o CertificateFile=/tmp/ci_key-cert.pub" \
            ./ [email protected]:/var/www/app

Notice the following security‑focused decisions:

  • The private key lives only in the runner’s /tmp directory and is never written to disk outside the job.
  • The certificate is limited to a five‑minute window, dramatically reducing the blast radius of any accidental exposure.
  • The CA token is stored as a GitHub secret and is scoped to the repository only.
  • All network traffic is still protected by SSH, but the host‑based trust model now includes certificate validation.

Security and Best Practices

Even with short‑lived certificates, a few practices are essential to keep the pipeline airtight:

  • Never log private material. GitHub Actions masks secrets, but accidental echo $PRIVATE_KEY statements can still appear in logs if not careful.
  • Restrict CA token scope. Generate a separate token per repository, and rotate it weekly.
  • Enable audit logging on the bastion. Every certificate issuance should be recorded with the GitHub run ID and timestamp.
  • Use a hardened bastion host. The CA private key must reside on a machine with minimal services, FIPS‑compliant OS, and hardware‑based TPM for key protection.
  • Validate host keys. The known_hosts file should be managed centrally, not built on‑the‑fly from ssh-keyscan in production.
"A credential that lives longer than it needs to is an invitation to compromise."

Conclusion

The convenience of embedding a static SSH private key in a CI pipeline is tempting, yet it creates a persistent foothold for attackers. By shifting to a short‑lived certificate model, teams gain the same automation speed while dramatically shrinking the window of opportunity for credential theft. The pattern shown here can be extended to any SSH‑based operation—Git pushes, remote command execution, or container image transfers— and it integrates cleanly with existing GitHub Actions workflows.

Remember: security is not a checkbox