Thursday Mar 12, 2009

HTTPS Login for Hudson deployed in Sun Java System Webserver 7

I helped setup Hudson for my group's automated builds.  We want to use HTTPS for secure transmission of passwords when logging in, but plain HTTP is fine for use of the application itself.  Here is how we set this up in Sun Java System Webserver 7:

  1. Install a certificate to be used for HTTPS into SJSWS.
  2. Add a second http-listener in the SJSWS configuration, so it now listens on both port 80 for http and port 443 for https.
  3. Navigate to the Virtual Server config, Content Handling tab, URL Redirects subtab.
    Add the following two redirects:
    • Condition: $url =~ '^http://[^/]*/login'
      TargetURL: https://{server-hostname}
      Fixed URL: not checked
    • Condition: $url =~ '^https:' and $uri !~ '^/(login|j_acegi_security_check)'
      TargetURL: http://{server-hostname}
      Fixed URL: not checked
  4. Manually edit $SJSWS_HOME/admin-server/config-store/{config-name}/config/{server-name}-obj.conf
    Find this line: NameTrans fn="ntrans-j2ee" name="j2ee"
    Move this line just below the last "</If>" line for the URL redirects.
  5. Deploy the configuration changes

The manual edit is unfortunate, but the default setup hands off requests to J2EE webapps before processing redirects... so requests handled by Hudson would ignore these redirect rules. Moving that line down allows the redirects to work.
NOTE: if you ever add/edit any of these redirects using SJSWS admin console then you need to again manually edit this file and move that line. It will always add new/editing entries just below that line.

How the redirects work: When you visit Hudson it will be HTTP. When you click the login link the first redirect jumps you over to HTTPS. The second redirect rule allows HTTPS requests for 3 things:

  1. /login -- viewing of the login page
  2. /loginError -- any errors when logging in
  3. /j_acegi_security_check -- this is where the actual login request with username/password is POSTed
After successful login Hudson will take you to some page that doesn't match the patterns allowed for HTTPS so it jumps you back to plain HTTP.