diff --git a/changelog/66818.fixed.md b/changelog/66818.fixed.md new file mode 100644 index 000000000000..240b14b77464 --- /dev/null +++ b/changelog/66818.fixed.md @@ -0,0 +1 @@ +Make x509 module compatible with `cryptography` module newer than `43.0.0` diff --git a/salt/utils/x509.py b/salt/utils/x509.py index 893756ed6c98..edd9fb42d50f 100644 --- a/salt/utils/x509.py +++ b/salt/utils/x509.py @@ -701,7 +701,8 @@ def load_privkey(pk, passphrase=None, get_encoding=False): return pk, "pem", None return pk except ValueError as err: - if "Bad decrypt" in str(err): + str_err = str(err) + if "Bad decrypt" in str_err or "Could not deserialize key data" in str_err: raise SaltInvocationError( "Bad decrypt - is the password correct?" ) from err diff --git a/tests/pytests/functional/states/test_x509_v2.py b/tests/pytests/functional/states/test_x509_v2.py index 3abda4f5fd94..b9cb6f900f71 100644 --- a/tests/pytests/functional/states/test_x509_v2.py +++ b/tests/pytests/functional/states/test_x509_v2.py @@ -4,6 +4,8 @@ import pytest +from tests.support.mock import patch + try: import cryptography import cryptography.x509 as cx509 @@ -2890,3 +2892,30 @@ def _get_privkey(pk, encoding="pem", passphrase=None): pk = base64.b64decode(pk) return pkcs12.load_pkcs12(pk, passphrase).key raise ValueError("Need correct encoding") + + +@pytest.mark.usefixtures("existing_pk") +@pytest.mark.parametrize("existing_pk", [{"passphrase": "password"}], indirect=True) +def test_exceptions_on_calling_load_pem_private_key(x509, pk_args): + pk_args["passphrase"] = "hunter1" + pk_args["overwrite"] = True + + with patch( + "cryptography.hazmat.primitives.serialization.load_pem_private_key", + side_effect=ValueError("Bad decrypt. Incorrect password?"), + ): + ret = x509.private_key_managed(**pk_args) + _assert_pk_basic(ret, "rsa", passphrase="hunter1") + + with patch( + "cryptography.hazmat.primitives.serialization.load_pem_private_key", + side_effect=ValueError( + "Could not deserialize key data. The data may be in an incorrect format, " + "the provided password may be incorrect, " + "it may be encrypted with an unsupported algorithm, " + "or it may be an unsupported key type " + "(e.g. EC curves with explicit parameters)." + ), + ): + ret = x509.private_key_managed(**pk_args) + _assert_pk_basic(ret, "rsa", passphrase="hunter1")