17

How do I set up Openshift app to work with let's encrypt ?

NB Openshift does not work with a simple python webserver approach to server, you need to use the correct port and bind to the correct IP address. Also the app/gear does not necessary have a html root.

(A question which I will post an answer below.)

1

5 Answers 5

13

First, vote here so that OpenShift makes 'Let’s Encrypt' their priority.

My steps will be valid for Django apps, but with small changes you can make them work on any OpenShift gear.
Generate certificate on your localhost/notebook/pc:

  1. git clone https://github.com/letsencrypt/letsencrypt to your local computer.
  2. cd letsencrypt
  3. ./letsencrypt-auto -a manual -d example.com -d www.example.com
    Now you will be asked to confirm you domain ownership.
  4. In your app, make sure example.com/.well-known/acme-challenge/{some hash} returns required hash. In django you can add this line to urls.py:

    url(r'^.well-known/acme-challenge/.*', views.https_confirmation, name="https_confirmation"),
    

    and this to view.py:

    def https_confirmation(request):
        if request.META['HTTP_HOST'] == 'www.example.com':
            return HttpResponse("fqTGI3nUiYnelm...", content_type="text/plain")
        else: #naked domain example.com
            return HttpResponse("HASH pre example.com", content_type="text/plain")
    

    If your acme confirmation pages does not show, restart OpenShift app.

  5. Just upload created certificates /etc/letsencrypt/archive/example.com to OpenShift web console. Fullchain.pem as SSL Certificate and privkey.pem as Certificate Private Key.

That is it, now you should get A rating on ssllabs.com.
Also, to require Django app to use HTTPS, set these:

  1. In settings.py:

    if not DEBUG:
        SESSION_COOKIE_SECURE = True
        CSRF_COOKIE_SECURE = True`
    
  2. Create file wsgi/.htaccess and put these lines there:

    RewriteEngine on
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
    
  3. Enable HTTPS for WSGI - in file wsgi/application:

    # make django aware that SSL is turned on
    os.environ['HTTPS'] = "on"
    

    That should be all :) You need to repeat these steps when renewing certificates, so every 90 days(60 days are better, so you do not end up having problems on last possible day). This are pretty annoying steps, so lets hope(and vote) OpenShift will implement Letsencrypt soon!

4

The answer by Lucus03 is good, I'd just like to add a general comment.

Assumptions You have at least a bronze Openshift account that allows a custom domain. This is working normally and you can access your site (without https). http://www.testdomain.com

We need to follow the manual process. Those who are new to certificates, like me, may not be clear on the general concepts.

Let's Encrypt needs to confirm you control the domain before they issue a certificate. This means putting temporary files on the server that hosts your site. Let's Encrypt then checks for these and issues a certificate.

In the manual process the temporary files get downloaded to your local pc first. Then you manually put the files in the correct location on the server. These text files must be viewable via your site or the process will fail.

Because of the variety of applications using Openshift you will see dfferent software stacks being used. eg http://velin-georgiev-blog.appspot.com/blog/details/5707532110659584 refers to Flask How to set up Openshift with let's encrypt (letsencrypt) by Lucas03 Django

If you can display the temporary files on www.testdomain.com using your browser you can probably ignore the software stack and stick with what you know.

3

Assuming the application is called https and the domain names for the the certificate is called www.example.com

First (if not done already) install the rhc tools, https://developers.openshift.com/en/managing-client-tools.html

Second (if not done already) Set up the CNAME record with your DNS provider - at developers.openshift.com/en/managing-domains-ssl.html Test www.example.com (http) works and directs to your openshift application before preceding.

Third Log into your application

rhc ssh -a https

From the application, install Simple Let's Encrypt Client and bring some of the python packages needed up to date

pip install git+https://github.com/kuba/simp_le
pip install --upgrade six
pip install --upgrade setuptools

Now stop the application (gear) Set up a python2 webserver, that with the correct port and correct IP. [$OPENSHIFT_PYTHON_IP & OPENSHIFT_PYTHON_PORT]

(Note this is a one line in python 3.4, python -m http.server $OPENSHIFT_PYTHON_PORT --bind $OPENSHIFT_PYTHON_IP but openshift at the time of writing has only python 3.2 or python 2. So a simple python 17 line script is needed)

gear stop
mkdir -p /tmp/http/.well-known/acme-challenge
cd /tmp/http
wget https://gist.githubusercontent.com/bmsleight/bc34254eed0ee458738e/raw/61110fe6e3980f0c6a401acae93f221f56b1eced/simple_acme_server.py
python2 simple_acme_server.py &

Go to the data directory as a good place to store the certificates and let simp_le works its magic

cd ~/app-root/data/
simp_le --email [email protected] -f account_key.json   -f fullchain.pem -f key.pem   -d www.example.com --default_root /tmp/http 

Assuming no errors, stop the python2 webserver, restart the application/gear and exit out of openshift server

killall python2
gear start
exit

Forth The uploading of the certificates and keys must be done outside of the application, so from your local machine - grab a copy and then upload them (yes scp is the wrong way around - RTFM)

rhc scp -a https download ./ ./app-root/data/fullchain.pem
rhc scp -a https download ./ ./app-root/data/key.pem
rhc alias update-cert https www.example.com --certificate fullchain.pem --private-key key.pem

Fifth Show some love at letsencrypt.org

3
  • 3
    Good answer, but I think it would be clearer to do this for an app with a different name. Naming your app https and using the protocol https make it unnecessarily confusing.
    – RonaldB
    Apr 8, 2016 at 19:23
  • Is there a way to set the certificate and the key server-side? rhc alias update-cert doesn't work server-side.
    – Flimm
    Jun 23, 2016 at 11:36
  • 1
    it is not working unless the "Bronze"-plan was subscribed. It was mentioned in "Assumptions" of johnc's answer. Jul 21, 2016 at 0:15
2

Answers above are correct but somewhat complicated. I found the answer below simplest, do give it a try. Referring to Let’s Encrypt on OpenShift,

Assuming that you've letsencrypt, create a new folder, say "ssl". Create config, work and logs dir in "ssl" folder. Then run the letsencrypt command.

$ mkdir ssl
$ cd ssl
$ mkdir config work logs
$ letsencrypt --text --email [email protected] --domains www.mydomain.com,mydomain.com,foo.mydomain.com --agree-tos --renew-by-default --manual certonly --config-dir ./config/ --work-dir ./work/ --logs-dir ./logs/

It instructs to upload a file to your openshift deployment. Once you've uploaded, you can proceed and certificates will be at your disposal. Nice and easy.

Also, make sure your endpoints are deployed at "http" protocol and not "https". Else letsencrypt will throw error that it already has a certificate. :-)

2
  • 1
    Is there a way to automate renewals?
    – Flimm
    Jun 23, 2016 at 11:36
  • 1
    For this kind of certificates, where it is generated on one machine and used on other machine, I couldn't find a proper way of renewing certificates automatically.
    – sbharti
    Jul 2, 2016 at 11:57
2

If deploying on OpenShift V3+, have a look at https://github.com/ibotty/openshift-letsencrypt for automated letsencrypt certificate management on exposed routes.

2
  • This was posted as an answer, but it does not attempt to answer the question. It should possibly be an edit, a comment, another question, or deleted altogether. Jun 3, 2017 at 6:28
  • 2
    It is still useful information as it addresses the question for those who may be using OpenShift 3, whereas other answers are for OpenShift 2. How it is done for each version is very different. Since the original question doesn't say which version and things have now changed to OpenShift 3, you will inevitably get people coming here and getting confused if don't know provide solution for OpenShift 3. Jun 3, 2017 at 7:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.