Why
After you have created a website, an SSL certificate has become a mandatory configuration in order to publish it to the Internet. Let’s Encrypt can issue a certificate for your website’s domain. When you want to get an SSL certificate from Let’s Encrypt, you have a number of tools to choose from. The most common tool is Certbot. If you are applying for an SSL certificate for the first time, you may have some trouble using this tool. This is because it is very powerful and there are so many options available for configuration. Especially when I want to generate a wildcard type certificate, there are so many manual steps that I may have to reapply every three months. I like automated tools. My DNS provider is alibaba cloud, so I started searching the web for an automated tool that could automatically request wildcard type letsencrypt certificates for alibaba cloud. But I was disappointed that I didn’t find the tools I wanted. Therefore, I decided to look for a relevant library and prepare to write one by myself.
The idea
If you want to implement a process to automatically obtain certificates, then you first need to understand its process. That’s the idea:
The implementation
I found the acme4j library. It is very clear about how to get a certificate from Letsencrypt.
Start a session
1 | // Create a session for Let's Encrypt. |
Create an account
1 |
|
Challenge
- The Challenage.TYPE in step 3 are DNS or HTTP.
- The step 4 is exactly the part I want to improve. It required you to manually log into your domain service provider to add TXT type domain values for the Let’s Encrypt server to initiate a challenge. The problem with this is that you still have to repeat the process of manually adding challenge values when the certificate is about to expire after three months.
Turn step 4 into automation
There are two keys to turning step 4 into automation.
- The first is to automatically write a text value, via the API provided by the DNS Provider, to the appropriate domain name. This step varies depending on the API provided by each DNS Provider. If you want to know how to call alibaba’s API, you can refer to the source code link at the end of this article. The sample code is here:
1 |
|
- Second, automatically determine if the text value you set is already in effect. Then, after it has taken effect, go ahead and trigger the Letsencrypt challenge process. I used a library called dnsjava to implement this step.
1 |
|
Create a certificated signing request(CSR)
1 |
|
What is a Certificate Signing Request (CSR)?
Get certificate
1 |
|
You just need to write the certificate and the domain key to the files.
Practices
Yes, I have written a complete working application. I only support using Alibaba Cloud as DNS provider. And only used DNS as the challenge type. If you want to support more DNS providers or other challenge types, it would be a very easy thing to do as long as you are familiar with the Java language.
The following are the related projects in Github:
- kennylee2008/letsencrypt-alibaba The source code of the project mentioned in this article.
- You can run with this kennylee2008/letsencrypt-alibaba docker image right now.
- If you prefer to use docker-compose, you can clone this project kennylee2008/letsencrypt-alibaba-docker, modify the environment variables, and run it right away.