Tag: https

Jenkins behind Apache with HTTPS – Proxy pass with SSL

Jenkins in an awesome integration server, that can be used for free. However, having it on a non-standard www port, without a SSL, might be a problem. Accessing it from a public network might create a security threat. If you've got a Apache in front of your server, you can easily provide a secured proxy to Jenkins.

To do so, you need to create a VirtualHost for Apache, which will contain both: Proxy and SSL. Also it would be wise, to redirect standard HTTP requests.

VirtualHost to redirect from HTTP to HTTPS

So, first lets create our HTTP VirtualHost and let's redirect it to HTTPS version:

<VirtualHost *:80>
  ServerName jenkins.my.domain
  ServerAlias www.jenkins.my.domain

  RewriteEngine on
  ReWriteCond %{SERVER_PORT} !^443$
  RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R,L]
</VirtualHost>

SSL keys

To get things started, we will need a key. To generate it, follow given steps (as root):

sudo su # or any other way to be a root
cd /etc/apache2/
mkdir ssl
cd ssl/
mkdir crt
mkdir key
openssl req -new -x509 -days 365 -keyout key/jenkins.key -out crt/jenkins.crt -nodes -subj  '/O=Jenkins/OU=Jenkins/CN=jenkins.my.domain'

Just remember to replace all the domain references from "jenkins.my.domain" to an appropriate one. After you execute the above commands, you should have a ssl key and ssl cert generated.

Installing Apache necessary mods

To create a SSL Proxy pass we need to install some Apache mods (still as a root):

a2enmod proxy
a2enmod proxy_http
a2enmod rewrite
a2enmod ssl

/etc/init.d/apache2 restart

HTTPS Jenkins Virtual Host

And finally, the virtual host for secured Jenkins proxy pass:

<VirtualHost *:443>
  ServerName jenkins.my.domain
  ServerAlias www.jenkins.my.domain

  SSLEngine On
  SSLCertificateFile    /etc/apache2/ssl/crt/jenkins.my.domain.crt
  SSLCertificateKeyFile /etc/apache2/ssl/key/jenkins.my.domain.key

  ProxyRequests     Off
  ProxyPass         /  http://localhost:8080/
  ProxyPassReverse  /  http://localhost:8080/
  ProxyPassReverse  /  http://my.jenkins.host/
  <Proxy http://localhost:8080/*>
    Order allow,deny
    Allow from all
  </Proxy>
  ProxyPreserveHost on
</VirtualHost>

Ruby 1.9.3 + OpenSSL::SSL::SSLError issue

Problem

OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3
read server certificate B: certificate verify failed
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:799:in
`connect'
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:799:in
`block in connect'
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/timeout.rb:68:in
`timeout'
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/timeout.rb:99:in
`timeout'
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:799:in
`connect'
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:755:in
`do_start'
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/net/http.rb:750:in
`start'
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/xmlrpc/client.rb:535:in
`do_rpc'
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/xmlrpc/client.rb:420:in
`call2'
from /usr/local/rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/xmlrpc/client.rb:410:in
`call'
from /path/infusionsoft/connection.rb:15:in
`connection'
from /path/infusionsoft/request.rb:29:in
`request'
from /path/infusionsoft/request.rb:5:in
`get'
from /path/infusionsoft/client/contact.rb:59:in
`contact_find_by_email'
from /path/infusionsoft.rb:18:in
`method_missing'
from (irb):2
from /path/rails/commands/console.rb:45:in
`start'
from /path/rails/commands/console.rb:8:in
`start'
from /path/rails/commands.rb:40:in
`<top (required)>'
from ./script/rails:6:in `require'
from ./script/rails:6:in

Solution

To work this around, we need to overwrite use_ssl method:

module Net
  class HTTP
    def use_ssl=(flag)
      flag = flag ? true : false
      if started? and @use_ssl != flag
        raise IOError, "use_ssl value changed, but session already started"
      end
      @use_ssl = flag
      if @use_ssl
        self.verify_mode = OpenSSL::SSL::VERIFY_NONE
      end
    end
  end
end

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