Skip to content

Commit e3a6ef4

Browse files
committed
Change the way the Wazuh admin password is generated to always be valid
1 parent fd5b8ac commit e3a6ef4

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

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 | default(lookup('password', '/dev/null length=30' ), true) }}"
10+
password: "{{ secrets_wazuh.wazuh_api_users[0].password | 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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,54 @@
1414
path: "{{ wazuh_secrets_path | dirname }}"
1515
state: directory
1616

17+
- 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
60+
61+
- name: Store the valid password
62+
set_fact:
63+
wazuh_password: "{{ random_pass }}"
64+
1765
- name: Template new secrets
1866
template:
1967
src: wazuh-secrets.yml.j2

0 commit comments

Comments
 (0)