From 03324b2fb6c6ee315c2e5535628f0cbf8a425d3d Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 8 May 2019 16:43:27 +0200 Subject: [PATCH] Config packages: Catch active stage exceptions in rare cases Typically this already is detected on startup. --- lib/remote/configobjectutility.cpp | 25 ++++++++++++++++++++++--- lib/remote/configpackageshandler.cpp | 8 +++++++- 2 files changed, 29 insertions(+), 4 deletions(-) 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 } })); } } -- 2.40.0