Skip to content

Commit 7492457

Browse files
vzhestkovagraul
authored andcommitted
Improve error handling with different OpenSSL versions
* Make error checking of x509 more flexible for most recent cryptography and openSSL versions * Add test for different exception value on loading private key * Add fix for test_privkey_new_with_prereq on old OpenSSL BACKPORT-UPSTREAM=saltstack#66818
1 parent 54af26b commit 7492457

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

salt/utils/x509.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,8 @@ def load_privkey(pk, passphrase=None, get_encoding=False):
695695
return pk, "pem", None
696696
return pk
697697
except ValueError as err:
698-
if "Bad decrypt" in str(err):
698+
str_err = str(err)
699+
if "Bad decrypt" in str_err or "Could not deserialize key data" in str_err:
699700
raise SaltInvocationError(
700701
"Bad decrypt - is the password correct?"
701702
) from err

tests/pytests/functional/states/test_x509_v2.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import pytest
55

6+
from tests.support.mock import patch
7+
68
try:
79
import cryptography
810
import cryptography.x509 as cx509
@@ -2826,3 +2828,30 @@ def _get_privkey(pk, encoding="pem", passphrase=None):
28262828
pk = base64.b64decode(pk)
28272829
return pkcs12.load_pkcs12(pk, passphrase).key
28282830
raise ValueError("Need correct encoding")
2831+
2832+
2833+
@pytest.mark.usefixtures("existing_pk")
2834+
@pytest.mark.parametrize("existing_pk", [{"passphrase": "password"}], indirect=True)
2835+
def test_exceptions_on_calling_load_pem_private_key(x509, pk_args):
2836+
pk_args["passphrase"] = "hunter1"
2837+
pk_args["overwrite"] = True
2838+
2839+
with patch(
2840+
"cryptography.hazmat.primitives.serialization.load_pem_private_key",
2841+
side_effect=ValueError("Bad decrypt. Incorrect password?"),
2842+
):
2843+
ret = x509.private_key_managed(**pk_args)
2844+
_assert_pk_basic(ret, "rsa", passphrase="hunter1")
2845+
2846+
with patch(
2847+
"cryptography.hazmat.primitives.serialization.load_pem_private_key",
2848+
side_effect=ValueError(
2849+
"Could not deserialize key data. The data may be in an incorrect format, "
2850+
"the provided password may be incorrect, "
2851+
"it may be encrypted with an unsupported algorithm, "
2852+
"or it may be an unsupported key type "
2853+
"(e.g. EC curves with explicit parameters)."
2854+
),
2855+
):
2856+
ret = x509.private_key_managed(**pk_args)
2857+
_assert_pk_basic(ret, "rsa", passphrase="hunter1")

tests/pytests/integration/states/test_x509_v2.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,13 @@ def privkey_new(x509_salt_master, tmp_path, ca_minion_id, x509_salt_call_cli):
195195
"""
196196
with x509_salt_master.state_tree.base.temp_file("manage_cert.sls", state):
197197
ret = x509_salt_call_cli.run("state.apply", "manage_cert")
198+
if (
199+
ret.returncode == 1
200+
and "NotImplementedError: ECDSA keys with unnamed curves" in ret.stdout
201+
):
202+
pytest.skip(
203+
"The version of OpenSSL doesn't support ECDSA keys with unnamed curves"
204+
)
198205
assert ret.returncode == 0
199206
assert ret.data[next(iter(ret.data))]["changes"]
200207
assert (tmp_path / "priv.key").exists()

0 commit comments

Comments
 (0)