Skip to content

Commit 47a900b

Browse files
authored
Merge pull request #76 from lkubb/small-fixes
Fixes #77 Fixes #78
2 parents ea72c7d + 0823dcc commit 47a900b

File tree

27 files changed

+118
-169
lines changed

27 files changed

+118
-169
lines changed

.pylintrc

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -422,41 +422,13 @@ confidence=HIGH,
422422
# no Warning level messages displayed, use "--disable=all --enable=classes
423423
# --disable=W".
424424
disable=R,
425-
locally-disabled,
426-
file-ignored,
427-
unexpected-special-method-signature,
428-
import-error,
429-
no-member,
430-
unsubscriptable-object,
431-
blacklisted-name,
432-
invalid-name,
433-
missing-docstring,
434-
empty-docstring,
435-
unidiomatic-typecheck,
436-
wrong-import-order,
437-
ungrouped-imports,
438-
wrong-import-position,
439-
bad-mcs-method-argument,
440-
bad-mcs-classmethod-argument,
441-
line-too-long,
442-
too-many-lines,
443-
bad-continuation,
444-
exec-used,
445-
attribute-defined-outside-init,
446-
protected-access,
447-
reimported,
448425
fixme,
449-
global-statement,
450-
redefined-outer-name,
451-
redefined-builtin,
452-
undefined-loop-variable,
453-
logging-format-interpolation,
454-
invalid-format-index,
455426
line-too-long,
456-
import-outside-toplevel,
457-
deprecated-method,
458-
keyword-arg-before-vararg,
427+
protected-access,
428+
ungrouped-imports,
429+
missing-docstring,
459430
logging-fstring-interpolation,
431+
wrong-import-position,
460432

461433
# Enable the message, report, category or checker with the given id(s). You can
462434
# either give multiple identifier separated by comma (,) or put this option

changelog/77.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed vault.update_config crash

changelog/78.changed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Required x509_v2 modules to be available for specific parameters to `vault_pki`, dropped direct dependency on cryptography

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ requires-python = ">= 3.8"
3838
dynamic = ["version"]
3939
dependencies = [
4040
"salt>=3006",
41-
"cryptography>=36",
4241
]
4342

4443
[project.readme]
@@ -81,6 +80,7 @@ tests = [
8180
"pytest-salt-factories>=1.0.0; sys_platform == 'win32'",
8281
"pytest-salt-factories[docker]>=1.0.0; sys_platform != 'win32'",
8382
"pytest-timeout",
83+
"cryptography>=36",
8484
]
8585

8686
[project.entry-points."salt.loader"]

src/saltext/vault/beacons/vault_lease.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
import salt.utils.beacons
115115
import salt.utils.dictupdate as dup
116116

117-
import saltext.vault.utils.vault as vault
117+
from saltext.vault.utils import vault
118118
from saltext.vault.utils.vault.helpers import timestring_map
119119

120120
log = logging.getLogger(__name__)

src/saltext/vault/modules/vault.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from salt.exceptions import SaltException
1313
from salt.exceptions import SaltInvocationError
1414

15-
import saltext.vault.utils.vault as vault
15+
from saltext.vault.utils import vault
1616
from saltext.vault.utils.versions import warn_until
1717

1818
log = logging.getLogger(__name__)

src/saltext/vault/modules/vault_db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
from salt.exceptions import CommandExecutionError
1717
from salt.exceptions import SaltInvocationError
1818

19-
import saltext.vault.utils.vault as vault
20-
import saltext.vault.utils.vault.db as vaultdb
19+
from saltext.vault.utils import vault
20+
from saltext.vault.utils.vault import db as vaultdb
2121

2222
log = logging.getLogger(__name__)
2323

src/saltext/vault/modules/vault_pki.py

Lines changed: 13 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,10 @@
1212
import logging
1313
from 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-
2315
from salt.exceptions import CommandExecutionError
2416
from salt.exceptions import SaltInvocationError
2517

26-
import saltext.vault.utils.vault as vault
18+
from saltext.vault.utils import vault
2719
from saltext.vault.utils.vault.pki import dec2hex
2820

2921
log = logging.getLogger(__name__)
@@ -32,8 +24,6 @@
3224

3325

3426
def __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

