Let’s EncryptでSSL設定

SSL

以前は自分のサイトを暗号化する為にSSLを導入するには、コスト的にもハードルが高い時代が長い間続きましたが、現状ではオープンに利用可能なLet’s Encryptのおかげでその敷居も非常に低くなりました。最近では様々なセキュリティ的な取り組みがブラウザーやサーバー側のヘッダーでも実装されてhttps以外での運用が更に厳しくなってきたので、個人でテスト用に利用しているサイトをSSLで暗号化して見ました。

Let’s Encryptフリー自動化されたオープンな認証局”

初期設定自体は、10分もかからないで出来るのであまり戸惑う事は無いかと思います。

STEP1:EPEL(拡張パッケージ)のインストール

※ 自分の環境はEC2なので以下のコマンドでインストールしています。

必要に応じて、yum install epel-release等に読み替えて下さい。

[root@ec2-rails-za user]# amazon-linux-extras install epel
Installing epel-release
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Cleaning repos: amzn2-core amzn2extra-docker amzn2extra-epel amzn2extra-nginx1.12 amzn2extra-ruby2.4
20 metadata files removed
8 sqlite files removed
0 metadata files removed
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                                                                                    | 3.7 kB  00:00:00     
amzn2extra-docker                                                                                                                             | 3.0 kB  00:00:00     
amzn2extra-epel                      

STEP2: certbot等の必要なソフトをインストール

[root@ec2-rails-za user]# yum install certbot python-certbot-apache
yum install certbot python-certbot-apache
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
218 packages excluded due to repository priority protections
Resolving Dependencies
--> Running transaction check
---> Package certbot.noarch 0:1.11.0-2.el7 will be installed

STEP3:証明書の発行

  • 発行コマンドの実行 。

ここでドキュメントルートの指定が間違えているとエラーになるので適宜調整して下さい

自分の環境では、Pumaが動いているので外部に公開しているPublicのルートを指定して作成しています。

[root@ec2-rails-za app]# certbot certonly --webroot -w /home/app/user_app/public -d oss-rdbms.afaik.jp
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Requesting a certificate for oss-rdbms.afaik.jp
Performing the following challenges:
http-01 challenge for oss-rdbms.afaik.jp

  • メールアドレスの登録
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): メールアドレスを入力
  • 利用規約に合意 (Yesで進む)
Starting new HTTPS connection (1): acme-v01.api.letsencrypt.org

-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree
in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
  • メールアドレスを共有するかどうか問われる(ここではNoを選択しています)
-------------------------------------------------------------------------------
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: N

  • インストール完了 (Default Path: /etc/letsencrypt/live/ )
IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/oss-rdbms.afaik.jp/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/oss-rdbms.afaik.jp/privkey.pem
   Your certificate will expire on 2022-01-26. To obtain a new or
   tweaked version of this certificate in the future, simply run
   certbot again. To non-interactively renew *all* of your
   certificates, run "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

STEP4:証明書をWebサーバーに登録

自分の環境はnginxの為、nginx.confの設定を変更しています。

  • 生成された認証鍵
[root@ec2-rails-za app]# ls -l /etc/letsencrypt/live/oss-rdbms.afaik.jp/
total 4
lrwxrwxrwx 1 root root  42 Oct 28 23:14 cert.pem -> ../../archive/oss-rdbms.afaik.jp/cert1.pem
lrwxrwxrwx 1 root root  43 Oct 28 23:14 chain.pem -> ../../archive/oss-rdbms.afaik.jp/chain1.pem
lrwxrwxrwx 1 root root  47 Oct 28 23:14 fullchain.pem -> ../../archive/oss-rdbms.afaik.jp/fullchain1.pem
lrwxrwxrwx 1 root root  45 Oct 28 23:14 privkey.pem -> ../../archive/oss-rdbms.afaik.jp/privkey1.pem
-rw-r--r-- 1 root root 692 Oct 28 23:14 README
  • 上記PEMをnginx.confのhttps (Port: 443) に設定
        ssl_certificate "/etc/letsencrypt/live/oss-rdbms.afaik.jp/fullchain.pem";
        ssl_certificate_key "/etc/letsencrypt/live/oss-rdbms.afaik.jp/privkey.pem";

  • nginxの再起動
