Lets Encrypt 인증서 발급받기

This entry is part 20 of 48 in the series FreeNAS 서버 만들기

앞선 포스팅에서 cloudflare 로 네임서버를 변경하였고, certbotNginx_WAF 감옥에 설치하였습니다. 이제 let’s encrypt 인증서를 발급받도록 하겠습니다.

https://12bme.tistory.com/80

SSL 이 무엇이고 어덯게 작동하는 지는 위 링크를 참고하시길 바랍니다.

https 보안 통신을 위해선 인증서가 필요합니다. 그런데 인증서를 발급받기 위해선 돈이 듭니다. let’s encrypt 는 이런 비용이 https 사이트를 보급하는데 방해가 된다 생각하고 무료 인증서를 발급하는 사업을 시작하였습니다. 현재 let’s encrypt 는 수많은 기업들과 제단이 참가하는 거대 프로젝트입니다. 그리고, Certbot 이라는 프로그램으로 쉽게 발급받고 갱신할 수 있습니다.

20200314 수정
파이썬이 3.6 버전에서 3.7 버전으로 업그레이드 됨에 따라, certbot 명령어도 certbot-3.6 에서 certbot-3.7 으로 변경되었습니다.

certbot-3.7 또는 certbot 명령어를 이용해주시면 되겠습니다.

Certbot 의 작동 방식

Let’s Encrypt 인증서는 3가지 단계를 거쳐 발급됩니다.

  1. 인증서 발급 요청
  2. 도메인의 소유권 확인
  3. 발급

Certbot 은 인증서를 발급받고 소유권 확인을 하고 발급하는 것을 자동화 한 것입니다.

인증서를 아무나 발급을수 있다면 그건 인증서라고 부를 수 없습니다. 발급받고자 하는 도메인을 소유하고 있다는 것을 증명해야만 발급받을 수 있습니다.

여러 가지 방법으로 도메인의 소유권을 증명할 수 있는데, 와일드카드 인증서를 발급받을 것이 아니라면 특정 데이터가 들어 있는 파일을 webroot 에 올리거나, 임시로 웹서버를 동작시키는 방법 등을 사용합니다. Certbot 의 플러그인을 설치할 시 DNS 인증 등의 다른 인증 방법을 사용할 수 있습니다.

그 중 이 포스팅에서 사용할 방법은 Cloudflare 의 API 를 이용해 DNS 의 TXT 필드를 사용해 인증받는 방식인데, Certbot 이 특정 도메인의 TXT 필드에 특정한 값을 등록하고, 값이 정상적으로 등록되었는지 검증한 후, 검증되었다면 해당 도메인을 소유하고 있는 것이 맞으므로 인증서를 발급합니다.

와일드카드 인증서

인증서는 대상으로 하는 도메인이 있습니다. example.com 이라는 도메인을 지원할 수도 있고, www.example.com 이라는 도메인을 지원할 수도 있죠. 또는 둘 모두를 대상으로 할 수도 있구요.

certbot 으로 let’s encrypt 인증서를 발급받을 때에도 어떤 도메인을 대상으로 할 지 설정할 수 있습니다. 필요로 하는 도메인을 전부 대상으로 하는 거죠. example.com www.example.com blog.example.com ….

그런데 SSL 을 적용할 도메인의 수가 많지 않고 변동이 없다면 별 문제가 되지 않지만, 서브 도메인의 수가 많다면 인증서를 발급할 때 모든 서브 도메인을 입력해 주어야 하고, 새로운 도메인을 추가하거나 변경하기 위해선 인증서를 새로 발급받아야 하기 때문에 불편합니다.

이런 문제를 해결하기 위해 와일드카드 인증서라는 것이 있습니다. 와일드카드 인증서는 *.example.com 형태의 인증서로서, example.com 의 서브도메인을 모두 대상으로 합니다.

인증 정보 파일 만들기

감옥 안에서 작업해야 합니다. 감옥으로 들어 가 줍시다.

iocage console Nginx_WAF

Certbot 에서 TXT 레코드를 이용해 인증서를 발급받고 자동으로 갱신하기 위해서는 인증 정보가 저장된 파일이 있어야 합니다.

먼저 클라우드플레어 API 를 받아 옵니다. ‘클라우드플레어 – API‘ 를 참고하시면 됩니다.

mkdir /root/.cert

인증 정보가 저장될 디렉토리를 만들고

vi /root/.cert/cert.ini

아래와 같이 자신의 인증 정보를 입력한 후 저장해 주세요.

dns_cloudflare_email = mail@example.com
dns_cloudflare_api_key = adfjk3asdkf239wur523h4129ehasjkdfkjanv1

중요한 정보이니 root 만 접근할 수 있도록 제한해 줍니다.

chmod -R 400 /root/.cert/

.cert 디렉토리를 포함한 하위 파일과 디렉토리를 전부 root 만 읽기 가능하도록 제한하였습니다.

