41
gitlab-ci-multi-runner register

gave me

couldn't execute POST against https://xxxx/ci/api/v1/runners/register.json:
Post https://xxxx/ci/api/v1/runners/register.json: 
x509: cannot validate certificate for xxxx because it doesn't contain any IP SANs

Is there a way to disable certification validation?

I'm using Gitlab 8.13.1 and gitlab-ci-multi-runner 1.11.2.

Etienne Gautier
  • 2,901
  • 4
  • 26
  • 35

6 Answers6

72

Based on Wassim's answer, and gitlab documentation about tls-self-signed and custom CA-signed certificates, here's to save some time if you're not the admin of the gitlab server but just of the server with the runners (and if the runner is run as root):

SERVER=gitlab.example.com
PORT=443
CERTIFICATE=/etc/gitlab-runner/certs/${SERVER}.crt

# Create the certificates hierarchy expected by gitlab
sudo mkdir -p $(dirname "$CERTIFICATE")

# Get the certificate in PEM format and store it
openssl s_client -connect ${SERVER}:${PORT} -showcerts </dev/null 2>/dev/null | sed -e '/-----BEGIN/,/-----END/!d' | sudo tee "$CERTIFICATE" >/dev/null

# Register your runner
gitlab-runner register --tls-ca-file="$CERTIFICATE" [your other options]

Update 1: CERTIFICATE must be an absolute path to the certificate file.

Update 2: it might still fail with custom CA-signed because of gitlab-runner bug #2675

liberforce
  • 11,189
  • 37
  • 48
  • 2
    This should be the accepted answer IMHO. This is way better than recreating the certificate/dhparam and editing server configuration files. – MemphiZ Sep 28 '18 at 09:26
  • This solution also works for Windows: simply get the certificate from the server, place it in the same directory as the runner exe and add it in config.toml as "tls-ca-file" under "[[runners]]". – MemphiZ Sep 28 '18 at 09:27
  • What is the PORT=443? – Majid Rajabi Jan 14 '19 at 11:34
  • To contact your server, you need its hostname and port. Here those are just examples. Port 443 is the usual port where a web server listens for HTTPS conections. – liberforce Jan 16 '19 at 12:33
  • 1
    This is the best answer I've encoured. – Paza Aug 13 '19 at 06:35
  • i ran in to this ` x509: certificate signed by unknown authority` error, but oddly just sudo'ed during register and got past the error! `sudo gitlab-runner register ...`. – pangyuteng Apr 29 '20 at 05:43
  • I followed all steps described above using openssl and also tried to generate ,pem flle but still getting this error ERROR: Registering runner... failed runner=VxUKPa5q status=couldn't execute POST against https://172.16.53.241/api/v4/runners: Post "https://172.16.53.241/api/v4/runners": x509: certificate has expired or is not yet valid: current time 2022-02-13T11:58:12-05:00 is after 2022-01-28T15:25:11Z PANIC: Failed to register the runner. You may be having network problems. – Alex Reichman Feb 13 '22 at 17:03
20

In my case I got it working by adding the path to the .pem file as following:

sudo gitlab-runner register --tls-ca-file /my/path/gitlab/gitlab.myserver.com.pem

Often, gitlab-runners are hosted in a docker container. In that case, one needs to make sure that the tls-ca-file is available in the container.

MichaelHuelsen
  • 386
  • 1
  • 4
  • 11
Luiz Dias
  • 1,947
  • 17
  • 22
  • 2
    IMO the correct answer. The only explanation I judge important is on how to obtain a .PEM (or .CRT) file for the server. I did it by visiting my GitLab instance URL on Firefox, then clicked on the lock icon at the address bar, navigated to "More Information" about the connection, "Security", "View Certificate", "Details", clicked at the GitLab line at "Certificate Hierarchy", and then "Export"ed the certificate as a PEM file. – Rui Pimentel Apr 26 '19 at 14:49
  • 2
    You're a life saver! – Alex May 08 '19 at 14:08
  • 1
    This worked for me, with a small tweak. I had to make sure to use the domain name that was in the cert when registering the runner, rather than the IP address alone, meaning `https://gitlab.local.com` vs `https://10.0.0.10`, even though gitlab's runner registration page listed the IP based URL. Hopefully this will be helpful to someone! – copeland3300 Feb 21 '20 at 05:52
