Giter VIP home page Giter VIP logo

jinhucheung / letscertbot Goto Github PK

View Code? Open in Web Editor NEW
144.0 7.0 29.0 116 KB

Let's Certbot is a tool builds automated scripts base on Certbot for obtaining, renewing, deploying SSL certificates.

Home Page: https://github.com/jinhucheung/letscertbot

License: MIT License

Python 99.02% Dockerfile 0.26% Shell 0.72%
certbot letsencrypt certificates ssl-certificates deployment-automation qcloud godaddy aliyun devops acme

letscertbot's Introduction

Let's Certbot

中文文档 Chinese document

Let's Certbot is a tool builds automated scripts base on Certbot for obtaining, renewing, deploying SSL certificates.

In order to verify your domains, Let's Certbot uses dns challenge on Certbot. Compared to http challenge, it means you can obtain a wildcard certificate and don't need to touch webserver.

On dns challenge, you need to set a TXT DNS record with specific contents on domain. Let's Certbot will help you do it via domain name registrar DNS API.

Supports domain name registrar at persent:

Example

example

Installation

Let's Certbot as a Certbot tool supports docker and non-docker environments.

Downloading Repository

Clone this repository to get Let's Certbot:

$ git clone [email protected]:jinhucheung/letscertbot.git

Then copy configurations:

$ cd letscertbot
$ cp config.json.example config.json

Installing with Docker

Run Let's Certbot with Docker:

$ sudo docker run --rm --name letscertbot -v "$your_letscertbot_home/config.json:/app/config.json" -v "$your_letscertbot_home/tlds.txt:/app/tlds.txt" -v "/etc/letsencrypt:/etc/letsencrypt" -v "/var/lib/letsencrypt:/var/lib/letsencrypt" jimcheung/letscertbot

You can run Let's Certbot with Compose if Docker Compose is installed:

$ sudo docker-compose run --rm app

Installing without Docker

Let's Certbot requires Python 2.7 or 3.4+ running on a UNIX-like operation system.

First, you need to confirm if python is installed:

$ python --version

If everything is ok, get Certbot on Official Document for your system.

After installing Certbot, run Certbot with root:

$ sudo certbot --version

Usage

Configuration

Before running Let's Certbot, you have the following configuration to change:

Name Required Description Default
base.email true Email address for important renewal notifications
dns true dns settings
dns.wait_time false dns challenge wait time (seconds) 20
dns.aliyun false Access Key Id and Access Key Secret of Aliyun account
dns.qcloud false Secret Id and Secret Key of Tencent Cloud account
dns.huaweicloud false Access Key Id and Secret Access Key of Huawei Cloud account
dns.godaddy false API Key and API Secret of GoDaddy account
dns.cloudflare false API Key or API Token of Cloudflare account
log.enable false Whether to enable log tracker false
log.logfile false The path of log file ./log/application.log
deploy.servers false The deployment servers
deploy.server.enable false Whether to run deployment script for server false
deploy.server.host false The host of deployment server, set "localhost" for local server, required on deploy.
deploy.server.port false The port of remote server SSH daemon 22
deploy.server.user false The user of remote server uses SSH login, run command root
deploy.server.password false The password of remote user
deploy.server.deploy_to false The stored path of certificate in server /etc/letsencrypt/live
deploy.server.restart_nginx false Whether to restart nginx in server false
deploy.server.after_hook false The command that server runs after successful deployment

In addition, tlds.txt contains some top level domains(TLD) and second level domains(SLD) for separating subdomain and main domain. If the TLD or SLD of your domain is not existed in tlds.txt, you need to append it in list.

DNS API

Before obtaining certificate, you can run manual script (manual.py) to test DNS API with with your access key:

# Running with docker
$ sudo docker-compose run --rm app manual --test --domain your.example.com --dns aliyun

# Running without docker
$ sudo python ./bin/manual.py --test --domain your.example.com --dns aliyun

The script will place _acme-challenge TXT record under your domain via specified DNS API.

Obtainment

Run the obtainment script (obtain.py) with root for obtaining certificate:

# Running with docker
$ sudo docker-compose run --rm app obtain -d your.example.com *.your.example.com

# Running without docker
$ sudo python ./bin/obtain.py -d your.example.com *.your.example.com

Then you will get a wildcard certificate names your.example.com in /etc/letsencrypt/live/

You can specify certificate name with --cert argument:

# Running with docker
$ sudo docker-compose run --rm app obtain -d x.example.com y.example.com --cert xny.example.com

# Running without docker
$ sudo python ./bin/obtain.py -d x.example.com y.example.com --cert xny.example.com

If your domain name registrar doesn't support api access, or if you're concerned about security problems from giving the access tokento your main domain, then you can use DNS alias argument:

$ sudo docker-compose run --rm app obtain -d x.main_domain.com y.main_domain.com --dns qcloud --challenge-alias alias_domain.com

# Running without docker
$ sudo python ./bin/obtain.py -d x.main_domain.com y.main_domain.com --dns qcloud --challenge-alias alias_domain.com

