1212import logging
1313from typing import Tuple
1414
15- try :
16- import salt .utils .x509 as x509util
17- from cryptography .hazmat .primitives import serialization
18-
19- HAS_CRYPTOGRAPHY = True
20- except ImportError :
21- HAS_CRYPTOGRAPHY = False
22-
2315from salt .exceptions import CommandExecutionError
2416from salt .exceptions import SaltInvocationError
2517
26- import saltext .vault .utils . vault as vault
18+ from saltext .vault .utils import vault
2719from saltext .vault .utils .vault .pki import dec2hex
2820
2921log = logging .getLogger (__name__ )
3224
3325
3426def __virtual__ ():
35- if not HAS_CRYPTOGRAPHY :
36- return (False , "Could not load cryptography" )
3727 return __virtualname__
3828
3929
@@ -62,20 +52,6 @@ def __virtual__():
6252 "tlsfeature" ,
6353)
6454
65- DIGEST_HASHES = (
66- "SHA1" ,
67- "SHA224" ,
68- "SHA256" ,
69- "SHA384" ,
70- "SHA512" ,
71- "SHA512_224" ,
72- "SHA512_256" ,
73- "SHA3_224" ,
74- "SHA3_256" ,
75- "SHA3_384" ,
76- "SHA3_512" ,
77- )
78-
7955
8056def list_roles (mount = "pki" ):
8157 """
@@ -508,7 +484,7 @@ def set_default_issuer(name, mount="pki"):
508484def generate_root (
509485 common_name ,
510486 mount = "pki" ,
511- type = "internal" ,
487+ type = "internal" , # pylint: disable=redefined-builtin
512488 issuer_name = None ,
513489 key_name = None ,
514490 ttl = None ,
@@ -814,7 +790,7 @@ def issue_certificate(
814790 issuer_ref = None ,
815791 alt_names = None ,
816792 ttl = None ,
817- format = "pem" ,
793+ format = "pem" , # pylint: disable=redefined-builtin
818794 exclude_cn_from_sans = False ,
819795 ** kwargs ,
820796):
@@ -929,6 +905,9 @@ def sign_certificate(
929905 The private key for which certificate should be issued. Can be text or path.
930906 Either ``csr`` or ``private_key`` parameter can be set, not both.
931907
908+ .. note::
909+ This parameter requires the :py:mod:`x509_v2 execution module <salt.modules.x509_v2>` to be available.
910+
932911 private_key_passphrase
933912 The passphrase for the ``private_key`` if encrypted. Not used in case of ``csr``.
934913
@@ -1007,7 +986,7 @@ def sign_certificate(
1007986
1008987 csr_args ["CN" ] = common_name
1009988
1010- csr = _build_csr (
989+ csr = __salt__ [ "x509.create_csr" ] (
1011990 private_key = private_key ,
1012991 private_key_passphrase = private_key_passphrase ,
1013992 digest = digest ,
@@ -1040,6 +1019,9 @@ def revoke_certificate(serial=None, certificate=None, mount="pki"):
10401019 certificate
10411020 Specifies the certificate (PEM or path) to revoke. Either ``serial`` or ``certificate`` must be specified.
10421021
1022+ .. note::
1023+ This parameter requires the :py:mod:`x509_v2 execution module <salt.modules.x509_v2>` to be available.
1024+
10431025 mount
10441026 The mount path the PKI backend is mounted to. Defaults to ``pki``.
10451027 """
@@ -1054,10 +1036,9 @@ def revoke_certificate(serial=None, certificate=None, mount="pki"):
10541036
10551037 try :
10561038 if certificate is not None :
1057- certificate = x509util .load_cert (certificate )
1058- cert_encoding = getattr (serialization .Encoding , "PEM" )
1059- cert_bytes = certificate .public_bytes (cert_encoding )
1060- payload ["certificate" ] = cert_bytes .decode ()
1039+ payload ["certificate" ] = __salt__ ["x509.encode_certificate" ](
1040+ certificate , encoding = "pem"
1041+ )
10611042 elif serial is not None :
10621043 if isinstance (serial , int ):
10631044 serial = dec2hex (serial )
@@ -1122,30 +1103,6 @@ def _split_sans(sans) -> Tuple[list, list, list, list]:
11221103 return dns_sans , ip_sans , uri_sans , other_sans
11231104
11241105
1125- def _build_csr (private_key , private_key_passphrase = None , digest = "sha256" , ** kwargs ):
1126- if digest .upper () not in DIGEST_HASHES :
1127- raise CommandExecutionError (
1128- f"Invalid value '{ digest } ' for digest. Valid: { ',' .join (DIGEST_HASHES )} "
1129- )
1130-
1131- builder , key = x509util .build_csr (
1132- private_key = private_key , private_key_passphrase = private_key_passphrase , ** kwargs
1133- )
1134- algorithm = None
1135- if x509util .get_key_type (key ) not in [
1136- x509util .KEY_TYPE .ED25519 ,
1137- x509util .KEY_TYPE .ED448 ,
1138- ]:
1139- algorithm = x509util .get_hashing_algorithm (digest )
1140-
1141- csr = builder .sign (key , algorithm = algorithm )
1142- csr = x509util .load_csr (csr )
1143- csr_bytes = csr .public_bytes (serialization .Encoding .PEM )
1144- csr = csr_bytes .decode ()
1145-
1146- return csr
1147-
1148-
11491106def _split_csr_kwargs (kwargs ):
11501107 csr_args = {}
11511108 extra_args = {}
0 commit comments