Nodejs(Express) で Let’s Encrypt の証明書を使う(3)。Certbot のmanual オプションで取得した証明書をrenewする

ネットワーク

こんにちは!

週末にプログラミングと料理を楽しむ、しずかなかずしです。

以前、NodejsでLet’s Encrypt の証明書を certbotのmanual オプション付きで実行する方法をご紹介しました。

本日は、この記事の続編、ということでrenew で証明書を更新してみます。

renew コマンドの実行でfail

前回の記事においてcertbot manualオプションで証明書を取得してあることが前提で、証明書の更新を試みます。これを行うためには、certbot renewというコマンド実行します。

ところが、そのまま実行すると以下のようなエラーが発生します。


$ sudo certbot renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log

Processing /etc/letsencrypt/renewal/ftp-exchange.com.conf

Cert is due for renewal, auto-renewing…
Could not choose appropriate plugin: The manual plugin is not working; there may be problems with your existing configuration.
The error was: PluginError('An authentication script must be provided with –manual-auth-hook when using the manual plugin non-interactively.’,)
Attempting to renew cert (ftp-exchange.com) from /etc/letsencrypt/renewal/ftp-exchange.com.conf produced an unexpected error: The manual plugin is not working; there may be problems with your existing configuration.
The error was: PluginError('An authentication script must be provided with –manual-auth-hook when using the manual plugin non-interactively.’,). Skipping.
All renewal attempts failed. The following certs could not be renewed:
/etc/letsencrypt/live/ftp-exchange.com/fullchain.pem (failure)


前回、manualで実行した時に、/etc/letsencrypt/renewalに 'ドメイン名.conf’のようなファイルが作成されているようで、そこにマニュアルで作ったよ、という事が書かれています。

しかし、上記のエラーの意味するところは、renewするときには–manual-auth-hookで指定するシェルスクリプトが必要なんだよ、というエラーメッセージのようです。

manualオプションで実行する場合は、記事にあったように手動でWebサーバーが参照する.well-known/acme-challengeというディレクトリにcertbotコマンドによって表示される謎の(!)文字列をファイル名に入れたり、ファイルの中に記載したり、といった事をやりました。

実は、renewの際は、あの手順を「自動」でやるためのスクリプトが必要になるようなのです。もはや、manual (手動)ではありませんね(笑)

–manual-auth-hook用のスクリプトで解決

ネットで調べてみたところ、本家のマニュアルにhookを説明する以下のようなページを見つけました。そしてその中に、HTTP-01チャレンジの場合のスクリプトの例が記載されています。

これをみると、–manual-auth-hookで指定したスクリプトが実行される場合に、以下のような環境変数に値が入ってくるようです。

環境変数の名前値の意味
CERTBOT_DOMAINrenew(再認証)しようとするドメイン名
CERTBOT_VALIDATIONvalidationのための謎の(!)文字列
CERTBOT_TOKENHTTP-01チャレンジに必要なToken(謎の文字列(!))

前回の記事で、http://example.com/.well-known/acme-challenge/xxxxxx のようにWebサーバーにファイルを配置しましょう、というお話が出てきていました。上記の環境変数に入ってくる文字列は、つまり xxxxxxのファイル名は環境変数CERTBOT_TOKENに入ってくる文字列が該当し、ファイルの中身がCERTBOT_VALIDATIONに入ってくる文字列が相当するということのようです。

なので、–manual-auth-hookで指定するhookのシェルスクリプトはこの状態になるように環境変数を利用し、以下のようなものになります。(authenticator.shというファイル名にします)

#!/bin/bash
echo $CERTBOT_VALIDATION > /var/www/htdocs/.well-known/acme-challenge/$CERTBOT_TOKEN

上記のスクリプトの/var/www/htdocs/の部分は、前回記事でExpress+Nodejsで以下のようなコーディングをした"public"に相当するディレクトリになるように書くのがミソです。

app.use(express.static(path.join(__dirname, 'public'), {dotfiles: 'allow'}));

さらに、–manual-auth-hookで指定したシェルスクリプトが作った$CERTBOT_TOKENのファイルが不要であれば、–manual-cleanup-hookで終了時に実行するスクリプトを指定することができるので、その中で削除します。(cleanup.shというファイル名にします)

#!/bin/bash
rm -f /var/www/htdocs/.well-known/acme-challenge/$CERTBOT_TOKEN

certbot renewを実行する

スクリプトの準備ができたので、実行してみます。

–manual-auth-hookには、上記のauthenticator.shを指定、

–manual-cleanup-hookには、上記のcleanup.shを指定して、

certobot renewを実行してみます。

$ sudo certbot renew --manual-auth-hook /var/hogehoge/authenticator.sh --manual-cleanup-hook /var/hogehoge/cleanup.sh


Saving debug log to /var/log/letsencrypt/letsencrypt.log

Processing /etc/letsencrypt/renewal/ftp-exchange.com.conf

Cert is due for renewal, auto-renewing…
Plugins selected: Authenticator manual, Installer None
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for ftp-exchange.com
Waiting for verification…
Cleaning up challenges

new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/ftp-exchange.com/fullchain.pem

Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/ftp-exchange.com/fullchain.pem (success)


うまく行きました。これで証明書の期限が延長されます。めでたしめでたし。

created by Rinker
オライリー・ジャパン
¥4,070 (2024/03/29 10:10:04時点 Amazon調べ-詳細)