Improve SSL handling #25

Merged
lukas merged 8 commits from feat/better-ssl into main 2021-05-14 21:58:12 +00:00
2 changed files with 45 additions and 21 deletions
Showing only changes of commit f1f28a80f1 - Show all commits

View File

@ -1,11 +1,17 @@
# If self_signed = true, in nginx/files: generate root CA (if regenereate_root_ca = true),
# and sign a wildcard certificate. Copy certificates to /etc/ssl/.
- name: ensure directories exist
file:
path: "{{ item }}"
state: directory
mode: '0755'
loop:
- "{{ nginx_confd_folder }}"
- name: generate and install self-signed certs
- "{{ data_folder }}/nginx"
- "{{ data_folder }}/nginx/conf.d"
- "{{ data_folder }}/nginx/sites-enabled"
- "{{ data_folder }}/nginx/sites-available"
- "{{ data_folder }}/nginx/snippets"
- name: generate self-signed certs
import_tasks: self-signed.yml
when: self_signed
- name: create nginx bridge network
@ -14,6 +20,18 @@
attachable: true
internal: true
state: present
- name: copy nginx.conf
template:
src: nginx.conf.j2
dest: "{{ data_folder }}/nginx/nginx.conf"
mode: '0755'
- name: copy snippets
template:
src: "{{ item }}"
dest: "{{ data_folder }}/nginx/snippets/{{ item | basename | regex_replace('.j2$', '') }}"
mode: '0755'
with_fileglob:
- "../templates/snippets/*.conf"
- name: run container
docker_container:
name: 'nginx'
@ -22,10 +40,13 @@
- name: bridge
- name: nginx-internal
volumes:
- "{{ data_folder }}/nginx/conf.d:/etc/nginx/conf.d"
- "{{ data_folder }}/nginx/nginx.conf:/etc/nginx/nginx.conf"
- "{{ data_folder }}/nginx/{{ base_domain }}.key:/etc/nginx/{{ base_domain }}.key"
- "{{ data_folder }}/nginx/{{ base_domain }}.crt:/etc/nginx/{{ base_domain }}.crt"
- "{{ nginx_confd_folder }}:/etc/nginx/conf.d"
- "{{ data_folder }}/nginx/sites-available:/etc/nginx/sites-available"
- "{{ data_folder }}/nginx/sites-enabled:/etc/nginx/sites-enabled"
- "{{ data_folder }}/nginx/snippets:/etc/nginx/snippets"
- "{{ data_folder }}/nginx/{{ base_domain }}.key:/etc/ssl/{{ base_domain }}.key"
- "{{ data_folder }}/nginx/{{ base_domain }}.crt:/etc/ssl/{{ base_domain }}.crt"
ports:
- "80:80"
- "443:443"

View File

@ -4,34 +4,37 @@
-new \
-nodes \
-newkey rsa:2048 \
-keyout "{{ data_folder }}/nginx/rootca.key" \
-out "{{ data_folder }}/nginx/rootca.pem" \
-keyout "{{ playbook_dir }}/roles/nginx/files/rootca.key" \
-out "{{ playbook_dir }}/roles/nginx/files/rootca.pem" \
-sha256 \
-days 3650 \
-subj "/C=CZ/L=Prague/CN=Homelab/emailAddress={{ admin_email }}"
when: generate_cert.root
- name: generate wildcard csr
command: openssl req \
-new \
-nodes \
-newkey rsa:2048 \
-keyout "{{ data_folder }}/nginx/{{ base_domain }}.key" \
-out "{{ data_folder }}/nginx/{{ base_domain }}.csr" \
-keyout "{{ playbook_dir }}/roles/nginx/files/{{ base_domain }}.key" \
-out "{{ playbook_dir }}/roles/nginx/files/{{ base_domain }}.csr" \
-subj "/C=CZ/L=Prague/CN=*.{{ base_domain }}/emailAddress={{ admin_email }}"
when: generate_cert.wildcard
- name: sign wildcard csr with root ca
command: openssl x509 \
-req \
-in "{{ data_folder }}/nginx/{{ base_domain }}.csr" \
-CA "{{ data_folder }}/nginx/rootca.pem" \
-CAkey "{{ data_folder }}/nginx/rootca.key" \
-in "{{ playbook_dir }}/roles/nginx/files/{{ base_domain }}.csr" \
-CA "{{ playbook_dir }}/roles/nginx/files/rootca.pem" \
-CAkey "{{ playbook_dir }}/roles/nginx/files/rootca.key" \
-CAcreateserial \
-out "{{ data_folder }}/nginx/{{ base_domain }}.crt" \
-out "{{ playbook_dir }}/roles/nginx/files/{{ base_domain }}.crt" \
-days 3650 \
-sha256
- name: install root ca
command: trust anchor "{{ data_folder }}/nginx/rootca.pem"
become: yes
- name: copy .conf file
template:
src: nginx.conf.j2
dest: "{{ data_folder }}/nginx/nginx.conf"
mode: '0755'
when: generate_cert.wildcard
- name: copy wildcard certificate and key
copy:
src: "{{ item }}"
dest: "{{ data_folder }}/nginx/{{ item }}"
mode: '0700'
loop:
- "{{ base_domain }}.crt"
- "{{ base_domain }}.key"