From bf535969ac23962b65b72ea3893c6b384e1d3218 Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Thu, 12 Aug 2021 16:42:23 +0200 Subject: [PATCH] ElasticsearchWriter: actually verify TLS server certificates And add a new option insecure_noverify to explicitly disable it if desired. --- doc/09-object-types.md | 1 + lib/perfdata/elasticsearchwriter.cpp | 12 ++++++++++++ lib/perfdata/elasticsearchwriter.ti | 3 +++ 3 files changed, 16 insertions(+) diff --git a/doc/09-object-types.md b/doc/09-object-types.md index b2c8417f2..746ed7c14 100644 --- a/doc/09-object-types.md +++ b/doc/09-object-types.md @@ -1232,6 +1232,7 @@ Configuration Attributes: username | String | **Optional.** Basic auth username if Elasticsearch is hidden behind an HTTP proxy. password | String | **Optional.** Basic auth password if Elasticsearch is hidden behind an HTTP proxy. enable\_tls | Boolean | **Optional.** Whether to use a TLS stream. Defaults to `false`. Requires an HTTP proxy. + insecure\_noverify | Boolean | **Optional.** Disable TLS peer verification. ca\_path | String | **Optional.** Path to CA certificate to validate the remote host. Requires `enable_tls` set to `true`. cert\_path | String | **Optional.** Path to host certificate to present to the remote host for mutual verification. Requires `enable_tls` set to `true`. key\_path | String | **Optional.** Path to host key to accompany the cert\_path. Requires `enable_tls` set to `true`. diff --git a/lib/perfdata/elasticsearchwriter.cpp b/lib/perfdata/elasticsearchwriter.cpp index 9ab277f20..97f906535 100644 --- a/lib/perfdata/elasticsearchwriter.cpp +++ b/lib/perfdata/elasticsearchwriter.cpp @@ -622,6 +622,18 @@ OptionalTlsStream ElasticsearchWriter::Connect() << "TLS handshake with host '" << GetHost() << "' on port " << GetPort() << " failed."; throw; } + + if (!GetInsecureNoverify()) { + if (!tlsStream.GetPeerCertificate()) { + BOOST_THROW_EXCEPTION(std::runtime_error("Elasticsearch didn't present any TLS certificate.")); + } + + if (!tlsStream.IsVerifyOK()) { + BOOST_THROW_EXCEPTION(std::runtime_error( + "TLS certificate validation failed: " + std::string(tlsStream.GetVerifyError()) + )); + } + } } return std::move(stream); diff --git a/lib/perfdata/elasticsearchwriter.ti b/lib/perfdata/elasticsearchwriter.ti index a072220de..e3b8e27f5 100644 --- a/lib/perfdata/elasticsearchwriter.ti +++ b/lib/perfdata/elasticsearchwriter.ti @@ -29,6 +29,9 @@ class ElasticsearchWriter : ConfigObject [config] bool enable_tls { default {{{ return false; }}} }; + [config] bool insecure_noverify { + default {{{ return false; }}} + }; [config] String ca_path; [config] String cert_path; [config] String key_path;