-
-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enabled TLS communication encryption
- Loading branch information
Showing
15 changed files
with
249 additions
and
98 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
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
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 |
---|---|---|
@@ -1,13 +1,24 @@ | ||
ETCD_NAME="{{ ansible_hostname }}" | ||
ETCD_LISTEN_CLIENT_URLS="http://{{ inventory_hostname }}:2379,http://127.0.0.1:2379" | ||
ETCD_ADVERTISE_CLIENT_URLS="http://{{ inventory_hostname }}:2379" | ||
ETCD_LISTEN_PEER_URLS="http://{{ inventory_hostname }}:2380" | ||
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://{{ inventory_hostname }}:2380" | ||
ETCD_LISTEN_CLIENT_URLS="{% if tls_cert_generate | bool %}https{% else %}http{% endif %}://{{ inventory_hostname }}:2379,{% if tls_cert_generate | bool %}https{% else %}http{% endif %}://127.0.0.1:2379" | ||
ETCD_ADVERTISE_CLIENT_URLS="{% if tls_cert_generate | bool %}https{% else %}http{% endif %}://{{ inventory_hostname }}:2379" | ||
ETCD_LISTEN_PEER_URLS="{% if tls_cert_generate | bool %}https{% else %}http{% endif %}://{{ inventory_hostname }}:2380" | ||
ETCD_INITIAL_ADVERTISE_PEER_URLS="{% if tls_cert_generate | bool %}https{% else %}http{% endif %}://{{ inventory_hostname }}:2380" | ||
ETCD_INITIAL_CLUSTER_TOKEN="{{ etcd_cluster_name }}" | ||
ETCD_INITIAL_CLUSTER="{% for host in groups['etcd_cluster'] %}{{ hostvars[host]['ansible_hostname'] }}=http://{{ hostvars[host]['inventory_hostname'] }}:2380{% if not loop.last %},{% endif %}{% endfor %}" | ||
ETCD_INITIAL_CLUSTER="{% for host in groups['etcd_cluster'] %}{{ hostvars[host]['ansible_hostname'] }}={% if tls_cert_generate | bool %}https{% else %}http{% endif %}://{{ hostvars[host]['inventory_hostname'] }}:2380{% if not loop.last %},{% endif %}{% endfor %}" | ||
ETCD_INITIAL_CLUSTER_STATE="new" | ||
ETCD_DATA_DIR="{{ etcd_data_dir }}" | ||
ETCD_ELECTION_TIMEOUT="5000" | ||
ETCD_HEARTBEAT_INTERVAL="1000" | ||
ETCD_INITIAL_ELECTION_TICK_ADVANCE="false" | ||
ETCD_AUTO_COMPACTION_RETENTION="1" | ||
{% if tls_cert_generate | bool %} | ||
ETCD_CERT_FILE="{{ tls_etcd_cert_path }}" | ||
ETCD_KEY_FILE="{{ tls_etcd_privatekey_path }}" | ||
ETCD_TRUSTED_CA_FILE="{{ tls_etcd_ca_cert_path }}" | ||
ETCD_PEER_CERT_FILE="{{ tls_etcd_cert_path }}" | ||
ETCD_PEER_KEY_FILE="{{ tls_etcd_privatekey_path }}" | ||
ETCD_PEER_TRUSTED_CA_FILE="{{ tls_etcd_ca_cert_path }}" | ||
ETCD_PEER_CLIENT_CERT_AUTH="true" | ||
ETCD_CLIENT_CERT_AUTH="true" | ||
ETCD_TLS_MIN_VERSION="TLS1.2" | ||
{% endif %} |
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
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 |
---|---|---|
@@ -1,42 +1,53 @@ | ||
--- | ||
# for add_pgnode.yml | ||
|
||
- name: Ensure TLS directories exist | ||
ansible.builtin.file: | ||
path: "{{ item | dirname }}" | ||
state: directory | ||
owner: "{{ tls_owner | default('postgres') }}" | ||
group: "{{ tls_owner | default('postgres') }}" | ||
mode: "0750" | ||
loop: | ||
- "{{ tls_privatekey_path | default('/etc/tls/server.key') }}" | ||
- "{{ tls_cert_path | default('/etc/tls/server.crt') }}" | ||
|
||
- name: Fetch TLS certificate and key from master | ||
- name: Fetch TLS certificate, key and CA from the master node into memory | ||
run_once: true | ||
ansible.builtin.fetch: | ||
ansible.builtin.slurp: | ||
src: "{{ item }}" | ||
dest: "files/tls/" | ||
validate_checksum: true | ||
flat: true | ||
delegate_to: "{{ groups.master[0] }}" | ||
register: tls_files | ||
loop: | ||
- "{{ tls_privatekey_path | default('/etc/tls/server.key') }}" | ||
- "{{ tls_cert_path | default('/etc/tls/server.crt') }}" | ||
- "/etc/tls/server.key" | ||
- "/etc/tls/server.crt" | ||
- "/etc/tls/ca.crt" | ||
|
||
- name: Copy TLS certificate and key to replica | ||
- name: Copy etcd TLS certificate, key and CA to all nodes from memory | ||
ansible.builtin.copy: | ||
src: "files/tls/{{ item.path | basename }}" | ||
content: "{{ tls_files.results[item.index].content | b64decode }}" | ||
dest: "{{ item.path }}" | ||
owner: "{{ tls_owner | default('postgres') }}" | ||
group: "{{ tls_owner | default('postgres') }}" | ||
owner: "etcd" | ||
group: "etcd" | ||
mode: "{{ item.mode }}" | ||
loop: | ||
- { path: "{{ tls_privatekey_path | default('/etc/tls/server.key') }}", mode: "{{ tls_privatekey_mode | default('0400') }}" } | ||
- { path: "{{ tls_cert_path | default('/etc/tls/server.crt') }}", mode: "{{ tls_cert_mode | default('0644') }}" } | ||
- { index: 0, path: "{{ tls_etcd_privatekey_path | default('/etc/etcd/server.key') }}", mode: "0400" } | ||
- { index: 1, path: "{{ tls_etcd_cert_path | default('/etc/etcd/server.crt') }}", mode: "0644" } | ||
- { index: 2, path: "{{ tls_etcd_ca_cert_path | default('/etc/etcd/ca.crt') }}", mode: "0644" } | ||
when: copy_for == 'etcd' | ||
|
||
- block: | ||
- name: Create directory {{ tls_privatekey_path | dirname }} | ||
ansible.builtin.file: | ||
dest: "{{ tls_privatekey_path | dirname }}" | ||
state: directory | ||
owner: "{{ tls_owner }}" | ||
group: "{{ tls_owner }}" | ||
mode: "0755" | ||
|
||
- name: Copy PostgreSQL TLS certificate, key and CA to all nodes | ||
ansible.builtin.copy: | ||
content: "{{ tls_files.results[item.index].content | b64decode }}" | ||
dest: "{{ item.path }}" | ||
owner: "{{ tls_owner }}" | ||
group: "{{ tls_owner }}" | ||
mode: "{{ item.mode }}" | ||
loop: | ||
- { index: 0, path: "{{ tls_privatekey_path | default('/etc/tls/server.key') }}", mode: "0400" } | ||
- { index: 1, path: "{{ tls_cert_path | default('/etc/tls/server.crt') }}", mode: "0644" } | ||
- { index: 2, path: "{{ tls_ca_cert_path | default('/etc/tls/ca.crt') }}", mode: "0644" } | ||
|
||
- name: Delete TLS certificate and key from the ansible controller | ||
ansible.builtin.file: | ||
path: "files/tls/" | ||
state: absent | ||
delegate_to: localhost | ||
- name: Delete TLS certificate and key from the ansible controller | ||
ansible.builtin.file: | ||
path: "files/tls/" | ||
state: absent | ||
delegate_to: localhost | ||
run_once: true | ||
when: copy_for == 'pg' |
109 changes: 109 additions & 0 deletions
109
automation/roles/tls_certificate/generate/tasks/main.yml
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,109 @@ | ||
--- | ||
- name: "Clean up existing certificates" | ||
ansible.builtin.file: | ||
path: "{{ item }}" | ||
state: absent | ||
loop: | ||
- "{{ tls_privatekey_path | default('/etc/tls/server.key') }}" | ||
- "{{ tls_cert_path | default('/etc/tls/server.crt') }}" | ||
- "{{ tls_ca_cert_path | default('/etc/tls/ca.crt') }}" | ||
- "{{ tls_ca_privatekey_path | default('/etc/tls/ca.key') }}" | ||
- "{{ tls_etcd_cert_path | default('/etc/etcd/server.crt') }}" | ||
- "{{ tls_etcd_ca_cert_path | default('/etc/etcd/ca.crt') }}" | ||
- "{{ tls_etcd_privatekey_path | default('/etc/etcd/server.key') }}" | ||
- "/etc/tls" | ||
|
||
- ansible.builtin.set_fact: | ||
all_san_entries: [] | ||
|
||
- name: "Gather host-specific network information" | ||
ansible.builtin.set_fact: | ||
san_entry: >- | ||
DNS:{{ ansible_hostname }},DNS:{{ ansible_fqdn }},IP:{{ ansible_default_ipv4.address }} | ||
- block: | ||
- name: "Aggregate all subjectAltName entries" | ||
ansible.builtin.set_fact: | ||
all_san_entries: "{{ all_san_entries + [hostvars[item].san_entry] }}" | ||
with_items: "{{ ansible_play_hosts }}" | ||
|
||
- name: "Join subjectAltName entries into a single string" | ||
ansible.builtin.set_fact: | ||
subject_alt_name: "{{ all_san_entries | join(',') + ',DNS:localhost,IP:127.0.0.1' }}" | ||
when: ansible_play_hosts | length > 1 | ||
|
||
- name: "Display Certificate subjectAltName future value" | ||
ansible.builtin.debug: | ||
var: subject_alt_name | ||
|
||
######## Generate CA ######## | ||
- name: "Ensure TLS directory exist" | ||
ansible.builtin.file: | ||
path: "/etc/tls" | ||
state: directory | ||
mode: "0700" | ||
|
||
- name: "Generate CA private key" | ||
community.crypto.openssl_privatekey: | ||
path: "/etc/tls/ca.key" | ||
size: "{{ tls_privatekey_size | default(4096) }}" | ||
type: "{{ tls_privatekey_type | default('RSA') }}" | ||
|
||
- name: "Create CSR for CA certificate" | ||
community.crypto.openssl_csr_pipe: | ||
privatekey_path: "/etc/tls/ca.key" | ||
common_name: PostgreSQL CA | ||
use_common_name_for_san: false | ||
basic_constraints: | ||
- 'CA:TRUE' | ||
basic_constraints_critical: true | ||
key_usage: | ||
- keyCertSign | ||
key_usage_critical: true | ||
register: ca_csr | ||
|
||
- name: "Create self-signed CA certificate from CSR" | ||
community.crypto.x509_certificate: | ||
path: "/etc/tls/ca.crt" | ||
csr_content: "{{ ca_csr.csr }}" | ||
privatekey_path: "/etc/tls/ca.key" | ||
provider: "{{ tls_cert_provider | default('selfsigned') }}" | ||
entrust_not_after: "+{{ tls_cert_valid_days | default(3650) }}d" | ||
|
||
######## Generate Server cert/key ######## | ||
- name: "Create server private key" | ||
community.crypto.openssl_privatekey: | ||
path: "/etc/tls/server.key" | ||
size: "{{ tls_privatekey_size | default(4096) }}" | ||
type: "{{ tls_privatekey_type | default('RSA') }}" | ||
|
||
- name: "Create server CSR" | ||
community.crypto.openssl_csr_pipe: | ||
privatekey_path: "/etc/tls/server.key" | ||
common_name: postgresql.cluster | ||
key_usage: | ||
- digitalSignature | ||
- keyEncipherment | ||
- dataEncipherment | ||
extended_key_usage: | ||
- clientAuth | ||
- serverAuth | ||
subject_alt_name: "{{ subject_alt_name }}" | ||
register: csr | ||
|
||
- name: "Sign server certificate with the CA" | ||
community.crypto.x509_certificate_pipe: | ||
csr_content: "{{ csr.csr }}" | ||
provider: ownca | ||
ownca_path: "/etc/tls/ca.crt" | ||
ownca_privatekey_path: "/etc/tls/ca.key" | ||
ownca_not_after: +3650d | ||
ownca_not_before: "-1d" | ||
register: certificate | ||
|
||
- name: "Write server certificate" | ||
ansible.builtin.copy: | ||
dest: "/etc/tls/server.crt" | ||
content: "{{ certificate.certificate }}" | ||
delegate_to: "{{ groups.master[0] }}" | ||
run_once: true |
Oops, something went wrong.