|
14 | 14 | path: "{{ wazuh_secrets_path | dirname }}"
|
15 | 15 | state: directory
|
16 | 16 |
|
| 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 | + |
17 | 65 | - name: Template new secrets
|
18 | 66 | template:
|
19 | 67 | src: wazuh-secrets.yml.j2
|
|
0 commit comments