Getting a free SSL certificate from Letsencrypt and configuring it on Nginx with automatic renewal

Finally Letsencrypt went to public beta and I really couldn’t wait to use it on my VPS (where this blog is hosted). Until few days ago I was using a free SSL certificate from StartSSL. The service is nice and I’m grateful to them for this important resource they are providing for free, but it must be said that their renewal procedure isn’t one of the most user friendly.

For people who don’t know the service yet, Letsencrypt not only gives free SSL certificates, they also provide a command line tool that people can use to request a new certificate or to renew an existing one. This means that you don’t have to worry anymore if/when your certificate expires, you can set a crontab command and have the certificate automatically renewed for you.

Client installation

To request a SSL certificate you need to install their command line utility. Unless it has already been packaged for your distribution, for the moment it’s much easier to get it from git as they explain in their installation instructions:

Getting the SSL certificate

There are a few different options available to request a certificate, but the easiest one is to use the –webroot option, specifying the document root of your website so that the client will be able to put there a verification (temporary) file that will be served to the remote service and used as verification method. In my case I only needed this command:

Please note that I had to specify both www.andreagrandi.it and andreagrandi.it as domains, otherwise it would have been invalid when requesting just andreagrandi.it resources.

Configuration files and certificates installation

The command above will save all the configuration under /etc/letsencrypt/ and all the generated certificates under /etc/letsencrypt/live/www.andreagrandi.it/*.pem (all the *.pem files here are symbolic links to the current certificate). If you are using Nginx the only files you need are fullchain.pem and privkey.pem and you can set them in your Nginx configuration using these two parameters:

In case you want to have a look at my full Nginx configuration file, as reference, you can find it here https://gist.github.com/andreagrandi/8b194c99cd3e77fdb5a8

Automatic renewal

The last thing to be configured is a crontab rule to call the script every… 2 months. Why 2 months? Letsencrypt SSL certificates expire in 3 months. Usually SSL certificates are valid at least for 1 year, but Letsencrypt decided to make it 3 months to incentivate the automation of the renewal. I set it to 2 months, so if anything goes wrong I still have plenty of time to do it manually. To edit crontab for root user execute crontab -e and add this line:

Just a final note. You may have noticed that this website presents an SSL certificate issued by COMODO. That’s because I have CloudFlare in front of my website and that’s how their SSL strict option works (at least for free plans).

 

Reversing a List in Python

Sometimes we need to reverse the order of the elements in a Python list. While there can be many different ways of implementing this task, I found three in particular that I appreciate for different reasons. Let’s define first a list of integers that we will reverse later.

List slicing

This method can be a bit obscure at first read, but basically it slices the whole list proceding in the reverse order:

Reversed method

We use the reversed method that returns an iterable object and a list comprehension to generate the new list

Swapping values in place

This last method is more verbose and it basically divides the list in two parts and swaps the first with sixth, the second with fifth, etc…

 

How to configure EncFS on OSX 10.10 (Yosemite)

With EncFS it’s possible to keep our data in almost any cloud (Dropbox, OneDrive, etc…), having a good level of privacy and security. Infact EncFS encrypt and decrypt our data automatically. It’s available for Linux as well and using a commercial solution (that is currently unsupported) even on Windows.

Installing EncFS

EncFS can be installed from brew. If you don’t have brew package manager installed on OSX you can install it using this command:

After brew, you need to install OSXFuse from this website http://osxfuse.github.io

Finally you can install encfs using this command:

Configuring the encrypted folder

Now that EncFS is installed, you can either mount an existing EncFS volume or create a new one. In both cases the command is the same:

If you are mounting an existing encrypted volume, you will be prompted for the password. If you are creating a new encrypted volume you will be asked some questions about EncFS parameters.

Note: if it’s important for you to keep compatibility with BoxCryptor Classic (in case you want to use the same volume under Windows), please refer to this other article I wrote: https://www.andreagrandi.it/2014/09/12/create-an-encfs-volume-compatible-with-boxcryptor-classic/

Mount the encrypted volume on startup

First of all you need to save the volume’s password inside the OSX keychain. Open the app Keychain Access and create a new entry with name encfs and account value encfs, then write your password and click Add:

encfs_keychain_access

Once the password is saved, open a text editor and paste this script and save it as encfs_mount.sh inside your $HOME folder:

Make it executable:

Open AppleScript editor and paste this text inside and save as an app in the $HOME folder:

Screenshot 2015-10-11 19.27.14

Finally, open “System Preferences” -> “Users & Groups” and add the previously saved application.

Screenshot 2015-10-11 19.27.44

Final notes

At this point encfs is configured to be mounted at startup and to save the encrypted files inside Dropbox. Please note: do not save anything directly on ~/Dropbox/Private, only read and save your files from ~/Private

References

Understanding Python decorators optimizing a recursive Fibonacci implementation

A decorator is a Python function that takes a function object as an argument and returns a function as a value. Here is an example of decorator definition:

To apply a decorator to an existing function, you just need to put @decorator_name in the line before its definition, like this example:

This decorator doesn’t do anything, so let’s think about a more concrete problem we could solve using decorators.

Fibonacci sequence

By definition, the first two numbers in the Fibonacci sequence are either 1 and 1 or 0 and 1. All the other numbers are the sum of the previous two numbers of the sequence. Example:

  1. 0, 1: the third number is 1
  2. 0, 1, 1: the fourth number is 2
  3. 0, 1, 1, 2: the fifth number is 3
  4. 0, 1, 1, 2, 3: the sixth number is 5
  5. etc…

If we wanted to give a math definition of the sequence, we could describe it in this way:

  • F(0): 0
  • F(1): 1
  • F(n): F(n-1) + F(n-2)

In Python we could have a recursive function like the following one:

What’s the problem with this implementation? The code works as expected, but it’s very inefficient. The next picture will explain what happens when we will try, for example, to calculate the 5th number of the sequence:

fibo

 

Fib(5) is Fib(4) + Fib(3), but Fib(4) itself is Fib(3) + Fib(2), and… the picture just tell us that we have calculated Fib(3) 2 times, Fib(2) 3 times, Fib(1) 5 times! Why are we repeating the same operation every time if we already calculated the result?

Memoization

In computing, memoization is an optimization technique used primarily to speed up computer programs by storing the results of expensive function calls and returning the cached result when the same inputs occur again.

We need to store values of the sequence we have already calculated and get them later when we need them. Let’s implement a simple memoization decorator:

The decorator defines a dict at the beginning that is used as a cache. When we want to find the n number of the sequence, it first checks if the value was already calculated and that value is returned instead of being calculated again. If the value is not found, then the original function is being called and then the value is store in the cache, then returned to the caller.

Using the memoize decorator

How much this decorator can speed up our fib method? Let’s try to benchmark the execution using Python timeit module.

The required time to calculate the 35th number of the Fibonacci sequence on my laptop is: 4.73480010033

The required time to calculate the 35th number of the Fibonacci sequence on my laptop is: 0.000133037567139

Quite faster, don’t you think? I will let you try how long does it take to calculate the 60th number of the sequence with and without using the decorator. Hint: grab a cup of coffee before beginning!

Django Notes: read values from settings in a safe way

Working on Django projects I find very often that many developers access the values that are defined in settings in this way

What happens if MY_SETTING has not been defined in settings.py? The code will raise an error and crash, of course. How can we make the code more reliable? We could try/except the code block that tries to read the value and maybe set a value if we get an exception, but this would not be a clean way to do this job.

A cleaner way to do it is to use getattr in this way:

getattr will try to read MY_SETTING value from settings.py, if the value doesn’t exist my_value will be assigned with my-default-value.