How to add SSL to your localhost development environment using Ruby on Rails with Puma
Tags: rails, puma, ssl, cookies
Published: 2020-09-21 12:22:50

Hi readers!

Well, with the new update from Chrome from 80, if we have third party cookies you will need to add theSameSite=None; Secure , but this means that third party cookies will only be sent over HTTPS connection.

The following video is explaining this way better than me:

{% youtube GPz7onXjP_4 %}


Also, I leave you guys a [blog post](https://blog.chromium.org/2020/02/samesite-cookie-changes-in-february.html) from Chrome with the SameSite Cookie Changes.

OK, so if you are like me, who is developing services that shared a cookie between each other and using Ruby on Rails with Puma as your web server you will need to have HTTPS connection.

## Creating a certificate

So the first thing you will need to do is to create a certificate for your service you want to add the HTTPS connection. Let's use [mkcert](https://github.com/FiloSottile/mkcert) to generate it, i'm going to localhost, but you can use whatever name you want

```
➜  ~ mkcert localhost
Using the local CA at "/Users/marianomatayoshi/Library/Application Support/mkcert" ✨
Warning: the local CA is not installed in the system trust store! ⚠️
Warning: the local CA is not installed in the Firefox trust store! ⚠️
Run "mkcert -install" to avoid verification errors ‼️
Created a new certificate valid for the following names 📜
- "localhost"
The certificate is at "./localhost.pem" and the key at "./localhost-key.pem" ✅
```

Now we should mark these cert as trusted, I'm working on a Mac computer, so not sure how to handle this particular part on Windows or on a Linux distro.

For Mac users open the Keychain Access and drag the certificate to the Keychain, the localhost.pem file, you could see some error, but it will end up adding it..

![alt text][adding-cert-1]

Double-click the cert you just added, click the -> Trust

![alt text][adding-cert-2]
![alt text][adding-cert-3]

And at the **When using this certificate**: select the **Always Trust** option.

![alt text][adding-cert-4]
![][adding-cert-5]

## Localhost on SSL

Now the interesting part:
In your puma config you will want to have something like this:

{% gist https://gist.github.com/MatayoshiMariano/53ada82a86c4d03147cfb3c94c02dc21 %}

I ended up creating a `local-certs` folder inside `config` folder and pasted the cert and key there.
Then running `bundle exec puma -C puma.rb` will prompt something like this:

```
➜  ~ bundle exec puma -C puma.rb
[97] Puma starting in cluster mode...
[97] * Version 4.1.0 (ruby 2.3.8-p459), codename: Fourth and One
[97] * Min threads: 5, max threads: 5
[97] * Environment: development
[97] * Process workers: 2
[97] * Phased restart available
[97] * Listening on tcp://0.0.0.0:3001
[97] * Listening on ssl://0.0.0.0:3000?cert=config/local-certs/localhost.pem&key=config/local-certs/localhost-key.pem&verify_mode=none&no_tlsv1=false&no_tlsv1_1=false
[97] Use Ctrl-C to stop
[97] - Worker 0 (pid: 105) booted, phase: 0
[97] - Worker 1 (pid: 108) booted, phase: 0
```
And if you go to https://localhost:3000, you will see something like this🤘

![][adding-cert-6]

SSL on localshot:3000

Hope it helps you to sort out the Chrome update, ping me here or follow me on [Twitter](https://twitter.com/MarianoMatayosh), [StackOverflow](https://stackoverflow.com/users/1798644/matayoshimariano), [LinkedIn](https://www.linkedin.com/in/marianomatayoshi/?locale=en_US) or [Github](https://github.com/MatayoshiMariano) :)

Mariano Matayoshi (matayoshi.mariano@gmail.com)
[adding-cert-1]: https://miro.medium.com/max/1400/1*OiXNKaFGmTbptpoyle-q_A.png "Just after adding the certificate"
[adding-cert-2]: https://cdn-images-1.medium.com/max/1200/1*_g6l5TM5aVchadlrktqKuA.png
[adding-cert-3]: https://cdn-images-1.medium.com/max/1200/1*b4uUZ8D35MnOdvUv3Dm6oA.png
[adding-cert-4]: https://cdn-images-1.medium.com/max/1200/1*cmmqC2nAThG4nP5LXR74JA.png
[adding-cert-5]: https://cdn-images-1.medium.com/max/1200/1*-SkYAFgMiFO9wDjMd9DtHw.png "Always trust the certLocalhost on SSL"
[adding-cert-6]: https://cdn-images-1.medium.com/max/1600/1*_7G3Hr8lRff0S0p3a-XRSQ.png