Skip to content

Commit 4459bf7

Browse files
committed
elasticapm/conf: block disabling of verify cert server in fips mode
1 parent 52fd979 commit 4459bf7

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

elasticapm/conf/__init__.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import threading
3838
from datetime import timedelta
3939

40+
import _hashlib
41+
4042
from elasticapm.conf.constants import BASE_SANITIZE_FIELD_NAMES, TRACE_CONTINUATION_STRATEGY
4143
from elasticapm.utils import compat, starmatch_to_regex
4244
from elasticapm.utils.logging import get_logger
@@ -373,6 +375,30 @@ def __call__(self, value, field_name):
373375
return value
374376

375377

378+
def _in_fips_mode():
379+
try:
380+
return _hashlib.get_fips_mode() == 1
381+
except AttributeError:
382+
# versions older of Python3.9 does not have the helper
383+
return False
384+
385+
386+
class SupportedValueInFipsModeValidator(object):
387+
"""If FIPS mode is enabled only supported_value is accepted"""
388+
389+
def __init__(self, supported_value) -> None:
390+
self.supported_value = supported_value
391+
392+
def __call__(self, value, field_name):
393+
if _in_fips_mode():
394+
if value != self.supported_value:
395+
raise ConfigurationError(
396+
"{}={} must be set to {} if FIPS mode is enabled".format(field_name, value, self.supported_value),
397+
field_name,
398+
)
399+
return value
400+
401+
376402
class EnumerationValidator(object):
377403
"""
378404
Validator which ensures that a given config value is chosen from a list
@@ -579,7 +605,9 @@ class Config(_ConfigBase):
579605
server_url = _ConfigValue("SERVER_URL", default="http://127.0.0.1:8200", required=True)
580606
server_cert = _ConfigValue("SERVER_CERT", validators=[FileIsReadableValidator()])
581607
server_ca_cert_file = _ConfigValue("SERVER_CA_CERT_FILE", validators=[FileIsReadableValidator()])
582-
verify_server_cert = _BoolConfigValue("VERIFY_SERVER_CERT", default=True)
608+
verify_server_cert = _BoolConfigValue(
609+
"VERIFY_SERVER_CERT", default=True, validators=[SupportedValueInFipsModeValidator(supported_value=True)]
610+
)
583611
use_certifi = _BoolConfigValue("USE_CERTIFI", default=True)
584612
include_paths = _ListConfigValue("INCLUDE_PATHS")
585613
exclude_paths = _ListConfigValue("EXCLUDE_PATHS", default=compat.get_default_library_patters())

tests/config/tests.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import mock
4040
import pytest
4141

42+
import elasticapm.conf
4243
from elasticapm.conf import (
4344
Config,
4445
ConfigurationError,
@@ -47,6 +48,7 @@
4748
FileIsReadableValidator,
4849
PrecisionValidator,
4950
RegexValidator,
51+
SupportedValueInFipsModeValidator,
5052
UnitValidator,
5153
VersionedConfig,
5254
_BoolConfigValue,
@@ -490,3 +492,31 @@ def test_exclude_range_validator_not_in_range():
490492
with pytest.raises(ConfigurationError) as e:
491493
validator(10, "field")
492494
assert "cannot be in range" in e.value.args[0]
495+
496+
497+
def test_supported_value_in_fips_mode_validator_in_fips_mode_with_invalid_value(monkeypatch):
498+
monkeypatch.setattr(elasticapm.conf, "_in_fips_mode", lambda: True)
499+
exception_message = "verify_server_cert=False must be set to True if FIPS mode is enabled"
500+
validator = SupportedValueInFipsModeValidator(supported_value=True)
501+
with pytest.raises(ConfigurationError) as e:
502+
validator(False, "verify_server_cert")
503+
assert exception_message == e.value.args[0]
504+
505+
config = Config({"VERIFY_SERVER_CERT": False})
506+
assert config.errors["verify_server_cert"] == exception_message
507+
508+
509+
def test_supported_value_in_fips_mode_validator_in_fips_mode_with_valid_value(monkeypatch):
510+
monkeypatch.setattr(elasticapm.conf, "_in_fips_mode", lambda: True)
511+
validator = SupportedValueInFipsModeValidator(supported_value=True)
512+
assert validator(True, "verify_server_cert") == True
513+
config = Config({"VERIFY_SERVER_CERT": True})
514+
assert config.verify_server_cert == True
515+
assert "verify_server_cert" not in config.errors
516+
517+
518+
def test_supported_value_in_fips_mode_validator_not_in_fips_mode(monkeypatch):
519+
monkeypatch.setattr(elasticapm.conf, "_in_fips_mode", lambda: False)
520+
validator = SupportedValueInFipsModeValidator(supported_value=True)
521+
assert validator(True, "field") == True
522+
assert validator(False, "field") == False

0 commit comments

Comments
 (0)