From 2ade57bcbb882c9f23f85d0a3de15d766078f2d5 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Wed, 7 Oct 2020 13:44:44 +0200 Subject: [PATCH] Add support influxdb basic auth fixes #7644 --- doc/09-object-types.md | 8 ++++++++ lib/perfdata/influxdbwriter.cpp | 12 ++++++++++++ lib/perfdata/influxdbwriter.ti | 7 +++++++ 3 files changed, 27 insertions(+) diff --git a/doc/09-object-types.md b/doc/09-object-types.md index ebbf2e546..6d5ee0405 100644 --- a/doc/09-object-types.md +++ b/doc/09-object-types.md @@ -1607,6 +1607,13 @@ object InfluxdbWriter "influxdb" { host = "127.0.0.1" port = 8086 database = "icinga2" + username = "icinga2" + password = "icinga2" + + basic_auth = { + username = "icinga" + password = "icinga" + } flush_threshold = 1024 flush_interval = 10s @@ -1636,6 +1643,7 @@ Configuration Attributes: database | String | **Required.** InfluxDB database name. Defaults to `icinga2`. username | String | **Optional.** InfluxDB user name. Defaults to `none`. password | String | **Optional.** InfluxDB user password. Defaults to `none`. + basic\_auth | Dictionary | **Optional.** Username and password for HTTP basic authentication. ssl\_enable | Boolean | **Optional.** Whether to use a TLS stream. Defaults to `false`. ssl\_ca\_cert | String | **Optional.** Path to CA certificate to validate the remote host. ssl\_cert | String | **Optional.** Path to host certificate to present to the remote host for mutual verification. diff --git a/lib/perfdata/influxdbwriter.cpp b/lib/perfdata/influxdbwriter.cpp index 4692b8b5d..5dbe785f8 100644 --- a/lib/perfdata/influxdbwriter.cpp +++ b/lib/perfdata/influxdbwriter.cpp @@ -19,6 +19,7 @@ #include "base/perfdatavalue.hpp" #include "base/stream.hpp" #include "base/json.hpp" +#include "base/base64.hpp" #include "base/networkstream.hpp" #include "base/exception.hpp" #include "base/statsfunction.hpp" @@ -504,6 +505,17 @@ void InfluxdbWriter::Flush() request.set(http::field::user_agent, "Icinga/" + Application::GetAppVersion()); request.set(http::field::host, url->GetHost() + ":" + url->GetPort()); + { + Dictionary::Ptr basicAuth = GetBasicAuth(); + + if (basicAuth) { + request.set( + http::field::authorization, + "Basic " + Base64::Encode(basicAuth->Get("username") + ":" + basicAuth->Get("password")) + ); + } + } + request.body() = body; request.set(http::field::content_length, request.body().size()); diff --git a/lib/perfdata/influxdbwriter.ti b/lib/perfdata/influxdbwriter.ti index 377c911ba..73a340cc3 100644 --- a/lib/perfdata/influxdbwriter.ti +++ b/lib/perfdata/influxdbwriter.ti @@ -38,6 +38,7 @@ class InfluxdbWriter : ConfigObject [config] String ssl_key{ default {{{ return ""; }}} }; + [config, no_user_view] Dictionary::Ptr basic_auth; [config, required] Dictionary::Ptr host_template { default {{{ return new Dictionary({ @@ -91,6 +92,12 @@ validator InfluxdbWriter { String "*"; }; }; + Dictionary basic_auth { + required username; + String username; + required password; + String password; + }; }; }