]> granicus.if.org Git - icinga2/commitdiff
Include api user configuration file during api setup
authorMichael Insel <michael@insel.email>
Thu, 23 Aug 2018 20:08:02 +0000 (22:08 +0200)
committerMichael Insel <michael@insel.email>
Tue, 4 Sep 2018 16:38:38 +0000 (18:38 +0200)
refs #6557

lib/cli/apisetuputility.cpp
lib/cli/nodeutility.cpp
lib/cli/nodeutility.hpp

index 640a878c9f1939db1a1484a1e24cfff44c647f0b..141bc94d0df82801e520d21bb3b710f425cea27c 100644 (file)
@@ -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" });
index 101607fe0045e11b5675685df5da3930d9ffe169..4d9fc2378ccafb3c5dee5cd649a583351de00c00 100644 (file)
@@ -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
index 3017a8ead8ea011f26a48d5db7ea89cbecda87a9..b253819057f850d14e596a5b55ff08b1d159f953 100644 (file)
@@ -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);