How to authenticate with a certificate

Mike Nöthiger
1 min readJun 25, 2019

You want to authenticate with a certificate rather than password (which is highly recommended). You can do this with the identity_file option of ssh (see man pages, search for identity_file). Following is what you need to do:

  1. Copy the pem file to the CI/CD environment: Since GitLab 11.11 this can be achieved with “File” variable types. When creating a File variable, GitLab will create a file with the contents of your variable and store the path to this file in an environment variable named after your CI/CD variable key.
  2. Set permissions on the pem file correctly (otherwise ssh will discard it, see this post). Add this to your deploy job’s script section, before running the ssh commands (it will remove all permissions on “group” and “others”): chmod og= $SERVER_PASSWORD ($SERVER_PASSWORD being your CI/CD variable key that holds the contents of your pem file)
  3. Run ssh with the identity_file option: ssh -i $SERVER_PASSWORD -o StrictHostKeyChecking=no $SERVER_USER@$SERVER_IP “command to execute on remote”

Here’s an example .yml configuration for a deploy job:

deploy:
stage: deploy
script:
- apk upgrade && apk update
- apk add openssh-client
- ssh -i "$STAGING_SERVER_PASSWORD" -o StrictHostKeyChecking=no $STAGING_SERVER_USER@$STAGING_SERVER docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
environment:
name: staging
url: https://mathflake.com
only:
- develop

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Mike Nöthiger
Mike Nöthiger

Written by Mike Nöthiger

Hi! 👋 I’m Mike — did you know the oldest computer was owned by Adam and Eve? It was an apple with very limited memory. Just one byte and everything crashed.

Responses (1)

Write a response