よくわからないエンジニア

よく分からないエンジニア(無音鈴鹿)の日々の記録

よくわからないエンジニア

Let's Encryptで証明書を発行する

通勤時に以下記事を閲覧した。
gigazine.net
昨年末に存在を知って、何度か導入していたが、ついにベータ版から正式版をリリースした模様。

前職の頃からSSL証明書は高額で企業の確認など面倒なイメージがついており、無料で簡単に発行出来る証明書が出現したとなれば、恐らく証明書業者は顔を真っ赤にして毒を吐いている事であろう。と思っていたら以下記事を目にした。

SSL/TLS証明書無料化は進むか?~Let's Encryptに見る無料SSL/TLS証明書の台頭とその注意点~ | ブログ | SSL・電子証明書ならGMOグローバルサイン

顔真っ赤どころか、物凄く丁寧に説明されていて分かりやすかった。大雑把な区分として、SSL化するだけならLet's Encryptや低価格な「ドメイン型証明書」でOK。フィッシング詐欺防止なを目的とするならば、「企業認証型証明書」以上が必要だと。自サイトに必要なレベルの認証を考慮して導入する必要があるとの事だ。

Let's Encryptを導入

上記の目的で言えば、検証用サーバにSSL証明書を使いたいだけならば、Let's Encryptで問題は無さそうだ。折角なので、以前にCentOS6.7に導入した際の手順をメモっておく。導入は以下サイトを参考にしました。
blog.doizaki.com

Let's Encryptクライアントの導入
#git clone https://github.com/letsencrypt/letsencrypt
Initialized empty Git repository in /usr/local/letsencrypt/.git/
remote: Counting objects: 34610, done.
remote: Compressing objects: 100% (420/420), done.
remote: Total 34610 (delta 151), reused 0 (delta 0), pack-reused 34170
Receiving objects: 100% (34610/34610), 9.41 MiB | 2.35 MiB/s, done.
Resolving deltas: 100% (24458/24458), done.
python 2.7のインストール

まず現環境のpythonのバージョンを確認します。

# python --version
Python 2.6.6

使用しているのはCentOS6.7なので、python古いです。腐ってます。Let's Encryptはpython2.7が必要です。Cent6で公式リポジトリから提供されているpythonは2.6なので、野良で拾ってくるでも良いのですが、環境を汚さないように、今回はSCL使って導入します。(SCLについてもそのうち記事にしたいです)

# yum install centos-release-SCL
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: www.ftp.ne.jp
 * extras: www.ftp.ne.jp
 * updates: www.ftp.ne.jp
Resolving Dependencies
--> Running transaction check
---> Package centos-release-SCL.x86_64 10:6-5.el6.centos will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================================================================================================================================================
 Package                                                 Arch                                        Version                                                  Repository                                   Size
================================================================================================================================================================================================================
Installing:
 centos-release-SCL                                      x86_64                                      10:6-5.el6.centos                                        extras                                      3.9 k

Transaction Summary
================================================================================================================================================================================================================
Install       1 Package(s)

Total download size: 3.9 k
Installed size: 453
Is this ok [y/N]: y
Downloading Packages:
centos-release-SCL-6-5.el6.centos.x86_64.rpm                                                                                                                                             | 3.9 kB     00:00
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
  Installing : 10:centos-release-SCL-6-5.el6.centos.x86_64                                                                                                                                                  1/1
  Verifying  : 10:centos-release-SCL-6-5.el6.centos.x86_64                                                                                                                                                  1/1

Installed:
  centos-release-SCL.x86_64 10:6-5.el6.centos

Complete!

#yum install python27 python27-scldevel
…
Complete!

それではここで、pythonのバージョンを確認します。

# scl enable python27 "python --version"
Python 2.7.5

SCLで導入したパッケージを使う場合は、頭にscl enable xxxをして上げて下さい。面倒だったら、bash上で有効化してもオッケー。

# scl enable python27 bash
# python --version
Python 2.7.5

ここまで出来たら、Let's Encryptが動くか確認します。足りないパッケージあると勝手にインストールされるのでお気をつけて下さい。

# scl enable python27 "./letsencrypt/letsencrypt-auto --help"
Bootstrapping dependencies for RedHat-based OSes...
yum is /usr/bin/yum
Loaded plugins: fastestmirror, security
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: www.ftp.ne.jp
 * extras: www.ftp.ne.jp
 * updates: www.ftp.ne.jp
…省略…
Checking for new version...
Creating virtual environment...
Installing Python packages...
Installation succeeded.
Requesting root privileges to run letsencrypt...
   /root/.local/share/letsencrypt/bin/letsencrypt --help

  letsencrypt-auto [SUBCOMMAND] [options] [-d domain] [-d domain] ...

The Let's Encrypt agent can obtain and install HTTPS/TLS/SSL certificates.  By
default, it will attempt to use a webserver both for obtaining and installing
the cert. Major SUBCOMMANDS are:

  (default) run        Obtain & install a cert in your current webserver
  certonly             Obtain cert, but do not install it (aka "auth")
  install              Install a previously obtained cert in a server
  renew                Renew previously obtained certs that are near expiry
  revoke               Revoke a previously obtained certificate
  rollback             Rollback server configuration changes made during install
  config_changes       Show changes made to server config during installation
  plugins              Display information about installed plugins

Choice of server plugins for obtaining and installing cert:

  --apache          Use the Apache plugin for authentication & installation
  --standalone      Run a standalone webserver for authentication
  (nginx support is experimental, buggy, and not installed by default)
  --webroot         Place files in a server's webroot folder for authentication

OR use different plugins to obtain (authenticate) the cert and then install it:

  --authenticator standalone --installer apache

More detailed help:

  -h, --help [topic]    print this message, or detailed help on a topic;
                        the available topics are:

   all, automation, paths, security, testing, or any of the subcommands or
   plugins (certonly, install, nginx, apache, standalone, webroot, etc)
証明書の取得

letsencrypt-autoを実行する。引数には以下情報を
-w:ドキュメントルート -d:ドメイン -m:メールアドレス --agree-tos:引数なし(利用規約に同意)

# scl enable python27 "./letsencrypt/letsencrypt-auto certonly --webroot -w /usr/local/apache/htdocs/ -d test.example.co.jp -m test@example.co.jp --agree-tos"
…中略…
Donating    to  ISRG    /   Let's   Encrypt:    https://letsencrypt.org/donate
Donating    to  EFF:    https://eff.org/donate-le

問題なければ、以下にファイルが作成される。

# ls /etc/letsencrypt/live/test.example.co.jp/
cert.pem  chain.pem  fullchain.pem  privkey.pem

これらを証明書として指定した上げれば、証明書導入完了。激簡単です。

証明書の更新

90日で証明書の期限が切れるので、cronに自動更新を仕掛けます。apacheの再起動あるのでご注意を。

#crontab -e
00 05 01 * * scl enable python27 "/usr/local/letsencrypt/letsencrypt-auto renew --force-renew && /etc/init.d/httpd reload"

scl enable python27忘れるとエラー出るのでご注意を。