Come usare Jenkins con SSL / https


39

Ho un server Fedora con Jenkins che installo tramite yum. Va tutto bene, posso accedervi con http://ci.mydomain.com.

Ma ora, voglio accedervi con https://ci.mydomain.comquindi l'accesso con nome utente e password è crittografato.

Come posso fare questo?

Quello che segue è il mio /etc/sysconfig/jenkinsfile. L'avvio di Jenkins funziona, ma non riesco ad accedere a Jenkins con il browser web con https://ci.mydomain.como http://ci.mydomain.com:443, ...

## Path:        Development/Jenkins
## Description: Configuration for the Jenkins continuous build server
## Type:        string
## Default:     "/var/lib/jenkins"
## ServiceRestart: jenkins
#
# Directory where Jenkins store its configuration and working
# files (checkouts, build reports, artifacts, ...).
#
JENKINS_HOME="/var/lib/jenkins"

## Type:        string
## Default:     ""
## ServiceRestart: jenkins
#
# Java executable to run Jenkins
# When left empty, we'll try to find the suitable Java.
#
JENKINS_JAVA_CMD=""

## Type:        string
## Default:     "jenkins"
## ServiceRestart: jenkins
#
# Unix user account that runs the Jenkins daemon
# Be careful when you change this, as you need to update
# permissions of $JENKINS_HOME and /var/log/jenkins.
#
JENKINS_USER="jenkins"

## Type:        string
## Default:     "-Djava.awt.headless=true"
## ServiceRestart: jenkins
#
# Options to pass to java when running Jenkins.
#
JENKINS_JAVA_OPTIONS="-Djava.awt.headless=true"

## Type:        integer(0:65535)
## Default:     8080
## ServiceRestart: jenkins
#
# Port Jenkins is listening on.
#
JENKINS_PORT="8080"

## Type:        integer(1:9)
## Default:     5
## ServiceRestart: jenkins
#
# Debug level for logs -- the higher the value, the more verbose.
# 5 is INFO.
#
JENKINS_DEBUG_LEVEL="5"

## Type:        yesno
## Default:     no
## ServiceRestart: jenkins
#
# Whether to enable access logging or not.
#
JENKINS_ENABLE_ACCESS_LOG="no"

## Type:        integer
## Default:     100
## ServiceRestart: jenkins
#
# Maximum number of HTTP worker threads.
#
JENKINS_HANDLER_MAX="100"

## Type:        integer
## Default:     20
## ServiceRestart: jenkins
#
# Maximum number of idle HTTP worker threads.
#
JENKINS_HANDLER_IDLE="20"

## Type:        string
## Default:     ""
## ServiceRestart: jenkins
#
# Pass arbitrary arguments to Jenkins.
# Full option list: java -jar jenkins.war --help
#
JENKINS_ARGS="--httpsPort=443 --httpsKeyStore=/root/.keystore --httpsKeyStorePassword=MYPASSWORD"

è possibile utilizzare authbind per utilizzare qualsiasi porta inferiore a 1000 ed eseguire jenkins come non root.

Risposte:


17

Questa pagina dovrebbe aiutarti a configurarlo dietro Apache (che gestirà HTTPS): https://wiki.eclipse.org/Hudson-ci/Running_Hudson_behind_Apache

Oltre ad essere un proxy inverso "normale", avrai bisogno di questo (come mostrato in quella pagina):

Header edit Location ^http://www.example.com/hudson/ https://www.example.com/hudson/

2
Grazie per la risposta. Non ho Apache attivo e funzionante, ho solo il server Linux con Jenkins.
— Tim

3
In questo caso, crea un keystore con il tuo certificato e utilizza httpsPort(e i relativi parametri): groups.google.com/group/jenkinsci-users/browse_thread/thread/…
— Bruno

Ok, ho il mio certificato aggiunto nel keystore. Ma cosa dovrei chiamare adesso? Dove dovrei farlo? In any case: if I put only --httpsPort=8443 or i put --httpsKeyStore=/ path/to/keystore --httpsKeyStorePassword=myPassowrd in my HUDSON_ARGS?
— Tim

Inserisci tutti i parametri richiesti (porta, posizione negozio e password). Quindi, avvia Jenkins e punta il tuo browser su http://yourhostname:8443/.
— Bruno,

1
@ Umesh.ABhat Ora dovrebbe essere risolto.
— Bruno,

21

Nel caso in cui tu stia utilizzando Nginx e non Apache, potresti voler utilizzare proxy_redirect http:// https://;per riscrivere l'intestazione Location quando la risposta ritorna da Jenkins.

Una configurazione nginx completa in cui SSL viene terminato con Nginx e inviato in proxy internamente a Jenkins usando 8080 potrebbe essere simile al seguente:

