service:letsencrypt
Различия
Показаны различия между двумя версиями страницы.
| Предыдущая версия справа и слеваПредыдущая версияСледующая версия | Предыдущая версия | ||
| service:letsencrypt [20.07.2022 08:47] – [Создание сертификата для HAProxy средствами Certbot] viacheslav | service:letsencrypt [22.05.2025 11:12] (текущий) – [Docker] viacheslav | ||
|---|---|---|---|
| Строка 1: | Строка 1: | ||
| + | ====== Let's encrypt ====== | ||
| + | |||
| + | Сервис по выдаче и автоматическому обновлению бесплатных SSL-сертификатов. Для Windows пока нет стандартной процедуры, | ||
| + | |||
| + | ===== Установка ===== | ||
| + | |||
| + | [[https:// | ||
| + | |||
| + | Чтобы сделать сертификат для домена, | ||
| + | <code bash> | ||
| + | certbot --apache --domain test.example.org | ||
| + | </ | ||
| + | |||
| + | ===== Настройка автообновления ===== | ||
| + | Проверять актуальность сертификата и обновлять при необходимости каждый день в 8:00 и 20:00. | ||
| + | <code bash> | ||
| + | nano / | ||
| + | </ | ||
| + | |||
| + | Добавить туда | ||
| + | < | ||
| + | 0 8,20 * * * root letsencrypt renew | ||
| + | </ | ||
| + | |||
| + | ===== Для Haproxy ===== | ||
| + | Штука в том, что порты 80 и 443 заняты, | ||
| + | |||
| + | <file bash / | ||
| + | # На фронтенде :80 | ||
| + | # Let's Encrypt URL | ||
| + | acl letsencrypt_url path_beg / | ||
| + | # Не пробрасывать на HTTPS | ||
| + | http-request redirect scheme https if !{ ssl_fc } !no-https-domains !letsencrypt_url | ||
| + | # Let's Encrypt backend | ||
| + | use_backend be_letsencrypt if letsencrypt_url | ||
| + | |||
| + | # Бэкенд | ||
| + | backend be_letsencrypt | ||
| + | server letsencrypt 127.0.0.1: | ||
| + | </ | ||
| + | |||
| + | <code bash> | ||
| + | # Получить сертификат для example.com и www.example.com | ||
| + | certbot certonly --standalone --preferred-challenges http-01 --http-01-port 54321 --keep --agree-tos --expand -m ssl@example.com -d example.com -d www.example.com | ||
| + | :' | ||
| + | Saving debug log to / | ||
| + | Requesting a certificate for example.com and www.example.com | ||
| + | |||
| + | Successfully received certificate. | ||
| + | Certificate is saved at: / | ||
| + | Key is saved at: / | ||
| + | This certificate expires on 2022-08-18. | ||
| + | These files will be updated when the certificate renews. | ||
| + | Certbot has set up a scheduled task to automatically renew this certificate in the background. | ||
| + | |||
| + | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| + | If you like Certbot, please consider supporting our work by: | ||
| + | * Donating to ISRG / Let`s Encrypt: | ||
| + | * Donating to EFF: https:// | ||
| + | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | ||
| + | ' | ||
| + | </ | ||
| + | <WRAP round info> | ||
| + | " | ||
| + | Если Certbot установлен через snap, то вместо cron он добавляет задачи на обновление сертификата в systemd timer. Посмотреть: | ||
| + | <code bash> | ||
| + | systemctl list-timers | ||
| + | </ | ||
| + | </ | ||
| + | https:// | ||
| + | [[https:// | ||
| + | |||
| + | |||
| + | <file bash / | ||
| + | #!/bin/bash | ||
| + | |||
| + | LE_CERT_DIR=/ | ||
| + | HAPROXY_CERT_DIR=/ | ||
| + | |||
| + | # Cat the certificate chain and the private key together for haproxy | ||
| + | for path in $(find $LE_CERT_DIR/ | ||
| + | cat $LE_CERT_DIR/ | ||
| + | done | ||
| + | </ | ||
| + | https:// | ||
| + | https:// | ||
| + | https:// | ||
| + | https:// | ||
| + | https:// | ||
| + | |||
| + | ==== Выпуск сертификата для HAProxy ==== | ||
| + | <file bash / | ||
| + | #!/bin/bash | ||
| + | |||
| + | for d in " | ||
| + | domains+=" | ||
| + | done | ||
| + | |||
| + | certbot certonly --standalone --preferred-challenges http-01 --http-01-port 54321 --keep --agree-tos --expand -m ssl@example.com $domains && \ | ||
| + | cat / | ||
| + | systemctl reload haproxy | ||
| + | </ | ||
| + | Использование: | ||
| + | <code bash> | ||
| + | chmod u+x / | ||
| + | # Файл сертификата будет назван по имени первого аргумента (/ | ||
| + | # Все аргументы будут добавлены в команду как домены (-d example.com -d www.example.com и т. д.) | ||
| + | / | ||
| + | </ | ||
| + | |||
| + | ==== Перевыпуск сертификата для HAProxy (автомат) ==== | ||
| + | <file bash / | ||
| + | #!/bin/bash | ||
| + | cat $RENEWED_LINEAGE/ | ||
| + | |||
| + | # --deploy-hook DEPLOY_HOOK | ||
| + | # Command to be run in a shell once for each | ||
| + | # successfully issued certificate. For this command, the | ||
| + | # shell variable $RENEWED_LINEAGE will point to the | ||
| + | # config live subdirectory (for example, | ||
| + | # "/ | ||
| + | # new certificates and keys; the shell variable | ||
| + | # $RENEWED_DOMAINS will contain a space-delimited list | ||
| + | # of renewed certificate domains (for example, | ||
| + | # " | ||
| + | </ | ||
| + | |||
| + | Перезапуск HAProxy | ||
| + | <code bash> | ||
| + | ### / | ||
| + | |||
| + | #!/bin/bash | ||
| + | if [[ $(find / | ||
| + | then | ||
| + | systemctl reload haproxy.service | ||
| + | fi | ||
| + | |||
| + | # to cron | ||
| + | echo -e "\n# Reload HAProxy if there are new certs\n0 3\t* * *\troot\t/ | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== Для Cisco ASA ===== | ||
| + | Сама она не умеет (asa5545X). Сетевики должны прокинуть на ней 80-й порт на внутренний/ | ||
| + | |||
| + | Далее всё делается на сервере. | ||
| + | <code bash> | ||
| + | # Необходимо установить certbot и expect для автоматизации работы с Cisco через ssh. | ||
| + | sudo snap install --classic certbot | ||
| + | sudo apt install expect -y | ||
| + | # Выпустить сертификат | ||
| + | certbot certonly --standalone --preferred-challenges http-01 --keep --agree-tos --expand -m ssl@example.com -d vpn.example.com | ||
| + | </ | ||
| + | |||
| + | Конфиг состоит из 3 файлов. | ||
| + | <code bash> | ||
| + | # скрипт, | ||
| + | touch / | ||
| + | chmod 700 / | ||
| + | # скрипт expect | ||
| + | touch / | ||
| + | chmod 700 / | ||
| + | # пароль экспорта сертификата для openssl | ||
| + | touch / | ||
| + | chmod 600 / | ||
| + | # Записать пароль в файл пароля | ||
| + | nano / | ||
| + | </ | ||
| + | |||
| + | <file bash / | ||
| + | #!/bin/bash | ||
| + | |||
| + | openssl pkcs12 -export \ | ||
| + | -password file:/ | ||
| + | -in $RENEWED_LINEAGE/ | ||
| + | -inkey $RENEWED_LINEAGE/ | ||
| + | -out / | ||
| + | |||
| + | openssl base64 -in / | ||
| + | |||
| + | / | ||
| + | </ | ||
| + | |||
| + | <file tcl / | ||
| + | # | ||
| + | |||
| + | set timeout 5 | ||
| + | set send_slow {10 .001} | ||
| + | set sshUser " | ||
| + | set sshIP " | ||
| + | set sshPass " | ||
| + | set exportPass [exec cat / | ||
| + | #set log "/ | ||
| + | |||
| + | #log_file -noappend $log | ||
| + | #log_user 0 | ||
| + | |||
| + | spawn ssh -o KexAlgorithms=+diffie-hellman-group1-sha1 -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no $sshUser@$sshIP | ||
| + | expect " | ||
| + | send -- " | ||
| + | expect ">" | ||
| + | send -- " | ||
| + | expect " | ||
| + | send -- " | ||
| + | expect "#" | ||
| + | #log_user 1 | ||
| + | send -- " | ||
| + | expect " | ||
| + | send -- "no crypto ca trustpoint ca_le\r" | ||
| + | expect { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | send -- " | ||
| + | expect " | ||
| + | send -- " | ||
| + | |||
| + | expect "#" | ||
| + | send -- " | ||
| + | expect " | ||
| + | |||
| + | #log_user 0 | ||
| + | send -- " | ||
| + | expect " | ||
| + | send -- [exec cat / | ||
| + | send -s " | ||
| + | |||
| + | # % The CA cert is not self-signed. | ||
| + | # % Do you also want to create trustpoints for CAs higher in the hierarchy? [yes/no]: | ||
| + | # OR | ||
| + | # % You already have RSA or ECDSA keys named ca_le. | ||
| + | # % If you replace them, all device certs issued using these keys | ||
| + | # % will be removed. | ||
| + | # % Do you really want to replace them? [yes/no]: | ||
| + | expect { | ||
| + | " | ||
| + | " | ||
| + | } | ||
| + | #log_user 1 | ||
| + | |||
| + | send -- "ssl trust-point ca_le outside\r" | ||
| + | |||
| + | expect " | ||
| + | send -- " | ||
| + | expect "#" | ||
| + | send -- " | ||
| + | expect eof | ||
| + | </ | ||
| + | |||
| + | ++++ Пример выполнения скрипта | | ||
| + | < | ||
| + | spawn ssh -o KexAlgorithms=+diffie-hellman-group1-sha1 -o HostKeyAlgorithms=+ssh-rsa -o StrictHostKeyChecking=no sshuser@192.168.1.254 | ||
| + | sshuser@192.168.1.254' | ||
| + | User sshuser logged in to gate | ||
| + | Logins over the last 91 days: 54. Last login: 11:39:24 MSK Jan 9 2023 from 192.168.1.100 | ||
| + | Failed logins since the last login: 0. | ||
| + | Type help or '?' | ||
| + | gate> enable | ||
| + | Password: ********** | ||
| + | gate# configure terminal | ||
| + | gate(config)# | ||
| + | WARNING: Removing an enrolled trustpoint will destroy all | ||
| + | certificates received from the related Certificate Authority. | ||
| + | |||
| + | Are you sure you want to do this? [yes/no]: yes | ||
| + | INFO: Be sure to ask the CA administrator to revoke your certificates. | ||
| + | gate(config)# | ||
| + | gate(config-ca-trustpoint)# | ||
| + | gate(config-ca-trustpoint)# | ||
| + | gate(config)# | ||
| + | |||
| + | Enter the base 64 encoded pkcs12. | ||
| + | End with the word " | ||
| + | MIIWzwIBAzCCFoUGCSqGSIb3DQEHAaCCFnYEghZyMIIWbjCCEOIGCSqGSIb3DQEH | ||
| + | BqCCENMwghDPAgEAMIIQyAYJKoZIhvcNAQcBMFcGCSqGSIb3DQEFDTBKMCkGCSqG | ||
| + | ... | ||
| + | dt/ | ||
| + | D3XnctPJR1TYytiVRCaVWuZHz+G0BAiEKmqw9Y6C4AICCAA= | ||
| + | quit | ||
| + | % You already have RSA or ECDSA keys named ca_le. | ||
| + | % If you replace them, all device certs issued using these keys | ||
| + | % will be removed. | ||
| + | % Do you really want to replace them? [yes/no]: yes | ||
| + | |||
| + | Trustpoint ' | ||
| + | |||
| + | Trustpoint CA certificate accepted. | ||
| + | WARNING: CA certificates can be used to validate VPN connections, | ||
| + | by default. | ||
| + | trustpoint to limit the validation scope, if necessary. | ||
| + | INFO: Import PKCS12 operation completed successfully. | ||
| + | gate(config)# | ||
| + | gate(config)# | ||
| + | gate# exit | ||
| + | |||
| + | Logoff | ||
| + | |||
| + | Connection to 192.168.1.254 closed by remote host. | ||
| + | Connection to 192.168.1.254 closed. | ||
| + | </ | ||
| + | ++++ | ||
| + | |||
| + | [[https:// | ||
| + | [[https:// | ||
| + | [[https:// | ||
| + | |||
| + | ===== Docker ===== | ||
| + | <code bash> | ||
| + | mkdir -p $HOME/ | ||
| + | |||
| + | port=54321 | ||
| + | |||
| + | docker run -it --rm --name certbot -u $(id -u):$(id -g) \ | ||
| + | -v " | ||
| + | -v " | ||
| + | -v " | ||
| + | -p $port:$port certbot/ | ||
| + | certonly --test-cert --standalone --preferred-challenges http-01 --http-01-port $port \ | ||
| + | --config-dir /tmp/conf --work-dir /tmp/work --logs-dir /tmp/logs \ | ||
| + | --keep --agree-tos --no-eff-email --expand -m email@example.com -d example.com | ||
| + | </ | ||
| + | |||
| + | https:// | ||
| + | [[https:// | ||
| + | [[https:// | ||
| + | https:// | ||
