How to Enable Conditional Just-in-Time SSH Access to GitHub Actions
Why SSH Keys Are a Security Risk
SSH keys are one of those things you never think about until you need to revoke access. You set up your key once and try not to think about how painful it was to add it to all of your servers. You might even copy your key from PC to PC just to avoid going through that again. We solved that problem six years ago with EZSSH, and we have blogged a lot about why SSH keys are bad since then, so I am not going to spend a lot of time on that here. Instead, today we are going to talk about deployment pipelines and how to grant SSH access to GitHub Actions only when a deployment needs it.
How to Securely Deploy to Your Servers from GitHub Actions
With cloud services such as Azure and AWS, deployment security is usually handled through short-lived credentials and federated identity. But as cloud gets more expensive and more organizations move some workloads back on-premises, SSH deployment is making a comeback. The old way of doing it does not hold up in a zero-trust world where AI agents make finding leaked credentials easier than shooting phish in a barrel. Instead of keeping a long-lived SSH key in your GitHub repository secrets (which may or may not be the same key your team uses to get into the server), you can give the pipeline an Entra ID service principal (or a managed identity if your runner lives in Azure), protect it with Conditional Access for workload identities, and have it request a short-lived SSH certificate for each deployment.
That is the theory. Below is exactly how we do it at Keytos to deploy to a machine in our office that has no SSH port open to the internet.
The Problem: Your Deployment Target Is Not on the Internet
We run a small Kubernetes cluster on a host in our office. It runs some of our self-hosted internal tools. That host lives on a private network with a private IP address, and port 22 is not reachable from the outside world. That is how it should be.
This breaks the typical “SSH into the server from GitHub Actions” tutorial, which assumes a GitHub-hosted runner can reach your server. The usual workarounds all have trade-offs:
- Open port 22 to GitHub’s IP ranges. GitHub publishes its Actions IP ranges through the meta API, but they are huge Azure ranges shared by every Actions customer. Allowlisting them is close to opening SSH to the world.
- Use a mesh VPN or tunnel in the workflow. Tools like Tailscale or Cloudflare Tunnel work well and are a big step up from an open port. But they add another identity plane to manage, and you still need an SSH credential on the other side of the tunnel.
- Use a self-hosted runner inside the network. The runner only makes outbound HTTPS connections to GitHub to pick up jobs, so you do not open any inbound ports. This is what we do.
The self-hosted runner solves the network problem, but not the credential problem. If you drop an id_ed25519 into a GitHub secret so the runner can SSH into the host, you are back to a key that never expires, works from anywhere that can reach the host, and does not show up in any of your identity logs. That is the part EZSSH fixes.
How Conditional Just-in-Time SSH Access to GitHub Actions Works
Here is the flow our Runners Deploy workflow follows every time someone runs it:
- The job lands on a self-hosted runner inside the office network.
- GitHub releases the job’s secrets only after the
productionenvironment’s protection rules pass. - The job signs in to EZSSH as an Entra ID service principal, using a certificate credential.
- EZSSH checks its access policy to confirm that this service principal is allowed to reach this
user@host, then issues an SSH certificate that expires in one hour. - The certificate goes into an
ssh-agentthat lives only for the duration of the job. - The job runs plain
scpandsshagainst the host to deploy a Helm chart.
The host never sees a static key from the pipeline. It trusts the EZSSH certificate authority and checks that the certificate is valid, unexpired, and issued for the principal it is logging in as. When the job ends, the agent goes away, and within the hour the certificate is useless anyway.
Prerequisites
- An EZSSH tenant with the host configured to trust EZSSH certificates
- A self-hosted GitHub runner that can reach the host over SSH (Linux, with
ssh-agentandssh-addavailable) - An Entra ID app registration for the pipeline, with a certificate credential
- An EZSSH policy that grants that service principal access to the
user@hostthe pipeline deploys as
Step 1: Give the Pipeline an Entra ID Identity Instead of an SSH Key
Create an app registration in Entra ID for the deployment pipeline and add a certificate credential to it. The EZSSH login documentation supports client secrets too, but we recommend a certificate. A certificate’s private key is harder to leak than a secret string, and if you issue it from EZCA you can rotate it automatically instead of setting a calendar reminder.
Then, in EZSSH, create a policy that grants this service principal access to the exact endpoint it needs, for example deploy@10.0.0.20. This is the first condition: the pipeline identity can only get a certificate for the hosts and Linux users you list in the policy, nothing else.
In GitHub, store the values the job needs:
OFFICE_CLUSTER_EZSSH_APP_IDandOFFICE_CLUSTER_EZSSH_TENANT_IDas variables (they are not secret)OFFICE_CLUSTER_SSH_KNOWN_HOSTSas a variable, holding the host’sknown_hostsline so the job verifies the host keyOFFICE_CLUSTER_EZSSH_CERTIFICATEas a secret, holding the PEM with the certificate and its private key
Scope the secret to the deployment environment rather than the whole repository. That way only jobs that pass the environment’s protection rules can read it.
Step 2: Gate the Deployment with a GitHub Environment
Our deploy job declares environment: production. GitHub environments let you require a reviewer to approve the run, restrict which branches can deploy, and add a wait timer. Environment secrets are not available to the job until those rules pass.
This is the second condition. A pull request from a fork, or a workflow someone sneaks into a feature branch, never gets the service principal credential, so it can never ask EZSSH for a certificate. We also set a concurrency group so two deployments never fight over the same host.
Step 3: Request a Just-in-Time SSH Certificate in GitHub Actions
Since we use EZSSH a lot (big fans of the technology), we wrapped the EZSSH steps in a small composite action, .github/actions/office-cluster-ssh/action.yml, so any workflow that needs ssh certificates can reuse it. Here is a version you can adapt. The only difference from ours is that we build the EZSSH CLI from source because it lives in the same repository. You will want the published Linux package instead.
name: Office cluster SSH
description: Signs in to EZSSH as a service principal and loads a short-lived SSH certificate into an ssh-agent for the rest of the job.
inputs:
ssh-endpoint:
description: "user@host, as the EZSSH policy grants it"
required: true
known-hosts:
description: "known_hosts line(s) for the host, so its host key is checked"
required: true
service-principal-app-id:
required: true
service-principal-tenant-id:
required: true
service-principal-certificate:
description: "PEM with the service principal's certificate and private key"
required: true
runs:
using: composite
steps:
- name: Install the EZSSH CLI
shell: bash
run: |
set -euo pipefail
mkdir -p "$RUNNER_TEMP/ezssh"
curl -fsSL https://download.keytos.io/Downloads/Linux/ezssh_linux-x64.tar.gz \
| tar -xz -C "$RUNNER_TEMP/ezssh"
chmod +x "$RUNNER_TEMP/ezssh/ezssh"
echo "$RUNNER_TEMP/ezssh" >> "$GITHUB_PATH"
- name: Start an ssh-agent for the job
shell: bash
env:
SSH_ENDPOINT: ${{ inputs.ssh-endpoint }}
run: |
set -euo pipefail
eval "$(ssh-agent -s)" > /dev/null
{
echo "SSH_AUTH_SOCK=$SSH_AUTH_SOCK"
echo "SSH_AGENT_PID=$SSH_AGENT_PID"
echo "OFFICE_CLUSTER_SSH_ENDPOINT=$SSH_ENDPOINT"
} >> "$GITHUB_ENV"
- name: Trust the host key
shell: bash
env:
SSH_ENDPOINT: ${{ inputs.ssh-endpoint }}
KNOWN_HOSTS: ${{ inputs.known-hosts }}
run: |
set -euo pipefail
mkdir -p ~/.ssh && chmod 700 ~/.ssh
printf '%s\n' "$KNOWN_HOSTS" >> ~/.ssh/known_hosts
printf 'Host %s\n StrictHostKeyChecking yes\n BatchMode yes\n' "${SSH_ENDPOINT#*@}" >> ~/.ssh/config
chmod 600 ~/.ssh/known_hosts ~/.ssh/config
- name: Request the SSH certificate
shell: bash
env:
SSH_ENDPOINT: ${{ inputs.ssh-endpoint }}
APP_ID: ${{ inputs.service-principal-app-id }}
TENANT_ID: ${{ inputs.service-principal-tenant-id }}
SP_CERTIFICATE: ${{ inputs.service-principal-certificate }}
run: |
set -euo pipefail
SP_CERT_PATH="$RUNNER_TEMP/ezssh-sp.pem"
trap 'rm -f "$SP_CERT_PATH"' EXIT
(umask 077 && printf '%s\n' "$SP_CERTIFICATE" > "$SP_CERT_PATH")
ezssh login --service-principal \
--username "$APP_ID" \
--tenant "$TENANT_ID" \
--certificate "$SP_CERT_PATH"
ezssh cert --endpoint "$SSH_ENDPOINT" < /dev/null
# Fail loudly if no policy granted the endpoint and no certificate was loaded.
if ! ssh-add -l | grep -q -- '-CERT)'; then
echo "::error::EZSSH loaded no SSH certificate for $SSH_ENDPOINT. Check the EZSSH policy."
exit 1
fi
A few details worth calling out, because each one came from something that bit us:
- The ssh-agent is exported through
GITHUB_ENV. Each step in a job runs in its own shell. WritingSSH_AUTH_SOCKandSSH_AGENT_PIDtoGITHUB_ENVlets every later step use the same agent, sosshandscpjust work without passing keys around. - The service principal PEM touches disk for as little time as possible. It is written with
umask 077and removed by atrapas soon as the login step exits, whether it succeeded or not. ezssh certputs the certificate in the agent. By default the certificate is valid for one hour, which is plenty for a deployment. If a job legitimately runs longer, pass--certLengthwith the number of hours you need, but keep it as short as you can.- We check the agent instead of trusting the exit code. If no policy grants the endpoint, we want the job to fail at this step with a clear message, not three steps later with a confusing
Permission denied (publickey). - The host key is pinned.
StrictHostKeyChecking yesand a knownknown_hostsline mean the pipeline will not hand its certificate to an impostor host.BatchMode yesmakessshfail instead of hanging on a prompt.
Step 4: SSH into the Private Server from the Workflow
With the composite action in place, the deployment workflow itself is refreshingly boring. Here is a trimmed version of our Runners Deploy workflow:
name: Runners Deploy
on:
workflow_dispatch:
concurrency:
group: runners-deploy
cancel-in-progress: false
jobs:
deploy-to-office-cluster:
runs-on: office-runners # self-hosted runner label inside the office network
environment:
name: production
steps:
- uses: actions/checkout@v6
- name: Get an SSH certificate for the office cluster host
uses: ./.github/actions/office-cluster-ssh
with:
ssh-endpoint: deploy@10.0.0.20
known-hosts: ${{ vars.OFFICE_CLUSTER_SSH_KNOWN_HOSTS }}
service-principal-app-id: ${{ vars.OFFICE_CLUSTER_EZSSH_APP_ID }}
service-principal-tenant-id: ${{ vars.OFFICE_CLUSTER_EZSSH_TENANT_ID }}
service-principal-certificate: ${{ secrets.OFFICE_CLUSTER_EZSSH_CERTIFICATE }}
- name: Copy the chart to the office cluster host
run: |
set -euo pipefail
tar -C infrastructure/runners -czf "$RUNNER_TEMP/helm-charts.tgz" helm-charts
scp "$RUNNER_TEMP/helm-charts.tgz" "$OFFICE_CLUSTER_SSH_ENDPOINT:runners/helm-charts-upload.tgz"
- name: Deploy the chart
run: |
ssh "$OFFICE_CLUSTER_SSH_ENDPOINT" bash -s <<'REMOTE_SCRIPT'
set -euo pipefail
cd ~/runners
rm -rf helm-charts.incoming && mkdir helm-charts.incoming
tar -C helm-charts.incoming -xzf helm-charts-upload.tgz && rm helm-charts-upload.tgz
rm -rf helm-charts.previous
if [ -d helm-charts ]; then mv helm-charts helm-charts.previous; fi
mv helm-charts.incoming/helm-charts helm-charts && rmdir helm-charts.incoming
helm upgrade --install ctlog-monitoring ./helm-charts --namespace runners-logs
REMOTE_SCRIPT
Notice what is missing: there is no ssh-keyscan, no echo "$SSH_PRIVATE_KEY" > id_rsa, and no -i flag anywhere. The run we used for this post went from checkout to a deployed Helm release in 38 seconds.
We also extract the chart into a fresh directory instead of over the old one. Extracting on top of an existing directory keeps templates you deleted from the repository, which is a fun way to deploy a CronJob you thought you removed months ago.
Adding Entra ID Conditional Access to the Pipeline Identity
Steps 1 and 2 already give you two conditions: the EZSSH policy limits what the pipeline can reach, and the GitHub environment limits when it can get its credential. Because the pipeline signs in with a real Entra ID identity, you can add a third condition on where it signs in from.
Conditional Access for workload identities lets you target a service principal with a policy that blocks sign-ins from outside a named location, or when Entra ID Protection flags the service principal as risky. Our runner sits in the office, so its sign-ins always come from the office’s public IP. A policy that blocks this service principal from everywhere else means that even if the PEM leaked from GitHub, it would be useless from an attacker’s laptop. Compare that to a leaked SSH private key, which works from anywhere that can reach the server.
A few things to know before you set this up:
- It requires Microsoft Entra Workload ID Premium licenses.
- It applies to single-tenant service principals registered in your tenant. Managed identities are not covered, which is one more reason we use a service principal for an on-premises runner.
- The only grant control is Block, so you write the policy as “block everything except the trusted location.”
- Assign the service principal to the policy directly as a workload identity. Conditional Access does not enforce a policy on a service principal through group membership.
Every sign-in also lands in the Entra ID service principal sign-in logs, and every certificate EZSSH issues lands in the EZSSH audit log, which you can export to Microsoft Sentinel or your SIEM of choice. When someone asks “who deployed to that box last Tuesday?”, you have an answer.
SSH Keys in GitHub Secrets vs. Just-in-Time SSH Certificates
To be fair, the service principal certificate is still a secret stored in GitHub. The difference is that it is not an SSH credential. It cannot open a session on any server by itself. Every time it is used, Entra ID and EZSSH decide whether to turn it into one.
| Static SSH key in a GitHub secret | EZSSH just-in-time SSH certificate | |
|---|---|---|
| Lifetime of the SSH credential | Until someone remembers to rotate it | One hour by default |
| Where it works | Anywhere that can reach the server | Only where Conditional Access allows the sign-in |
| What it can reach | Every server with the public key in authorized_keys |
Only the endpoints in the EZSSH policy |
| Revoking access | Edit authorized_keys on every server |
Disable the service principal or remove it from the policy |
| Audit trail | sshd logs on each server |
Entra ID sign-in logs plus the EZSSH audit log |
| Inbound ports needed | Port 22 reachable by the runner | None from the internet with a self-hosted runner |
If you want to go deeper on the principles behind this, our post on what zero trust SSH is covers the model, and our SSH best practices for Entra ID post covers the rest of your SSH estate, not just pipelines.
Conclusion
A self-hosted runner gets your pipeline onto the private network without opening a single inbound port. EZSSH replaces the static SSH key with a one-hour certificate that a policy grants to an Entra ID identity. GitHub environments and Conditional Access decide when and from where that identity can ask for one. The result is a deployment pipeline that can SSH into a machine that is invisible to the internet, without any standing SSH credential sitting in your repository.
If you want to set this up for your own pipelines, get started with EZSSH, or schedule a FREE call with one of our identity experts and we will walk you through it.
Get Started with EZSSH Just-in-Time SSH Access