8056
def list_roles(mount="pki"):
8157
"""
@@ -508,7 +484,7 @@ def set_default_issuer(name, mount="pki"):
508484
def 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-
11491106
def _split_csr_kwargs(kwargs):
11501107
csr_args = {}
11511108
extra_args = {}

src/saltext/vault/pillar/vault.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@
9595
from salt.exceptions import InvalidConfigError
9696
from salt.exceptions import SaltException
9797

98-
import saltext.vault.utils.vault as vault
99-
import saltext.vault.utils.vault.helpers as vhelpers
98+
from saltext.vault.utils import vault
99+
from saltext.vault.utils.vault import helpers
100100
from saltext.vault.utils.versions import warn_until
101101

102102
log = logging.getLogger(__name__)
@@ -202,7 +202,7 @@ def _get_paths(path_pattern, minion_id, pillar):
202202

203203
paths = []
204204
try:
205-
for expanded_pattern in vhelpers.expand_pattern_lists(path_pattern, **mappings):
205+
for expanded_pattern in helpers.expand_pattern_lists(path_pattern, **mappings):
206206
paths.append(expanded_pattern.format(**mappings))
207207
except KeyError:
208208
log.warning("Could not resolve pillar path pattern %s", path_pattern)

src/saltext/vault/runners/vault.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
from salt.exceptions import SaltInvocationError
2424
from salt.exceptions import SaltRunnerError
2525

26-
import saltext.vault.utils.vault as vault
27-
import saltext.vault.utils.vault.cache as vcache
28-
import saltext.vault.utils.vault.factory as vfactory
29-
import saltext.vault.utils.vault.helpers as vhelpers
26+
from saltext.vault.utils import vault
27+
from saltext.vault.utils.vault import cache as vcache
28+
from saltext.vault.utils.vault import factory
29+
from saltext.vault.utils.vault import helpers
3030
from saltext.vault.utils.versions import warn_until
3131

3232
log = logging.getLogger(__name__)
@@ -410,7 +410,7 @@ def _get_role_id(minion_id, issue_params, wrap):
410410
issue_params_parsed = _parse_issue_params(issue_params)
411411

412412
if approle is False or (
413-
vhelpers._get_salt_run_type(__opts__) != vhelpers.SALT_RUNTYPE_MASTER_IMPERSONATING
413+
helpers._get_salt_run_type(__opts__) != helpers.SALT_RUNTYPE_MASTER_IMPERSONATING
414414
and not _approle_params_match(approle, issue_params_parsed)
415415
):
416416
# This means the role has to be created/updated first
@@ -492,9 +492,9 @@ def generate_secret_id(minion_id, signature, impersonated_by_master=False, issue
492492
if approle_meta is False:
493493
raise vault.VaultNotFoundError(f"No AppRole found for minion {minion_id}.")
494494

495-
if vhelpers._get_salt_run_type(
495+
if helpers._get_salt_run_type(
496496
__opts__
497-
) != vhelpers.SALT_RUNTYPE_MASTER_IMPERSONATING and not _approle_params_match(
497+
) != helpers.SALT_RUNTYPE_MASTER_IMPERSONATING and not _approle_params_match(
498498
approle_meta, issue_params
499499
):
500500
_manage_approle(minion_id, issue_params)
@@ -871,7 +871,7 @@ def clear_cache(master=True, minions=True):
871871
Defaults to true. Set this to a list of minion IDs to only clear
872872
cached data pertaining to thse minions.
873873
"""
874-
config, _, _ = vfactory._get_connection_config("vault", __opts__, __context__, force_local=True)
874+
config, _, _ = factory._get_connection_config("vault", __opts__, __context__, force_local=True)
875875
cache = vcache._get_cache_backend(config, __opts__)
876876

877877
if cache is None:
@@ -937,7 +937,7 @@ def _get_policies(minion_id, refresh_pillar=None, **kwargs): # pylint: disable=
937937
policies = []
938938
for pattern in _config("policies:assign"):
939939
try:
940-
for expanded_pattern in vhelpers.expand_pattern_lists(pattern, **mappings):
940+
for expanded_pattern in helpers.expand_pattern_lists(pattern, **mappings):
941941
policies.append(expanded_pattern.format(**mappings).lower()) # Vault requirement
942942
except KeyError:
943943
log.warning("Could not resolve policy pattern %s for minion %s", pattern, minion_id)
@@ -1027,7 +1027,7 @@ def _get_metadata(minion_id, metadata_patterns, refresh_pillar=None):
10271027
for key, pattern in metadata_patterns.items():
10281028
metadata[key] = []
10291029
try:
1030-
for expanded_pattern in vhelpers.expand_pattern_lists(pattern, **mappings):
1030+
for expanded_pattern in helpers.expand_pattern_lists(pattern, **mappings):
10311031
metadata[key].append(expanded_pattern.format(**mappings))
10321032
except KeyError:
10331033
log.warning(
@@ -1197,11 +1197,11 @@ def _manage_entity_alias(minion_id):
11971197

11981198

11991199
def _get_approle_api():
1200-
return vfactory.get_approle_api(__opts__, __context__, force_local=True)
1200+
return vault.get_approle_api(__opts__, __context__, force_local=True)
12011201

12021202

12031203
def _get_identity_api():
1204-
return vfactory.get_identity_api(__opts__, __context__, force_local=True)
1204+
return vault.get_identity_api(__opts__, __context__, force_local=True)
12051205

12061206

12071207
def _get_master_client():

0 commit comments

Comments
 (0)