From: Michael Friedrich Date: Wed, 8 May 2019 14:43:27 +0000 (+0200) Subject: Config packages: Catch active stage exceptions in rare cases X-Git-Tag: v2.11.0-rc1~111^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=refs%2Fpull%2F7168%2Fhead;p=icinga2 Config packages: Catch active stage exceptions in rare cases Typically this already is detected on startup. --- diff --git a/lib/remote/configobjectutility.cpp b/lib/remote/configobjectutility.cpp index 503cdbb45..c1a0f6543 100644 --- a/lib/remote/configobjectutility.cpp +++ b/lib/remote/configobjectutility.cpp @@ -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); diff --git a/lib/remote/configpackageshandler.cpp b/lib/remote/configpackageshandler.cpp index fd60661f7..c1b1a1076 100644 --- a/lib/remote/configpackageshandler.cpp +++ b/lib/remote/configpackageshandler.cpp @@ -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 } })); } }