upstream jenkins {
  server 127.0.0.1:8080 fail_timeout=0;
}

server {
  listen 80 default;
  server_name 127.0.0.1 *.mydomain.com;
  rewrite ^ https://$server_name$request_uri? permanent;
}

server {
  listen 443 default ssl;
  server_name 127.0.0.1 *.mydomain.com;

  ssl_certificate           /etc/ssl/certs/my.crt;
  ssl_certificate_key       /etc/ssl/private/my.key;

  ssl_session_timeout  5m;
  ssl_protocols  SSLv3 TLSv1;
  ssl_ciphers HIGH:!ADH:!MD5;
  ssl_prefer_server_ciphers on;

  # auth_basic            "Restricted";
  # auth_basic_user_file  /home/jenkins/htpasswd;

  location / {
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_redirect http:// https://;

    add_header Pragma "no-cache";

    proxy_pass http://jenkins;
  }
}

14

Nota che (a partire da qualche tempo?) Jenkins può generare la chiave per te, tutto ciò che devi fare è impostare il --httpsPort=(portnum)parametro in JENKINS_ARGS.

Nel mio caso ho impostato JENKINS_PORT="-1"(disabilita http) e impostato --httpsPort=8080che ha funzionato bene per i miei scopi.

Basta notare che qualsiasi porta inferiore a 1000 richiede generalmente l'accesso come root, quindi scegli una porta più alta di quella ...

( Link per maggiori informazioni)


3
wiki.jenkins-ci.org/display/JENKINS/… è la documentazione ufficiale per questo, tra l'altro.
— Jesse Glick,

2
Sarebbe bello se quella pagina avesse qualche menzione sulla generazione della propria chiave - sfortunatamente devi inferire questa capacità notando che l'utilizzo di un "certificato esistente" richiede passaggi diversi rispetto al valore predefinito (che utilizza la propria auto-generata)
— Adam Rofer,

1
Attenzione: è abbastanza facile per le chiavi auto-generate; tuttavia, ho provato a usare quelle istruzioni con un vero certificato e impostare il keystore è stato un grosso problema (dal momento che i keystore hanno due password e gli strumenti standard non sono davvero trasparenti al riguardo).
— Blaisorblade,

1
Nota: questo non funziona con OpenJDK, solo con Oracle JRE, perché si basa susun.security.x509.CertAndKeyGen . Inoltre, è stato rotto con Java 8 fino a poco tempo fa (risolto Jenkins 2.38). Peggio ancora, dice il log delle modifiche per quella versione This option is deprecated and will be removed in a future release. We strongly recommend you create self-signed certificates yourself and use --httpsKeyStore.
— nh2,

9

Per un server Ubuntu (supponendo che tu abbia installato con apt-get install jenkins):

Ti consigliamo di modificare /etc/default/jenkinsnella parte inferiore del file, modificare Jenkins_args. Nelle mie discussioni, ho disabilitato l'accesso http (usando -1) e ho messo SSL sulla porta Jenkins predefinita (8080). La parte più importante qui è che hai inviato un httpsPort e un certificato / chiave (se ne hai uno, altrimenti puoi lasciarli disattivati ​​per quello auto-generato). Metto le crts in apache e poi le uso per entrambi, ma potresti metterle ovunque.

JENKINS_ARGS="--webroot=/var/cache/jenkins/war --httpsPort=$HTTP_PORT --httpPort=-1 --httpsCertificate=/etc/apache2/ssl.crt/CERT.crt --httpsPrivateKey=/etc/apache2/ssl.key/KEY.key --ajp13Port=$AJP_PORT"

In alcuni casi, dovrai utilizzare un archivio chiavi Java. Innanzitutto, converti le tue chiavi:

openssl pkcs12 -inkey /var/lib/jenkins/jenkins.key.pem -in /var/lib/jenkins/jenkins.crt.pem  -export -out keys.pkcs12

keytool -importkeystore -srckeystore keys.pkcs12 -srcstoretype pkcs12 -destkeystore jenkins.jks

Ora usa Jenkins sostiene

JENKINS_ARGS="--webroot=/var/cache/$NAME/war --httpsPort=$HTTP_PORT --httpPort=-1 --httpsKeyStore=/etc/apache2/ssl.crt/jenkins.jks --httpsKeyStorePassword=thePassword --ajp13Port=$AJP_PORT"

Inoltre, consultare https://serverfault.com/a/569898/300544


1
Assicurarsi che la password di esportazione fornita opensslcorrisponda alla "password del keystore di origine" richiesta da keytool. Inoltre, la password non può essere vuota.
— vescovo,
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.