Certbot và Cloudflare

Certbot gặp vấn đề với một số nhà cung cấp DNS như Cloudflare. Lỗi thường gặp là phải dành port 80 cho Certbot hay không nhận được phản hồi từ DNS.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Could not bind TCP port 80 because it is already in use by another process on this system (such as a web server). Please stop the program in question and then try again.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Certbot failed to authenticate some domains (authenticator: standalone). The Certificate Authority reported these problems:
  Domain: example.com
  Type:   connection
  Detail: xxx.xxx.xxx.xxx: Fetching http://domain.com/.well-known/acme-challenge/1234567890IZNVMv0fJNPxEOdioyTDLvKpewec0iAf4: Timeout during connect (likely firewall problem)

Người dùng phải tạo TXT record trên DNS để xác nhận là chủ của domain, mà thường phải tạo nhiều record TXT theo yêu cầu của Certbot!

Plugin dns_cloudflare tự động tạo TXT record theo yêu cầu của Certbot, rồi xóa đi khi không cần nữa, bằng cách dùng Cloudflare API. Tuy nhiên người dùng phải chuẩn bị một số bước như sau:

1. Cài đặt Certbot qua snapd (*)

Mặc dù Debian cho phép cài certbot qua dòng lệnh apt install certbot, nhưng https://certbot.eff.org hướng dẫn cài qua snapd mới có được các addon cần thiết (nếu đã cài bằng apt thì phải gở ra).

sudo apt install snapd -y
sudo snap install core && sudo snap refresh core
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot

2. Cài plugin tùy theo nhà cung cấp DNS (**)

sudo snap set certbot trust-plugin-with-root=ok
sudo snap install certbot-dns-cloudflare

3. Tạo token và file cấu hình

mkdir ~/.secrets
echo 'dns_cloudflare_api_token = your_token' > ~/.secrets/cloudflare.ini
sudo chmod 0700 ~/.secrets/
sudo chmod 0400 ~/.secrets/cloudflare.ini

4. Chạy Cerbot với plugin dns_cloudflare

sudo certbot certonly \
--dns-cloudflare \
--dns-cloudflare-credentials ~/.secrets/cloudflare.ini \
--agree-tos \
--no-eff-email \
--staple-ocsp \
--email user@example.com \
--preferred-challenges dns-01 \
-d example.com,*.example.com

Với token được tạo để chứng thực All Zones, có thể chạy certbot với nhiều (sub)domain khác nhau

certbot certonly \
  --dns-cloudflare \
  --dns-cloudflare-credentials ~/.secrets/cloudflare.ini \
  -d example.com \
  -d example.org

Chú thích

+ Cài certbot và plugin qua dòng lệnh

Phiên bản này cũ hơn phiên bản cài qua snap, không hoàn toàn tương thích nhau

apt install python3-certbot-dns-cloudflare

Certbot vẫn làm việc được với các DNS không có plugin.

+ Cài certbot và plugin dùng pip

Cách này tốn dung lượng đĩa hơn cách trên, nhưng cài đặt phiên bản mới nhất của certbot và hoạt động đơn giản hơn snap.

apt install python3-pip
pip install certbot-dns-cloudflare

+ Script lấy SSL qua plugin dns-cloudflare

Giả sử đã cài đặt certbot và plugin cloudflare

#!/bin/bash
function usage(){
cat <<EOT
cf_certbot, version 20220720
© 2022 LNT <lnt@ly-le.info>

Certbot using Cloudflare plugin
usage: cf_certbot -t token -d domain
EOT
exit
}
t=''
d=()
while getopts :t:d: opt; do
  case $opt in
  t) t="$OPTARG";;
  d) d+=("$OPTARG");;
  ?) usage
  esac
done
s=~/.secrets
i=cloudflare.ini
[ -d $s ] || mkdir $s
if [[ -z $t && ! -e $s/$i ]]; then
  usage
else
  echo "dns_cloudflare_api_token = $t" > $s/$i
  sudo chmod 0700 $s
  sudo chmod 0400 $s/$i
fi
if [ ${#d[@]} -ne 0 ]; then
  D="${d[@]}"
  D=${D// /,}
  certbot certonly --dns-cloudflare --dns-cloudflare-credentials $s/$i --agree-tos --no-eff-email --staple-ocsp --email user@$D -d $D
fi

Thí dụ:

./cf_certbot -t token -d example.com -d *.example.com

+ cerbot renew

Sau khi lấy chứng chỉ SSL lần đầu, certbot đã ghi nhớ cách lấy chứng chỉ SSL (có hoặc không dùng plugin), sau này chỉ cần gọi renew thì certbot sẽ lấy chứng chỉ SSL theo cách đã dùng cho từng nhóm domain.

0 0 0 * * /usr/local/bin/certbot renew

+ Hướng dẫn sử dụng plugin của một số nhà cung cấp DNS

Comments Off on Certbot và Cloudflare

Filed under Software

Comments are closed.