%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python2.7/site-packages/salt/states/
Upload File :
Create Path :
Current File : //lib/python2.7/site-packages/salt/states/x509.pyo

�
���^c@@s*dZddlmZmZmZddlZddlZddlZddlZddl	Z
ddlmZyddl
mZWnek
r�nXd�Zd�Zd�Zd	deed
�Zd	ddeeed�Zd
�Zdddd�Zdddddded�Zed�ZdS(um
Manage X509 Certificates

.. versionadded:: 2015.8.0

:depends: M2Crypto

This module can enable managing a complete PKI infrastructure including creating private keys, CA's,
certificates and CRLs. It includes the ability to generate a private key on a server, and have the
corresponding public key sent to a remote CA to create a CA signed certificate. This can be done in
a secure manner, where private keys are always generated locally and never moved across the network.

Here is a simple example scenario. In this example ``ca`` is the ca server,
and ``www`` is a web server that needs a certificate signed by ``ca``.

For remote signing, peers must be permitted to remotely call the
:mod:`sign_remote_certificate <salt.modules.x509.sign_remote_certificate>` function.


/etc/salt/master.d/peer.conf

.. code-block:: yaml

    peer:
      .*:
        - x509.sign_remote_certificate


/srv/salt/top.sls

.. code-block:: yaml

    base:
      '*':
        - cert
      'ca':
        - ca
      'www':
        - www


This state creates the CA key, certificate and signing policy. It also publishes the certificate to
the mine where it can be easily retrieved by other minions.

/srv/salt/ca.sls

.. code-block:: yaml

    salt-minion:
      service.running:
        - enable: True
        - listen:
          - file: /etc/salt/minion.d/signing_policies.conf

    /etc/salt/minion.d/signing_policies.conf:
      file.managed:
        - source: salt://signing_policies.conf

    /etc/pki:
      file.directory

    /etc/pki/issued_certs:
      file.directory

    /etc/pki/ca.crt:
      x509.certificate_managed:
        - signing_private_key: /etc/pki/ca.key
        - CN: ca.example.com
        - C: US
        - ST: Utah
        - L: Salt Lake City
        - basicConstraints: "critical CA:true"
        - keyUsage: "critical cRLSign, keyCertSign"
        - subjectKeyIdentifier: hash
        - authorityKeyIdentifier: keyid,issuer:always
        - days_valid: 3650
        - days_remaining: 0
        - backup: True
        - managed_private_key:
            name: /etc/pki/ca.key
            bits: 4096
            backup: True
        - require:
          - file: /etc/pki

    mine.send:
      module.run:
        - func: x509.get_pem_entries
        - kwargs:
            glob_path: /etc/pki/ca.crt
        - onchanges:
          - x509: /etc/pki/ca.crt


The signing policy defines properties that override any property requested or included in a CRL. It also
can define a restricted list of minons which are allowed to remotely invoke this signing policy.

/srv/salt/signing_policies.conf

.. code-block:: yaml

    x509_signing_policies:
      www:
        - minions: 'www'
        - signing_private_key: /etc/pki/ca.key
        - signing_cert: /etc/pki/ca.crt
        - C: US
        - ST: Utah
        - L: Salt Lake City
        - basicConstraints: "critical CA:false"
        - keyUsage: "critical keyEncipherment"
        - subjectKeyIdentifier: hash
        - authorityKeyIdentifier: keyid,issuer:always
        - days_valid: 90
        - copypath: /etc/pki/issued_certs/


This state will instruct all minions to trust certificates signed by our new CA.
Using jinja to strip newlines from the text avoids dealing with newlines in the rendered yaml,
and the  :mod:`sign_remote_certificate <salt.states.x509.sign_remote_certificate>` state will
handle properly formatting the text before writing the output.

/srv/salt/cert.sls

.. code-block:: jinja

    /usr/local/share/ca-certificates:
      file.directory

    /usr/local/share/ca-certificates/intca.crt:
      x509.pem_managed:
        - text: {{ salt['mine.get']('ca', 'x509.get_pem_entries')['ca']['/etc/pki/ca.crt']|replace('\n', '') }}


This state creates a private key then requests a certificate signed by ca according to the www policy.

/srv/salt/www.sls

.. code-block:: yaml

    /etc/pki/www.crt:
      x509.certificate_managed:
        - ca_server: ca
        - signing_policy: www
        - public_key: /etc/pki/www.key
        - CN: www.example.com
        - days_remaining: 30
        - backup: True
        - managed_private_key:
            name: /etc/pki/www.key
            bits: 4096
            backup: True

i(tabsolute_importtunicode_literalstprint_functionN(tsix(tRSAErrorcC@sdtkrdStdfSdS(uO
    only load this module if the corresponding execution module is loaded
    ux509.get_pem_entryux509u/Could not load x509 state: m2crypto unavailableN(t__salt__tFalse(((s4/usr/lib/python2.7/site-packages/salt/states/x509.pyt__virtual__�sc	C@s�g}x�|D]�}x�tj|�D]~\}}i}x\|D]T}xKtj|�D]:\}}t|tj�r�|jd�}n|||<qRWq<W|j|�q#Wq
