Skip to content

Commit b23ba38

Browse files
committed
Generate Wazuh password and encrypt the file at the end.
1 parent e3a6ef4 commit b23ba38

File tree

3 files changed

+57
-50
lines changed

3 files changed

+57
-50
lines changed

etc/kayobe/ansible/scripts/pwgen.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import random
2+
import string
3+
import re
4+
5+
# The password requirements required by Wazuh (wazuh/framework/wazuh/security.py)
6+
valid_password = re.compile(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,}$')
7+
8+
# Generate a random password containg at least one of each:
9+
# special character, digit, lowercase letter, uppercase letter
10+
def pw_gen(pw_len):
11+
random_pass = ([random.choice("@$!%*?&-_"),
12+
random.choice(string.digits),
13+
random.choice(string.ascii_lowercase),
14+
random.choice(string.ascii_uppercase),
15+
]
16+
+ [random.choice(string.ascii_lowercase
17+
+ string.ascii_uppercase
18+
+ "@$!%*?&-_"
19+
+ string.digits) for i in range(pw_len)])
20+
21+
random.shuffle(random_pass)
22+
random_pass = ''.join(random_pass)
23+
return random_pass
24+
25+
# Check if the generated password meets the requirements
26+
def check_user_password(password):
27+
if valid_password.match(password):
28+
return True
29+
else:
30+
return False
31+
32+
# Generate a password
33+
random_password = pw_gen(30)
34+
35+
# Check if the generated password meets the requirements
36+
# if not, keep generating a new password until it does
37+
while not check_user_password(random_password):
38+
#print("Password does not meet the requirements, creating a new one...")
39+
random_password = pw_gen(30)
40+
else:
41+
print(random_password)

etc/kayobe/ansible/templates/wazuh-secrets.yml.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ secrets_wazuh:
77
# Strengthen default wazuh api user pass
88
wazuh_api_users:
99
- username: "wazuh"
10-
password: "{{ secrets_wazuh.wazuh_api_users[0].password | wazuh_password }}"
10+
password: "{{ secrets_wazuh.wazuh_api_users[0].password | default(wazuh_password) }}"
1111
# OpenSearch 'admin' user pass
1212
opendistro_admin_password: "{{ secrets_wazuh.opendistro_admin_password | default(lookup('password', '/dev/null'), true) }}"
1313
# OpenSearch 'kibanaserver' user pass

etc/kayobe/ansible/wazuh-secrets.yml

Lines changed: 15 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -15,61 +15,27 @@
1515
state: directory
1616

1717
- name: Generate a random password which meets the Wazuh password requirements
18-
cmd: python3
19-
stdin: |
20-
import random
21-
import string
22-
import re
23-
24-
# The password requirements required by Wazuh (wazuh/framework/wazuh/security.py)
25-
valid_password = re.compile(r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,}$')
26-
27-
# Generate a random password containg at least one of each:
28-
# special character, digit, lowercase letter, uppercase letter
29-
def pw_gen(pw_len):
30-
random_pass = ([random.choice("@$!%*?&-_"),
31-
random.choice(string.digits),
32-
random.choice(string.ascii_lowercase),
33-
random.choice(string.ascii_uppercase),
34-
]
35-
+ [random.choice(string.ascii_lowercase
36-
+ string.ascii_uppercase
37-
+ "@$!%*?&-_"
38-
+ string.digits) for i in range(pw_len)])
39-
40-
random.shuffle(random_pass)
41-
random_pass = ''.join(random_pass)
42-
return random_pass
43-
44-
# Check if the generated password meets the requirements
45-
def check_user_password(password):
46-
if valid_password.match(password):
47-
return True
48-
else:
49-
return False
50-
51-
# Generate a password
52-
random_pass = pw_gen(30)
53-
54-
# Check if the generated password meets the requirements
55-
# if not, keep generating a new password until it does
56-
while not check_user_password(random_pass):
57-
random_pass = pw_gen(30)
58-
59-
register: random_pass
18+
no_log: True
19+
command:
20+
cmd: python3 scripts/pwgen.py
21+
register: random_password
6022

6123
- name: Store the valid password
24+
no_log: True
6225
set_fact:
63-
wazuh_password: "{{ random_pass }}"
26+
wazuh_password: "{{ random_password.stdout }}"
6427

6528
- name: Template new secrets
29+
no_log: True
6630
template:
6731
src: wazuh-secrets.yml.j2
6832
dest: "{{ wazuh_secrets_path }}"
69-
notify: Please encrypt keys
7033

71-
handlers:
72-
- name: Please encrypt keys
73-
debug:
74-
msg: >-
75-
Please encrypt the keys using Ansible Vault.
34+
- name: In-place encrypt wazuh-secrets
35+
copy:
36+
content: "{{ lookup('ansible.builtin.file', wazuh_secrets_path) | ansible.builtin.vault(ansible_vault_password) }}"
37+
dest: "{{ wazuh_secrets_path }}"
38+
decrypt: false
39+
vars:
40+
ansible_vault_password: "{{ lookup('ansible.builtin.env', 'KAYOBE_VAULT_PASSWORD') }}"
41+

0 commit comments

Comments
 (0)