Skip to content

Commit 97c4199

Browse files
committed
Expand on the ways to detect if FIPS is enabled or not
Signed-off-by: Pedro Algarvio <palgarvio@vmware.com>
1 parent d5446db commit 97c4199

3 files changed

Lines changed: 58 additions & 18 deletions

File tree

requirements/tests.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ docker
22
pytest-subtests
33
pyfakefs==4.4.0; python_version == '3.5'
44
pyfakefs; python_version > '3.5'
5+
cryptography

src/pytestskipmarkers/utils/platform.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
..
88
PYTEST_DONT_REWRITE
99
"""
10+
import contextlib
11+
import hashlib
1012
import multiprocessing
1113
import os
1214
import pathlib
@@ -210,23 +212,32 @@ def is_fips_enabled() -> bool:
210212
):
211213
return True
212214
sysctl_path = shutil.which("sysctl")
213-
if not sysctl_path:
214-
return False
215-
ret = subprocess.run(
216-
[sysctl_path, "crypto.fips_enabled"],
217-
check=False,
218-
shell=False,
219-
stdout=subprocess.PIPE,
220-
text=True,
221-
)
222-
if ret.returncode == 0:
223-
stripped_output = ret.stdout.strip()
224-
if not stripped_output:
225-
# No output?
226-
return False
227-
if "=" not in stripped_output:
228-
# Don't know how to parse this
229-
return False
230-
if stripped_output.split("=")[-1].strip() == "1":
215+
if sysctl_path:
216+
ret = subprocess.run(
217+
[sysctl_path, "crypto.fips_enabled"],
218+
check=False,
219+
shell=False,
220+
stdout=subprocess.PIPE,
221+
text=True,
222+
)
223+
if ret.returncode == 0:
224+
stripped_output = ret.stdout.strip()
225+
if (
226+
stripped_output
227+
and "=" in stripped_output
228+
and stripped_output.split("=")[-1].strip() == "1"
229+
):
230+
return True
231+
232+
with contextlib.suppress(ImportError):
233+
import cryptography.hazmat.backends.openssl.backend
234+
235+
if cryptography.hazmat.backends.openssl.backend._fips_enabled:
236+
return True
237+
238+
try:
239+
hashlib.md5() # nosec
240+
except ValueError as exc:
241+
if str(exc) == "[digital envelope routines] unsupported":
231242
return True
232243
return False

tests/unit/utils/test_platform.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212

1313
import pytestskipmarkers.utils.platform
1414

15+
try:
16+
import cryptography.hazmat.backends.openssl.backend as backend
17+
18+
cryptography_import_error = ""
19+
except ImportError as exc: # pragma: no cover
20+
backend = None
21+
cryptography_import_error = f"Failed to import cryptography: {exc}"
22+
1523
log = logging.getLogger(__name__)
1624

1725

@@ -206,6 +214,26 @@ def test_is_fips_enabled_sysctl(output, expected):
206214
assert pytestskipmarkers.utils.platform.is_fips_enabled() is expected
207215

208216

217+
@pytest.mark.skipif(backend is None, reason=cryptography_import_error)
218+
@pytest.mark.parametrize("fips_enabled", [True, False], ids=lambda x: f"fips_enabled={x}")
219+
def test_is_fips_enabled_cryptography(fs, fips_enabled):
220+
fs.create_file("/proc/sys/crypto/fips_enabled", contents="0")
221+
with mock.patch("shutil.which", return_value=None):
222+
with mock.patch.object(backend, "_fips_enabled", fips_enabled):
223+
assert pytestskipmarkers.utils.platform.is_fips_enabled() is fips_enabled
224+
225+
226+
def test_is_fips_by_catch_exception(fs):
227+
fs.create_file("/proc/sys/crypto/fips_enabled", contents="0")
228+
with mock.patch("shutil.which", return_value=None):
229+
with mock.patch("hashlib.md5", return_value="a random md5 hash"):
230+
assert pytestskipmarkers.utils.platform.is_fips_enabled() is False
231+
with mock.patch(
232+
"hashlib.md5", side_effect=ValueError("[digital envelope routines] unsupported")
233+
):
234+
assert pytestskipmarkers.utils.platform.is_fips_enabled() is True
235+
236+
209237
def test_is_spawning_platform():
210238
with mock.patch("multiprocessing.get_start_method", return_value="spawn"):
211239
assert pytestskipmarkers.utils.platform.is_spawning_platform() is True

0 commit comments

Comments
 (0)