How To Force SSL for Tomcat with AWS ELB in Front

The problem

You have an awesome Java app that is growing like crazy and you need to be on top of it. You will start spawning servers to scale horizontally and putting a reliable balancer in front. AWS ELB is a good one but it will not solve all your needs out of the box. You need to tweak it a little bit to fit your needs.

Your app is secure, you have a SSL certificate installed but the problem is how do I redirect or force all HTTP traffic to HTTPS ?

The approach

Put an NGINX in each Tomcat instance. You will say.. another webserver ? yes, another one. Another point of failure but a very reliable one. Nginx is super reliable and has the smallest footprint I ever seen in a serious web server. (NodeJS is not a serious one, that is why people puts NGINX in front of it)

NGINX Config

NGINX will rewrite all requests to the ELB calling the HTTPS port utilizing status 301.

server {
  listen 80;
  server_name myhost.com;
  # add ssl settings
  return 301 https://myhost.com$request_uri;
}

Tomcat config

Now you need to touch the server.xml configuration of Tomcat (located @ $TOMCAT/conf/server.xml) .

<Connector scheme="https" secure="true" proxyPort="443"
  port="8080" protocol="HTTP/1.1"
  connectionTimeout="25000"
  URIEncoding="UTF-8"
  redirectPort="8443" />

Amazon Elastic Load Balancer

You are not done yet. You have to configure in the AWS ELB the following listeners.

 HTTP 80 -> HTTP 80 (nginx)
 HTTPS 443 -> HTTP 8080 (tomcat)

I hope it works for you. It did for me.

Read More

How to Copy all the Jenkins Jobs programmatically

We are moving our projects from dev to qa and production. It is kind of painful to redo all jobs in jenkins or copy them individually. So using the scripting capabilities of Jenkins (groovy) we can copy the jobs with a new name and move them if you want to another view.

import hudson.model.*

def viewName = "product-build-dev"
def search = "-dev"
def replace = "-prod"

def view = Hudson.instance.getView(viewName)

/* now you copy all jobs of the view copy all projects of a view */
for(item in view.getItems()) {

  /* create the new project name */
  newName = item.getName().replace(search , replace)

  /* now copy the job */
  def job = Hudson.instance.copy(item, newName)
  job.save()

}

Read More

Track Functionality Usage With Splunk

I have been playing lately with Splunk and let me tell you… WOW! Awesome tool. You can have N servers forwarding logs to a main server where you can search using SPL to query all over those inputs.

Here I will demostrate how to make a simple stat usage of your website that you can aggregate later with Splunk to check how your users are using your stuff.

First you have to change the way you log. Splunk likes eating key=values all over the place. So feed them


action=user_searching_stuff , age=Some , email=some@email.com , gender=M... etc

Now that you know how to log lets imagine a scenario like this. You have a screen with a few filters and you want to know what filters are used the most. So your log will be something like this..

User searching by email. Only email field was filled.


action=user_searching_stuff , name= , email=some@email.com , gender= , ... etc

User searching by gender. Only gender field was filled.


action=user_searching_stuff , name= , email= , gender=M , ... etc

So after a while of users using it you come up with the following Splunk search string.


* index="test" statistic "action=user_search_stuff" | stats count(eval(name!="")) as name, count(eval(email!="")) as email, count(eval(gender!="")) as gender

This will give you a table with counts per event per this user_searching_stuff action. Super useful info to keep track of how your users use your product.

Read More

How To Use GSON To Parse Dates for MongoDB

Working with MongoDB is really cool. Is easy to setup and use any API in pretty much any programming lang out there. When you work in Java like me you will have a few challenges. First of all what I hate from Java and love from NodeJS is the dynamic creation of objects using directly JSON. This is also a nice ability of C#. Now when you need to serialize java objects to JSON to transform into data objects for the Mongo driver you will find GSON (google json parser) really useful but one of the problems will be that one of the types will be transformed to string, not to the type you really want inside Mongo. Dates.

So the solution for this case if you don’t use Spring Data Mongo or Morphia is to create and register a GSON converter with this code.

	
private static final String MONGO_UTC_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";

public JsonElement serialize(Date src, Type type, JsonSerializationContext context) {
  if (src == null) {
    return null;
  } else {
    SimpleDateFormat format = new SimpleDateFormat(MONGO_DATE_FORMAT);
    JsonObject jo = new JsonObject();
    jo.addProperty("$date", format.format(src));
    return jo;
  }
}
@Override
public Date deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException {
  Date date = null;
  SimpleDateFormat format = new SimpleDateFormat(MONGO_DATE_FORMAT);
    try {
      date = format.parse(json.getAsJsonObject().get("$date").getAsString());
    } catch (ParseException e) {
      date = null;
    }
  return date;
}

Read More

How to mount a serial USB in linux

First of all you have to check if the device is connected to the system

lsusb

The output will be something like

Bus 002 Device 006: ID 046d:c05a Logitech, Inc. M90/M100 Optical Mouse
Bus 002 Device 005: ID 046d:081b Logitech, Inc. Webcam C310
Bus 002 Device 004: ID 046d:0a0b Logitech, Inc. ClearChat Pro USB
Bus 002 Device 003: ID 413c:2513 Dell Computer Corp. internal USB Hub of E-Port Replicator
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 004: ID 0c45:643f Microdia 
Bus 001 Device 003: ID 413c:8197 Dell Computer Corp. 
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 007: ID 1608:0240 Inside Out Networks [hex] Edgeport/1
Bus 003 Device 006: ID 413c:2011 Dell Computer Corp. Multimedia Pro Keyboard
Bus 003 Device 004: ID 413c:1005 Dell Computer Corp. Multimedia Pro Keyboard Hub
Bus 003 Device 003: ID 413c:2513 Dell Computer Corp. internal USB Hub of E-Port Replicator
Bus 003 Device 002: ID 046d:c52f Logitech, Inc. Unifying Receiver
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

So, search for the device you know is the usb to serial. In my case..

Bus 003 Device 007: ID 1608:0240 Inside Out Networks [hex] Edgeport/1

Now load the kernel module using this command (check that the vendor and product are taken from the ID in the previous line)

sudo modprobe usbserial vendor=0x1608 product=0x0240

You are good to go! Use minicom or some software like that to connect to the serial device.

Read More