In the above command, Let's Certbot transfers x.main_domain.com, y.main_domain.com challenge to alias_domain.com, and sets the txt record of alias_domain.com via qcloud API. So you need to add CNAME record for challenged domain in advance:

_acme-challenge.x.main_domain.com => _acme-challenge.alias_domain.com
_acme-challenge.y.main_domain.com => _acme-challenge.alias_domain.com

Renewal

Renew certificates with the renewal script (renewal.py):

# Running with docker
$ sudo docker-compose run --rm app renewal

# Running without docker
$ sudo python ./bin/renewal.py

Then Certbot will try renew all certificates which will be expired soon.

You can add renewal script as schedule task to crontab:

# Running with docker
0 0 */7 * * sudo docker-compose -f $your_letscertbot_home/docker-compose.yml run --rm app renewal > /var/log/letscertbot-renewal.log 2>&1

# Running without docker
0 0 */7 * * sudo $your_letscertbot_home/bin/renewal.py > /var/log/letscertbot-renewal.log 2>&1

The task will run renewal script every 7 days.

If you need to force renew specified certificates, provide --force and --certs arguments:

# Running with docker
$ sudo docker-compose run --rm app renewal --certs xny.example.com --force

# Running without docker
$ sudo python ./bin/renewal.py --certs xny.example.com --force

Deployment

If you set deploy.server.enable to true, Certbot will run the deployment script (deploy.py) on deploy hook. The script receives renewed certificate and push it to configured servers.

Let's Certbot deploys certificate to remote server via SSH, it means that local server runs Certbot must be able to connect remote server. In order to connect, you need to add the public key of local server to remote server or provide deploy.server.password for sshpass.

In order to add certificate to deploy.server.deploy_to or restart nginx, Let's Certbot requires deploy.server.user has permissions.

You can get deployment script by running the following command:

# Running with docker
$ sudo docker-compose run --rm app deploy --check

# Running without docker
$ sudo python ./bin/deploy.py --check

And push certificate to server:

# Running with docker
$ sudo docker-compose run --rm app deploy --push --cert $certificate_name --server $server_host

# Running without docker
$ sudo python ./bin/deploy.py --push --cert $certificate_name --server $server_host

Note: If deploy.server enables SELinux in enforcing mode, you need to confirm that nginx has access to the SElinux security context of deploy.server.deploy_to.

Note: If you run Let's Certbot via container and restart nginx in local server, you should set local server as remote.

Thanks

Contributing

Bug report or pull request are welcome.

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)

Please write unit test with your code if necessary.

License

The repository is available as open source under the terms of the MIT License.

letscertbot's People

Contributors

brobird avatar jinhucheung avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

letscertbot's Issues

SSL 证书部署到执行脚本的机器问题

问题描述

当将 SSL 证书部署到执行脚本的机器,并将 deploy_to 设置为 /etc/letsencrypt/live。部署脚本将拷贝最新的 SSL 证书到 live 目录,这破坏了原证书的软连接,导致下次续期时脚本执行出错。

重新步骤

  1. config.json deploy 节点 添加以下配置:
  "deploy": {
    "enable": true,
    "keep_backups": 3,
    "servers": [
      {
        "host": "localhost",
        "port": 22,
        "user": "root",
        "password": "",
        "deploy_to": "/etc/letsencrypt/live",
        "nginx": {
          "restart": true
        }
      }
    ]
  }
  1. 执行强制续期脚本
$ sudo python ./bin/renewal.py --force
  1. 在部署成功后,再执行强制续期脚本,此时续期失败

期待

SSL 证书部署时,将关联的续期信息也一同部署

ERROR: unsatisfiable constraints

image

