replace random with secrets when generating passwords (#85971)

---------

Signed-off-by: Thomas Sjögren <konstruktoid@users.noreply.github.com>
Co-authored-by: Felix Fontein <felix@fontein.de>
pull/86220/head
Thomas Sjögren 2 weeks ago committed by GitHub
parent ce84d3157d
commit 6d428ca8f0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,3 @@
bugfixes:
- password lookup plugin - replace random.SystemRandom() with secrets.SystemRandom() when
generating passwords (https://github.com/ansible/ansible/issues/85956, https://github.com/ansible/ansible/pull/85971).

@ -67,7 +67,7 @@ DOCUMENTATION = """
description: description:
- A seed to initialize the random number generator. - A seed to initialize the random number generator.
- Identical seeds will yield identical passwords. - Identical seeds will yield identical passwords.
- Use this for random-but-idempotent password generation. - B(Note) that a weak seed, one without enough entropy, will not create a sufficiently secure encryption for the password.
type: str type: str
notes: notes:
- A great alternative to the password lookup plugin, - A great alternative to the password lookup plugin,
@ -113,7 +113,7 @@ EXAMPLES = """
ansible.builtin.set_fact: ansible.builtin.set_fact:
random_pod_name: "web-{{ lookup('ansible.builtin.password', '/dev/null', chars=['ascii_lowercase', 'digits'], length=8) }}" random_pod_name: "web-{{ lookup('ansible.builtin.password', '/dev/null', chars=['ascii_lowercase', 'digits'], length=8) }}"
- name: create random but idempotent password - name: create idempotent password for use in testing/CI, not recommended for production
ansible.builtin.set_fact: ansible.builtin.set_fact:
password: "{{ lookup('ansible.builtin.password', '/dev/null', seed=inventory_hostname) }}" password: "{{ lookup('ansible.builtin.password', '/dev/null', seed=inventory_hostname) }}"
""" """

@ -63,9 +63,10 @@ def random_password(length=DEFAULT_PASSWORD_LENGTH, chars=C.DEFAULT_PASSWORD_CHA
raise AnsibleAssertionError(f'{chars=!r} ({type(chars)}) is not a {type(str)}.') raise AnsibleAssertionError(f'{chars=!r} ({type(chars)}) is not a {type(str)}.')
if seed is None: if seed is None:
random_generator = random.SystemRandom() random_generator = secrets.SystemRandom()
else: else:
random_generator = random.Random(seed) random_generator = random.Random(seed)
return u''.join(random_generator.choice(chars) for dummy in range(length)) return u''.join(random_generator.choice(chars) for dummy in range(length))

Loading…
Cancel
Save