Skip to content

cloudflare dns challenge #585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion roles/infra/tasks/nginx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@
upstrema_conf: '{% if "conf" in item.value %}{{ item.value.conf }}{% elif item.key == "home" %}home.conf{% elif "path" in item.value %}path.conf{% elif "endpoint" in item.value %}endpoint.conf{% endif %}'
with_dict: "{{ infra_portal|default({}) }}"

- name: Create Cloudflare credentials file for Certbot
copy:
dest: /etc/nginx/conf.d/cert/cloudflare.ini
content: |
# Cloudflare API token used by Certbot
dns_cloudflare_api_token = {{ dns_cloudflare_api_token }}
owner: root
group: root
mode: '0600'
when: dns_cloudflare_api_token is defined and dns_cloudflare_api_token != ''
become: yes # Ensure file is created as root

#--------------------------------------------------------------#
# 2. Nginx Cert [nginx_cert]
Expand Down Expand Up @@ -183,6 +194,7 @@
when: certbot_sign is defined and certbot_sign|bool
ignore_errors: true
command: /etc/nginx/sign-cert
run_once: true

- name: reload nginx service
tags: nginx_reload
Expand Down Expand Up @@ -211,4 +223,4 @@
- name: wait for nginx exporter
wait_for: host=127.0.0.1 port=9113 state=started timeout=10

...
...
36 changes: 28 additions & 8 deletions roles/infra/templates/nginx/sign-cert.j2
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ set -euo pipefail
EMAIL="{{ certbot_email | default('[email protected]') }}"
WEBROOT="{{ nginx_home|default('/www') + '/acme' }}"
OPTIONS=${1-"{{ certbot_options | default('') }}"}
CLOUDFLARE_CREDENTIALS_FILE="/etc/nginx/conf.d/cert/cloudflare.ini"

{% set certbot_map = {} %}
{% for name, item in infra_portal.items() %}
{% if item.certbot is defined and item.certbot %}
Expand Down Expand Up @@ -45,16 +47,34 @@ echo "issue {{ cert_name }} : {{ domains|join(', ') }}"

if [ -f "/etc/letsencrypt/renewal/{{ cert_name }}.conf" ]; then
echo " -> Certificate exists. Attempting to expand/renew if needed..."
certbot certonly --non-interactive --cert-name {{ cert_name }} \
--webroot --webroot-path="$WEBROOT" \
--email "$EMAIL" --agree-tos --no-eff-email --expand \
{% for domain in domains %}-d "{{ domain }}" {% endfor %} $OPTIONS
if [ -f "$CLOUDFLARE_CREDENTIALS_FILE" ]; then # Check if the file exists
echo " -> Using Cloudflare DNS challenge."
certbot certonly --non-interactive --cert-name {{ cert_name }} \
--dns-cloudflare --dns-cloudflare-credentials "$CLOUDFLARE_CREDENTIALS_FILE" --dns-cloudflare-propagation-seconds 30 \
--email "$EMAIL" --agree-tos --no-eff-email --expand \
{% for domain in domains %}-d "{{ domain }}" {% endfor %} $OPTIONS
else
echo " -> Using Webroot challenge."
certbot certonly --non-interactive --cert-name {{ cert_name }} \
--webroot --webroot-path="$WEBROOT" \
--email "$EMAIL" --agree-tos --no-eff-email --expand \
{% for domain in domains %}-d "{{ domain }}" {% endfor %} $OPTIONS
fi
else
echo " -> Certificate does not exist. Obtaining a new certificate..."
certbot certonly --non-interactive --cert-name {{ cert_name }} \
--webroot --webroot-path="$WEBROOT" \
--email "$EMAIL" --agree-tos --no-eff-email \
{% for domain in domains %}-d "{{ domain }}" {% endfor %} $OPTIONS
if [ -f "$CLOUDFLARE_CREDENTIALS_FILE" ]; then # Check if the file exists
echo " -> Using Cloudflare DNS challenge."
certbot certonly --non-interactive --cert-name {{ cert_name }} \
--dns-cloudflare --dns-cloudflare-credentials "$CLOUDFLARE_CREDENTIALS_FILE" --dns-cloudflare-propagation-seconds 30 \
--email "$EMAIL" --agree-tos --no-eff-email \
{% for domain in domains %}-d "{{ domain }}" {% endfor %} $OPTIONS
else
echo " -> Using Webroot challenge."
certbot certonly --non-interactive --cert-name {{ cert_name }} \
--webroot --webroot-path="$WEBROOT" \
--email "$EMAIL" --agree-tos --no-eff-email \
{% for domain in domains %}-d "{{ domain }}" {% endfor %} $OPTIONS
fi
fi

echo " -> Done for {{ cert_name }}: {{ domains|join(', ') }}"
Expand Down