-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkibana-security.yml
264 lines (236 loc) · 8.4 KB
/
kibana-security.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
---
- name: Install packages for security tasks
ansible.builtin.package:
name:
- unzip
- python3-cryptography
- openssl
tags:
- certificates
- renew_ca
- renew_kibana_cert
- name: Set elasticstack_ca variable if not already done by user
ansible.builtin.set_fact:
elasticstack_ca: "{{ groups['elasticsearch'][0] }}"
when:
- elasticstack_ca is undefined
- groups['elasticsearch'] is defined
tags:
- certificates
- renew_ca
- renew_kibana_cert
- name: Ensure kibana certificate exists
ansible.builtin.stat:
path: "/etc/kibana/certs/{{ ansible_hostname }}-kibana.p12"
register: kibana_cert_exists
- name: Get the kibana certificate expiration date
cert_info:
path: "/etc/kibana/certs/{{ ansible_hostname }}-kibana.p12"
passphrase: "{{ kibana_tls_key_passphrase | default(omit, true) }}"
register: kibana_cert_expiration_date
when: kibana_cert_exists.stat.exists | bool
- name: Set the kibana certificate expiration date in days
ansible.builtin.set_fact:
kibana_cert_expiration_days: "{{ ((kibana_cert_expiration_date.not_valid_after | to_datetime()) - (ansible_date_time.date | to_datetime('%Y-%m-%d'))).days }}"
when: kibana_cert_expiration_date.skipped is not defined
- name: Set kibana certificate will expire soon to true
ansible.builtin.set_fact:
kibana_cert_will_expire_soon: true
when: kibana_cert_expiration_days is defined and kibana_cert_expiration_days | int <= kibana_cert_expiration_buffer | int
- name: Print the kibana certificate renew message
ansible.builtin.debug:
msg: |
Your kibana certificate will expire in {{ kibana_cert_expiration_days }} days.
Ansible will renew it.
when: kibana_cert_expiration_days is defined and kibana_cert_expiration_days | int <= kibana_cert_expiration_buffer | int
- name: Backup kibana certs then remove
when: "'renew_kibana_cert' in ansible_run_tags or 'renew_ca' in ansible_run_tags or kibana_cert_will_expire_soon | bool"
tags:
- renew_ca
- renew_kibana_cert
block:
- name: Check if cert directory exists
ansible.builtin.stat:
path: /etc/kibana/certs
register: kibana_check_cert_path
- name: Move cert directory
ansible.builtin.copy:
src: /etc/kibana/certs
dest: "/etc/kibana/certs_{{ ansible_date_time.iso8601_micro }}"
mode: preserve
remote_src: true
when: kibana_check_cert_path.stat.exists
register: kibana_move_cert_directory
- name: Remove cert directory
ansible.builtin.file:
path: /etc/kibana/certs
state: absent
when: kibana_move_cert_directory.changed
- name: Backup kibana certs on elasticstack_ca host then remove
when: "'renew_kibana_cert' in ansible_run_tags or 'renew_ca' in ansible_run_tags or kibana_cert_will_expire_soon | bool"
delegate_to: "{{ elasticstack_ca }}"
tags:
- renew_ca
- renew_kibana_cert
block:
- name: Check if cert file exists
ansible.builtin.stat:
path: "{{ elasticstack_ca_dir }}/{{ ansible_hostname }}-kibana.p12"
register: kibana_check_cert_file
- name: Move cert file
ansible.builtin.copy:
src: "{{ elasticstack_ca_dir }}/{{ ansible_hostname }}-kibana.p12"
dest: "{{ elasticstack_ca_dir }}/{{ ansible_hostname }}-kibana.p12_{{ ansible_date_time.iso8601_micro }}"
mode: preserve
remote_src: true
when: kibana_check_cert_file.stat.exists
register: kibana_move_cert_file
- name: Remove cert file
ansible.builtin.file:
path: "{{ elasticstack_ca_dir }}/{{ ansible_hostname }}-kibana.p12"
state: absent
when: kibana_move_cert_file.changed
- name: Backup kibana cert on localhost then remove
delegate_to: localhost
when: "'renew_kibana_cert' in ansible_run_tags or 'renew_ca' in ansible_run_tags or kibana_cert_will_expire_soon | bool"
tags:
- renew_ca
- renew_kibana_cert
block:
- name: Check the existance of cert on localhost
ansible.builtin.stat:
path: /tmp/{{ ansible_hostname }}-kibana.p12
register: kibana_check_temporary_cert
- name: Move temporary cert file
ansible.builtin.copy:
src: /tmp/{{ ansible_hostname }}-kibana.p12
dest: "/tmp/{{ ansible_hostname }}-kibana.p12_{{ ansible_date_time.iso8601_micro }}"
mode: preserve
when: kibana_check_temporary_cert.stat.exists
register: kibana_move_cert_file
- name: Remove temporary cert file
ansible.builtin.file:
path: /tmp/{{ ansible_hostname }}-kibana.p12
state: absent
when: kibana_move_cert_file.changed
- name: Block for key generation
delegate_to: "{{ elasticstack_ca }}"
run_once: true
tags:
- certificates
- renew_ca
- renew_kibana_cert
block:
- name: Generate encryption key # noqa: risky-shell-pipe
ansible.builtin.shell: >
if test -n "$(ps -p $$ | grep bash)"; then set -o pipefail; fi;
openssl rand -base64 36 >
{{ elasticstack_ca_dir }}/encryption_key
changed_when: false
args:
creates: "{{ elasticstack_ca_dir }}/encryption_key"
- name: Fetch encryption key
ansible.builtin.command: cat {{ elasticstack_ca_dir }}/encryption_key
changed_when: false
register: kibana_encryption_key
- name: Generate saved objects encryption key # noqa: risky-shell-pipe
ansible.builtin.shell: >
if test -n "$(ps -p $$ | grep bash)"; then set -o pipefail; fi;
openssl rand
-base64 36 >
{{ elasticstack_ca_dir }}/savedobjects_encryption_key
changed_when: false
args:
creates: "{{ elasticstack_ca_dir }}/savedobjects_encryption_key"
- name: Fetch saved objects encryption key
ansible.builtin.command: cat {{ elasticstack_ca_dir }}/savedobjects_encryption_key
changed_when: false
register: kibana_savedobjects_encryption_key
- name: Create certificate directory
ansible.builtin.file:
path: /etc/kibana/certs
state: directory
owner: root
group: kibana
mode: 0750
tags:
- certificates
- renew_ca
- renew_kibana_cert
- name: Create individual certificates for Kibana
ansible.builtin.command: >
/usr/share/elasticsearch/bin/elasticsearch-certutil cert
--days {{ kibana_cert_validity_period }}
--ca {{ elasticstack_ca_dir }}/elastic-stack-ca.p12
--ca-pass {{ elasticstack_ca_pass }}
--name {{ ansible_hostname }}
--ip {{ ansible_default_ipv4.address | default(ansible_all_ipv4_addresses[0]) }}
--dns {{ ansible_hostname }},{{ ansible_fqdn }},{{ inventory_hostname }}
--pass {{ kibana_tls_key_passphrase }}
--out {{ elasticstack_ca_dir }}/{{ ansible_hostname }}-kibana.p12
delegate_to: "{{ elasticstack_ca }}"
no_log: "{{ elasticstack_no_log }}"
args:
creates: "{{ elasticstack_ca_dir }}/{{ ansible_hostname }}-kibana.p12"
tags:
- certificates
- renew_ca
- renew_kibana_cert
- name: Fetch certificate from ca host to master
ansible.builtin.fetch:
src: "{{ elasticstack_ca_dir }}/{{ ansible_hostname }}-kibana.p12"
dest: "/tmp/"
flat: yes
delegate_to: "{{ elasticstack_ca }}"
tags:
- certificates
- renew_ca
- renew_kibana_cert
- name: Copy the certificate to actual node
ansible.builtin.copy:
src: "/tmp/{{ ansible_hostname }}-kibana.p12"
dest: "/etc/kibana/certs"
owner: root
group: kibana
mode: 0640
notify:
- Restart Kibana
tags:
- certificates
- renew_ca
- renew_kibana_cert
- name: Fetch Kibana password # noqa: risky-shell-pipe
ansible.builtin.shell: >
if test -n "$(ps -p $$ | grep bash)"; then set -o pipefail; fi;
grep "PASSWORD kibana_system " /usr/share/elasticsearch/initial_passwords |
awk {' print $4 '}
register: kibana_password
changed_when: false
no_log: "{{ elasticstack_no_log }}"
delegate_to: "{{ elasticstack_ca }}"
- name: Fetch ca certificate from ca host to master
ansible.builtin.fetch:
src: "{{ elasticstack_ca_dir }}/ca.crt"
dest: /tmp/ca.crt
flat: yes
delegate_to: "{{ elasticstack_ca }}"
tags:
- certificates
- renew_ca
- renew_kibana_cert
- name: Copy the ca certificate to actual node
ansible.builtin.copy:
src: /tmp/ca.crt
dest: /etc/kibana/certs
owner: root
group: kibana
mode: 0640
notify:
- Restart Kibana
tags:
- certificates
- renew_ca
- renew_kibana_cert
- name: Create default web certificate
ansible.builtin.include_tasks: kibana-web-cert.yml
when: kibana_tls | bool