Rss Feed Tweeter button Facebook button Technorati button Reddit button Linkedin button Webonews button Delicious button Digg button Flickr button Stumbleupon button Newsvine button

A Waage Blog

Ruby, Rails, Life

Quick! create a self-signed SSL certificate in Ubuntu

with 2 comments

Especially for testing SSL, I can never remember how to create a self-signed certificate. Here’s a quick and dirty guide to setting up a self-signed certificate. Obviously, not recommended for production setups :)

We will create the certificate in the “/etc/ssl/self-signed” directory

# Become root first !
$ mkdir /etc/ssl/self-signed && cd /etc/ssl/self-signed
$ openssl genrsa -des3 -out server.key 1024
$ openssl rsa -in server.key -out server.key.insecure
$ mv server.key server.key.secure && mv server.key.insecure server.key
$ openssl req -new -key server.key -out server.csr
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Now, you can use the files server.key and server.crt in your web server setups. See my post on Setting up Node.js with to accept HTTP and HTTPS connections for a quick example of using this SSL certificate with Node.js!

Written by Andrew Waage

February 27th, 2012 at 4:20 pm

Posted in linux, ubuntu

Node.js and RESTify Server with both HTTP and HTTPS

with 4 comments

Here’s a quick guide on setting up a Node.js server using the restify module that accepts BOTH http and https connections.

We want to re-use as much code as possible because the two servers (HTTP and HTTPS) are identical besides the protocol.

In the example, we created a self-signed SSL certificate. If you need help on that, check out this quick guide on how to set up a self-signed SSL certificate on Ubuntu.

var restify = require('restify');
var fs = require('fs');

// Setup some https server options
var https_options = {
  key: fs.readFileSync('/etc/ssl/self-signed/server.key'),
  certificate: fs.readFileSync('/etc/ssl/self-signed/server.crt')
};

// Instantiate our two servers
var server = restify.createServer();
var https_server = restify.createServer(https_options);

// Put any routing, response, etc. logic here. This allows us to define these functions
// only once, and it will be re-used on both the HTTP and HTTPs servers
var setup_server = function(app) {
        function respond(req, res, next) {
           res.send('I see you ' + req.params.name);
        }

        // Routes
        app.get('/test/:name', respond);
}

// Now, setup both servers in one step
setup_server(server);
setup_server(https_server);

// Start our servers to listen on the appropriate ports
server.listen(80, function() {
  console.log('%s listening at %s', server.name, server.url);
});

https_server.listen(443, function() {
  console.log('%s listening at %s', https_server.name, https_server.url);
});

Written by Andrew Waage

February 27th, 2012 at 4:05 pm

Posted in Node.js

How to install Node.js on Ubuntu – in under 5 minutes

with 9 comments

Follow along, as we create our first Node.js app using the “express” framework in under 5 minutes!
Note: We’re on Ubuntu 10.04 LTS (as root!)

# Update the system and install some necessary packages

$ apt-get update
$ apt-get install g++ curl libssl-dev apache2-utils
$ apt-get install git-core

# Find the latest package to install at nodejs.org (example uses 0.6.11)

$ wget http://nodejs.org/dist/v0.6.11/node-v0.6.11.tar.gz
$ tar xzf node-v0.6.11.tar.gz
$ cd node-v0.6.11
$ ./configure
$ make
$ make install

# Install npm (Node.js package manager)

$ cd /usr/local/src
$ git clone http://github.com/isaacs/npm.git
$ cd npm/
$ make install

# Install express framework, -g flag installs the express executable globally

$ npm install -g express

# Create an app with the express framework

$ express /var/www/foo
$ cd /var/www/foo/

# Install dependencies

$ npm install -d

# Start the node app!

$ node app.js

Voila! I told you it was fast :)

Written by Andrew Waage

February 23rd, 2012 at 10:35 pm

Posted in Node.js

Tagged with , , ,

CSV Escaping Double Quotes

with 3 comments

What is the proper way to escape double-quotes in CSV files?

When you try to export from MySQL in CSV format, the MySQL reference manual itself suggests using

FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'

Now, I thought I had a nicely formatted CSV file where fields that have double quotes would be properly escaped. Looking through this .csv file, I can see that sure enough, all the double-quotes are turned from ” into \”.

This is fine, except that when I tried to use Excel to open the CSV file or FasterCSV (a nice Ruby library for working with CSV files), I find that the double quotes were not escaped “properly”. They are expecting to escape double-quotes with TWO double-quotes (”"). According to CSVreader.com, this is a known discrepancy between the way different programs escape double-quotes in CSV files. Great.

Well, don’t worry – here’s a sed 1-liner that you can use on the command line to convert those \” (backslash double-quote) into “” (two double-quotes):

$cat file.csv | sed -E 's/\\"/""/g' > file2.csv

Hope that helps someone out there!

Written by Andrew Waage

February 16th, 2012 at 12:51 am

Ruby floats, BigDecimals and money (currency)

with 3 comments

Fellow Ruby-ers, please be warned!!! DO NOT use Ruby floats when performing arithmetic calculations involving money!

My calculations work in IRB, so I was really confused when I ran into this weird situation where (what I thought was) a simple arithmetic calculation led to strange results in my unit tests (I cannot stress the importance of good unit testing!).

My backend calculation was basically this (simplified):

# arbitrary amounts for these two variables
percentage = 12
total_in_cents = 400

discount = percentage.to_f / 100.0
total_in_float = total_in_cents.to_f * 100.0
new_price = (total_in_float * discount ).round / 100

Now, it’s pretty obvious that 12% of (400 cents) $4.00 should just be $0.48 (48 cents)
However, my barrage of unit tests kept producing strange results where a simple calculation was returning incorrect results. Doing some research, I discovered a series of articles worth reading including:

Also, check out the Money gem – I’ve never used it personally, but people have said good things about it.

Heeding the advice I found online, I re-wrote all my money-related calculations using BigDecimals instead of Floats.

percentage = 12
total_in_cents = 400
discount = BigDecimal(percentage.to_s) / 100
total_in_float = BigDecimal(total_in_cents.to_s) * 100
new_price = (total_in_float * discount ).to_i / 100

After switching over from Floats to BigDecimals, my unit tests all passed!
Lesson learned and hope this heads-up helps you guys too.

Summary:
Use BigDecimals for money calculations and remember to write good UNIT TESTS!!

Written by Andrew Waage

November 9th, 2011 at 5:29 pm