# SSH via GitLab CI opkssh supports SSHing into servers from GitLab CI/CD jobs using GitLab's OpenID Connect (OIDC) ID tokens. This allows CI/CD pipelines to authenticate over SSH without managing static SSH keys or long-lived secrets. ## How it works GitLab CI/CD can issue [OIDC ID tokens](https://docs.gitlab.com/ci/secrets/id_token_authentication/) to a job. These tokens prove the identity of a pipeline run, including claims such as the project path, branch/tag ref, and pipeline source. When `opkssh login gitlab-ci` runs inside a GitLab CI environment, opkssh reads the ID token from the `OPENPUBKEY_JWT` environment variable and uses it to create an SSH certificate. The SSH server verifies the certificate against the server-side provider configuration and policy. GitLab CI tokens are verified using OpenPubkey's GitLab CI provider. These tokens are GQ-bound (`GQ256`) rather than using the normal browser-login nonce flow, so the server must have a GitLab CI provider entry in `/etc/opk/providers`. ## Server setup ### 1. Install opkssh on the server Follow the standard [installation instructions](../README.md#installing-on-a-linux-server) to install opkssh on your server. ### 2. Add the GitLab CI provider Add a GitLab CI provider entry to `/etc/opk/providers`. The second field must be the OpenPubkey PKToken audience used by your GitLab CI job. Using the explicit audience from the GitLab CI token: ```bash echo "https://gitlab.com OPENPUBKEY-PKTOKEN:GITLAB-CI:ssh-deploy-prod oidc" | sudo tee -a /etc/opk/providers ``` For self-managed GitLab, use your GitLab instance URL as the issuer instead: ```bash echo "https://gitlab.example.com OPENPUBKEY-PKTOKEN:GITLAB-CI:ssh-deploy-prod oidc" | sudo tee -a /etc/opk/providers ``` If you also want to allow normal interactive GitLab logins, keep a GitLab provider entry with the client ID of your own GitLab OAuth application as well (see [Default client IDs](audit.md#default-client-ids) for how to create one): ```text https://gitlab.com oidc https://gitlab.com OPENPUBKEY-PKTOKEN:GITLAB-CI:ssh-deploy-prod oidc ``` The default GitLab client ID that the installers write, `8d8b7024…`, is intended for testing and should not be used in production, and `opkssh audit` reports it as a warning (see [Default client IDs](audit.md#default-client-ids)). For self-managed GitLab, use the same issuer in `/etc/opk/auth_id` as in `/etc/opk/providers`. ### 3. Authorize a GitLab project and ref Use `opkssh add` to allow a specific GitLab project and branch to SSH into the server as a given user. The identity typically matches GitLab's `sub` claim: ```text project_path:/:ref_type::ref: ``` For example, to allow the `main` branch of `cgroschupp/opkssh-test` to log in as `root`: ```bash sudo opkssh add root "project_path:cgroschupp/opkssh-test:ref_type:branch:ref:main" "https://gitlab.com" ``` This adds a line like this to `/etc/opk/auth_id`: ```text root project_path:cgroschupp/opkssh-test:ref_type:branch:ref:main https://gitlab.com ``` For self-managed GitLab, use your instance issuer: ```bash sudo opkssh add root "project_path:mygroup/myproject:ref_type:branch:ref:main" "https://gitlab.example.com" ``` Validate the server configuration: ```bash sudo opkssh audit sudo opkssh permissions check ``` ## GitLab CI workflow Your GitLab CI job needs an `id_tokens` entry that creates `OPENPUBKEY_JWT`. The `aud` value becomes part of the OpenPubkey PKToken audience. Here is a minimal example that logs in with opkssh and SSHes into a remote server: ```yaml stages: - test test-ssh: id_tokens: OPENPUBKEY_JWT: aud: OPENPUBKEY-PKTOKEN:GITLAB-CI:ssh-deploy-prod image: ubuntu stage: test script: - apt-get update && apt-get install -y curl openssh-client - curl -L -o /usr/local/bin/opkssh https://github.com/openpubkey/opkssh/releases/latest/download/opkssh-linux-amd64 - chmod +x /usr/local/bin/opkssh - opkssh login gitlab-ci - opkssh inspect /root/.ssh/id_ecdsa-cert.pub - ssh -o StrictHostKeyChecking=accept-new root@your-server.example.com "echo 'Hello from GitLab CI'" ``` The example downloads the latest Linux amd64 opkssh binary from the official GitHub release. For other architectures, use the matching asset from the [latest release](https://github.com/openpubkey/opkssh/releases/latest). For self-managed GitLab, set `CI_SERVER_URL` to your GitLab issuer URL before running `opkssh login gitlab-ci`: ```yaml test-ssh: variables: CI_SERVER_URL: https://gitlab.example.com id_tokens: OPENPUBKEY_JWT: aud: OPENPUBKEY-PKTOKEN:GITLAB-CI:ssh-deploy-prod image: ubuntu stage: test script: - apt-get update && apt-get install -y curl openssh-client - curl -L -o /usr/local/bin/opkssh https://github.com/openpubkey/opkssh/releases/latest/download/opkssh-linux-amd64 - chmod +x /usr/local/bin/opkssh - opkssh login gitlab-ci - ssh -o StrictHostKeyChecking=accept-new root@your-server.example.com "echo 'Hello from GitLab CI'" ``` ### Key workflow requirements - **`id_tokens.OPENPUBKEY_JWT`**: This must be configured so GitLab creates an OIDC ID token and exposes it as the `OPENPUBKEY_JWT` environment variable. - **Audience must match server policy**: If the job uses `aud: OPENPUBKEY-PKTOKEN:GITLAB-CI:ssh-deploy-prod`, the server should have `https://gitlab.com OPENPUBKEY-PKTOKEN:GITLAB-CI:ssh-deploy-prod oidc` in `/etc/opk/providers`. - **`opkssh login gitlab-ci`**: The `gitlab-ci` argument tells opkssh to use the GitLab CI provider. It reads `OPENPUBKEY_JWT` from the environment. - **`CI_SERVER_URL`**: Optional. Set this to your self-managed GitLab issuer URL, such as `https://gitlab.example.com`. If unset, opkssh uses `https://gitlab.com`. - **Server-side identity**: The identity in `/etc/opk/auth_id` must match the token's `sub` claim. Since gitlab allows anyone to request a ID token with an `aud` of their choice, `sub` security critical and distinguishes ID Tokens issued to different orgs. - **Server-side identity**: The identity in `/etc/opk/auth_id` must match the token's `sub` claim, for example `project_path:mygroup/myproject:ref_type:branch:ref:main`. This is the check that decides who may log in. The audience is not a strong protection in gitlab-ci. Any project on the same GitLab instance can request an ID token with whatever `aud` it likes, including yours, so a matching audience only proves the token was minted for opkssh, not which project minted it. Only `sub`, which GitLab sets from the pipeline's project and ref and which a job cannot choose, identifies the caller. ## Identity format GitLab's default `sub` claim commonly has this format: ```text project_path:/:ref_type::ref: ``` Examples: | Pattern | Example | |---------|---------| | Project + branch | `project_path:mygroup/myproject:ref_type:branch:ref:main` | | Project + tag | `project_path:mygroup/myproject:ref_type:tag:ref:v1.0.0` | A GitLab CI token also contains additional claims such as `project_path`, `ref`, `ref_type`, `pipeline_source`, `job_project_path`, and `user_login`. The opkssh server policy usually authorizes the `sub` claim, but you can inspect the generated certificate to confirm the exact identity: ```bash opkssh inspect /root/.ssh/id_ecdsa-cert.pub ``` Look for: ```text Subject: project_path:cgroschupp/opkssh-test:ref_type:branch:ref:main Issuer: https://gitlab.com ``` ## Troubleshooting **Login fails in GitLab CI** Make sure the job defines `id_tokens.OPENPUBKEY_JWT` and runs `opkssh login gitlab-ci` inside GitLab CI. The environment must contain `GITLAB_CI=true` and `OPENPUBKEY_JWT`. For self-managed GitLab, also set `CI_SERVER_URL` to the issuer URL used by your GitLab instance. **SSH connection rejected** Check the server policy and provider configuration: ```bash sudo cat /etc/opk/auth_id sudo cat /etc/opk/providers sudo opkssh audit ``` Verify that the identity in `/etc/opk/auth_id` matches the `Subject` shown by `opkssh inspect`. **Audience mismatch** If the token audience is `OPENPUBKEY-PKTOKEN:GITLAB-CI:ssh-deploy-prod`, add a matching provider line: ```text https://gitlab.com OPENPUBKEY-PKTOKEN:GITLAB-CI:ssh-deploy-prod oidc ``` **Normal GitLab login works but GitLab CI fails** Normal GitLab logins use the browser/OIDC nonce flow. GitLab CI uses GQ-bound tokens (`GQ256`). Ensure the server has a GitLab CI provider line with an explicit `OPENPUBKEY-PKTOKEN:GITLAB-CI:*` audience in `/etc/opk/providers`. The GitLab CI token must also include the required CI claims: `ci_config_ref_uri`, `job_id`, `job_project_path`, and `pipeline_id`. **Verify fails during SSH authentication** Check the opkssh server log: ```bash sudo tail -n 200 /var/log/opkssh.log ``` You can also test verification manually on the server: ```bash typ=$(awk '{print $1}' /root/.ssh/id_ecdsa-cert.pub) cert=$(awk '{print $2}' /root/.ssh/id_ecdsa-cert.pub) sudo -u opksshuser /usr/local/bin/opkssh verify root "$cert" "$typ" ```