
ãLet's EncryptãSSLèšŒææžãcertbotã§èªåæŽæ° (ããŒã80䜿ã£ãŠãªã人åã) ïœPythonã§ããŒã80éæŸãèªååããŠæŽæ°ïœ
ããã«ã¡ã¯Rcatã§ãã
ååã®èšäºã§certbotã䜿çšããSSLèšŒææžã®ååŸãè¡ããŸããã
ãªããšãååŸã«æã¡èŸŒãããã®ã®ãããŒã80ã䜿çšããŠèªèšŒãè¡ããããèªåæŽæ°ã¯ã§ããŸããã§ããã
ãšããããšã§ãä»åã¯æŽæ°ãèªååããããã®ããã°ã©ã ãæžããŠããããšæããŸãã
ååã®èšäºã¯ãã¡ã
èªãã§ããªãå ŽåããããããªãåæãåºãŠããã®ã§å
ã«èªãã§ããããšããå§ãããŸãã
ã¯ããã«
å©çšèŠçŽ
æ å ±ãäœåã®æŽ»çšæã¯äºåã«å©çšèŠçŽãã確èªãã ããã
ã³ã¡ã³ãã«ã€ããŠ
å©çšèŠçŽã®ã¬ã€ãã©ã€ã³ã確èªã®äžã³ã¡ã³ãããŠãã ãã
æŠèŠ
äžéšã®æ¹ã¯ããïŒäœã§èªåæŽæ°ã§ããªãã®?æ®éãŠã§ãã§äœ¿ã£ãŠããã??ããšæã£ããããããŸãããã
åæãšããŠãç§ã¯ããŒã80çªã§WebãµãŒããŒãç«ãŠãŠããŸããããšãããå人ã§ããããããšããŠã人ã£ãŠããªããªãããªãããããªãã§ãããã?ãªã¹ã¯ãé«ãã§ããã
ãªã®ã§certbotã䜿çšããŠèšŒææžãæŽæ°ããã«ã¯ããã®æã ã§80çªããŒããéæŸããå¿ èŠããããŸãã
ãšããããã§ãä»åã¯Pythonã䜿ã£ãŠ80çªããŒãã®éæŸãèªåã§è¡ãèšŒææžã®ååŸãŸã§ãèªååããããšæããŸãã
certbotãšã¯?
å ã»ã©ããäœåãåºãŠããŠããcertbotã§ããããã¡ãã¯Let's EncryptããSSLèšŒææžãç°¡åã«ååŸããããã®ããŒã«ã§ãã
詳ããã¯ä»¥äžã§ç޹ä»ããŠããŸãã
èªåæŽæ°ãµãŒãã¹ã確èªãã
ãµãŒãã¹ã®å 容確èª
ååã¯æåŸã«ç¡å¹åããŸãããããããããã®ãµãŒãã¹ã§ã©ããªããšããã£ãŠããã®ãåèã«ãããã確èªããŸãã
ãã¡ãã®ã³ãã³ãã§ç¢ºèªããŸãã
äžã®æ¹ã§å
¥ã£ããã§ããã
$ sudo ls /etc/systemd/system/ | grep cert
$ sudo ls /lib/systemd/system/ | grep cert
certbot.service
certbot.timer
systemctl cat certbot.service
# /lib/systemd/system/certbot.service
[Unit]
Description=Certbot
Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html
Documentation=https://certbot.eff.org/docs
[Service]
Type=oneshot
ExecStart=/usr/bin/certbot -q renew
PrivateTmp=true
$ systemctl cat certbot.timer
# /lib/systemd/system/certbot.timer
[Unit]
Description=Run certbot twice daily
[Timer]
OnCalendar=*-*-* 00,12:00:00
RandomizedDelaySec=43200
Persistent=true
[Install]
WantedBy=timers.targetæ®éã®ãµãŒãã¹ãšã¿ã€ããŒãšããã®ãå
¥ã£ãŠãŸãããã¿ã€ããŒãšããã®ã¯åããŠèŠãŸããã
ãããŒå匷ã«ãªããŸãããä»åºŠäœ¿ãæ©äŒãããã°äœ¿ããŸãããã
â䜿ããŸãã
ã§ç¢ºèªãããã®ããã¡ã
ExecStart=/usr/bin/certbot -q renew
ãããå®è¡ã³ãã³ãã§ãããã§ãããã§äœããã£ãŠããã®ãããŸã確èªããŸãããã
詳现ã確èªãã
å šãŠã®ãã«ãã衚瀺ããŸãã
certbot -h allãããå®è¡ãããšãèšå€§ãªéã®ãã«ãã衚瀺ãããŸãã
äžã§äœ¿ãããŠãåŒæ°ã¯ä»¥äžã®ããã«ç¢ºèªã§ããŸããã
-qã¯äœèšãªè¡šç€ºããããªããããªæå³ãããã¿ããã§ãã
renewã¯ãæå¹æéã®è¿ãèšŒææžãæŽæ°ãããšããæå³ã¿ããã§ãã
-q, --quiet Silence all output except errors. Useful for automation via cron. Implies --non-interactive. (default: False)
manage:
Various subcommands and flags are available for managing your certificates:
certificates List certificates managed by Certbot
delete Clean up all files related to a certificate
renew Renew all certificates (or one specified with --cert-name)
revoke Revoke a certificate specified with --cert-path or --cert-name
reconfigure Update renewal configuration for a certificate specified by --cert-nameååäœææã®åŒæ°ã調ã¹ãŠã¿ããšã以äžã®ãããªããšãåãããŸããã
certonly
èšŒææžã®ååŸãšæŽæ°ã¯ããŸãããã€ã³ã¹ããŒã«ã¯ããªããšããèšå®ã§ãã
ååŸããåŸãPythonã®ãã©ã«ãã«æã£ãŠãã®ã§ããã§æ£è§£ã§ããã
standalone
èªèšŒçšã«WebãµãŒããŒãç«ã¡äžããèšå®ã¿ããã§ãã
ãªãã»ã©ãããã§WebãµãŒããŒãç«ã¡äžãããããåå80çªã暪åãããããã§ããã
ãŸãã©ã£ã¡ã«ãã80çªã䜿ãã£ãŠããã®ã¯è¯ããªãã®ã§ãããçšã«ãããŠãããŸãã
-d ãã¡ã€ã³
ãã¡ã€ã³ã®æå®ã§ãã
ãã¡ã€ã³ã®éšåã¯ãŸãæŽæ°ã ããèªåã§èªã¿åããšããããã§ãããæ®ãã®2ã€ã¯ãŸãåãåäœãããŠãããã®ã§ããããïŒ
ãšæã£ããæžããŠãããŸãããã©ããããªãã·ã§ã³ã¯åå©çšãããã¿ããã§ãã
renew:
The 'renew' subcommand will attempt to renew any certificates previously obtained if they are close to expiry, and print a summary of the results. By default,
'renew' will reuse the plugins and options used to obtain or most recently renew each certificate. You can test whether future renewals will succeed with `--dry-
run`. Individual certificates can be renewed with the `--cert-name` option. Hooks are available to run commands before and after renewal; see
https://certbot.eff.org/docs/using.html#renewal for more information on these.ã¹ã¯ãªããã倿Žãã
çŸåšã®æŽæ°çšã³ãã³ãã¯ä»¥äžã®ããã«ãªã£ãŠããŸããããã¡ããæ¬¡ã®ããã«æžãæããŸã
å
ã®å
容
ExecStart=/usr/bin/certbot -q renew
倿Žããå
容
ExecStart=/SSD/certbot/venv/bin/certbot renew --pre-hook "/SSD/certbot/portopen.sh open" --post-hook "/SSD/certbot/portopen.
sh close"
倿Žã®ãã€ã³ã
åæã§ãããèªåçã«èšå®ãããæŽæ°ã¿ã€ããŒã䜿ããŸãã
Pythonãªã®ã§ã€ã³ããŒãããŠãè¡ããããšæã£ããã§ãããæã£ãããé£ããããªã®ã§ã¿ã€ããŒæµçšã«ããŸãã
ãŸããå®è¡ãã¡ã€ã«ãå€ããŸãã
ãšããã®ãæ®éãªããã®ãŸãŸã§ããã®ãããããŸããããç§ãä»åãã£ãå
容ã§ã¯ä»®æ³ç°å¢ã«ã€ã³ã¹ããŒã«ããŠããã®ã§ãããããèµ·åããªããã°ãªããªãããã§ãã
次ã«2ã€ã®ãªãã·ã§ã³ã远å ããŸãã
--pre-hook
èšŒææžæŽæ°äœæ¥åã«å®è¡ããã¹ã¯ãªãããæå®ããŸãã
ããã§ããŒããéæŸããã¹ã¯ãªãããåŒã³åºããŸãã--post-hook
èšŒææžæŽæ°äœæ¥åŸã«å®è¡ããã¹ã¯ãªãããæå®ããŸãã
ããã§ããŒããééããã¹ã¯ãªãããåŒã³åºããŸãã
ããããããšã§ããŒãéæŸäœæ¥ãèªåå®è¡ã«çµã¿èŸŒãããšãã§ããŸãã
åäœç¢ºèª
ãªãã·ã§ã³ã®äžã«å®éšã®ããã«äœ¿ã"--dry-run"ãšããã®ãããã®ã§ããããæå®ãã€ã€ãäžã®ã³ãã³ããå®è¡ããŠã¿ãŸãã
$ sudo ./venv/bin/certbot renew --dry-run --pre-hook "/SSD/certbot/portopen.sh open" --post-hook "/SSD/certbot/portopen.sh close"
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/DNS.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if response_ocsp.next_update and response_ocsp.next_update < now - timedelta(minutes=5):
Hook 'pre-hook' ran with output:
æäœã«æåããŸãã
Simulating renewal of an existing certificate for DNS.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations, all simulated renewals succeeded:
/etc/letsencrypt/live/DNS.com/fullchain.pem (success)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Hook 'post-hook' ran with output:
æäœã«æåããŸããããŒãæäœã®ã¹ã¯ãªããã¯çµæãããªã³ãããã®ã§ãæäœãæåããããšãããããŸãã
éµã®æŽæ°ã®æ¹ãæ£åžžã«ã¢ã¯ã»ã¹ã§ããŠããã¿ããã§ãã
ãœãŒã¹ã³ãŒã
ãšããããã§ãäžã§äœ¿ã£ãããŒãéæŸçšã®ã¹ã¯ãªãããèŠãŠã¿ãŸãããã
åŒã³åºãçšã·ã§ã«ã¹ã¯ãªãã
Pythonãèµ·åããããã ãã®ã·ã§ã«ã§ãã
ã³ãã³ãèªäœãrootã§å®è¡ããã®ã§ãä»®æ³ç°å¢ã§å®è¡ããªããšã«ãŒãã®Pythonã§èµ·åããŠããŸãã®ã§ãã©ã€ãã©ãªãèªã¿èŸŒããŸããã
ãã®ãããªåœ¢ã§èµ·åããããã«ããŸãããã

