Marcos Placona Blog

Programming, technology and the taming of the web.

How to secure Apache with Let’s Encrypt and CloudFlare on Centos

Reading time: 4 – 7 minutes

I took it upon myself to converting a couple of my domains to use Let’s Encrypt in order to offer a secure connection to them. If you haven’t heard about Let’s Encrypt by now you’ have probably been living under a rock. If that’s the case though, have a read at this page and you’ll get up to speed with it.

Their getting started page describes the entire process of installation, but that didn’t really resonate with me. Upon some googling I found a great Digital Ocean article which made a lot more sense to me. That is an absolutely fine tutorial if you’re not using CloudFlare. If you came to this article from a Google search though, chances are you’re also using CloudFlare and are having issues like some of the following:

  • Failed authorization procedure
  • The following ‘urn:acme:error:unauthorized’ errors were reported by the server
  • urn:acme:error:unauthorized :: The client lacks sufficient authorization ::

Hopefully this article will show you how to get that nice green padlock showing on your website. Props to the article on Cloudflare’s support page that took me halfway the process.

Install the dependencies

I usually SSH to my server to get these things done, but this step may vary if you access your server differently.

On your terminal start by installing EPEL (Extra Packages for Enterprise Linux) repository:

Then install GIT. We will use it to get the latest version of the Let’s Encrypt Client.

Download and install Let’s Encrypt Client

Start off by cloning the repository and then saving it to /opt/letsencrypt. Feel free to save it elsewhere but /opt is a good location for third party packages.

Generate a new SSL certificate

This is where we go differently from the Digital Ocean article as we will generate our SSL certificate using the webroot option.

We’ve used the following flags for this setup.

  • –webroot-path is the directory on your server where your site is located. This is not your webserver’s root directory but your website’s
  • –renew-by-default selects renewal by default when domains are a superset of a previously attained cert
  • –email is the email used for registration and recovery contact.
  • –text displays text output
  • –agree-tos agrees to Let’s Encrypt’s Subscriber Agreement
  • -d specifies hostnames to add to the SAN. You can specify as many domains and subdomains as you need here as shown above

After you run that you should get a message saying your certificate chain has been saved.

letsencrypt

Apparently I also need to read about upgrading Python on Centos without breaking everything

Setting up the SSL certificate with Apache

With your certificate created it’s time to tell Apache that you want it to use that. On terminal run:

And you should get a screen that looks like this:

letsencrypt2

Apache still doesn’t know about this new certificate but we’re about to change that by selecting option 1 and on the subsequent screen choosing whether we want to make HTTP required or optional. I chose Secure here as I want all of my requests to be redirected to HTTPS.

You should then end up with a confirmation screen that tells you to check that your certificates are correctly installed. This procedure will have modified your httpd.conf file to add redirects so all requests that are non HTTPS are now redirected to be HTTPS.

letsencrypt3

Go ahead and hit those URL’s and you should see that they both get a grade A pass.

letsencrypt4

Updating Cloudflare

We need to tell CloudFlare that we now have an SSL certificate and want the communication to our website to use it. On CloudFlare’s dashboard for your chosen website choose Crypto and under SSL choose Full (Strict). You will probably want to use Flexible here if during the previous step you chose HTTPS to be optional.

letsencrypt5

At this point you should be done and your website should be showing a nice green padlock on the URL bar.

Unless…

You’re using WordPress. In this case you will also want to update it so the URL is always HTTPS. You can do that by going into WordPress Admin, and then navigating to Settings > General.

letsencrypt6

And that will make sure every image and every URL on your WordPress site is served via HTTPS.

Multi-Line C# Strings

Reading time: 2 – 3 minutes

I’ve seen this question being asked on StackOverflow so many times I event thought about writing a bot to automatically reply to it.

The answers vary slightly according to the experience of each developer but the question is always the same. How do you create multi-line C# strings?

Assigning multi-line strings to a variable

