-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Jenkins Administrator's Guide
By :

TLS enables HTTPS to encrypt the message between two parties, but the message needs to be decrypted in the end so that each party can read it. In Jenkins, there are two places where the message can be decrypted, or in industry lingo, where the TLS is terminated.
A popular configuration is to set up a reverse proxy in front of Jenkins and terminate the TLS at the reverse proxy:
80
, the reverse proxy redirects the traffic to HTTPS port 443
.443
, the reverse proxy decrypts the message and forwards it to the Jenkins controller on HTTP port 8080
.8080
.This is what the configuration looks like:
Figure 2.7 – Architecture of Jenkins with reverse proxy
There are several benefits to this configuration:
There are also drawbacks:
Let's examine the steps for configuring a reverse proxy for each Jenkins controller.
Setting up an ELB for Jenkins is largely a three-part exercise:
8080
/
login
path80
, which is redirected to port 443
443
, which is forwarded to the target group from step 2Here are the actual steps that we can follow:
jenkins
. Add two listeners, HTTP and HTTPS. Choose the availability zones for the ELB and click Next.Figure 2.8 – Choose the first option and click the link below to create a new certificate from ACM
default
and jenkins-elb
, which we made in Chapter 1, Jenkins Infrastructure with TLS/SSL and Reverse Proxy. Click Next.jenkins-controller-8080
and change the Port from 80
to 8080
so that it points to the Jenkins controller running on port 8080
. Set the Health checks Path to /login
. The default /
fails the health check because unauthenticated access returns 403 authentication error,
whereas /login
always succeeds as long as Jenkins is running. Click Next.jenkins
load balancer, click the Listeners tab, and then edit HTTP : 80. Delete the current rule of forwarding to jenkins-controller-8080
and replace it with a Redirect to HTTPS at port 443
, so that the traffic to HTTP is redirected to HTTPS.
This is what we should see on the Application ELB Listeners configuration:
Figure 2.9 – Application ELB Listener configuration
jenkins-1394494507.us-west-2.elb.amazonaws.com
) on HTTP or HTTPS to see that Jenkins loads with a TLS certificate error. You can find the load balancer's URL at EC2 Dashboard | Load Balancing | Load Balancers | jenkins
| Description | DNS name:
Figure 2.10 – AWS Jenkins on the load balancer URL showing a TLS certificate error
jenkins-aws.lvin.ca
) and point it to the ELB. Once the DNS cache refreshes, we can go to the Jenkins URL on HTTP or HTTPS and see it load without an error:Figure 2.11 – AWS Jenkins on the Jenkins URL showing a valid TLS certificate
AWS Jenkins with ELB is up and running using HTTPS.
Let's create an NGINX Docker container on the firewalled Jenkins controller host that acts as the reverse proxy for the Jenkins controller Docker container:
~/nginx/certs/
directory.
robot_acct@firewalled-controller:~$ mkdir -p nginx/certs/
robot_acct@firewalled-controller:~$ cp ~/letsencrypt/certs/live/jenkins-firewalled
.lvin.ca/{fullchain.pem,privkey.pem} ~/nginx/certs/
~/nginx/nginx.conf
NGINX configuration file as seen in the following snippet. Replace the domains in the two server/server_name
sections with your own domains. This file can be downloaded from the book's GitHub repository (https://github.com/PacktPublishing/Jenkins-Administrators-Guide/blob/main/ch2/nginx.conf):
~/nginx/nginx.conf
server {
listen 80;
server_name jenkins-firewalled.lvin.ca;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name jenkins-firewalled.lvin.ca;
ssl_certificate /certs/fullchain.pem;
ssl_certificate_key /certs/privkey.pem;
location / {
proxy_pass http://localhost:8080;
proxy_http_version 1.1;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Max upload size. Useful for a custom plugin.
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_buffering off;
proxy_request_buffering off;
proxy_set_header Connection "";
}
}
robot_acct@firewalled-controller:~$ docker run \
--detach \
--restart on-failure \
-v ~/nginx/certs:/certs:ro \
-v ~/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro \
--network=host \
--name nginx \
nginx
There are a few flags in this command. Let's examine what they mean:
--detach
: Run in the background.--restart on-failure
: Automatically restart if the container crashes or the machine reboots.-v ~/nginx/certs:/certs:ro
: Bind mount the host ~/nginx/certs
directory to /certs
inside the container so that the TLS certificates are available to the NGINX process. Mount as read-only because NGINX doesn't need to modify them.-v ~/nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro
: Bind mount the host ~/nginx/nginx.conf
file to /etc/nginx/conf.d/default.conf
inside the container so that the configuration file is available to the NGINX process. Mount as read-only because NGINX doesn't need to modify it.--network=host
: Allow the container to have direct access to the host's network so that it can manage the traffic for the Jenkins controller container.--name nginx
: Name the running container nginx
for a memorable name and easier access.192.168.1.16
) on HTTP or HTTPS to see that Jenkins loads with a TLS certificate error:
Figure 2.12 – Firewalled Jenkins on a reverse proxy IP showing a TLS certificate error
jenkins-firewalled.lvin.ca
) and point it to the reverse proxy. Once the DNS cache refreshes, we can go to the Jenkins URL on either HTTP or HTTPS and see it load without an error:
Figure 2.13 – Firewalled Jenkins on the Jenkins URL showing a valid TLS certificate
Firewalled Jenkins with an NGINX reverse proxy is up and running using HTTPS.
It is also possible to terminate the TLS directly on the Jenkins controller instead of using a reverse proxy. This reduces the amount of infrastructure to manage; however, it is suitable only in a trusted environment for a small-scale Jenkins instance, due to the lack of protection and scaling that a reverse proxy provides. It also lacks the HTTP to HTTPS redirect, as illustrated in the following figure:
Figure 2.14 – Architecture of Jenkins terminating the TLS certificate directly on the controller
Let's begin:
~/certs/
directory:
robot_acct@firewalled-controller:~$ mkdir ~/certs/
robot_acct@firewalled-controller:~$ cp ~/letsencrypt/certs/live/jenkins-firewalled.lvin.ca/{fullchain.pem,privkey.pem} ~/certs/
robot_acct@firewalled-controller:~$ openssl rsa \
-in ~/certs/privkey.pem \
-out ~/certs/privkey.pkcs1.pem
robot_acct@firewalled-controller:~$ docker run \
--detach \
--restart on-failure \
-u $(id -u):$(id -g) \
-v ~/jenkins_home:/var/jenkins_home \
-v ~/certs:/certs:ro \
-e JENKINS_OPTS="
--httpsPort=443
--httpsCertificate=/certs/fullchain.pem
--httpsPrivateKey=/certs/privkey.pkcs1.pem" \
-p 443:443 -p 50000:50000 \
--name jenkins_controller \
calvinpark/jenkins:2.263.1-lts
There are three changes:
-v ~/certs
:/certs:ro
~/certs
host directory to /certs
inside the container so that the certificates are available to Jenkins. Mount as read-only because Jenkins doesn't need to modify it.-e JENK
INS_OPTS="
--htt
psPort=443
--httpsCertificate=/certs/ful
lchain.pem
--httpsPrivateKey=/certs/privkey.
pkcs1.pem"
-p 443:443
443
on the host to HTTPS port 443
inside the container so that Jenkins is accessible on HTTPS.192.168.1.16
) on HTTPS to see that Jenkins loads with a TLS certificate error. See Figure 2.10.jenkins-firewalled.lvin.ca
) and point it to the controller host's IP. Once the DNS cache refreshes, we can go to the Jenkins URL on HTTPS and see it load without an error. See Figure 2.11.Figure 2.15 – Accessing Jenkins using HTTP fails without a reverse proxy
Firewalled Jenkins without a reverse proxy is up and running using HTTPS.
Change the font size
Change margin width
Change background colour