
    Vh/                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ d dlmZmZ d d	lmZ d d
lmZmZ  G d de      Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionaW  
module: openssl_csr
short_description: Generate OpenSSL Certificate Signing Request (CSR)
description:
  - Please note that the module regenerates an existing CSR if it does not match the module's options, or if it seems to be
    corrupt. If you are concerned that this could overwrite your existing CSR, consider using the O(backup) option.
author:
  - Yanis Guenane (@Spredzy)
  - Felix Fontein (@felixfontein)
extends_documentation_fragment:
  - ansible.builtin.files
  - community.crypto.attributes
  - community.crypto.attributes.files
  - community.crypto.module_csr
attributes:
  check_mode:
    support: full
  safe_file_operations:
    support: full
options:
  state:
    description:
      - Whether the certificate signing request should exist or not, taking action if the state is different from what is
        stated.
    type: str
    default: present
    choices: [absent, present]
  force:
    description:
      - Should the certificate signing request be forced regenerated by this ansible module.
    type: bool
    default: false
  path:
    description:
      - The name of the file into which the generated OpenSSL certificate signing request will be written.
    type: path
    required: true
  backup:
    description:
      - Create a backup file including a timestamp so you can get the original CSR back if you overwrote it with a new one
        by accident.
    type: bool
    default: false
  return_content:
    description:
      - If set to V(true), will return the (current or generated) CSR's content as RV(csr).
    type: bool
    default: false
    version_added: "1.0.0"
  privatekey_content:
    version_added: "1.0.0"
  name_constraints_permitted:
    version_added: 1.1.0
  name_constraints_excluded:
    version_added: 1.1.0
  name_constraints_critical:
    version_added: 1.1.0
seealso:
  - module: community.crypto.openssl_csr_pipe
a  
---
- name: Generate an OpenSSL Certificate Signing Request
  community.crypto.openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    common_name: www.ansible.com

- name: Generate an OpenSSL Certificate Signing Request with an inline key
  community.crypto.openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_content: "{{ private_key_content }}"
    common_name: www.ansible.com

- name: Generate an OpenSSL Certificate Signing Request with a passphrase protected private key
  community.crypto.openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    privatekey_passphrase: ansible
    common_name: www.ansible.com

- name: Generate an OpenSSL Certificate Signing Request with Subject information
  community.crypto.openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    country_name: FR
    organization_name: Ansible
    email_address: jdoe@ansible.com
    common_name: www.ansible.com

- name: Generate an OpenSSL Certificate Signing Request with subjectAltName extension
  community.crypto.openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    subject_alt_name: 'DNS:www.ansible.com,DNS:m.ansible.com'

- name: Generate an OpenSSL CSR with subjectAltName extension with dynamic list
  community.crypto.openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    subject_alt_name: "{{ item.value | map('regex_replace', '^', 'DNS:') | list }}"
  with_dict:
    dns_server:
      - www.ansible.com
      - m.ansible.com

- name: Force regenerate an OpenSSL Certificate Signing Request
  community.crypto.openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    force: true
    common_name: www.ansible.com

- name: Generate an OpenSSL Certificate Signing Request with special key usages
  community.crypto.openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    common_name: www.ansible.com
    key_usage:
      - digitalSignature
      - keyAgreement
    extended_key_usage:
      - clientAuth

- name: Generate an OpenSSL Certificate Signing Request with OCSP Must Staple
  community.crypto.openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    common_name: www.ansible.com
    ocsp_must_staple: true

- name: Generate an OpenSSL Certificate Signing Request for WinRM Certificate authentication
  community.crypto.openssl_csr:
    path: /etc/ssl/csr/winrm.auth.csr
    privatekey_path: /etc/ssl/private/winrm.auth.pem
    common_name: username
    extended_key_usage:
      - clientAuth
    subject_alt_name: otherName:1.3.6.1.4.1.311.20.2.3;UTF8:username@localhost

- name: Generate an OpenSSL Certificate Signing Request with a CRL distribution point
  community.crypto.openssl_csr:
    path: /etc/ssl/csr/www.ansible.com.csr
    privatekey_path: /etc/ssl/private/ansible.com.pem
    common_name: www.ansible.com
    crl_distribution_points:
      - full_name:
          - "URI:https://ca.example.com/revocations.crl"
        crl_issuer:
          - "URI:https://ca.example.com/"
        reasons:
          - key_compromise
          - ca_compromise
          - cessation_of_operation
a  
privatekey:
  description:
    - Path to the TLS/SSL private key the CSR was generated for.
    - Will be V(none) if the private key has been provided in O(privatekey_content).
  returned: changed or success
  type: str
  sample: /etc/ssl/private/ansible.com.pem
filename:
  description: Path to the generated Certificate Signing Request.
  returned: changed or success
  type: str
  sample: /etc/ssl/csr/www.ansible.com.csr
subject:
  description: A list of the subject tuples attached to the CSR.
  returned: changed or success
  type: list
  elements: list
  sample: [['CN', 'www.ansible.com'], ['O', 'Ansible']]
subjectAltName:
  description: The alternative names this CSR is valid for.
  returned: changed or success
  type: list
  elements: str
  sample: ['DNS:www.ansible.com', 'DNS:m.ansible.com']
keyUsage:
  description: Purpose for which the public key may be used.
  returned: changed or success
  type: list
  elements: str
  sample: ['digitalSignature', 'keyAgreement']
extendedKeyUsage:
  description: Additional restriction on the public key purposes.
  returned: changed or success
  type: list
  elements: str
  sample: ['clientAuth']