[root@ec2-rails-za nginx]# systemctl restart nginx

動作確認

  • opensslコマンドでの確認
~ [14:05:31]> openssl s_client -servername oss-rdbms.afaik.jp -showcerts -connect oss-rdbms.afaik.jp:443
CONNECTED(00000003)
depth=2 C = US, O = Internet Security Research Group, CN = ISRG Root X1
verify return:1
depth=1 C = US, O = Let's Encrypt, CN = R3
verify return:1
depth=0 CN = oss-rdbms.afaik.jp
verify return:1
---
Certificate chain
 0 s:CN = oss-rdbms.afaik.jp
   i:C = US, O = Let's Encrypt, CN = R3

  • curlでの確認
~ [ 9:12:05]> curl -s -v --tlsv1.1 https://oss-rdbms.afaik.jp < /dev/null 2>&1 | grep -i 'SSL connection'
* SSL connection using TLSv1.1 / ECDHE-RSA-AES256-SHA
~ [ 9:12:12]> curl -s -v --tlsv1.2 https://oss-rdbms.afaik.jp < /dev/null 2>&1 | grep -i 'SSL connection'
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
  • ブラウザーでの確認
 Let's Encrypt
SSL by Let’s Encrypt

  • 外部サイトからの確認
外部確認
SSL Report

SSLのステータスを確認:https://www.ssllabs.com/ssltest/

証明書の更新

Let’s Encrypt で発行した証明書は、有効期限が3ヶ月となっているので、定期的に自動更新出来る様にしてあげれば運用は楽になるかと思います。その他、必要な時にメンテナンスしてあげればOKでしょう。

[root@ec2-rails-za letsencrypt]# certbot renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/oss-rdbms.afaik.jp.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not yet due for renewal

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The following certificates are not due for renewal yet:
  /etc/letsencrypt/live/oss-rdbms.afaik.jp/fullchain.pem expires on 2022-01-26 (skipped)
No renewals were attempted.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

直近では以下のような対応も必要でしたので参考までに。

https://letsencrypt.org/ja/docs/dst-root-ca-x3-expiration-september-2021/

備考:

Let’s Encrypt 動作の仕組み

Certbot

備忘録:

  • 今回は、nginxでproxyしてpumaに転送したのですが、以下のエラーが発生してしまいhttpsにした後にログインページへのリダイレクトで以下のエラーが発生してしまいました。
HTTP Origin header (https://oss-rdbms.afaik.jp) didn't match request.base_url (http://oss-rdbms.afaik.jp)
Completed 422 Unprocessable Entity in 0ms (ActiveRecord: 0.0ms)

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):
  • ログインしていない場合にログイン画面に転送しているので、転送した時に迷子になって422エラーが発生していたので、以下の設定をnginx.confに追加してあげたら解決しました。
    proxy_set_header X-Forwarded-Proto $scheme;

“X-Forwarded-Proto (XFP) ヘッダーは、プロキシまたはロードバランサーへ接続するのに使っていたクライアントのプロトコル (HTTP または HTTPS) を特定するために事実上の標準となっているヘッダーです。サーバーのアクセスログにはサーバーとロードバランサーの間で使われたプロトコルが含まれていますが、クライアントとロードバランサーの間で使用されたプロトコルは含まれていません。クライアントとロードバランサーの間で使用されたプロトコルを特定するには、 X-Forwarded-Proto リクエストヘッダーを使用することができます。”

X-Forwarded-Proto

カテゴリー:

最近のコメント

表示できるコメントはありません。