ããŒãéæŸçšPython
ããŒããéæŸããæ¬äœã§ãã
ãã®ã¹ã¯ãªããã«ã¯åŒæ°ã«"open"ã"close"ã®æååãæž¡ãããšã§åäœã倿Žã§ããŸãã
ãã®ã¹ã¯ãªããã§ã¯ãããŒãéæŸæã«30åãšããæéå¶éãèšããŠããŸããäžãäžã¯ããŒãºãããŸããããªãã£ãå Žåã§ãã30ååŸã«ã¯åæã«éããŸãã

ã¡ãªã¿ã«ã©ã€ãã©ãªã®ã€ã³ããŒãã¯ãããªæãã«ãªã£ãŠããŸãã
ããŒããéããããã®msãšããã®ã¯èªäœã®ããŒãæäœã¢ãžã¥ãŒã«ã§ãã

M_search2ã¢ãžã¥ãŒã«ã«é¢ããŠã¯ä»¥äžã®èšäºã§ç޹ä»/é åžããŠããŸãã
systemdã«åæ ããã
ã§ã¯ãä»ãŸã§ã®å 容ããŸãšããŠæäœã«åæ ãããŸãããã
ãŠããããã¡ã€ã«ãç·šéãã
ãŸãã¯systemdã®ãŠããããã¡ã€ã«ãç·šéããŸãã
äžéšã ãäžæžããšããããšãã§ããã¿ãããªã®ã§ãä»åã¯ããã§è¡ããŸãã
sudo systemctl edit certbot.serviceãšãã£ã¿ãŒãèµ·åããã®ã§äžã®æ¹ã«ããã³ã¡ã³ããšã³ã¡ã³ãã®éã«å€æŽããè¡ã远å ããŸãã
ä»åã¯Serviceã®è¡ã远å ããŠããã®ãããããŸãã
ExecStartã ããæžã蟌ããšããŸããããªãã£ãã®ã§ãã¡ãã£ãšèª¿ã¹ãã以äžã¿ãããªæžãæ¹ãããã£ãŠããã®ããã£ãã®ã§è©Šãããšãããããã§ããŸããããŸããã
### Editing /etc/systemd/system/certbot.service.d/override.conf
### Anything between here and the comment below will become the new contents of the file
[Service]
ExecStart=
ExecStart=/SSD/certbot/venv/bin/certbot renew --pre-hook "/SSD/certbot/portopen.sh open" --post-hook "/SSD/certbot/portopen.sh close"
### Lines below this comment will be discarded
### /lib/systemd/system/certbot.service
# [Unit]
# Description=Certbot
# Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html
# Documentation=https://certbot.eff.org/docs
# [Service]
# Type=oneshot
# ExecStart=/usr/bin/certbot -q renew
# PrivateTmp=trueä¿åããæã«å€ãªååã§ä¿åãããã®ã§ãããããã§ååããã¡ããšæå®ããªããšå¯Ÿå¿ãããŸããã
ãã¡ã€ã«åã¯ä»¥äžã®æãã«ãªãããã«ããŠãã ããã
/etc/systemd/system/certbot.service.d/override.conf
ããã©ã«ãã§ã¯ãæåŸã®ååã®ãšããã«ã©ã³ãã ãªæååãä»ããŠããããããšæãã®ã§ããããã£ããã®ãå€ããŸãã
å
容ã衚瀺ããŠã¿ãŸãã
ãªããå¢ããŠãŸããã
ããããŸããã£ãæããªãã§ããããïŒ
ãŸããå®éã«åããã°ãããã§ãããã
$ systemctl cat certbot.service
# /lib/systemd/system/certbot.service
[Unit]
Description=Certbot
Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html
Documentation=https://certbot.eff.org/docs
[Service]
Type=oneshot
ExecStart=/usr/bin/certbot -q renew
PrivateTmp=true
# /etc/systemd/system/certbot.service.d/override.conf
### Editing /etc/systemd/system/certbot.service.d/override.conf
### Anything between here and the comment below will become the new contents of the file
[Service]
ExecStart=
ExecStart=/SSD/certbot/venv/bin/certbot renew --pre-hook "/SSD/certbot/portopen.sh open" --post-hook "/SSD/certbot/portopen.sh close"
### Lines below this comment will be discarded
### /lib/systemd/system/certbot.service
# [Unit]
# Description=Certbot
# Documentation=file:///usr/share/doc/python-certbot-doc/html/index.html
# Documentation=https://certbot.eff.org/docs
# [Service]
# Type=oneshot
# ExecStart=/usr/bin/certbot -q renew
# PrivateTmp=trueã¡ãªã¿ã«ãåæã«èšå®ãããŠããèªåæŽæ°ã¯ä»¥äžã®ããã«ãªã£ãŠããŠèœã¡ãŠããŸããã
ãããããã§ããããåããªããã¡ã€ã«ãæå®ããŠãããã§ãããã
ã€ãŸãããã®ãµãŒãã¹ãåéããæã«å³èœã¡ããªããã°ããŸããã£ããšãèšããŸãã
$ systemctl status certbot.service
à certbot.service - Certbot
Loaded: loaded (/lib/systemd/system/certbot.service; static)
Active: failed (Result: exit-code) since Thu 2024-09-05 13:15:02 JST; 2 days ago
Docs: file:///usr/share/doc/python-certbot-doc/html/index.html
https://certbot.eff.org/docs
Main PID: 1484963 (code=exited, status=1/FAILURE)
CPU: 101ms
9æ 05 13:15:02 rcat-Green-G5 certbot[1484963]: File "/usr/lib/python3/dist-packages/OpenSSL/crypto.py", line 17, in <module>
9æ 05 13:15:02 rcat-Green-G5 certbot[1484963]: from OpenSSL._util import (
9æ 05 13:15:02 rcat-Green-G5 certbot[1484963]: File "/usr/lib/python3/dist-packages/OpenSSL/_util.py", line 6, in <module>
9æ 05 13:15:02 rcat-Green-G5 certbot[1484963]: from cryptography.hazmat.bindings.openssl.binding import Binding
9æ 05 13:15:02 rcat-Green-G5 certbot[1484963]: File "/usr/lib/python3/dist-packages/cryptography/hazmat/bindings/openssl/binding.py", line 14, in <module>
9æ 05 13:15:02 rcat-Green-G5 certbot[1484963]: from cryptography.hazmat.bindings._openssl import ffi, lib
9æ 05 13:15:02 rcat-Green-G5 certbot[1484963]: ModuleNotFoundError: No module named '_cffi_backend'
9æ 05 13:15:02 rcat-Green-G5 systemd[1]: certbot.service: Main process exited, code=exited, status=1/FAILURE
9æ 05 13:15:02 rcat-Green-G5 systemd[1]: certbot.service: Failed with result 'exit-code'.
9æ 05 13:15:02 rcat-Green-G5 systemd[1]: Failed to start Certbot.ãµãŒãã¹ãåèµ·åãã
ãšããããã§ãªã¹ã¿ãŒãããŠã¿ãŸãã
ãã°ã確èªãããšå³èœã¡ã¯ããŠããªãã¿ããã§ããã
$ sudo systemctl restart certbot.service
$ sudo systemctl status certbot.service
â certbot.service - Certbot
Loaded: loaded (/lib/systemd/system/certbot.service; static)
Drop-In: /etc/systemd/system/certbot.service.d
mqoverride.conf
Active: inactive (dead)
Docs: file:///usr/share/doc/python-certbot-doc/html/index.html
https://certbot.eff.org/docs
9æ 08 13:27:35 rcat-Green-G5 certbot[2356085]: if response_ocsp.next_update and response_ocsp.next_update < now - timedelta(minutes=5):
9æ 08 13:27:35 rcat-Green-G5 certbot[2356085]: Certificate not yet due for renewal
9æ 08 13:27:35 rcat-Green-G5 certbot[2356085]: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9æ 08 13:27:35 rcat-Green-G5 certbot[2356085]: The following certificates are not due for renewal yet:
9æ 08 13:27:35 rcat-Green-G5 certbot[2356085]: /etc/letsencrypt/live/DNS.com/fullchain.pem expires on 2024-11-26 (skipped)
9æ 08 13:27:35 rcat-Green-G5 certbot[2356085]: No renewals were attempted.
9æ 08 13:27:35 rcat-Green-G5 certbot[2356085]: No hooks were run.
9æ 08 13:27:35 rcat-Green-G5 certbot[2356085]: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9æ 08 13:27:35 rcat-Green-G5 systemd[1]: certbot.service: Deactivated successfully.
9æ 08 13:27:35 rcat-Green-G5 systemd[1]: Finished Certbot.ãã®ç¶æ
ã ãšããããäœãããŠãããªãã£ãã®ã§ããã©ã€ã©ã³ãå
¥ãçŽããŠå床å®è¡ããŠã¿ãŸããã
ãã°ã®äžã«ããŒãéæŸãæäœããèšè¿°ããããŸãããã©ãããããŸããã£ãŠãã¿ããã§ãã
$ sudo systemctl status certbot.service
â certbot.service - Certbot
Loaded: loaded (/lib/systemd/system/certbot.service; static)
Drop-In: /etc/systemd/system/certbot.service.d
mqoverride.conf
Active: inactive (dead)
Docs: file:///usr/share/doc/python-certbot-doc/html/index.html
https://certbot.eff.org/docs
9æ 08 13:45:22 rcat-Green-G5 certbot[2375987]: æäœã«æåããŸãã
9æ 08 13:45:22 rcat-Green-G5 certbot[2375987]: Simulating renewal of an existing certificate for rcatnekokawaii.dynu.net
9æ 08 13:45:31 rcat-Green-G5 certbot[2375987]: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9æ 08 13:45:31 rcat-Green-G5 certbot[2375987]: Congratulations, all simulated renewals succeeded:
9æ 08 13:45:31 rcat-Green-G5 certbot[2375987]: /etc/letsencrypt/live/rcatnekokawaii.dynu.net/fullchain.pem (success)
9æ 08 13:45:31 rcat-Green-G5 certbot[2375987]: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9æ 08 13:45:32 rcat-Green-G5 certbot[2375987]: Hook 'post-hook' ran with output:
9æ 08 13:45:32 rcat-Green-G5 certbot[2375987]: æäœã«æåããŸãã
9æ 08 13:45:32 rcat-Green-G5 systemd[1]: certbot.service: Deactivated successfully.
9æ 08 13:45:32 rcat-Green-G5 systemd[1]: Finished Certbot.ã¿ã€ããŒãåèµ·åãã
å忢ããŠããŸã£ãã¿ã€ããŒãåèµ·åããŸãã
èªåèµ·åãæå¹ã«ããŠãªã¹ã¿ãŒãããããŸãã
$ sudo systemctl enable certbot.timer
Created symlink /etc/systemd/system/timers.target.wants/certbot.timer â /lib/systemd/system/certbot.timer.
$ sudo systemctl restart certbot.timerã¹ããŒã¿ã¹ã確èªãããšä»¥äžã®ããã«ãªã£ãŠããŸããã
æ£åžžã«åãåºããã¿ããã§ãã
systemctl status certbot.timer
â certbot.timer - Run certbot twice daily
Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: enabled)
Active: active (waiting) since Sun 2024-09-08 13:53:25 JST; 36s ago
Trigger: Mon 2024-09-09 10:30:09 JST; 20h left
Triggers: â certbot.service
9æ 08 13:53:25 rcat-Green-G5 systemd[1]: Started Run certbot twice daily.説æãèŠããšãããæéã1ã¶æã«è¿«ããšæŽæ°ããããã§ãã
ä»ã®ãšããç¥ããªããã¡ã«æŽæ°ãããŠããŸãã
ãŸãšã
ä»åã¯ç¡æã®SSLèšŒææžã®èªåæŽæ°ã«ã€ããŠèããŠã¿ãŸããã
æ®éã®ç°å¢ã§ããã°ãæ¢åã®WebãµãŒããŒã䜿ã£ãŠç°¡åã«æŽæ°ã§ããããã«èšèšãããŠããã¿ããã§ãããç§ã®ããã«ã¡ãã£ãšéãäœ¿ãæ¹ããã人ã«ãšã£ãŠã¯å°ã
工倫ãå¿
èŠã§ããã
ãšã¯ãããç°¡åã§ããããšã«ã¯ééããªãã®ã§éåžžã«ãããããã§ããã
ããã§ã¯ãŸããäŒãããŸãããã