This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tasks for generating TLS certificates (#17)
Add the remaining tasks from our existing docker role. These tasks create CA, server, and client certificates. - add a variable `docker_generate_certificates `. If `true`, certificates will be generated. Defaults to `false` - add separate task files for creating CA, server, and client certificates - a client certificate is created for each client listed in `docker_client_hostnames `. The certificates are copied to the Ansible controller - add description of all new variables to the README - add to the README a short discussion of when you would want to generates the certificates --------- Co-authored-by: Daniel Matthews <[email protected]>
- Loading branch information
1 parent
20e2391
commit 7d6b491
Showing
14 changed files
with
288 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,5 @@ jobs: | |
steps: | ||
- uses: UCL-MIRSG/.github/actions/[email protected] | ||
with: | ||
ansible-roles-config: ./meta/requirements.yml | ||
pre-commit-config: ./.pre-commit-config.yaml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
collections: | ||
- community.crypto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
- name: Prepare | ||
hosts: all | ||
become: false | ||
gather_facts: true | ||
tasks: | ||
- name: Install EPEL-release | ||
ansible.builtin.yum: | ||
name: "epel-release" | ||
state: installed | ||
|
||
- name: Install Python | ||
ansible.builtin.package: | ||
name: "{{ item }}" | ||
update_cache: true | ||
state: present | ||
loop: | ||
- python | ||
- python-pip | ||
- python-setuptools | ||
|
||
- name: Update pip | ||
ansible.builtin.pip: | ||
name: pip | ||
version: "20.3.4" | ||
|
||
- name: Install cryptography with pip - needed to generate certificates | ||
ansible.builtin.pip: | ||
name: | ||
- cryptography |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--- | ||
docker_generate_certificates: true | ||
docker_client_hostnames: ["docker-client.com"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
- name: Prepare | ||
hosts: all | ||
become: false | ||
gather_facts: true | ||
tasks: | ||
- name: Install EPEL-release | ||
ansible.builtin.yum: | ||
name: "epel-release" | ||
state: installed | ||
|
||
- name: Install Python | ||
ansible.builtin.package: | ||
name: "{{ item }}" | ||
update_cache: true | ||
state: present | ||
loop: | ||
- python3 | ||
- python3-pip | ||
- python3-setuptools | ||
|
||
- name: Update pip | ||
ansible.builtin.pip: | ||
name: pip | ||
version: "21.3.1" | ||
|
||
- name: Install cryptography with pip - needed to generate certificates | ||
ansible.builtin.pip: | ||
name: | ||
- cryptography |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
- name: Ensure docker cert dir exists | ||
ansible.builtin.file: | ||
path: "{{ docker_certificate_directory }}" | ||
state: directory | ||
owner: "{{ docker_owner }}" | ||
group: "{{ docker_group }}" | ||
mode: "0700" | ||
|
||
- name: Generate CA private key | ||
community.crypto.openssl_privatekey: | ||
path: "{{ docker_ca_key }}" | ||
owner: "{{ docker_owner }}" | ||
group: "{{ docker_group }}" | ||
mode: "0400" | ||
|
||
- name: Generate CA CSR | ||
community.crypto.openssl_csr: | ||
path: "{{ docker_ca_csr }}" | ||
privatekey_path: "{{ docker_ca_key }}" | ||
common_name: "{{ docker_server_hostname }}" | ||
subject_alt_name: "IP:{{ docker_server_ip }}" | ||
basic_constraints_critical: true | ||
basic_constraints: ["CA:TRUE"] | ||
|
||
- name: Generate self-signed CA certificate | ||
community.crypto.x509_certificate: | ||
path: "{{ docker_ca_cert }}" | ||
privatekey_path: "{{ docker_ca_key }}" | ||
csr_path: "{{ docker_ca_csr }}" | ||
provider: selfsigned | ||
owner: "{{ docker_owner }}" | ||
group: "{{ docker_group }}" | ||
mode: "0400" | ||
notify: | ||
- Restart docker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
- name: Ensure docker client cert dir exists on server | ||
ansible.builtin.file: | ||
path: "{{ docker_client_certificate_directory }}" | ||
state: directory | ||
owner: "{{ docker_owner }}" | ||
group: "{{ docker_group }}" | ||
mode: "0700" | ||
|
||
- name: Generate OpenSSL client private key | ||
community.crypto.openssl_privatekey: | ||
path: "{{ docker_client_certificate_directory }}/key.pem" | ||
owner: "{{ docker_owner }}" | ||
group: "{{ docker_group }}" | ||
mode: "0400" | ||
|
||
- name: Generate OpenSSL CSR for each client using private key | ||
community.crypto.openssl_csr: | ||
path: "{{ docker_client_certificate_directory }}/{{ item }}.csr" | ||
privatekey_path: "{{ docker_client_certificate_directory }}/key.pem" | ||
common_name: "{{ item }}" | ||
register: new_docker_client_csr_generated | ||
loop: "{{ docker_client_hostnames }}" | ||
|
||
- name: Generate client certificates signed by server CA | ||
community.crypto.x509_certificate: | ||
path: "{{ docker_client_certificate_directory }}/{{ item }}.cert" | ||
csr_path: "{{ docker_client_certificate_directory }}/{{ item }}.csr" | ||
provider: ownca | ||
ownca_path: "{{ docker_ca_cert }}" | ||
ownca_privatekey_path: "{{ docker_ca_key }}" | ||
mode: "0400" | ||
owner: "{{ docker_owner }}" | ||
group: "{{ docker_group }}" | ||
loop: "{{ docker_client_hostnames }}" | ||
|
||
- name: Copy signed client certificates to temp dir on Ansible controller | ||
ansible.builtin.fetch: | ||
src: "{{ docker_client_certificate_directory }}/{{ item }}.cert" | ||
dest: "{{ docker_client_certificate_cache_directory }}/{{ item }}.cert" | ||
flat: true | ||
loop: "{{ docker_client_hostnames }}" | ||
|
||
- name: Copy private key to temp dir on Ansible controller | ||
ansible.builtin.fetch: | ||
src: "{{ docker_client_certificate_directory }}/key.pem" | ||
dest: "{{ docker_client_certificate_cache_directory }}/key.pem" | ||
flat: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
- name: Generate server private key | ||
community.crypto.openssl_privatekey: | ||
path: "{{ docker_server_key }}" | ||
owner: "{{ docker_owner }}" | ||
group: "{{ docker_group }}" | ||
mode: "0400" | ||
|
||
- name: Generate server CSR | ||
community.crypto.openssl_csr: | ||
path: "{{ docker_server_csr }}" | ||
privatekey_path: "{{ docker_server_key }}" | ||
common_name: "{{ docker_server_hostname }}" | ||
subject_alt_name: "IP:{{ docker_server_ip }}" | ||
|
||
- name: Generate server certificate | ||
community.crypto.x509_certificate: | ||
path: "{{ docker_server_cert }}" | ||
csr_path: "{{ docker_server_csr }}" | ||
provider: ownca | ||
ownca_path: "{{ docker_ca_cert }}" | ||
ownca_privatekey_path: "{{ docker_ca_key }}" | ||
owner: "{{ docker_owner }}" | ||
group: "{{ docker_group }}" | ||
mode: "0400" | ||
notify: | ||
- Restart docker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"hosts": ["tcp://{{ docker_server_ip }}:{{ docker_server_port }}", "unix:///var/run/docker.sock"], | ||
"tlsverify": true, | ||
"tlscacert": "{{ docker_ca_cert }}", | ||
"tlscert": "{{ docker_server_cert }}", | ||
"tlskey": "{{ docker_server_key }}" | ||
} |