[root@iZuf61m0zmp2dl71ksncolZ letscertbot]# docker-compose run --rm app
Creating network "letscertbot_default" with the default driver
Building app
Step 1/5 : FROM certbot/certbot
---> 66cd8df4ec6e
Step 2/5 : RUN apk update && apk add openssh sshpass mandoc man-pages
---> Running in 77151bf9c294
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/main/x86_64/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.11/community/x86_64/APKINDEX.tar.gz
v3.11.11-84-ge67f5bdecb [http://dl-cdn.alpinelinux.org/alpine/v3.11/main]
v3.11.11-82-g6efedde556 [http://dl-cdn.alpinelinux.org/alpine/v3.11/community]
OK: 11293 distinct packages available
ERROR: unsatisfiable constraints:
mandoc (missing):
required by: world[mandoc]
ERROR: Service 'app' failed to build : The command '/bin/sh -c apk update && apk add openssh sshpass mandoc man-pages' returned a non-zero code: 1
[root@iZuf61m0zmp2dl71ksncolZ letscertbot]#

有两个域名证书,强制更新其中一个的时候会报错

有两个域名证书(test.com test1.com),强制更新其中一个(test.com)的时候会报错(主要是因为两个证书对应的阿里云账号不是同一个,不知道如何处理key的事情,先指定其中一个,结果报错)。
如果不加 --certs test.com --force ,没有出现报错。

另: 请问 --certs test.com --force, 包含了 *.test.com吗?

[root@iZuf61m0zmp2dl71ksncolZ letscertbot]# docker-compose -f /home/letscertbot/docker-compose.yml run --rm app renewal --certs test.com --force
Creating letscertbot_app_run ... done
Saving debug log to /var/log/letsencrypt/letsencrypt.log


Processing /etc/letsencrypt/renewal/test.com.conf


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 (test.com) from /etc/letsencrypt/renewal/test.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/test.com/fullchain.pem (failure)

Certbot 验证模式支持

支持 Certbot 除 manual 外的验证模式,其余模式如下:

--apache 使用Apache插件进行身份认证和安装
--standalone 运行一个独立的网页服务器用于身份认证
--nginx 使用Nginx插件进行身份认证和安装
--webroot 将文件放置在网站根目录中以进行身份​​验证

DNS problem: SERVFAIL looking up TXT for _acme-challenge.hifool.cn - the domain's

域名在华为云。运行测试是通过的

[rui@Alicloud letscertbot]$ sudo docker-compose run --rm app manual --test --domain *.hifool.cn --dns huaweicloud
start to test *.hifool.cn in DNS huaweicloud API
add TXT record(domain=hifool.cn, rr=_acme-challenge.*, value=SHhMNgnrFck0vUfl) to huaweicloud DNS
added TXT record
waiting 20 seconds...
remove above TXT record
removed TXT record
tested *.hifool.cn in DNS huaweicloud API

运行申请证书出现问题

[rui@Alicloud letscertbot]$  sudo docker-compose run --rm app obtain -d  *.hifool.cn --dns huaweicloud
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for hifool.cn
Running manual-auth-hook command: python /app/bin/../bin/manual.py --auth --dns huaweicloud
Waiting for verification...
Challenge failed for domain hifool.cn
dns-01 challenge for hifool.cn
Cleaning up challenges
Running manual-cleanup-hook command: python /app/bin/../bin/manual.py --cleanup --dns huaweicloud
Some challenges have failed.

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: hifool.cn
   Type:   dns
   Detail: DNS problem: SERVFAIL looking up TXT for
   _acme-challenge.hifool.cn - the domain's nameservers may be
   malfunctioning

下图是域名解析列表
img
请问是哪里的问题呢?
我用的另一个工具也是出现同样的错误。

Running manual-auth-hook command: /home/rui/certbot/certbot-letencrypt-wildcardcertificates-alydns-au/au.sh python hwy add
Waiting for verification...
Challenge failed for domain hifool.cn
dns-01 challenge for hifool.cn
Cleaning up challenges
Running manual-cleanup-hook command: /home/rui/certbot/certbot-letencrypt-wildcardcertificates-alydns-au/au.sh python hwy clean
Some challenges have failed.

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: hifool.cn
   Type:   dns
   Detail: DNS problem: SERVFAIL looking up CAA for hifool.cn - the
   domain's nameservers may be malfunctioning

之前用这个工具成功申请过腾讯云的泛域名ssl证书,但是这个华为云的就不行了。
是我的问题还是华为云的问题?

CERTIFICATE_VERIFY_FAILED] certificate verify

我已经把 access_key_id access_key_secret 添加到 config 文件中了

[root@iz2ze4u808b24i3gdq1m5uz letscertbot]# python ./bin/manual.py --test --domain xxx.cn --dns aliyun
start to test xxx.cn in DNS aliyun API
add TXT record(domain=168seo.cn, rr=_acme-challenge, value=urFUmj695SaEThq7) to aliyun DNS
ERROR:logger:test raise Exception:<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)>

报使用错误:Unable to register an account with ACME server

image
看样子是邮箱问题,已经换过了gmail、qq、还有自己域名下的邮箱试过,都失败。

已经配置了 email 和 阿里云的key。

调用测试版本
docker-compose run --rm app manual --test --domain www.a.cn --dns aliyun 正常

调用
docker-compose run --rm app obtain -d a.cn -d "*.a.cn"

求解答,谢谢。

python版本有问题?

[root@ecs-6289 letscertbot]# python ./bin/manual.py --test --domain your.example.com --dns aliyun
start to test your.example.com in DNS aliyun API
add TXT record(domain=example.com, rr=_acme-challenge.your, value=Ich0vX28RDdVOWaC) to aliyun DNS
ERROR:logger:aliyun#__request raise urllib2.HTTPError: HTTP Error 400: Bad Request
HTTP Error 400: Bad Request

[root@ecs-6289 letscertbot]# python --version
Python 2.7.5

增加容器使用方式

需求描述

增加容器部署,避免因使用环境安装依赖失败的问题,便于使用

注意点

容器里的证书目录 (/etc/letsencrypt/) 应该映射到宿主机上,避免因容器销毁,而导致续期失败

增加 DNS 验证等待时间参数

问题描述

现 DNS 验证,脚本会等待 20 秒待 DNS 生效后,再让 Certbot 验证。

当遇到像 GoDaddy 这类域名提供商,在添加 DNS 记录后,超过 20 秒仍未失效时, Certbot 就会验证失败。

解决方案

增加验证等待时间参数,给用户设置

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.