File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/states/__pycache__/x509_v2.cpython-310.pyc
o
�N�g� � @ s� d Z ddlZddlZddlZddlZddlmZmZmZ ddl Z
ddlmZm
Z
ddlmZ zddlmZ ddlmZ ddlmZ ddlm mZ dZW n eyX d ZY nw e�e�Zd
Z dd� Z!
d6dd�Z"
d7dd�Z#
d8dd�Z$dd� Z%
d9dd�Z&dd� Z'dd� Z(dd� Z)d:d d!�Z*d"d#� Z+ d;d$d%�Z,d&d'� Z-d(d)� Z.d*d+� Z/d,d-� Z0d.d/� Z1d0d1� Z2d2d3� Z3d4d5� Z4dS )<a/
Manage X.509 certificates
=========================
.. versionadded:: 3006.0
This module represents a complete rewrite of the original ``x509`` modules
and is named ``x509_v2`` since it introduces breaking changes.
:depends: cryptography
.. note::
All parameters that take a public key, private key, certificate,
CSR or CRL can be specified either as a PEM/hex/base64 string or
a path to a local file encoded in all supported formats for the type.
Configuration instructions and general remarks are documented
in the :ref:`execution module docs <x509-setup>`.
For the list of breaking changes versus the previous ``x509`` modules,
please also refer to the :ref:`execution module docs <x509-setup>`.
About
-----
This module can enable managing a complete PKI infrastructure, including creating
private keys, CAs, 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.
Example
-------
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``.
.. note::
Remote signing requires the setup of :term:`Peer Communication` and signing
policies. Please see the :ref:`execution module docs <x509-setup>`.
/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.
.. code-block:: yaml
# /srv/salt/ca.sls
Configure the x509 module:
file.managed:
- name: /etc/salt/minion.d/x509.conf
- source: salt://x509.conf
Restart Salt minion:
cmd.run:
- name: 'salt-call service.restart salt-minion'
- bg: true
- onchanges:
- file: /etc/salt/minion.d/x509.conf
Ensure PKI directories exist:
file.directory:
- name: /etc/pki/issued_certs
- makedirs: true
Create CA private key:
x509.private_key_managed:
- name: /etc/pki/ca.key
- keysize: 4096
- backup: true
- require:
- file: /etc/pki/issued_certs
Create self-signed CA certificate:
x509.certificate_managed:
- name: /etc/pki/ca.crt
- 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:always,issuer
- days_valid: 3650
- days_remaining: 0
- backup: true
- require:
- x509: /etc/pki/ca.key
.. code-block:: yaml
# /srv/salt/x509.conf
# enable x509_v2
features:
x509_v2: true
# publish the CA certificate to the mine
mine_functions:
x509.get_pem_entries: [/etc/pki/ca.crt]
# define at least one signing policy for remote signing
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:always,issuer
- days_valid: 30
- copypath: /etc/pki/issued_certs/
This example state will instruct all minions to trust certificates signed by
our new CA. Mind that this example works for Debian-based OS only.
Also note the Jinja call to encode the string to JSON, which will avoid
YAML issues with newline characters.
.. code-block:: jinja
# /srv/salt/cert.sls
Ensure the CA trust bundle exists:
file.directory:
- name: /usr/local/share/ca-certificates
Ensure our self-signed CA certificate is included:
x509.pem_managed:
- name: /usr/local/share/ca-certificates/myca.crt
- text: {{ salt["mine.get"]("ca", "x509.get_pem_entries")["ca"]["/etc/pki/ca.crt"] | json }}
This state creates a private key, then requests a certificate signed by our CA
according to the www policy.
.. code-block:: yaml
# /srv/salt/www.sls
Ensure PKI directory exists:
file.directory:
- name: /etc/pki
Create private key for the certificate:
x509.private_key_managed:
- name: /etc/pki/www.key
- keysize: 4096
- backup: true
- require:
- file: /etc/pki
Request certificate:
x509.certificate_managed:
- name: /etc/pki/www.crt
- ca_server: ca
- signing_policy: www
- private_key: /etc/pki/www.key
- CN: www.example.com
- days_remaining: 7
- backup: true
- require:
- x509: /etc/pki/www.key
� N)�datetime� timedelta�timezone)�CommandExecutionError�SaltInvocationError)�STATE_INTERNAL_KEYWORDS)�UnsupportedAlgorithm)�hashesTF�x509c C s t sdS td �d�s
dS tS )N)FzCould not load cryptographyZfeaturesZx509_v2)Fz�x509_v2 needs to be explicitly enabled by setting `x509_v2: true` in the minion configuration value `features` until Salt 3008 (Argon).)�HAS_CRYPTOGRAPHY�__opts__�get�__virtualname__� r r �G/opt/saltstack/salt/lib/python3.10/site-packages/salt/states/x509_v2.py�__virtual__� s
r �pem�sha256c / K s$ |du r |du r zt jj�dd� d}W n ty d}Y nw |du r<zt jj�dd� d}W n ty; d}Y nw d |v rMt jj�dd
� |�d �}t�|�}| i ddd
�}d }}i }d}tt |��\}}|plg }t
|t�su|g}�zwt| fddd�|��} | d du r�d|d<