[Tips] Google CloudでLet’s EncryptのHTTP認証を一時的に許可する方法

皆さん、Let’s Encryptはご利用でしょうか。サーバ証明書を無償で利用でき、ありがたい存在ですよね。ただその反面、設定や運用を自身で行う必要があるので玄人向けであるとも言えるかと思います。

今回は証明書更新時の課題に関するTipsです。

課題:証明書更新時に一時的にLet’s EncryptのHTTPアクセスを通したい

Let’s Encryptでサーバ証明書を発行・更新する際は、当該FQDNが実在しているかどうかのチェックが行われます。認証方法は下記のいずれかになります。

  • DNS認証
  • HTTP認証

当方ではGoogle Compute Engine上で稼働しているサーバがあり、サーバ証明書はHTTP認証で発行しました。

Let’s Encrypt認証サーバのIPアドレスは公開されていないため、Any(0.0.0.0/0)からのアクセスを許可する必要がありますが、更新時にも認証する必要があるためHTTPを開放しておく必要があります。

しかし本サーバではHTTPでのサービスは行っていないため、常時HTTPを開放していることはセキュリティ上望ましくありません。

解決:バッチ処理で一時的にファイアウォールルールを追加

そこで、バッチ処理で認証時のみ通信を開けて一時的に通信できるようにします。
Google Cloudにはgloud CLIコマンドがありますので、これを用いて一連のジョブとして設定することとします。

gcloudコマンド初期化

gcloudコマンドの初期化は以下コマンドで実施します。

$ sudo gcloud init

指示に従い入力すると、Googleアカウント認証用のURLが表示されます。
アクセスし認証を行うと、認証コードが発行されますので、これを入力します。
デフォルトのプロジェクト、リージョンを指定し完了となります。 

シェルスクリプト

以下のように実装しました。

#!/bin/bash
/bin/gcloud compute firewall-rules create firewall-rule --direction=INGRESS \
  --network=network --action=ALLOW --rules=tcp:80 --source-ranges=0.0.0.0/0 \
  --target-tags=web-server-tag
/usr/local/bin/certbot renew
/bin/gcloud compute firewall-rules delete firewall-rule --quiet

これをcronに設定することで、定期的に更新がなされます。
サーバ証明書更新時の一瞬のみ開ける処理のため、安心になったかと思います。

斜体部は適宜ご自身の環境に読み替えてください。

実行結果

(証明書が更新されなかった場合)

$ sudo ./cert-renew
Creating firewall...?Created [https://www.googleapis.com/compute/v1/projects/project/global/firewalls/firewall-rule].
Creating firewall...done.
NAME           NETWORK     DIRECTION  PRIORITY  ALLOW   DENY  DISABLED
firewall-rule  network     INGRESS    1000      tcp:80        False
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/webserver.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not yet due for renewal

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The following certificates are not due for renewal yet:
  /etc/letsencrypt/live/web-fqdn/fullchain.pem expires on 2022-XX-XX (skipped)
No renewals were attempted.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Deleted [https://www.googleapis.com/compute/v1/projects/project/global/firewalls/firewall-rule].

(証明書が更新された場合)

Creating firewall...
..Created [https://www.googleapis.com/compute/v1/projects/project/global/firewalls/firewall-rule].
NAME          NETWORK  DIRECTION  PRIORITY  ALLOW   DENY  DISABLED
firewall-rule  network  INGRESS    1000      tcp:80        False
done.
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/webserver.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert is due for renewal, auto-renewing...
Non-interactive renewal: random delay of 215.6757469833429 seconds
Plugins selected: Authenticator webroot, Installer None
Renewing an existing certificate for cert-fqdn
Performing the following challenges:
http-01 challenge for cert-fqdn
Waiting for verification...
Cleaning up challenges
Running deploy-hook command: /usr/bin/ct-submit.sh
Output from deploy-hook command ct-submit.sh:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/cert-fqdn/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all renewals succeeded:
  /etc/letsencrypt/live/cert-fqdn/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Running post-hook command: systemctl reload nginx
Deleted [https://www.googleapis.com/compute/v1/projects/project/global/firewalls/firewall-rule].

皆様もご参考にしてください。

参考記事

gcloud app firewall-rules  |  Google Cloud CLI Documentation
GCP VPC の基本的な使い方
概要 GCP VPC について基本的な使い方を記載します。 custom サブネットモードによる VPC 作成 VPC 作成 gcloud compute networks create mynetwork --subnet-mode=cu...
AWS EC2でアクセス元IP制限つきWebサーバーのLet’s Encrypt証明書の取得、自動更新設定(HTTP認証) | 稲葉サーバーデザイン
はじめにHTTP(S)のアクセス元IPアドレスを限定して運用している、プライベート向けWebシステムが、AWS EC2上で稼働しているものとします。このWebサーバー上で、Let's Encrypt証明書を取得、運用する方法についてまとめま...

コメント