Say you’re trying to assign an XML string to a variable and want your code to still look presentable. The correct way to do that would be as follows:

Notice all I had to do was add an @ before the speechmarks, and this gives multi-line capabilities to my string.

But what if I wanted to have dynamic values inside my string? Say values coming from variables for example.

Assigning dynamic multi-line strings to a variable

What most people will do here is make the assumption you can use concatenation to inject variables in by doing something like this.

That is a horrible way of doing this though. Every time you add a new variable, you need to use an @ again to tell the compiler you’re starting a new multi-line string.

To assign dynamic values to a multi-line string you can use interpolation by adding the variables with curly braces within the string and then using string.Format() to tell it to replace those with the values I provide.

So much more elegant right? My string remains the same but I can now replace values in it in compile time and just keep increasing the number within the curly braces.

Let’s kick it up a notch and say your string has attributes. We’d now be looking at adding speechmarks in each of them as such:

Unfortunately it would also mean your code is now broken and you will start getting something like CS1002: ; expected. To go around that just make sure you double the speechmarks wherever you need them within your string.

And that will make your code work again and best of all – look really neat!

I’m joining Twilio

Reading time: 2 – 2 minutes

So the time has come for me to move on and accept a new challenge. As of the October 20th,  I’ll be joining Twilio as a Developer Evangelist.

Twilio has been no stranger to me for quite a while now,  and when I saw an open position in their devangelism team,  I wasted no time and applied for it.

The whole process took a bit of time, but both Twilio and I were quite keen to make sure we would be a good fit for each other.

I had a number of telephone interviews,  and then flew over to Twilio HQ 3.0 for the final round of interviews. The process was quite thrilling,  and I got so excited about it, this was the only company I actually applied for. Halfway the interview process, and after having met and spoken to some of the most clever people I have ever spoken with, I knew I wanted nothing but to work  with them.

I am supper excited with the prospect of not only writing  lot of code and working with some amazingly clever people, but also helping other developers writing some kick a$$ code in conferences, meetups, hackatons, stackoverflow…. or the pub.

Also, I will loudly and proudly wear my Twilio jacket to make sure people know they can approach me to have a chat about any Twilio integration, development in general, or life if they fancy it.

Here’s an example of what I will be doing.

So keep tuned, and get in touch!

Dart Pub packages stats

Reading time: 2 – 2 minutes

So modulecounts came to my attention, and I thought the idea was pretty neat.

I then contacted Erik (the author), and asked if he would mind also adding Dart.

He replied saying he wouldn’t mind, but quite rightly pointed out there seemed to be no way on Dart’s package manager to actually find out how many packages were live.

I asked some community members on G+, and while they seemed to have some solutions that kinda worked, there didn’t seem to be anything accurate or likely to work every time.

My solution was to then quickly knock-up a scraper up that would navigate through all pages on the website, grab some information and counts, aggregate and then generate a JSON output that could then be used by anyone trying to get some package information.

The JSON package is generated once a day as to not overload the website.

I then created a very simple app-engine client application that consumes the JSON packet, and shows information about it.

Check it out here: http://pub-stats.appspot.com

Source code can also be seen in my GitHub account: https://github.com/mplacona/pub-stats

I have also exposed another endpoint for anyone wanting to use the JSON packet on their applications.

The packet is also cached daily, to make sure my app-engine account doesn’t get abused 🙂

You can see the JSON endpoint here: http://pub-stats.appspot.com/json

Collaboration and pull requests welcome!

Sending and Receiving SMS messages with Dart

Reading time: 5 – 8 minutes

Dart - SMS example

I wanted to be able to send SMS messages with Dart, and because it doesn’t actually support it natively (not that I would expect it to anyway), I decided to write a library that allows me to do just that.