basicConstraints:
  description: Indicates if the certificate belongs to a CA.
  returned: changed or success
  type: list
  elements: str
  sample: ['CA:TRUE', 'pathLenConstraint:0']
ocsp_must_staple:
  description: Indicates whether the certificate has the OCSP Must Staple feature enabled.
  returned: changed or success
  type: bool
  sample: false
name_constraints_permitted:
  description: List of permitted subtrees to sign certificates for.
  returned: changed or success
  type: list
  elements: str
  sample: ['email:.somedomain.com']
  version_added: 1.1.0
name_constraints_excluded:
  description: List of excluded subtrees the CA cannot sign certificates for.
  returned: changed or success
  type: list
  elements: str
  sample: ['email:.com']
  version_added: 1.1.0
backup_file:
  description: Name of backup file created.
  returned: changed and if O(backup) is V(true)
  type: str
  sample: /path/to/www.ansible.com.csr.2019-03-09@11:22~
csr:
  description: The (current or generated) CSR's content.
  returned: if O(state) is V(present) and O(return_content) is V(true)
  type: str
  version_added: "1.0.0"
N)	to_native)OpenSSLObjectError)get_csr_argument_specselect_backend)OpenSSLObject)load_file_if_exists
write_filec                   4     e Zd Z fdZd Z fdZd Z xZS )CertificateSigningRequestModulec                 ^   t         t        |   |j                  d   |j                  d   |j                  d   |j                         || _        |j                  d   | _        |j                  d   | _        d | _        | j
                  j                  t        | j                  |             y )Npathstateforcereturn_contentbackup)superr   __init__params
check_modemodule_backendr   r   backup_fileset_existingr   r   )selfmoduler   	__class__s      p/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/crypto/plugins/modules/openssl_csr.pyr   z(CertificateSigningRequestModule.__init__  s    -t=MM&!MM'"MM'"		
 -$mm,<=mmH-(()<TYY)OP    c                    | j                   s| j                  j                         r| j                  sl| j                  j	                          | j                  j                         }| j                  r |j                  | j                        | _	        t        ||       d| _        |j                  |j                        }|j                  |d         rd| _        y|j                  || j                        | _        y)z)Generate the certificate signing request.Tr   N)r   r   needs_regenerationr   generate_csrget_csr_datar   backup_localr   r   r   changedload_file_common_argumentsr   check_file_absent_if_check_modeset_fs_attributes_if_different)r   r   result	file_argss       r   generatez(CertificateSigningRequestModule.generate  s    ::,,??A??##002,,99;;;'-':':499'ED$66*DL55fmmD	11)F2CDDL!@@4<<DLr    c                     | j                   j                  d        | j                  r,| j                  s |j	                  | j
                        | _        t        t        | '  |       y )N)
r   r   r   r   r%   r   r   r   r   remove)r   r   r   s     r   r.   z&CertificateSigningRequestModule.remove-  sK    ((.;;t%22499=D-t;FCr    c                     | j                   j                  | j                        }|j                  | j                  | j
                  d       | j                  r| j                  |d<   |S )z'Serialize the object into a dictionary.)include_csr)filenamer&   r   )r   dumpr   updater   r&   r   )r   r*   s     r   r2   z$CertificateSigningRequestModule.dump3  s`    $$))d6I6I)J II<<	
 $($4$4F=!r    )__name__
__module____qualname__r   r,   r.   r2   __classcell__)r   s   @r   r   r   	  s    Q&Dr    r   c                     t               } | j                  j                  t        t        ddddg      t        dd      t        dd	
      t        dd      t        dd                   | j                  j                  | j                  D cg c]  }dd|d	f
 c}       g | _        | j                  d	d	      }t        j                  j                  |j                  d         xs d}t        j                  j                  |      s|j                  |d|z         	 |j                  d   }t        ||      \  }}t        ||      }|j                  d   dk(  r|j!                  |       n|j#                  |       |j%                         } |j&                  di | y c c}w # t(        $ r%}|j                  t+        |             Y d }~y d }~ww xY w)Nstrpresentabsent)typedefaultchoicesboolF)r<   r=   r   T)r<   required)r   r   r   r   r   r   )add_file_common_argssupports_check_mode.z>The directory %s does not exist or the file is not a directory)namemsgselect_crypto_backend)rE    )r   argument_specr3   dictrequired_ifextendrequired_one_ofcreate_ansible_moduleosr   dirnamer   isdir	fail_jsonr	   r   r,   r.   r2   	exit_jsonr   r   )	rH   rofr   base_dirbackendr   csrr*   excs	            r   mainrX   A  s   )+M&&E9x>STFE26D1VU3VU;	
 $$4A4Q4QRS'9c4	(R %'M!00!  1 F
 wwv}}V45<H77=="P 	 	
--- 78"0"A-fnE==!Y.LL JJv"6"7 	S8  -Ys^,,-s   F'%BF, ,	G5GG__main__)
__future__r   r   r   r<   __metaclass__DOCUMENTATIONEXAMPLESRETURNrN   +ansible.module_utils.common.text.convertersr   Fansible_collections.community.crypto.plugins.module_utils.crypto.basicr   Tansible_collections.community.crypto.plugins.module_utils.crypto.module_backends.csrr   r	   Hansible_collections.community.crypto.plugins.module_utils.crypto.supportr
   <ansible_collections.community.crypto.plugins.module_utils.ior   r   r   rX   r4   rG   r    r   <module>rd      sr    A @ ;z^@H
T 
 A5m 5p)-X zF r    