]> granicus.if.org Git - icinga2/commitdiff
Config packages: Catch active stage exceptions in rare cases 7168/head
authorMichael Friedrich <michael.friedrich@icinga.com>
Wed, 8 May 2019 14:43:27 +0000 (16:43 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Wed, 8 May 2019 14:43:27 +0000 (16:43 +0200)
Typically this already is detected on startup.

lib/remote/configobjectutility.cpp
lib/remote/configpackageshandler.cpp

index 503cdbb45b21c48571e836d8da0056c811ff4b81..c1a0f6543e18181ed067021c7d60a190272d72c8 100644 (file)
@@ -16,6 +16,7 @@ using namespace icinga;
 
 String ConfigObjectUtility::GetConfigDir()
 {
+       /* This may throw an exception the caller above must handle. */
        return ConfigPackageUtility::GetPackageDir() + "/_api/" +
                ConfigPackageUtility::GetActiveStage("_api");
 }
@@ -25,7 +26,10 @@ String ConfigObjectUtility::GetObjectConfigPath(const Type::Ptr& type, const Str
        String typeDir = type->GetPluralName();
        boost::algorithm::to_lower(typeDir);
 
-       return GetConfigDir() + "/conf.d/" + typeDir +
+       /* This may throw an exception the caller above must handle. */
+       String prefix = GetConfigDir();
+
+       return prefix + "/conf.d/" + typeDir +
                "/" + EscapeName(fullName) + ".conf";
 }
 
@@ -102,7 +106,15 @@ bool ConfigObjectUtility::CreateObject(const Type::Ptr& type, const String& full
                return false;
        }
 
-       String path = GetObjectConfigPath(type, fullName);
+       String path;
+
+       try {
+               path = GetObjectConfigPath(type, fullName);
+       } catch (const std::exception& ex) {
+               errors->Add("Config package broken: " + DiagnosticInformation(ex, false));
+               return false;
+       }
+
        Utility::MkDirP(Utility::DirName(path), 0700);
 
        std::ofstream fp(path.CStr(), std::ofstream::out | std::ostream::trunc);
@@ -216,7 +228,14 @@ bool ConfigObjectUtility::DeleteObjectHelper(const ConfigObject::Ptr& object, bo
                return false;
        }
 
-       String path = GetObjectConfigPath(object->GetReflectionType(), name);
+       String path;
+
+       try {
+               path = GetObjectConfigPath(object->GetReflectionType(), name);
+       } catch (const std::exception& ex) {
+               errors->Add("Config package broken: " + DiagnosticInformation(ex, false));
+               return false;
+       }
 
        Utility::Remove(path);
 
index fd60661f71e146d59ffc8b742af2a6df40182e3d..c1b1a10767eb2e3134fbfbf9fc992cdd7f72cbc6 100644 (file)
@@ -66,10 +66,16 @@ void ConfigPackagesHandler::HandleGet(
                boost::mutex::scoped_lock lock(ConfigPackageUtility::GetStaticPackageMutex());
 
                for (const String& package : packages) {
+                       String activeStage;
+
+                       try {
+                               activeStage = ConfigPackageUtility::GetActiveStage(package);
+                       } catch (const std::exception&) { } /* Should never happen. */
+
                        results.emplace_back(new Dictionary({
                                { "name", package },
                                { "stages", Array::FromVector(ConfigPackageUtility::GetStages(package)) },
-                               { "active-stage", ConfigPackageUtility::GetActiveStage(package) }
+                               { "active-stage", activeStage }
                        }));
                }
        }