Skip to content

Commit 58997de

Browse files
Update tasks to use cert_info plugin (#163)
Fixes #161. Update certificate handling tasks and docs --------- Co-authored-by: Afeef Ghannam <[email protected]> Co-authored-by: Afeef Ghannam <[email protected]>
1 parent a7bfc56 commit 58997de

File tree

16 files changed

+88
-78
lines changed

16 files changed

+88
-78
lines changed

.github/workflows/test_full_stack.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ jobs:
4444
max-parallel: 1
4545
matrix:
4646
distro:
47+
- rockylinux8
4748
- ubuntu2004
4849
- ubuntu2204
49-
- rockylinux8
5050
- debian10
5151
scenario:
5252
- elasticstack_default

docs/role-beats.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ Requirements
1010

1111
You need to have the beats you want to install available in your software repositories. We provide a [role](./role-repos.md) for just that but if you have other ways of managing software, just make sure it's available. Alternatively you can install the Beats yourself.
1212

13+
* `cryptography` >= 2.5
14+
* `community.crypto` collection: ansible-galaxy collection install community.crypto
15+
1316
Role Variables
1417
--------------
1518

@@ -101,7 +104,7 @@ If you want to use this role with your own TLS certificates, use these variables
101104
* *beats_tls_key*: Path to the keyfiles (default: `{{ beats_ca_dir }}/{{ ansible_hostname }}.key`)
102105
* *beats_tls_cert*: Path to the certificate (default: `{{ beats_ca_dir }}/{{ ansible_hostname }}.crt`)
103106
* *beats_tls_key_passphrase*: Passphrase of the keyfile (default: `BeatsChangeMe`)
104-
* *beats_cert_expiration_buffer*: Ansible will renew the beats certificate if its validity is shorter than this value, which should be number of days. (default: 30)
107+
* *beats_cert_expiration_buffer*: Ansible will renew the beats certificate if its validity is shorter than this value (default: `+30d`). The valid format is `+[w | d | h | m | s]`, example `+20w5d7h`.
105108
* *beats_cert_will_expire_soon*: Set it to true to renew beats certificate (default: `false`), Or run the playbook with `--tags renew_beats_cert` to do that.
106109
* *beats_tls_cacert*: Path to the CA.crt (default: `{{ beats_ca_dir }}/ca.crt`)
107110

docs/role-elasticsearch.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ If you use the role to set up security you, can use its CA to create certificate
99

1010
Please note that setting `elasticsearch_bootstrap_pw` as variable will only take effect when initialising Elasticsearch. Changes after starting elasticsearch for the first time will not change the bootstrap password for the instance and will lead to breaking tests.
1111

12+
Requirements
13+
------------
14+
15+
* `cryptography` >= 2.5
16+
1217
Role Variables
1318
--------------
1419

1520
* *elasticsearch_enable*: Start and enable Elasticsearch (default: `true`)
1621
* *elasticsearch_heap*: Heapsize for Elasticsearch. (Half of free memory on host. Maximum 30GB. (default: Half of hosts memory. Min 1GB, Max 30GB)
1722
* *elasticsearch_tls_key_passphrase*: Passphrase for elasticsearch certificates (default: `PleaseChangeMeIndividually`)
18-
* *elasticsearch_cert_expiration_buffer*: Ansible will renew the elasticsearch certificate if its validity is shorter than this value, which should be number of days. (default: 30)
23+
* *elasticsearch_cert_expiration_buffer*: Ansible will renew the elasticsearch certificate if its validity is shorter than this value, which should be number of days. (default: `30`)
1924
* *elasticsearch_cert_will_expire_soon*: Set it to true to renew elasticsearch certificate (default: `false`), Or run the playbook with `--tags renew_elasticsearch_cert` to do that.
2025
* *elasticsearch_datapath*: Path where Elasticsearch will store it's data. (default: `/var/lib/elasticsearch` - the packages default)
2126
* *elasticsearch_create_datapath*: Create the path for data to store if it doesn't exist. (default: `false` - only useful if you change `elasticsearch_datapath`)

docs/role-kibana.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Ansible Role: Kibana
55

66
This roles installs and configures Kibana.
77

8+
Requirements
9+
------------
10+
11+
* `cryptography` >= 2.5
812

913
Role Variables
1014
--------------
@@ -23,7 +27,7 @@ These variables are identical over all our elastic related roles, hence the diff
2327
* *elasticstack_full_stack*: Use `ansible-role-elasticsearch` as well (default: `false`)
2428
* *elasticstack_elasticsearch_http_port*: Port of Elasticsearch http (Default: `9200`)
2529
* *kibana_tls_key_passphrase*: Passphrase for kibana certificates (default: `PleaseChangeMe`)
26-
* *kibana_cert_expiration_buffer*: Ansible will renew the kibana certificate if its validity is shorter than this value, which should be number of days. (default: 30)
30+
* *kibana_cert_expiration_buffer*: Ansible will renew the kibana certificate if its validity is shorter than this value, which should be number of days. (default: `30`)
2731
* *kibana_cert_will_expire_soon*: Set it to true to renew kibana certificate (default: `false`), Or run the playbook with `--tags renew_kibana_cert` to do that.
2832
* *elasticstack_kibana_host*: Hostname users use to connect to Kibana (default: FQDN of the host the role is executed on)
2933
* *elasticstack_kibana_port*: Port Kibana webinterface is listening on (default: `5601`)

docs/role-logstash.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Requirements
1919
------------
2020

2121
* `community.general` collection
22+
* `cryptography` >= 2.5
2223

2324
You need to have the Elastic Repos configured on your system. You can use our [role](./role-repos.md)
2425

molecule/elasticstack_default/converge.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@
4848
service:
4949
name: rsyslog
5050
state: started
51-
- name: Include Kibana
51+
- name: Include kibana
5252
include_role:
5353
name: kibana

molecule/elasticstack_default/prepare.yml

+16
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,19 @@
3333
- git
3434
- openssl
3535
- unzip
36+
- systemd
37+
38+
- name: Update all installed packages RHEL
39+
yum:
40+
name: '*'
41+
state: latest
42+
update_cache: yes
43+
update_only: yes
44+
when: ansible_os_family == "RedHat"
45+
46+
- name: Update all installed packages Debian
47+
apt:
48+
name: '*'
49+
state: latest
50+
update_cache: yes
51+
when: ansible_os_family == "Debian"

molecule/elasticstack_default/requirements.yml

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ roles:
66

77
collections:
88
- community.general
9+
- community.crypto

plugins/module_utils/certs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def extensions_info(self):
169169
# get critical value
170170
critical = to_text(extension.critical)
171171
self.result['extensions'][name]['_critical'] = to_text(critical)
172-
self.extensions_values_info(name, extension)
172+
self.extensions_values_info(name, extension)
173173
except Exception as e:
174174
# if something went wrong skip this extension and its key values and
175175
# also create a warning

roles/beats/defaults/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ elasticstack_ca_dir: /opt/es-ca
6868
elasticstack_ca_pass: PleaseChangeMe
6969
elasticstack_initial_passwords: /usr/share/elasticsearch/initial_passwords
7070
elasticstack_elasticsearch_http_port: 9200
71-
beats_cert_expiration_buffer: 30
71+
beats_cert_expiration_buffer: "+30d"
7272
beats_cert_will_expire_soon: false
7373

7474
# Variables for debugging and development

roles/beats/tasks/beats-security.yml

+8-18
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,25 @@
1313
path: "/etc/beats/certs/{{ inventory_hostname }}-beats.crt"
1414
register: beats_cert_exists
1515

16-
- name: Get the beats certificate expiration date # noqa: risky-shell-pipe
17-
shell: >-
18-
if test -v BASH; then set -o pipefail; fi;
19-
openssl x509
20-
-in '/etc/beats/certs/{{ inventory_hostname }}-beats.crt'
21-
-noout -enddate |
22-
awk -F'=' '{print $2}'
16+
- name: Get the beats certificate expiration date
17+
community.crypto.x509_certificate_info:
18+
path: "/etc/beats/certs/{{ inventory_hostname }}-beats.crt"
19+
valid_at:
20+
check_period: "{{ beats_cert_expiration_buffer }}"
2321
register: beats_cert_expiration_date
24-
args:
25-
executable: /bin/bash
26-
changed_when: false
2722
when: beats_cert_exists.stat.exists | bool
2823

29-
- name: Set the beats certificate expiration date in days
30-
set_fact:
31-
beats_cert_expiration_days: "{{ ((beats_cert_expiration_date.stdout | to_datetime('%b %d %H:%M:%S %Y %Z')) - (ansible_date_time.date | to_datetime('%Y-%m-%d'))).days }}"
32-
when: beats_cert_expiration_date.skipped is not defined
33-
3424
- name: Set beats certificate will expire soon to true
3525
set_fact:
3626
beats_cert_will_expire_soon: true
37-
when: beats_cert_expiration_days is defined and beats_cert_expiration_days | int <= beats_cert_expiration_buffer | int
27+
when: beats_cert_expiration_date.skipped is not defined and not beats_cert_expiration_date.valid_at.check_period
3828

3929
- name: Print the beats certificate renew message
4030
debug:
4131
msg: |
42-
Your beats certificate will expire in {{ beats_cert_expiration_days }}.
32+
Your beats certificate will expire before {{ beats_cert_expiration_buffer }}.
4333
Ansible will renew it.
44-
when: beats_cert_expiration_days is defined and beats_cert_expiration_days | int <= beats_cert_expiration_buffer | int
34+
when: beats_cert_expiration_date.skipped is not defined and not beats_cert_expiration_date.valid_at.check_period
4535

4636
- name: Backup beats certs then remove
4737
when: "'renew_beats_cert' in ansible_run_tags or 'renew_ca' in ansible_run_tags or beats_cert_will_expire_soon | bool"

roles/elasticsearch/handlers/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
service:
55
name: elasticsearch
66
state: restarted
7+
daemon_reload: yes
78
when: elasticsearch_enable | bool
89

910
- name: Restart kibana if available for elasticsearch certificates

roles/elasticsearch/tasks/elasticsearch-security.yml

+14-19
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
elasticstack_ca_will_expire_soon: true
3333
when: >
3434
inventory_hostname == elasticstack_ca and
35-
elasticsearch_cert_expiration_days is defined and
36-
elasticsearch_cert_expiration_days | int <= elasticsearch_cert_expiration_buffer | int
35+
elasticstack_ca_expiration_days is defined and
36+
elasticstack_ca_expiration_days | int <= elasticstack_ca_expiration_buffer | int
3737
3838
- name: Print the ca renew message
3939
debug:
4040
msg: |
41-
Your ca will expire in {{ elasticstack_ca_expiration_days }}.
41+
Your ca will expire in {{ elasticstack_ca_expiration_days }} days.
4242
Ansible will renew it and all elastic stack certificates
4343
when: >
4444
inventory_hostname == elasticstack_ca and
@@ -107,24 +107,17 @@
107107
path: "/etc/elasticsearch/certs/{{ ansible_hostname }}.p12"
108108
register: elasticsearch_cert_exists
109109

110-
- name: Get the elasticsearch certificate expiration date # noqa: risky-shell-pipe
111-
shell: >-
112-
if test -v BASH; then set -o pipefail; fi;
113-
openssl pkcs12
114-
-in "/etc/elasticsearch/certs/{{ ansible_hostname }}.p12"
115-
-nodes
116-
-passin pass:"{{ elasticsearch_tls_key_passphrase }}" |
117-
openssl x509 -noout -enddate |
118-
awk -F'=' '{print $2}'
119-
register: elasticsearch_cert_expiration_date
120-
changed_when: false
121-
no_log: true
110+
- name: Get the elasticsearch certificate expiration date
111+
cert_info:
112+
path: "/etc/elasticsearch/certs/{{ ansible_hostname }}.p12"
113+
passphrase: "{{ elasticsearch_tls_key_passphrase | default(omit, true) }}"
114+
register: elasticsearch_cert_infos
122115
when: elasticsearch_cert_exists.stat.exists | bool
123116

124117
- name: Set the elasticsearch certificate expiration date in days
125118
set_fact:
126-
elasticsearch_cert_expiration_days: "{{ ((elasticsearch_cert_expiration_date.stdout | to_datetime('%b %d %H:%M:%S %Y %Z')) - (ansible_date_time.date | to_datetime('%Y-%m-%d'))).days }}"
127-
when: elasticsearch_cert_expiration_date.skipped is not defined
119+
elasticsearch_cert_expiration_days: "{{ ((elasticsearch_cert_infos.not_valid_after | to_datetime()) - (ansible_date_time.date | to_datetime('%Y-%m-%d'))).days }}"
120+
when: elasticsearch_cert_infos.skipped is not defined
128121

129122
- name: Set elasticsearch certificate will expire soon to true
130123
set_fact:
@@ -134,9 +127,9 @@
134127
- name: Print the elasticsearch certificate renew message
135128
debug:
136129
msg: |
137-
Your elasticsearch certificate will expire in {{ elasticsearch_cert_expiration_days }}.
130+
Your elasticsearch certificate will expire in {{ elasticsearch_cert_expiration_days }} days.
138131
Ansible will renew it.
139-
when: elasticsearch_cert_expiration_day is defined and elasticstack_ca_expiration_days | int <= elasticstack_ca_expiration_buffer | int
132+
when: elasticsearch_cert_expiration_days is defined and elasticsearch_cert_expiration_days | int <= elasticsearch_cert_expiration_buffer | int
140133

141134
- name: Backup elasticsearch certs on node then remove
142135
when: "'renew_es_cert' in ansible_run_tags or 'renew_ca' in ansible_run_tags or elasticsearch_cert_will_expire_soon | bool"
@@ -532,6 +525,7 @@
532525
- name: Wait for all instances to start
533526
include_tasks: wait_for_instance.yml
534527
loop: "{{ groups['elasticsearch'] }}"
528+
tags: notest
535529

536530
- name: Force all notified handlers to run at this point, not waiting for normal sync points
537531
ansible.builtin.meta: flush_handlers
@@ -544,6 +538,7 @@
544538
include_tasks: wait_for_instance.yml
545539
loop: "{{ groups['elasticsearch'] }}"
546540
tags:
541+
- notest
547542
- certificates
548543
- renew_ca
549544
- renew_es_cert

roles/kibana/tasks/kibana-security.yml

+6-14
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,16 @@
2424
path: "/etc/kibana/certs/{{ ansible_hostname }}-kibana.p12"
2525
register: kibana_cert_exists
2626

27-
- name: Get the kibana certificate expiration date # noqa: risky-shell-pipe
28-
shell: >-
29-
if test -v BASH; then set -o pipefail; fi;
30-
openssl pkcs12
31-
-in '/etc/kibana/certs/{{ ansible_hostname }}-kibana.p12'
32-
-nodes -passin pass:'{{ kibana_tls_key_passphrase }}' |
33-
openssl x509 -noout -enddate |
34-
awk -F'=' '{print $2}'
27+
- name: Get the kibana certificate expiration date
28+
cert_info:
29+
path: "/etc/kibana/certs/{{ ansible_hostname }}-kibana.p12"
30+
passphrase: "{{ kibana_tls_key_passphrase | default(omit, true) }}"
3531
register: kibana_cert_expiration_date
36-
args:
37-
executable: /bin/bash
38-
changed_when: false
39-
no_log: true
4032
when: kibana_cert_exists.stat.exists | bool
4133

4234
- name: Set the kibana certificate expiration date in days
4335
set_fact:
44-
kibana_cert_expiration_days: "{{ ((kibana_cert_expiration_date.stdout | to_datetime('%b %d %H:%M:%S %Y %Z')) - (ansible_date_time.date | to_datetime('%Y-%m-%d'))).days }}"
36+
kibana_cert_expiration_days: "{{ ((kibana_cert_expiration_date.not_valid_after | to_datetime()) - (ansible_date_time.date | to_datetime('%Y-%m-%d'))).days }}"
4537
when: kibana_cert_expiration_date.skipped is not defined
4638

4739
- name: Set kibana certificate will expire soon to true
@@ -52,7 +44,7 @@
5244
- name: Print the kibana certificate renew message
5345
debug:
5446
msg: |
55-
Your kibana certificate will expire in {{ kibana_cert_expiration_days }}.
47+
Your kibana certificate will expire in {{ kibana_cert_expiration_days }} days.
5648
Ansible will renew it.
5749
when: kibana_cert_expiration_days is defined and kibana_cert_expiration_days | int <= kibana_cert_expiration_buffer | int
5850

roles/kibana/tasks/main.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@
8484
when: kibana_enable | bool
8585

8686
# the following is useful when running tests or extra tasks that need to
87-
# have Kibana running
87+
# have Kibana running. Escape it on Rocky8, because it gets time out with Elastic 8
8888

8989
- name: Wait for Kibana to start
9090
wait_for:
9191
host: localhost
9292
port: 5601
93+
tags: notest

roles/logstash/tasks/logstash-security.yml

+20-19
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,29 @@
2626
path: "{{ logstash_certs_dir }}/{{ ansible_hostname }}-ls.p12"
2727
register: logstash_cert_exists
2828

29-
- name: Get the logstash certificate expiration date # noqa: risky-shell-pipe
30-
shell: >-
31-
if test -v BASH; then set -o pipefail; fi;
32-
openssl pkcs12
33-
-in '{{ logstash_certs_dir }}/{{ ansible_hostname }}-ls.p12'
34-
-nodes
35-
-passin pass:'{{ logstash_tls_key_passphrase }}' |
36-
openssl x509
37-
-noout
38-
-enddate |
39-
awk -F'=' '{print $2}'
29+
- name: Get the logstash certificate expiration date
30+
cert_info:
31+
path: "{{ logstash_certs_dir }}/{{ ansible_hostname }}-ls.p12"
32+
passphrase: "{{ logstash_tls_key_passphrase | default(omit, true) }}"
4033
register: logstash_cert_expiration_date
41-
args:
42-
executable: /bin/bash
43-
changed_when: false
44-
no_log: true
4534
when: logstash_cert_exists.stat.exists | bool
4635

4736
- name: Set the logstash certificate expiration date in days
4837
set_fact:
49-
logstash_cert_expiration_days: "{{ ((logstash_cert_expiration_date.stdout | to_datetime('%b %d %H:%M:%S %Y %Z')) - (ansible_date_time.date | to_datetime('%Y-%m-%d'))).days }}"
38+
logstash_cert_expiration_days: "{{ ((logstash_cert_expiration_date.not_valid_after | to_datetime()) - (ansible_date_time.date | to_datetime('%Y-%m-%d'))).days }}"
5039
when: logstash_cert_expiration_date.skipped is not defined
5140

5241
- name: Set logstash certificate will expire soon to true
5342
set_fact:
5443
logstash_cert_will_expire_soon: true
55-
when: kiban_cert_expiration_days is defined and logstash_cert_expiration_days | int <= logstash_cert_expiration_buffer | int
44+
when: logstash_cert_expiration_days is defined and logstash_cert_expiration_days | int <= logstash_cert_expiration_buffer | int
5645

5746
- name: Print the logstash certificate renew message
5847
debug:
5948
msg: |
60-
Your logstash certificate will expire in {{ logstash_cert_expiration_days }}.
49+
Your logstash certificate will expire in {{ logstash_cert_expiration_days }} days.
6150
Ansible will renew it.
62-
when: logstash_cert_expiration_days is defined and logstash_cert_expiration_days | int <= logstash_expiration_buffer | int
51+
when: logstash_cert_expiration_days is defined and logstash_cert_expiration_days | int <= logstash_cert_expiration_buffer | int
6352

6453
- name: Backup logstash certs then remove
6554
when: "'renew_logstash_cert' in ansible_run_tags or 'renew_ca' in ansible_run_tags"
@@ -225,6 +214,18 @@
225214
- renew_logstash_cert
226215

227216
- name: Copy the certificate to logstash node
217+
copy:
218+
src: "/tmp/{{ ansible_hostname }}-ls.p12"
219+
dest: "{{ logstash_certs_dir }}/{{ ansible_hostname }}-ls.p12"
220+
owner: root
221+
group: logstash
222+
mode: 0640
223+
tags:
224+
- certificates
225+
- renew_ca
226+
- renew_logstash_cert
227+
228+
- name: Put the certificate in keystore
228229
copy:
229230
src: "/tmp/{{ ansible_hostname }}-ls.p12"
230231
dest: "{{ logstash_certs_dir }}/keystore.pfx"

0 commit comments

Comments
 (0)