Archive for February, 2012
Quick! create a self-signed SSL certificate in Ubuntu
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!
Node.js and RESTify Server with both HTTP and HTTPS
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);
});
How to install Node.js on Ubuntu – in under 5 minutes
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
CSV Escaping Double Quotes
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!
