gunicornでSSLを使用する
ちょっとしたWEBアプリ100行程度(WAFなし)書いた時に、オレオレ証明書でいいのでSSLにしたかったというのがあり、gunicorn使ってやってみたのでそのメモ。
サンプルコードは以下にまとめました。 https://github.com/tanacasino/gunicorn-ssl
まずは、普通にgunicornのアプリを起動できるようにするまでのチュートリアル的なメモ。 pythonの virtualenv と opensslコマンドは事前にインストールしている前提。
いろいろインストールし環境を整える。
$ mkdir gunicorn-ssl $ cd gunicorn-ssl $ virtualenv .venv --no-site-packages $ . .venv/bin/activate $ pip install gunicorn
起動するアプリを作る。(Hello World!返すだけのやつ。)
# -*- coding: utf-8 - def app(environ, start_response): data = 'Hello, World!\n' status = '200 OK' response_headers = [ ('Content-type','text/plain'), ('Content-Length', str(len(data))) ] start_response(status, response_headers) return iter([data])
gunicornを使ってアプリを起動する
$ gunicorn --workers=2 app:app 2013-08-22 22:36:37 [21814] [INFO] Starting gunicorn 17.5 2013-08-22 22:36:37 [21814] [INFO] Listening at: http://127.0.0.1:8000 (21814) 2013-08-22 22:36:37 [21814] [INFO] Using worker: sync 2013-08-22 22:36:37 [21819] [INFO] Booting worker with pid: 21819 2013-08-22 22:36:37 [21820] [INFO] Booting worker with pid: 21820
worker 2なのでプロセスが2つ上がってます。 curlでアクセスしてみる。
$ curl http://localhost:8000/ Hello, World!
「Hello, World!」ごちそうさま。
では本題のSSL化へ。 まずは、オレオレ証明書をサクッと作る。
コマンドは以下の3つのみ。
openssl genrsa 2048 > server.key
openssl req -new -key server.key > server.csr
openssl x509 -days 3650 -req -signkey server.key < server.csr > server.crt
実際に実行すると以下の感じ。Country Name等色々入れてくれと言れろやい言われます。
$ openssl genrsa 2048 > server.key Generating RSA private key, 2048 bit long modulus ............................+++ ....................................+++ e is 65537 (0x10001) $ openssl req -new -key server.key > server.csr You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. ----- Country Name (2 letter code) [AU]: State or Province Name (full name) [Some-State]: Locality Name (eg, city) []: Organization Name (eg, company) [Internet Widgits Pty Ltd]: Organizational Unit Name (eg, section) []: Common Name (e.g. server FQDN or YOUR name) []: Email Address []: Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: An optional company name []: $ openssl x509 -days 3650 -req -signkey server.key < server.csr > server.crt Signature ok subject=/C=AU/ST=Some-State/O=Internet Widgits Pty Ltd Getting Private key
ではでは、gunicornをSSLオプションで起動するぜよ。
$ gunicorn --workers=2 app:app --keyfile server.key --certfile server.crt 2013-08-22 22:46:25 [27734] [INFO] Starting gunicorn 17.5 2013-08-22 22:46:25 [27734] [INFO] Listening at: https://127.0.0.1:8000 (27734) 2013-08-22 22:46:25 [27734] [INFO] Using worker: sync 2013-08-22 22:46:26 [27746] [INFO] Booting worker with pid: 27746 2013-08-22 22:46:26 [27750] [INFO] Booting worker with pid: 27750
httpsきたー!!のでcurlでアクセスする!
けどオレオレ証明書なので、--insecure
オプションを忘れずに!
$ curl --insecure https://localhost:8000 Hello, World!
おしまい。