와일드카드 인증서 발급받기

인증 파일을 만들었다면 와일드카드 인증서를 발급받아 보도록 하겠습니다.

certbot-3.6 certonly --dns-cloudflare-credentials /root/.cert/cert.ini

아래와 같은 출력이 나타날 것입니다.

Saving debug log to /var/log/letsencrypt/letsencrypt.log
How would you like to authenticate with the ACME CA?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: Obtain certificates using a DNS TXT record (if you are using Cloudflare for DNS). (dns-cloudflare)
2: Spin up a temporary webserver (standalone)
3: Place files in webroot directory (webroot)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-3] then [enter] (press 'c' to cancel):

어떤 방식으로 인증 할 것이냐 묻는 것입니다. Cloudflare 의 DNS TXT 레코드를 이용해 인증하겠다를 선택합니다.

1을 입력해 계속합니다.

Plugins selected: Authenticator dns-cloudflare, Installer None Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel):

Certbot 갱신 알림이나 보안 알림 등을 받을 이메일을 입력하라는 것입니다. 자신의 이메일을 입력한 후 엔터.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel:

약관에 동의할 것이냐 묻습니다. A를 입력해 계속합니다.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o:

Electronic Frontier Foundation 에서 보내는 메일을 받을 것이냐 묻습니다. 받을 것이라면 Y, 아니라면 N.

Please enter in your domain name(s) (comma and/or space separated)  (Enter 'c'
to cancel):

인증서를 발급받을 도메인을 쉼표나 스페이스바로 분리해 이름을 입력해 주면 됩니다. 와일드카드 인증서를 발급받을 것이니 example.com *.example.com 을 입력합니다.

Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for example.com
dns-01 challenge for example.com
Waiting 10 seconds for DNS changes to propagate
Waiting for verification...
Cleaning up challenges

인증 과정을 거친 후

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /usr/local/etc/letsencrypt/live/example.com/fullchain.pem
   Your key file has been saved at:
   /usr/local/etc/letsencrypt/live/example.com/privkey.pem
   Your cert will expire on 2019-08-27. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /usr/local/etc/letsencrypt. You should
   make a secure backup of this folder now. This configuration
   directory will also contain certificates and private keys obtained
   by Certbot so making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:
   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

안내 메세지가 나타납니다. 위와 같은 메세지가 나타난다면 성공적으로 인증서를 발급받은 것입니다.

인증서는 /usr/local/etc/letsencrypt/live/도메인 아래에 저장됩니다.

인증서 갱신

갱신은 훨씬 간단하고 자동화되어 있습니다.

certbot-3.6 renew

갱신이 잘 되는지 확인하기 위해선 -dry-run 인수를 붙이면 됩니다. 한번 확인해보겠습니다.

certbot-3.6 renew --dry-run

Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /usr/local/etc/letsencrypt/renewal/example.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator dns-cloudflare, Installer None
Renewing an existing certificate
Performing the following challenges:
dns-01 challenge for example.com
dns-01 challenge for example.com
Waiting 10 seconds for DNS changes to propagate
Waiting for verification...
Cleaning up challenges
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed without reload, fullchain is
/usr/local/etc/letsencrypt/live/example.com/fullchain.pem
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates below have not been saved.)
Congratulations, all renewals succeeded. The following certs have been renewed:
  /usr/local/etc/letsencrypt/live/example.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
**          (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
IMPORTANT NOTES:
 - Your account credentials have been saved in your Certbot
   configuration directory at /usr/local/etc/letsencrypt. You should
   make a secure backup of this folder now. This configuration
   directory will also contain certificates and private keys obtained
   by Certbot so making regular backups of this folder is ideal.

드라이 런 결과 성공적으로 갱신됨을 알 수 있습니다.

Cron 으로 자동 갱신

letsencrypt 인증서는 발급일부터 90일간 유효하고, 발급 후 60일부터 갱신이 가능합니다. 갱신일 이전에 갱신을 시도할 시 갱신일이 아니다는 메세지를 띄우며, 너무 많이 시도할 시 하루 동안 모든 요청이 차단됩니다. 그러니 자동 갱신 되도록 cron 에 넣도록 하겠습니다.

vi /etc/crontab

0       3       *       *       6       root    /usr/local/bin/certbot-3.6 renew --post-hook "service nginx restart"

1주일에 한번 새벽 3시에 root 권한으로 certbot renew 작업을 실행한 후 갱신에 성공할 시 nginx 를 재시작 하도록 설정하였습니다.

마치면서

다음 포스팅은 Nginx 의 설정을 다룹니다. 분량이 꾀 길어질거 같은데 조절이 될지 모르겠네요.

시리즈 네비게이션<< Nginx WAF 리버스 프록시 – 개념 설명과 환경 구성Nginx 설정 >>

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다