W|S(ue
    Turn the mess of OrderedDicts and Lists into a list of dicts for
    use in the CRL module.
    u%Y-%m-%d %H:%M:%S(Rt	iteritemst
isinstancetdatetimetstrftimetappend(	trevstlist_trevtrev_nametpropstdict_tproptpropnametval((s4/usr/lib/python2.7/site-packages/salt/states/x509.pyt_revoked_to_list�s
	
c	K@s�ddddddddd	g	}i}i}x=|j�D]/\}}||kr_|||<q:|||<q:W||d
<||fS(Nuuserugroupumodeumakedirsudir_modeubackupucreateufollow_symlinksu	check_cmduname(titems(tnametkwargstvalid_file_argst	file_argst
extra_argstktv((s4/usr/lib/python2.7/site-packages/salt/states/x509.pyt_get_file_args�s"	

icC@s�d}tjj|�r}ytdd|d|�}Wq}tjjk
rNq}tk
ry|sztjjd��qzq}Xn||ko�|S(Niux509.get_private_key_sizetprivate_keyt
passphraseu7The provided passphrase cannot decrypt the private key.(	tostpathtisfileRtsaltt
exceptionstSaltInvocationErrorRtCommandExecutionError(RtbitsR!tnewt	overwritetcurrent_bits((s4/usr/lib/python2.7/site-packages/salt/states/x509.pyt_check_private_key�s

	uaes_128_cbccK@s�t||�\}}t}	t|d|d|d|d|�r\td|dd�|d<n5t}	td	d
td|d|d|d|�|d<tjjj|d�|d<t	d
|�}
|
dr�|	r�idd6|
d<n|
S(uG
    Manage a private key's existence.

    name:
        Path to the private key

    bits:
        Key length in bits. Default 2048.

    passphrase:
        Passphrase for encrypting the private key.

    cipher:
        Cipher for encrypting the private key.

    new:
        Always create a new key. Defaults to False.
        Combining new with :mod:`prereq <salt.states.requsities.preqreq>`, or when used as part of a `managed_private_key` can allow key rotation whenever a new certificiate is generated.

    overwrite:
        Overwrite an existing private key if the provided passphrase cannot decrypt it.

    verbose:
        Provide visual feedback on stdout, dots while key is generated.
        Default is True.

        .. versionadded:: 2016.11.0

    kwargs:
        Any kwargs supported by file.managed are supported.

    Example:

    The jinja templating in this example ensures a private key is generated if the file doesn't exist
    and that a new private key is generated whenever the certificate that uses it is to be renewed.

    .. code-block:: jinja

        /etc/pki/www.key:
          x509.private_key_managed:
            - bits: 4096
            - new: True
            {% if salt['file.file_exists']('/etc/pki/www.key') -%}
            - prereq:
              - x509: /etc/pki/www.crt
            {%- endif %}
    R)R!R*R+ux509.get_pem_entrytpem_typeuRSA PRIVATE KEYucontentsux509.create_private_keyttexttciphertverboseufile.manageduchangesuNew private key generatedunew(
RRR-RtTrueR%tutilststringutilstto_strt
__states__(RR)R!R0R*R+R1RRtnew_keytret((s4/usr/lib/python2.7/site-packages/salt/states/x509.pytprivate_key_managed�s7!
%cK@s�ytd|�}Wn&tjjk
r<dj|�}nXt||�\}}tddt|�|d<td|�}|dr�td|d�}||kr�i|d6|d	6|d<q�n|S(
u�
    Manage a Certificate Signing Request

    name:
        Path to the CSR

    properties:
        The properties to be added to the certificate request, including items like subject, extensions
        and public key. See above for valid properties.

    kwargs:
        Any arguments supported by :py:func:`file.managed <salt.states.file.managed>` are supported.

    Example:

    .. code-block:: yaml

        /etc/pki/mycert.csr:
          x509.csr_managed:
             - private_key: /etc/pki/mycert.key
             - CN: www.example.com
             - C: US
             - ST: Utah
             - L: Salt Lake City
             - keyUsage: 'critical dataEncipherment'
    u
x509.read_csru{0} is not a valid csr.ux509.create_csrR/ucontentsufile.manageduchangesuOlduNew(RR%R&R'tformatRR2R6(RRtoldRR8R*((s4/usr/lib/python2.7/site-packages/salt/states/x509.pytcsr_managedAs
iZcK@s>d|kr|jd�}nt||�\}}t}t}|rWi|d6td6td6dd6d>d6dd	6td
6}|j|�|d|d<|dr�t}t|d<nt|dd|dd
|dd|dd|d�r	td|ddd�}	nAt}tddtd|dd
|dd|d	d|d
�}	|	|d<nd}
i}tj	j
|�r�ytdd|�}tj|�}d|kr|jd�d|kry)t
jdd|d d!�|d d!<Wq�tk
r�q�Xqn|jd"�|jd#�|jd$�|jd%�|jd&�}
tjj|
d'�tjj�j}
|dkr�|
d(}nWq�tjjk
r�d)j|�}q�Xnd*j|�}d+|kr�d,|kr�tjjd-��ntd.d/t|�}t|t�r�tj|�}|jd0�d|kr�|jd�d|kr�y)t
jdd|d d!�|d d!<Wq�tk
r�q�Xq�n|jd"�|jd&�|jd#�|jd$�|jd%�|jd0�}n|}t}||kr8|
|kr8td1||�r8td|dd2�}nc|r|rt}tddtd|dd|d
�}	|	|d<nt}td.dt|�}d3|d4<i}|r'|d|kr�|	|d4<q'tj|�}t|�\}}|j|�|	|d4<td5|�}|d6s'|Sn|d4ctjjj|�7<|sUg}nx.|D]&}|d4ctd|dd2�7<q\Wt|d7<td5|�}|d8r�i|d8d96|d8<n
i|d8<|r�|d8r�|d8|d8d:<n|rd;|d8d:<n|r:i|d<6tdd|�d=6|d8d9<n|S(?u	
    Manage a Certificate

    name
        Path to the certificate

    days_remaining : 90
        The minimum number of days remaining when the certificate should be
        recreated. A value of 0 disables automatic renewal.

    managed_private_key
        Manages the private key corresponding to the certificate. All of the
        arguments supported by :py:func:`x509.private_key_managed
        <salt.states.x509.private_key_managed>` are supported. If `name` is not
        speicified or is the same as the name of the certificate, the private
        key and certificate will be written together in the same file.

    append_certs:
        A list of certificates to be appended to the managed file.

    kwargs:
        Any arguments supported by :py:func:`x509.create_certificate
        <salt.modules.x509.create_certificate>` or :py:func:`file.managed
        <salt.states.file.managed>` are supported.

    Examples:

    .. code-block:: yaml

        /etc/pki/ca.crt:
          x509.certificate_managed:
            - signing_private_key: /etc/pki/ca.key
            - CN: ca.example.com
            - C: US
            - ST: Utah
            - L: Salt Lake City
            - basicConstraints: "critical CA:true"
            - keyUsage: "critical cRLSign, keyCertSign"
            - subjectKeyIdentifier: hash
            - authorityKeyIdentifier: keyid,issuer:always
            - days_valid: 3650
            - days_remaining: 0
            - backup: True


    .. code-block:: yaml

        /etc/ssl/www.crt:
          x509.certificate_managed:
            - ca_server: pki
            - signing_policy: www
            - public_key: /etc/ssl/www.key
            - CN: www.example.com
            - days_valid: 90
            - days_remaining: 30
            - backup: True

    upathunameunewu	overwriteiubitsu
passphraseuaes_128_cbcucipheruverboseupublic_key_passphraseR)R!R*R+ux509.get_pem_entryR.uRSA PRIVATE KEYux509.create_private_keyR/R0R1u
public_keyiux509.read_certificatetcertificateu
serial_numberu
Serial Numberusigning_certu!serial:([0-9A-F]{2}:)*[0-9A-F]{2}u	serial:--uX509v3 ExtensionsuauthorityKeyIdentifieru
Not BeforeuMD5 Finger PrintuSHA1 Finger PrintuSHA-256 Finger Printu	Not Afteru%Y-%m-%d %H:%M:%Siu{0} is not a valid Certificate.u{0} does not exist.u	ca_serverusigning_policyu1signing_policy must be specified if ca_server is.ux509.create_certificatettestrunuIssuer Public Keyux509.verify_signatureuCERTIFICATEuucontentsufile.manageduresultushow_changesuchangesuCertificateuPrivate KeyuNew private key generateduOlduNewN(tpopRRtNoneR2tupdateR-RR"R#R$tcopytdeepcopytretsubtKeyErrorR
tstrptimetnowtdaysR%R&R'R:R	tdictR6R3R4R5(Rtdays_remainingtmanaged_private_keytappend_certsRRtrotate_private_keytnew_private_keytprivate_key_argsR tcurrent_days_remainingtcurrent_comptcurrenttcurrent_notafterR*tnew_comptnew_issuer_public_keytnew_certificateR=tprivate_rettprivate_file_argstunique_private_file_argst_tappend_certR8((s4/usr/lib/python2.7/site-packages/salt/states/x509.pytcertificate_managedns�?
















	
















	
!


"iduic	K@s"|dkrg}nt|�}d}
i}tjj|�r�y}tdd|�}|j�}|jd�|jd�}
tjj	|
d�tjj
�j}
|dkr�|
d}nWq�tj
jk
r�dj|�}q�Xnd	j|�}td
dtd|d
|d|d|d|d|d|�}tdd|�}|j�}|jd�|jd�t||	�\}}	t}||kr�|
|kr�td||�r�td|dd�|d<nt}||d<td|�}|ri|d6tdd|�d6|d<n|S(u�
    Manage a Certificate Revocation List

    name
        Path to the certificate

    signing_private_key
        The private key that will be used to sign this crl. This is
        usually your CA's private key.

    signing_private_key_passphrase
        Passphrase to decrypt the private key.

    signing_cert
        The certificate of the authority that will be used to sign this crl.
        This is usually your CA's certificate.

    revoked
        A list of certificates to revoke. Must include either a serial number or a
        the certificate itself. Can optionally include the revocation date and
        notAfter date from the certificate. See example below for details.

    days_valid : 100
        The number of days the certificate should be valid for.

    digest
        The digest to use for signing the CRL. This has no effect on versions
        of pyOpenSSL less than 0.14.

    days_remaining : 30
        The crl should be automatically recreated if there are less than
        ``days_remaining`` days until the crl expires. Set to 0 to disable
        automatic renewal.

    include_expired : False
        If ``True``, include expired certificates in the CRL.

    kwargs
        Any arguments supported by :py:func:`file.managed <salt.states.file.managed>` are supported.

    Example:

    .. code-block:: yaml

        /etc/pki/ca.crl:
          x509.crl_managed:
            - signing_private_key: /etc/pki/myca.key
            - signing_cert: /etc/pki/myca.crt
            - revoked:
              - compromized_Web_key:
                - certificate: /etc/pki/certs/badweb.crt
                - revocation_date: 2015-03-01 00:00:00
                - reason: keyCompromise
              - terminated_vpn_user:
                - serial_number: D6:D2:DC:D8:4D:5C:C0:F4
                - not_after: 2016-01-01 00:00:00
                - revocation_date: 2015-02-25 00:00:00
                - reason: cessationOfOperation
    iu
x509.read_crltcrluLast UpdateuNext Updateu%Y-%m-%d %H:%M:%Siu{0} is not a valid CRL.u{0} does not exist.ux509.create_crlR/tsigning_private_keytsigning_private_key_passphrasetsigning_certtrevokedt
days_validtdigesttinclude_expiredux509.verify_crlux509.get_pem_entryR.uX509 CRLucontentsufile.manageduOlduNewuchangesN(R@RR"R#R$RRBR?R
RGRHRIR%R&R'R:R2RRR6(RR_R`RaRbRcRdRKReRRQRRRSRTtnew_crlR*RURtnew_crl_createdR8((s4/usr/lib/python2.7/site-packages/salt/states/x509.pytcrl_managedEsLE	
!



cK@s:t||�\}}tdd|�|d<td|�S(uC
    Manage the contents of a PEM file directly with the content in text, ensuring correct formatting.

    name:
        The path to the file to manage

    text:
        The PEM formatted text to write.

    kwargs:
        Any arguments supported by :py:func:`file.managed <salt.states.file.managed>` are supported.
    ux509.get_pem_entryR/ucontentsufile.managed(RRR6(RR/tbackupRR((s4/usr/lib/python2.7/site-packages/salt/states/x509.pytpem_managed�s(t__doc__t
__future__RRRR
R"RDRBtsalt.exceptionsR%tsalt.extRtM2Crypto.RSARtImportErrorRRRR@RR-R2R9R<R]RhRj(((s4/usr/lib/python2.7/site-packages/salt/states/x509.pyt<module>�sD
	
		F	.�q

Zerion Mini Shell 1.0