From 46163700b4c18cd234fac17ddc4ec55d29576ce2 Mon Sep 17 00:00:00 2001 From: Michael Insel Date: Thu, 23 Aug 2018 22:08:02 +0200 Subject: [PATCH 1/3] Include api user configuration file during api setup refs #6557 --- lib/cli/apisetuputility.cpp | 9 ++++++++ lib/cli/nodeutility.cpp | 43 +++++++++++++++++++++++++++++++++++++ lib/cli/nodeutility.hpp | 1 + 3 files changed, 53 insertions(+) diff --git a/lib/cli/apisetuputility.cpp b/lib/cli/apisetuputility.cpp index 640a878c9..141bc94d0 100644 --- a/lib/cli/apisetuputility.cpp +++ b/lib/cli/apisetuputility.cpp @@ -198,6 +198,15 @@ bool ApiSetupUtility::SetupMasterApiUser() bool ApiSetupUtility::SetupMasterEnableApi() { + /* + * Ensure the api-users.conf file is included, when conf.d inclusion is disabled. + */ + if (!NodeUtility::GetConfigurationIncludeState("\"conf.d\"", true)) + NodeUtility::UpdateConfiguration("\"conf.d/api-users.conf\"", true, false); + + /* + * Enable the API feature + */ Log(LogInformation, "cli", "Enabling the 'api' feature."); FeatureUtility::EnableFeatures({ "api" }); diff --git a/lib/cli/nodeutility.cpp b/lib/cli/nodeutility.cpp index 101607fe0..4d9fc2378 100644 --- a/lib/cli/nodeutility.cpp +++ b/lib/cli/nodeutility.cpp @@ -265,6 +265,49 @@ void NodeUtility::SerializeObject(std::ostream& fp, const Dictionary::Ptr& objec fp << "}\n\n"; } +/* +* Returns true if the include is found, otherwise false +*/ +bool NodeUtility::GetConfigurationIncludeState(const String& value, bool recursive) { + String configurationFile = Application::GetConst("ConfigDir") + "/icinga2.conf"; + + Log(LogInformation, "cli") + << "Reading '" << configurationFile << "'."; + + std::ifstream ifp(configurationFile.CStr()); + + String affectedInclude = value; + + if (recursive) + affectedInclude = "include_recursive " + affectedInclude; + else + affectedInclude = "include " + affectedInclude; + + bool isIncluded = false; + + std::string line; + + while(std::getline(ifp, line)) { + /* + * Trying to find if the inclusion is enabled. + * First hit breaks out of the loop. + */ + + if (!line.compare(affectedInclude)) { + isIncluded = true; + + /* + * We can safely break out here, since an enabled include always win. + */ + break; + } + } + + ifp.close(); + + return isIncluded; +} + /* * include = false, will comment out the include statement * include = true, will add an include statement or uncomment a statement if one is existing diff --git a/lib/cli/nodeutility.hpp b/lib/cli/nodeutility.hpp index 3017a8ead..b25381905 100644 --- a/lib/cli/nodeutility.hpp +++ b/lib/cli/nodeutility.hpp @@ -44,6 +44,7 @@ public: static bool WriteNodeConfigObjects(const String& filename, const Array::Ptr& objects); + static bool GetConfigurationIncludeState(const String& value, bool recursive); static bool UpdateConfiguration(const String& value, bool include, bool recursive); static void UpdateConstant(const String& name, const String& value); From cc417ca1760189f9cf305e6bfa5f1bb991061868 Mon Sep 17 00:00:00 2001 From: Michael Insel Date: Tue, 4 Sep 2018 18:56:54 +0200 Subject: [PATCH 2/3] Fix CI builds --- lib/cli/nodeutility.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli/nodeutility.cpp b/lib/cli/nodeutility.cpp index 4d9fc2378..d7ea8bce3 100644 --- a/lib/cli/nodeutility.cpp +++ b/lib/cli/nodeutility.cpp @@ -269,7 +269,7 @@ void NodeUtility::SerializeObject(std::ostream& fp, const Dictionary::Ptr& objec * Returns true if the include is found, otherwise false */ bool NodeUtility::GetConfigurationIncludeState(const String& value, bool recursive) { - String configurationFile = Application::GetConst("ConfigDir") + "/icinga2.conf"; + String configurationFile = Configuration::ConfigDir + "/icinga2.conf"; Log(LogInformation, "cli") << "Reading '" << configurationFile << "'."; From 1352b1522010f5899bee166792e50a24cad5bbb3 Mon Sep 17 00:00:00 2001 From: Michael Insel Date: Wed, 5 Dec 2018 20:12:06 +0100 Subject: [PATCH 3/3] Fix string comparison on edge cases This fixes a few edge cases where the string comparison failed before. --- lib/cli/nodeutility.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/cli/nodeutility.cpp b/lib/cli/nodeutility.cpp index d7ea8bce3..b8df144a0 100644 --- a/lib/cli/nodeutility.cpp +++ b/lib/cli/nodeutility.cpp @@ -293,7 +293,7 @@ bool NodeUtility::GetConfigurationIncludeState(const String& value, bool recursi * First hit breaks out of the loop. */ - if (!line.compare(affectedInclude)) { + if (line.compare(0, affectedInclude.GetLength(), affectedInclude) == 0) { isIncluded = true; /*