8

Ok I followed step by step this post http://moonlightbox.logdown.com/posts/2016/09/12/gitlab-ci-runner-register-x509-error and then it worked like a charm. To prevent dead link I copy the steps below:

First edit ssl configuration on the GitLab server (not the runner)

vim /etc/pki/tls/openssl.cnf

[ v3_ca ]
subjectAltName=IP:192.168.1.1 <---- Add this line. 192.168.1.1 is your GitLab server IP.

Re-generate self-signed certificate

cd /etc/gitlab/ssl
sudo openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -keyout /etc/gitlab/ssl/192.168.1.1.key -out /etc/gitlab/ssl/192.168.1.1.crt
sudo openssl dhparam -out /etc/gitlab/ssl/dhparam.pem 2048
sudo gitlab-ctl restart

Copy the new CA to the GitLab CI runner

scp /etc/gitlab/ssl/192.168.1.1.crt root@192.168.1.2:/etc/gitlab-runner/certs

Thanks @Moon Light @Wassim Dhif

Wassim Dhif
  • 1,278
  • 12
  • 21
Etienne Gautier
  • 2,901
  • 4
  • 26
  • 35
  • command line add subjectAltName ref : https://security.stackexchange.com/questions/74345/provide-subjectaltname-to-openssl-directly-on-the-command-line ie:`-extensions san -config <(echo '[req]'; echo 'distinguished_name=req';echo '[san]'; echo 'subjectAltName=IP:192.168.101.100') ` – qxo Jan 09 '19 at 16:43
7

In my setup the following the following worked as well. It's just important that IP/Name used for creating certificate matches IP/Name used for registering the runner.

gitlab-runner register --tls-ca-file /my/path/gitlab/gitlab.myserver.com.pem

Furthermore, it could be necessary to add a line for hostname lookup to the runners config.toml file also (section [runners.docker]): extra_hosts = ["git.domain.com:192.168.99.100"] see also https://gitlab.com/gitlab-org/gitlab-runner/issues/2209

In addition, there could be some network-trouble if for gitlab/gitlab-runner network-mode host is used, it has to be added to the config.toml as well, as it starts additional containers, which otherwise could have a problem to connect to the gitlab-host ((section [runners.docker]): network_mode="host"

Finally, there might be an issue with the self-signed SSL-Cert (https://gitlab.com/gitlab-org/gitlab-runner/issues/2659). A dirty workaround is to add environment = ["GIT_SSL_NO_VERIFY=true"] to the [[runners]] section.

jfreundo
  • 91
  • 1
  • 3
7

The following steps worked in my environment. (Ubuntu)

Download certificate
I did not have access to the gitlab server. Therefore,

  1. Open https://some-host-gitlab.com in browser (I use chrome).
  2. View site information, usually a green lock in URL bar.
  3. Download/Export certificate by navigating to certificate information(chrome, firefox has this option)

In gitlab-runner host

  1. Rename the downloaded certificate with .crt

    $ mv some-host-gitlab.com some-host-gitlab.com.crt

  2. Register the runner now with this file

    $ sudo gitlab-runner register --tls-ca-file /path/to/some-host-gitlab.com.crt

I was able to register runner to a project.

ChethanSuresh
  • 123
  • 1
  • 6
6

Currently there is no possibility to run the multi runner with an insecure ssl option.

There is currently an open issue at GitLab about that.

Still you should be able to get your certificate, make it a PEM file and give it to the runner command using --tls-ca-file

To craft the PEM file use openssl.
openssl x509 -in mycert.crt -out mycert.pem -outform PEM

Wassim Dhif
  • 1,278
  • 12
  • 21
  • Thanks @wassim Dhif I also found this https://github.com/ayufan/gitlab-ci-multi-runner/blob/master/docs/configuration/tls-self-signed.md but still stuck.. – Etienne Gautier Jun 09 '17 at 15:17