관리-도구
편집 파일: password.cpython-39.opt-1.pyc
a �����)g�F����������������������@���s��d�dl�mZmZmZ�eZdZdZdZd�dl Z d�dl Z d�dlZd�dlZd�dl mZmZ�d�dlmZmZmZ�d�dlmZ�d�d lmZ�d�d lmZ�d�dlmZmZmZmZ�d�dlm Z �e!d �Z"dd��Z#dd��Z$dd��Z%ddd�Z&dd��Z'dd��Z(dd��Z)G�dd��de�Z*dS�)�����)�absolute_import�division�print_functiona!�� name: password version_added: "1.1" author: - Daniel Hokka Zakrisson (!UNKNOWN) <daniel@hozac.com> - Javier Candeira (!UNKNOWN) <javier@candeira.com> - Maykel Moya (!UNKNOWN) <mmoya@speedyrails.com> short_description: retrieve or generate a random password, stored in a file description: - Generates a random plaintext password and stores it in a file at a given filepath. - If the file exists previously, it will retrieve its contents, behaving just like with_file. - 'Usage of variables like C("{{ inventory_hostname }}") in the filepath can be used to set up random passwords per host, which simplifies password management in C("host_vars") variables.' - A special case is using /dev/null as a path. The password lookup will generate a new random password each time, but will not write it to /dev/null. This can be used when you need a password without storing it on the controller. options: _terms: description: - path to the file that stores/will store the passwords required: True encrypt: description: - Which hash scheme to encrypt the returning password, should be one hash scheme from C(passlib.hash; md5_crypt, bcrypt, sha256_crypt, sha512_crypt). - If not provided, the password will be returned in plain text. - Note that the password is always stored as plain text, only the returning password is encrypted. - Encrypt also forces saving the salt value for idempotence. - Note that before 2.6 this option was incorrectly labeled as a boolean for a long time. ident: description: - Specify version of Bcrypt algorithm to be used while using C(encrypt) as C(bcrypt). - The parameter is only available for C(bcrypt) - U(https://passlib.readthedocs.io/en/stable/lib/passlib.hash.bcrypt.html#passlib.hash.bcrypt). - Other hash types will simply ignore this parameter. - 'Valid values for this parameter are: C(2), C(2a), C(2y), C(2b).' type: string version_added: "2.12" chars: version_added: "1.4" description: - A list of names that compose a custom character set in the generated passwords. - 'By default generated passwords contain a random mix of upper and lowercase ASCII letters, the numbers 0-9, and punctuation (". , : - _").' - "They can be either parts of Python's string module attributes or represented literally ( :, -)." - "Though string modules can vary by Python version, valid values for both major releases include: 'ascii_lowercase', 'ascii_uppercase', 'digits', 'hexdigits', 'octdigits', 'printable', 'punctuation' and 'whitespace'." - Be aware that Python's 'hexdigits' includes lower and upper case versions of a-f, so it is not a good choice as it doubles the chances of those values for systems that won't distinguish case, distorting the expected entropy. - "when using a comma separated string, to enter comma use two commas ',,' somewhere - preferably at the end. Quotes and double quotes are not supported." type: list elements: str default: ['ascii_letters', 'digits', ".,:-_"] length: description: The length of the generated password. default: 20 type: integer seed: version_added: "2.12" description: - A seed to initialize the random number generator. - Identical seeds will yield identical passwords. - Use this for random-but-idempotent password generation. type: str notes: - A great alternative to the password lookup plugin, if you don't need to generate random passwords on a per-host basis, would be to use Vault in playbooks. Read the documentation there and consider using it first, it will be more desirable for most applications. - If the file already exists, no data will be written to it. If the file has contents, those contents will be read in as the password. Empty files cause the password to return as an empty string. - 'As all lookups, this runs on the Ansible host as the user running the playbook, and "become" does not apply, the target file must be readable by the playbook user, or, if it does not exist, the playbook user must have sufficient privileges to create it. (So, for example, attempts to write into areas such as /etc will fail unless the entire playbook is being run as root).' a;�� - name: create a mysql user with a random password community.mysql.mysql_user: name: "{{ client }}" password: "{{ lookup('ansible.builtin.password', 'credentials/' + client + '/' + tier + '/' + role + '/mysqlpassword', length=15) }}" priv: "{{ client }}_{{ tier }}_{{ role }}.*:ALL" - name: create a mysql user with a random password using only ascii letters community.mysql.mysql_user: name: "{{ client }}" password: "{{ lookup('ansible.builtin.password', '/tmp/passwordfile', chars=['ascii_letters']) }}" priv: '{{ client }}_{{ tier }}_{{ role }}.*:ALL' - name: create a mysql user with an 8 character random password using only digits community.mysql.mysql_user: name: "{{ client }}" password: "{{ lookup('ansible.builtin.password', '/tmp/passwordfile', length=8, chars=['digits']) }}" priv: "{{ client }}_{{ tier }}_{{ role }}.*:ALL" - name: create a mysql user with a random password using many different char sets community.mysql.mysql_user: name: "{{ client }}" password: "{{ lookup('ansible.builtin.password', '/tmp/passwordfile', chars=['ascii_letters', 'digits', 'punctuation']) }}" priv: "{{ client }}_{{ tier }}_{{ role }}.*:ALL" - name: create lowercase 8 character name for Kubernetes pod name ansible.builtin.set_fact: random_pod_name: "web-{{ lookup('ansible.builtin.password', '/dev/null', chars=['ascii_lowercase', 'digits'], length=8) }}" - name: create random but idempotent password ansible.builtin.set_fact: password: "{{ lookup('ansible.builtin.password', '/dev/null', seed=inventory_hostname) }}" zD _raw: description: - a password type: list elements: str N)�AnsibleError�AnsibleAssertionError)�to_bytes� to_native�to_text)�string_types)�parse_kv)� LookupBase)�BaseHash� do_encrypt�random_password�random_salt)� makedirs_safe)�length�encrypt�chars�ident�seedc�����������������C���sV���d}t�j�|��rRt|�d��}|������}W�d����n1�s<0����Y��t|dd�}|S�)z�Read the contents of a password file and return it :arg b_path: A byte string containing the path to the password file :returns: a text string containing the contents of the password file or None if no password file was present. N�rb�surrogate_or_strict��errors)�os�path�exists�open�read�rstripr ���)�b_path�content�f� b_content��r%����C/usr/lib/python3.9/site-packages/ansible/plugins/lookup/password.py�_read_password_file����s����*r'���c�����������������C���sJ���g�}|�D�]"}|��tttt|�|�dd���qd�|��dd��dd�}|S�)ah��Generate a string containing all valid chars as defined by ``characters`` :arg characters: A list of character specs. The character specs are shorthand names for sets of characters like 'digits', 'ascii_letters', or 'punctuation' or a string to be included verbatim. The values of each char spec can be: * a name of an attribute in the 'strings' module ('digits' for example). The value of the attribute will be added to the candidate chars. * a string of characters. If the string isn't an attribute in 'string' module, the string will be directly added to the candidate chars. For example:: characters=['digits', '?|']`` will match ``string.digits`` and add all ascii digits. ``'?|'`` will add the question mark and pipe characters directly. Return will be the string:: u'0123456789?|' �strictr������"�')�appendr ����getattr�stringr����join�replace)Z charactersr���Z chars_specr%���r%���r&����_gen_candidate_chars����s ���� r1���c�����������������C���s����|�}d}d}d}d}d}z|���|�}W�n�ty8���Y�n"0�|�|t|��d��}|�d|��}|r�z|��|�}W�n�ty����|}Y�n"0�||t|��d��}|d|��}|||fS�)z�parse our password data format into password and salt :arg content: The data read from the file :returns: password and salt Nz salt=z ident=r)���)�rindex� ValueError�len)r"����password�saltr���Z salt_slugZ ident_slugZrem�sepr%���r%���r&����_parse_content����s(���� r8���c�����������������C���s6���|s|s|�S�|st�d��|r*d|�||f�S�d|�|f�S�)a���Format the password and salt for saving :arg password: the plaintext password to save :arg salt: the salt to use when encrypting a password :arg encrypt: Which method the user requests that this password is encrypted. Note that the password is saved in clear. Encrypt just tells us if we must save the salt value for idempotence. Defaults to None. :arg ident: Which version of BCrypt algorithm to be used. Valid only if value of encrypt is bcrypt. Defaults to None. :returns: a text string containing the formatted information .. warning:: Passwords are saved in clear. This is because the playbooks expect to get cleartext passwords from this lookup. zF_format_content was called with encryption requested but no salt valuez%s salt=%s ident=%sz %s salt=%s)r���)r5���r6���r���r���r%���r%���r&����_format_content����s����r9���c�����������������C���sl���t�j�|��}t|dd��t|�d��6}t��|�d��t|dd�d�}|�|��W�d�����n1�s^0����Y��d�S�)N�����mode�wbi���r���r������� )r���r����dirnamer���r����chmodr����write)r!���r"���� b_pathdirr#���r$���r%���r%���r&����_write_password_file����s����rC���c�������������� ���C���s����d}t�j�|��}tdt�|�������}t�j�||�}t�j�|�s�|�td�kr�z2t |dd��t�� |t�jt�jB��}t�� |��d}W�n0�ty��}�z|jdkr���W�Y�d}~n d}~0�0�d }t�j�|�r�|s�t�d |���|d kr�td|���|d7�}q�||fS�) z'Get the lock for writing password file.Fz%s.ansible_lockfile� /dev/nullr:���r;���TzFile existsNr�������z�Password lookup cannot get the lock in 7 seconds, abort...This may caused by un-removed lockfileyou can manually remove it from controller machine at %s and try again����)r���r���r?���r����hashlibZsha1Z hexdigestr/���r���r���r����O_CREAT�O_EXCL�close�OSError�strerror�time�sleepr���)r!���� first_processrB���Z lockfile_name�lockfile�fd�eZcounterr%���r%���r&���� _get_lock��s,���� � rS���c�����������������C���s���t�j�|��rt��|���dS�)z?Release the lock so other processes can read the password file.N)r���r���r����remove)rP���r%���r%���r&���� _release_lock$��s����rU���c�������������������@���s���e�Zd�Zdd��Zdd��ZdS�)�LookupModulec�����������������C���sp��|��dd�}t|�dkr$|}t��}nF|d�}t|d��}d|v�rjd�||d�f�}|d=�|�|�sjtd��t|����� t �}|r�tdd�|����t|�d|�� d���|d<�|�d |�� d ��|d <�|�d |�� d ��|d <�|�d|�� d��|d<�|�d|�� d��|d<�|d��rht|d�t��rhg�}d |d�v��r:|�d��|�dd��|d��d d���d�D����||d<�||fS�)z�Hacky parsing of params See https://github.com/ansible/ansible-modules-core/issues/1968#issuecomment-136842156 and the first_found lookup For how we want to fix this later � rF���r���Z_raw_paramszFUnrecognized value after key=value parameters given to password lookupz6Unrecognized parameter(s) given to password lookup: %sz, r���r���r���r���r���z,,�,c�����������������s���s���|�]}|r|V��qd�S�)Nr%���)�.0�cr%���r%���r&���� <genexpr>W�������z1LookupModule._parse_parameters.<locals>.<genexpr>)�splitr4����dictr���r/���� startswithr���� frozenset�keys� difference�VALID_PARAMS�int�getZ get_option� isinstancer ���r,����extendr0���)�self�termZfirst_split�relpath�paramsZinvalid_paramsZ tmp_charsr%���r%���r&����_parse_parameters,��s4���� &zLookupModule._parse_parametersc�������������� ���K���s���g�}|�j�||d��|D��]b}d�}|��|�\}}|�j�|�} t| dd�} t|d��}d�}d�} d�}�z�t| �\} }t| �}|d�u�s�| td�kr�t|d�||d��}d�}d}nt |�\}}}|d �}|r�|s�d}zt tj|�j �}W�n�ty����t ��}Y�n0�|d �}|�r�|�s�d}ztj|�j}W�n�t�y8���d�}Y�n0�|d �}|�r�|�s�d}zt tj|�j �}W�n�t�y����t ��}Y�n0�|�s�|d �}n,|d ��r�||d �k�r�td||d �f���|�r�|�s�ztj|�j}W�n�t�y����d�}Y�n0�|�r�d}|�r,| td�k�r,t||||d�}t| |��W�| �rNt|��n| �rLt|��0�|�rpt||||d �}|�|��q|�|��q|S�)N)Zvar_optionsZdirectr���r���r���rD���r���r���Tr���r���zEThe ident parameter provided (%s) does not match the stored one (%s).)r���r���)r6���r���)Zset_optionsrl���Z_loaderZ path_dwimr���r1���rS���r'���r���r8���r���r ���Z algorithmsZ salt_size�KeyErrorZimplicit_identr���r9���rC���rU���r���r,���)rh���ZtermsZ variables�kwargs�retri���Zchangedrj���rk���r���r!���r���r���rO���rP���r"���Zplaintext_passwordr6���r���r5���r%���r%���r&����run\��s~���� � zLookupModule.runN)�__name__� __module__�__qualname__rl���rp���r%���r%���r%���r&���rV���*��s���0rV���)NN)+Z __future__r���r���r����typeZ __metaclass__Z DOCUMENTATIONZEXAMPLESZRETURNr���r.���rM���rG���Zansible.errorsr���r���Zansible.module_utils._textr���r���r ���Zansible.module_utils.sixr ���Zansible.parsing.splitterr���Zansible.plugins.lookupr���Zansible.utils.encryptr ���r���r���r���Zansible.utils.pathr���r`���rc���r'���r1���r8���r9���rC���rS���rU���rV���r%���r%���r%���r&����<module>���s0���L" #