ansible-roles/roles/includes/get_rand_pass.yml

180 lines
6.8 KiB
YAML
Raw Normal View History

2021-12-01 19:13:34 +01:00
---
2022-02-27 18:00:05 +01:00
- name: Install tools
package:
name:
- pwgen
- openssl
when: rand_pass_tools_installed is not defined or not rand_pass_tools_installed
# Mark tool sas installed so we do not check each time, as it can be slow
- set_fact: rand_pass_tools_installed=True
# Check if a non encrypted file exists. We do it first for backward compatibility
2022-02-16 15:00:05 +01:00
- name: Check if password file exists
2021-12-01 19:13:34 +01:00
stat: path={{ pass_file }}
2022-03-30 16:00:07 +02:00
register: rand_pass_file_clear
2022-02-27 18:00:05 +01:00
2022-03-30 16:00:07 +02:00
# Check if and encrypted file exists using pbkdf2 key derivation
- name: Check if an encrypted using PBKDF2 password file exists
stat: path={{ pass_file }}.aes256-pbkdf2
register: rand_pass_file_enc_pbkdf2
# Now check if an encrypted file exists (without pbkdf2)
2022-02-27 18:00:05 +01:00
- name: Check if an encrypted password file exists
stat: path={{ pass_file }}.aes256
2022-03-30 16:00:07 +02:00
register: rand_pass_file_enc
2022-02-27 18:00:05 +01:00
# When no clear nor encrypted file exists, generate a random pass with pwgen
2022-03-30 16:00:07 +02:00
- when:
- not rand_pass_file_clear.stat.exists
- not rand_pass_file_enc.stat.exists
- not rand_pass_file_enc_pbkdf2.stat.exists
block:
- name: Generate a random password
shell: pwgen {% if complex | default(True) %}-y -r \`\'\"\\\|\^\# {% endif %}-s {{ pass_size | default(50) }} 1
register: rand_pass_new
- set_fact: rand_pass_new={{ rand_pass_new.stdout | trim }}
# Check if openssl supports pbkdf2
- name: Check if openssl supports pbkdf2 key derivation
command: openssl enc -pbkdf2 -list
register: pass_openssl_pbkdf2
failed_when: False
changed_when: False
2022-02-27 18:00:05 +01:00
# New pass generation ? Encrypt it with openssl, unless encryption is disabled, or the global rand_pass_encryption_key isn't defined
2022-03-30 16:00:07 +02:00
# If openssl supports PBKDF2 key derivation, use it
- when:
- not rand_pass_file_clear.stat.exists
- not rand_pass_file_enc.stat.exists
- not rand_pass_file_enc_pbkdf2.stat.exists
- encryption | default(True)
- rand_pass_encryption_key is defined
2021-12-01 19:13:34 +01:00
block:
2022-02-27 18:00:05 +01:00
- name: Encrypt the generated password
2022-03-30 16:00:07 +02:00
shell: openssl enc -e -a -aes256 {% if pass_openssl_pbkdf2.rc == 0 %}-pbkdf2 {% endif %}-pass pass:{{ rand_pass_encryption_key | quote }}
2022-02-27 18:00:05 +01:00
args:
2022-03-30 16:00:07 +02:00
stdin: "{{ rand_pass_new }}"
register: rand_pass_enc
2022-04-05 18:00:07 +02:00
no_log: True
2022-03-30 16:00:07 +02:00
- copy: content={{ rand_pass_enc.stdout | trim }} dest={{ pass_file }}.aes256{% if pass_openssl_pbkdf2.rc == 0 %}-pbkdf2{% endif %} mode=600
2022-02-27 18:00:05 +01:00
# New pass generation but with encryption disabled, or the global rand_pass_encryption_key not defined
# in this case, store the password as plain text
2022-03-30 16:00:07 +02:00
- when:
- not rand_pass_file_clear.stat.exists
- not rand_pass_file_enc.stat.exists
- not rand_pass_file_enc_pbkdf2.stat.exists
- not encryption | default(True) or rand_pass_encryption_key is not defined
name: Store the generated password as clear text
copy: content={{ rand_pass_new }} dest={{ pass_file }} mode=600
2021-12-01 19:13:34 +01:00
2022-03-30 16:00:07 +02:00
# Read the encrypted pass (with PBKDF2)
- when:
- not rand_pass_file_clear.stat.exists
- not rand_pass_file_enc.stat.exists
2022-04-22 16:00:12 +02:00
- rand_pass_file_enc_pbkdf2.stat.exists or (rand_pass_new is defined and pass_openssl_pbkdf2.rc == 0)
2022-03-30 16:00:07 +02:00
- pass_openssl_pbkdf2.rc == 0
- encryption | default(True)
- rand_pass_encryption_key is defined
block:
- name: Read the encrypted (with PBKDF2) password
slurp: src={{ pass_file }}.aes256-pbkdf2
register: rand_pass_enc_pbkdf2
- name: Decrypt (using PBKDF2) the password
shell: openssl enc -d -a -aes256 -pbkdf2 -pass pass:{{ rand_pass_encryption_key | quote }}
args:
stdin: "{{ rand_pass_enc_pbkdf2.content | b64decode | trim }}"
register: rand_pass_dec_pbkdf2
changed_when: False
2022-04-05 18:00:07 +02:00
no_log: True
2022-03-30 16:00:07 +02:00
- set_fact: rand_pass_dec_pbkdf2={{ rand_pass_dec_pbkdf2.stdout | trim }}
# Read the encrypted pass (without PBKDF2)
- when:
- not rand_pass_file_clear.stat.exists
2022-04-22 16:00:12 +02:00
- rand_pass_file_enc.stat.exists or (rand_pass_new is defined and pass_openssl_pbkdf2.rc != 0)
2022-03-30 16:00:07 +02:00
- not rand_pass_file_enc_pbkdf2.stat.exists
- encryption | default(True)
- rand_pass_encryption_key is defined
2022-02-27 18:00:05 +01:00
block:
- name: Read the encrypted password
slurp: src={{ pass_file }}.aes256
2022-03-30 16:00:07 +02:00
register: rand_pass_enc
2022-02-27 18:00:05 +01:00
- name: Decrypt the password
shell: openssl enc -d -a -aes256 -pass pass:{{ rand_pass_encryption_key | quote }}
args:
2022-03-30 16:00:07 +02:00
stdin: "{{ rand_pass_enc.content | b64decode | trim }}"
register: rand_pass_dec
2022-02-27 18:00:05 +01:00
changed_when: False
2022-04-05 18:00:07 +02:00
no_log: True
2022-02-27 18:00:05 +01:00
2022-03-30 16:00:07 +02:00
- set_fact: rand_pass_dec={{ rand_pass_dec.stdout }}
2022-02-27 18:00:05 +01:00
# Read unencrypted pass file
2022-03-30 16:00:07 +02:00
- when: not encryption | default(True) or rand_pass_encryption_key is not defined or rand_pass_file_clear.stat.exists
2022-02-27 18:00:05 +01:00
block:
- name: Read the clear text password
slurp: src={{ pass_file }}
register: rand_pass_clear
2022-03-30 16:00:07 +02:00
- set_fact: rand_pass_clear={{ rand_pass_clear.content | b64decode | trim }}
# Reencrypt the password from clear text when possible
- when:
- rand_pass_file_clear.stat.exists
- encryption | default(True)
- rand_pass_encryption_key is defined
block:
- name: Re encrypt the clear text password
shell: openssl enc -e -a -aes256 {% if pass_openssl_pbkdf2.rc == 0 %}-pbkdf2 {% endif %}-pass pass:{{ rand_pass_encryption_key | quote }}
args:
stdin: "{{ rand_pass_clear }}"
register: rand_pass_reenc
2022-04-05 18:00:07 +02:00
no_log: True
2022-03-30 16:00:07 +02:00
- name: Store the re encrypted password
copy: content={{ rand_pass_reenc.stdout | trim }} dest={{ pass_file }}.aes256{% if pass_openssl_pbkdf2.rc == 0 %}-pbkdf2{% endif %} mode=600
- name: Remove the clear text pass file
file: path={{ pass_file }} state=absent
# Reencrypt the password from aes256 to aes256 with PBKDF2 when possible
- when:
- rand_pass_file_enc.stat.exists
- not rand_pass_file_enc_pbkdf2.stat.exists
- pass_openssl_pbkdf2.rc == 0
- encryption | default(True)
- rand_pass_encryption_key is defined
block:
- name: Re encrypt the password using PBKDF2
shell: openssl enc -e -a -aes256 -pbkdf2 -pass pass:{{ rand_pass_encryption_key | quote }}
args:
stdin: "{{ rand_pass_dec }}"
register: rand_pass_reenc
2022-04-05 18:00:07 +02:00
no_log: True
2022-03-30 16:00:07 +02:00
- name: Store the re encrypted password with PBKDF2
copy: content={{ rand_pass_reenc.stdout | trim }} dest={{ pass_file }}.aes256-pbkdf2 mode=600
- name: Remove the encrypted pass file without PBKDF2
file: path={{ pass_file }}.aes256 state=absent
2022-02-27 18:00:05 +01:00
# Now set either the decrypted, or the clear text pass in the rand_pass variable which will be used by the caller
- set_fact:
rand_pass: >-
2022-03-30 16:00:07 +02:00
{%- if rand_pass_dec_pbkdf2 is defined and rand_pass_dec_pbkdf2.skipped is not defined -%}{{ rand_pass_dec_pbkdf2 }}
{%- elif rand_pass_dec is defined and rand_pass_dec.skipped is not defined -%}{{ rand_pass_dec }}
{%- elif rand_pass_clear is defined and rand_pass_clear.skipped is not defined -%}{{ rand_pass_clear }}
2022-02-27 18:00:05 +01:00
{%- else -%}{%- endif -%}
2021-12-01 19:13:34 +01:00