Let's Encrypt ワイルドカード自動更新(ConoHa)

Contents


はじめに

2回に渡りLet's EncryptのSSL証明書を試してきましたが、今回はワイルドカードの自動更新にチャレンジします。 初回のSSL証明書取得の時にDNS認証を自動で行い、以後の更新をcronで自動更新します。 過去記事もよろしければご参考にどうぞ。

環境

DNS認証にWEBサーバである必要はないので、今回は別にサーバを建てて試しました。SSL証明書をLBに入れるケースも多いと思うので、WEBサーバである必要は特にありません。 DNSはAPIが使える環境が楽だと思います。今回はConoHa VPSの環境を使っているのでConoHa DNSのAPIを使って自動認証を行います。
バージョン
CentOS77.6.1810
certbot0.31.0
jq1.5
DNSConoHa

手順

インストール

epel リポジトリインストール
# yum install epel-release
jq インストール
# yum --enablerepo=epel install jq
certbot インストール
# yum install --enablerepo=epel certbot

DNS認証自動化のスクリプト作成

certbot 実行時に発行されるトークンをDNSのTXTレコードに登録する必要がありますが、この処理をスクリプトで自動化します。 このスクリプトをcertbot 実行時にオプションで指定することで自動化が可能になります。レコード登録時のオプションは--manual-auth-hook、レコード削除時のオプションは--manual-cleanup-hook で指定します。 certbot 実行時に発行されるトークンの値をどのようにスクリプトに渡すかですが、certbot から以下の変数が渡されるので、これをスクリプトで指定します。
変数内容
CERTBOT_DOMAIN認証を行うドメイン
CERTBOT_VALIDATION認証に使用するトークン
今回は以下のようなスクリプトを用意しました。
ファイル名内容
conoha_dns_api.shConoHa APIのリクエスト関数
conoha_idConoHa APIに必要なID・PASSを登録
create_conoha_dns_record.shConoHa DNSにレコードを登録
delete_conoha_dns_record.shConoHa DNSからレコードを削除
スクリプトの中身は今回がんばってGitHubにあげてみました(初!)。よかったら参照してみてください。

※慣れないことしたので至らない点もあると思いますが、よろしくお願いします<(_ _)>

letsencrypt-dns-conoha

ワイルドカードの指定

ベースドメインと当該ベースドメインのワイルドカードの両方を含める場合はcertbot 実行時に両方指定する必要がありそうです。 例)
-d "eastforest.jp" 
-d "*.eastforest.jp"

ワイルドカードSSL証明書取得テスト

まずはドライランで実行して問題ないか確認してみる。
# certbot certonly \
--dry-run \
--manual \
--agree-tos \
--no-eff-email \
--manual-public-ip-logging-ok \
--preferred-challenges dns-01 \
--server https://acme-v02.api.letsencrypt.org/directory \
-d "eastforest.jp" \
-d "*.eastforest.jp" \
-m "<mail address>" \
--manual-auth-hook /usr/local/bin/letsencrypt-dns-conoha/create_conoha_dns_record.sh \
--manual-cleanup-hook /usr/local/bin/letsencrypt-dns-conoha/delete_conoha_dns_record.sh
問題なければ以下のようなメッセージが返ってきます。
IMPORTANT NOTES:
The dry run was successful.

ワイルドカードSSL証明書取得

ドライランで問題がなければ、--dry-run オプションを外して実行。
# certbot certonly \
--manual \
--agree-tos \
--no-eff-email \
--manual-public-ip-logging-ok \
--preferred-challenges dns-01 \
--server https://acme-v02.api.letsencrypt.org/directory \
-d "eastforest.jp" \
-d "*.eastforest.jp" \
-m "<mail address>" \
--manual-auth-hook /usr/local/bin/letsencrypt-dns-conoha/create_conoha_dns_record.sh \
--manual-cleanup-hook /usr/local/bin/letsencrypt-dns-conoha/delete_conoha_dns_record.sh
もしApacheで証明書の設定をしていて、すぐ反映させるなら以下のようにオプションを追加しておけばOKです。更新後にWEBサーバを再起動してくれます。
--post-hook "systemctl restart httpd.service"
成功すれば以下のようなメッセージが表示されます。
Resetting dropped connection: acme-v02.api.letsencrypt.org

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/eastforest.jp/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/eastforest.jp/privkey.pem
   Your cert will expire on 2019-06-19. 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

自動更新ファイルの確認

上記でSSL証明書を取得した時(certbotを実行時)の情報が /etc/letsencrypt/renewal/ 配下のファイルに保存されて、次回以降の更新はこのファイルを参照して更新が行われます。
# cat /etc/letsencrypt/renewal/eastforest.jp.conf
# renew_before_expiry = 30 days
version = 0.31.0
archive_dir = /etc/letsencrypt/archive/eastforest.jp
cert = /etc/letsencrypt/live/eastforest.jp/cert.pem
privkey = /etc/letsencrypt/live/eastforest.jp/privkey.pem
chain = /etc/letsencrypt/live/eastforest.jp/chain.pem
fullchain = /etc/letsencrypt/live/eastforest.jp/fullchain.pem

# Options used in the renewal process
[renewalparams]
authenticator = manual
account = *******************************
manual_public_ip_logging_ok = True
manual_auth_hook = /usr/local/bin/letsencrypt-dns-conoha/create_conoha_dns_record.sh
server = https://acme-v02.api.letsencrypt.org/directory
manual_cleanup_hook = /usr/local/bin/letsencrypt-dns-conoha/delete_conoha_dns_record.sh
pref_challs = dns-01,

ワイルドカードSSL証明書 自動更新テスト

certbot renew だと更新期限1ヵ月以内でなければ実際に更新処理が行われないため、強制更新をドライランで試して、certbot renewで更新可能かテストしておきます。
# certbot renew --force-renewal --dry-run 
問題がなければ以下のようにsuccessとなります。
Waiting for verification...
Cleaning up challenges
Resetting dropped connection: acme-staging-v02.api.letsencrypt.org

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/eastforest.jp/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
  /etc/letsencrypt/live/eastforest.jp/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

ワイルドカードSSL証明書 自動更新

ここまでくればあとはcertbot renew をcron に登録しておくだけです。
# crontab -e
0 1 * * * certbot renew
お疲れ様でした! 参考 Pre and Post Validation Hooks ACME v2 とワイルドカード証明書の技術情報 ConoHa API Documantation Conoha × Let’s encryptにてワイルドカード証明書を自動取得する