The library itself is a wrapper to the Twilio API, which is a wicked service that allows you to not only send or receive SMS/MMS messages, but also lets you make phone calls, start conference calls etc, all through their API. This offers immense scope to build applications on top of, since you could build an entire telecommunications central on top of their infrastructure without having to invest largely on buying and maintaining your own kit.

The principle is that all you should need to do before you can get coding, is crate a Twilio account, and make a note of the account ID and the token it will provide you with. After that, you can get coding.

Intro

In a Dart application, you can do the following to get the library installed:

  1. Add a dependency to twilio_dart to your pubspec.yaml
  2. Run pub get

With the library now installed on your application, you can start using it as such:

import 'package:twilio_dart/twilio.dart';
//
var key = "your_twilio_key";
var authToken = "your_auth_token";
var version = "2010-04-01";
Twilio twilio = new Twilio(key, authToken, version);

This gives you a Twilio object, and you can use that to call the methods in the API. Make sure you replace key and authToken with the appropriate values provided upon registration with Twilio. You can also find them on your dashboard.

Everything being OK so far, you should now be able to actually send an SMS message using the number Twilio gave you when you created your account. You can also find this on your Twilio account under the numbers section.

Sending

An SMS message is composed of three main parts:

  1. A from number – this should be the number Twilio provided you.
  2. A to number – this can be any number capable of receiving text messages (i.e. your mobile number).
  3. A message – This is the text message you want to send.
var from = "your_twilio_phone";
var to = "your_mobile_number";
var body = "Look ma! Dart can now send SMS's in under 15 lines";

All there is to do now is actually try and send this message. To do so, we will use the sendSMS method. The signature for it is as follows:

Future sendSMS(String from, String to, String body, [String mediaUrl = null])

So straight away we know this method will return a Future, and that it takes a from, a to, a body, and optionally the URL for an image, which will then turn your message into an MMS by attaching that media directly to your message.

Therefore our code should look like the following when completed:

import 'package:twilio_dart/twilio.dart';
main() {
    var key = "your_twilio_key";
    var authToken = "your_auth_token";
    var version = "2010-04-01";
    var from = "your_twilio_phone";
    var to = "your_mobile_number";
    var body = "Look ma! Dart can now send SMS's in under 15 lines";
    //
    //create a new twilio object
    Twilio twilio = new Twilio(key, authToken, version);
    //
    // Send SMS for LOLZ
    twilio.sendSMS(from, to, body).then((response) => print("Your message has been sent!")).catchError((error) => print(error.toString()));
}

Which even with all the line breaks and comments, still is just 15 lines.

If you execute this code, you will be able to verify two things:

  • you’ve now got a new message on the phone you’ve chosen
  • under your Twilio account, you will also be able to read this message if you view the logs.

Listing

I bet you’re somehow impressed by now as to how little you actually had to code to get our little application to send an SMS message. But we need a way to be able to retrieve those messages from the server without having to have a device in our hands all the time. the next thing we’re going to do, is come up with a way to list our messages. We can do that by using the method readSMSList, which does exactly what it says in the tin, it will give you a list of all the SMS messages in an account.

twilio.readSMSList().then((response){
        JSON.decode(response)['messages'].forEach((e) => print(e));       
    }).catchError((error) => print(error.toString()));

Reading

Very simple and very neat. Get a JSON packet back, and iterate through the results. Each of the messages has a sid, which is the ID for this message, so the next thing we need to do, is to be able to drill down to each of the messages from a list of messages.

To do so, we will use the method readSMS, which takes a message id as a parameter, and returns its full contents.

twilio.readSMS(sId).then((response) => print(response.toString())).catchError((error) => print(error.toString()));

And the above will give you all the information pertaining a single message.

Wrap up…

So in this article, we’ve gone through the following:

  • how to send an SMS message with Dart through the Twilio API by using the method sendSMS.
  • how to read a list of received messages from the server by using the method readSMSList.
  • how to read the contents of a single message by using the method readSMS.

The source code to the library can be found on my GitHub account along with samples and documentation.

« Older posts