From 16e54218bdf148f67f0449f0846815b736c4918f Mon Sep 17 00:00:00 2001 From: Dmitriy Pavlov Date: Wed, 12 Apr 2023 17:28:05 -0400 Subject: [PATCH] TLS check: implement StartTLS protocol for Mysql (#14352) * TLS check: implement StartTLS protocol for Mysql * fix model validation --- tls/assets/configuration/spec.yaml | 3 ++- .../tls/config_models/instance.py | 2 +- tls/datadog_checks/tls/data/conf.yaml.example | 2 +- tls/datadog_checks/tls/tls_remote.py | 23 ++++++++++++++++++- tls/tests/compose/Dockerfile | 5 ++++ tls/tests/compose/docker-compose.yml | 14 +++++++++++ tls/tests/conftest.py | 13 ++++++++++- tls/tests/test_remote.py | 14 +++++++++++ 8 files changed, 71 insertions(+), 5 deletions(-) diff --git a/tls/assets/configuration/spec.yaml b/tls/assets/configuration/spec.yaml index 67091e9641282..7a69f99ad14a5 100644 --- a/tls/assets/configuration/spec.yaml +++ b/tls/assets/configuration/spec.yaml @@ -140,10 +140,11 @@ files: irc, postgres, mysql, lmtp, nntp, sieve, and ldap. Currently this checks supports only the below protocols: - postgres + postgres, mysql value: type: string enum: - postgres + - mysql - template: instances/default - template: instances/tls diff --git a/tls/datadog_checks/tls/config_models/instance.py b/tls/datadog_checks/tls/config_models/instance.py index 661250a77bbdb..9a37b81081aec 100644 --- a/tls/datadog_checks/tls/config_models/instance.py +++ b/tls/datadog_checks/tls/config_models/instance.py @@ -49,7 +49,7 @@ class Config: server: str server_hostname: Optional[str] service: Optional[str] - start_tls: Optional[Literal['postgres']] + start_tls: Optional[Literal['postgres', 'mysql']] tags: Optional[Sequence[str]] timeout: Optional[int] tls_ca_cert: Optional[str] diff --git a/tls/datadog_checks/tls/data/conf.yaml.example b/tls/datadog_checks/tls/data/conf.yaml.example index 52618761dd14b..da1f08328f4bd 100644 --- a/tls/datadog_checks/tls/data/conf.yaml.example +++ b/tls/datadog_checks/tls/data/conf.yaml.example @@ -133,7 +133,7 @@ instances: ## irc, postgres, mysql, lmtp, nntp, sieve, and ldap. ## ## Currently this checks supports only the below protocols: - ## postgres + ## postgres, mysql # # start_tls: diff --git a/tls/datadog_checks/tls/tls_remote.py b/tls/datadog_checks/tls/tls_remote.py index 9a61c8ad76a04..96d2cbf888cde 100644 --- a/tls/datadog_checks/tls/tls_remote.py +++ b/tls/datadog_checks/tls/tls_remote.py @@ -3,7 +3,7 @@ # Licensed under a 3-clause BSD style license (see LICENSE) import ssl from hashlib import sha256 -from struct import pack +from struct import pack, unpack from cryptography.x509.base import load_der_x509_certificate from cryptography.x509.extensions import ExtensionNotFound @@ -134,6 +134,27 @@ def _switch_starttls(self, sock): data = self._read_n_bytes_from_socket(sock, 1) if data != b'S': raise Exception('Postgres endpoint does not support TLS') + elif protocol == "mysql": + self.log.debug('Switching connection to encrypted for %s protocol', protocol) + cap_protocol_41 = 1 << 9 + cap_ssl = 1 << 11 + cap_secure_connection = 1 << 15 + capabilities = cap_protocol_41 | cap_ssl | cap_secure_connection + max_packet_len = 2**24 - 1 + charset_id = 8 # latin1 + # Form Protocol::SSLRequest packet + data_init = pack("