Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow SSLContext ciphers to be customized #19312

Merged
merged 5 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions datadog_checks_base/changelog.d/19312.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow for Ciphers to be customizable in the SSLContext creation
10 changes: 10 additions & 0 deletions datadog_checks_base/datadog_checks/base/utils/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'tls_private_key': None,
'tls_private_key_password': None,
'tls_validate_hostname': True,
'tls_ciphers': 'ALL',
}


Expand Down Expand Up @@ -115,6 +116,15 @@ def _create_tls_context(self):
else:
context.check_hostname = False

ciphers = self.config.get('tls_ciphers')
if ciphers:
if 'ALL' in ciphers:
updated_ciphers = "ALL"
else:
updated_ciphers = ":".join(ciphers)

context.set_ciphers(updated_ciphers)

# https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_verify_locations
# https://docs.python.org/3/library/ssl.html#ssl.SSLContext.load_default_certs
ca_cert = self.config['tls_ca_cert']
Expand Down
58 changes: 58 additions & 0 deletions datadog_checks_base/tests/base/utils/test_tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,64 @@ def test_client_key_expanded_tls_verify_false(self):
check.get_tls_context()
mock_expand.assert_called_with('~/foo')

@pytest.mark.parametrize(
'instance,expected_ciphers',
[
pytest.param(
{'tls_verify': False},
'ALL',
id="Construct ciphers with no config",
),
pytest.param(
{'tls_ciphers': ['TLS_RSA_WITH_SEED_CBC_SHA', 'TLS_SM4_GCM_SM3']},
'TLS_RSA_WITH_SEED_CBC_SHA:TLS_SM4_GCM_SM3',
id='Construct ciphers with specific ciphers',
),
pytest.param(
{'tls_ciphers': ['ALL']},
'ALL',
id="Construct Ciphers with 'ALL' ciphers",
),
],
)
def test_cipher_construction(self, instance, expected_ciphers):
with patch.object(ssl.SSLContext, 'set_ciphers') as mock_set_ciphers:
check = AgentCheck('test', {}, [instance])
check.get_tls_context()
mock_set_ciphers.assert_called_once_with(expected_ciphers)

@pytest.mark.parametrize(
'instance,expected_ciphers',
[
pytest.param(
{'tls_verify': False},
'ALL',
id="No Ciphers, default to 'ALL'",
),
pytest.param(
{'tls_ciphers': ['PSK-CAMELLIA128-SHA256', 'DHE-PSK-CAMELLIA128-SHA256']},
'PSK-CAMELLIA128-SHA256:DHE-PSK-CAMELLIA128-SHA256',
id='Add specific ciphers only',
),
pytest.param(
{'tls_ciphers': ['ALL']},
'ALL',
id="'ALL' manually",
),
],
)
def test_ciphers(self, instance, expected_ciphers):
check = AgentCheck('test', {}, [instance])
context = check.get_tls_context()

expected_context = ssl.SSLContext(protocol=ssl.PROTOCOL_TLS_CLIENT)
expected_context.set_ciphers(expected_ciphers)

actual_ciphers = sorted(cipher['name'] for cipher in context.get_ciphers())
expected_ciphers_list = sorted(cipher['name'] for cipher in expected_context.get_ciphers())

assert actual_ciphers == expected_ciphers_list


class TestTLSContextOverrides:
def test_override_context(self):
Expand Down
Loading