Move ValidateMacroString into the MacroProcessor class

refs #7311
This commit is contained in:
Michael Friedrich 2015-02-11 16:08:02 +01:00
parent 0d18b96c39
commit 24852e05c7
9 changed files with 41 additions and 38 deletions

View File

@ -1256,23 +1256,3 @@ String Utility::UnescapeString(const String& s)
return result.str();
}
bool Utility::ValidateMacroString(const String& macro)
{
if (macro.IsEmpty())
return true;
size_t pos_first, pos_second, offset;
offset = 0;
while((pos_first = macro.FindFirstOf("$", offset)) != String::NPos) {
pos_second = macro.FindFirstOf("$", pos_first + 1);
if (pos_second == String::NPos)
return false;
offset = pos_second + 1;
}
return true;
}

View File

@ -118,8 +118,6 @@ public:
static String EscapeString(const String& s, const String& chars);
static String UnescapeString(const String& s);
static bool ValidateMacroString(const String& macro);
static void SetThreadName(const String& name, bool os = true);
static String GetThreadName(void);

View File

@ -18,6 +18,7 @@
******************************************************************************/
#include "icinga/command.hpp"
#include "icinga/macroprocessor.hpp"
#include "base/function.hpp"
#include "base/exception.hpp"
#include "base/objectlock.hpp"
@ -81,7 +82,7 @@ void Command::ValidateArguments(const String& location, const Command::Ptr& obje
String argstr = argval;
if (!Utility::ValidateMacroString(argstr)) {
if(!MacroProcessor::ValidateMacroString(argstr)) {
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
location + ": Closing $ not found in macro format string '" + argstr + "'.", object->GetDebugInfo()));
}
@ -102,7 +103,7 @@ void Command::ValidateEnvironmentVariables(const String& location, const Command
if (!envval.IsString() || envval.IsEmpty())
continue;
if (!Utility::ValidateMacroString(envval)) {
if(!MacroProcessor::ValidateMacroString(envval)) {
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
location + ": Closing $ not found in macro format string '" + envval + "'.", object->GetDebugInfo()));
}

View File

@ -18,6 +18,7 @@
******************************************************************************/
#include "icinga/customvarobject.hpp"
#include "icinga/macroprocessor.hpp"
#include "base/logger.hpp"
#include "base/function.hpp"
#include "base/exception.hpp"
@ -88,7 +89,7 @@ void CustomVarObject::ValidateCustomAttributes(const String& location, const Cus
if (kv_var.second.IsEmpty())
continue;
if (!Utility::ValidateMacroString(kv_var.second)) {
if (!MacroProcessor::ValidateMacroString(kv_var.second)) {
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
location + ": Closing $ not found in macro format string '" + kv_var.second + "'.", object->GetDebugInfo()));
}
@ -102,7 +103,7 @@ void CustomVarObject::ValidateCustomAttributes(const String& location, const Cus
if (arrval.IsEmpty())
continue;
if (!Utility::ValidateMacroString(arrval)) {
if (!MacroProcessor::ValidateMacroString(arrval)) {
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
location + ": Closing $ not found in macro format string '" + arrval + "'.", object->GetDebugInfo()));
}
@ -113,7 +114,7 @@ void CustomVarObject::ValidateCustomAttributes(const String& location, const Cus
String varstr = varval;
if (!Utility::ValidateMacroString(varstr)) {
if (!MacroProcessor::ValidateMacroString(varstr)) {
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
location + ": Closing $ not found in macro format string '" + varstr + "'.", object->GetDebugInfo()));
}

View File

@ -302,3 +302,24 @@ Value MacroProcessor::InternalResolveMacros(const String& str, const ResolverLis
return result;
}
bool MacroProcessor::ValidateMacroString(const String& macro)
{
if (macro.IsEmpty())
return true;
size_t pos_first, pos_second, offset;
offset = 0;
while((pos_first = macro.FindFirstOf("$", offset)) != String::NPos) {
pos_second = macro.FindFirstOf("$", pos_first + 1);
if (pos_second == String::NPos)
return false;
offset = pos_second + 1;
}
return true;
}

View File

@ -47,6 +47,8 @@ public:
const Dictionary::Ptr& resolvedMacros = Dictionary::Ptr(),
bool useResolvedMacros = false);
static bool ValidateMacroString(const String& macro);
private:
MacroProcessor(void);

View File

@ -234,12 +234,12 @@ Value GraphiteWriter::EscapeMacroMetric(const Value& value)
void GraphiteWriter::ValidateNameTemplates(const String& location, const GraphiteWriter::Ptr& object)
{
if (!Utility::ValidateMacroString(object->GetHostNameTemplate())) {
if (!MacroProcessor::ValidateMacroString(object->GetHostNameTemplate())) {
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
location + ": Closing $ not found in macro format string '" + object->GetHostNameTemplate() + "'.", object->GetDebugInfo()));
}
if (!Utility::ValidateMacroString(object->GetServiceNameTemplate())) {
if (!MacroProcessor::ValidateMacroString(object->GetServiceNameTemplate())) {
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
location + ": Closing $ not found in macro format string '" + object->GetServiceNameTemplate() + "'.", object->GetDebugInfo()));
}

View File

@ -142,12 +142,12 @@ void PerfdataWriter::RotationTimerHandler(void)
void PerfdataWriter::ValidateFormatTemplates(const String& location, const PerfdataWriter::Ptr& object)
{
if (!Utility::ValidateMacroString(object->GetHostFormatTemplate())) {
if (!MacroProcessor::ValidateMacroString(object->GetHostFormatTemplate())) {
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
location + ": Closing $ not found in macro format string '" + object->GetHostFormatTemplate() + "'.", object->GetDebugInfo()));
}
if (!Utility::ValidateMacroString(object->GetServiceFormatTemplate())) {
if (!MacroProcessor::ValidateMacroString(object->GetServiceFormatTemplate())) {
BOOST_THROW_EXCEPTION(ScriptError("Validation failed for " +
location + ": Closing $ not found in macro format string '" + object->GetHostFormatTemplate() + "'.", object->GetDebugInfo()));
}

View File

@ -52,15 +52,15 @@ BOOST_AUTO_TEST_CASE(simple)
BOOST_CHECK(result->GetLength() == 2);
/* verify the config validator macro checks */
BOOST_CHECK(Utility::ValidateMacroString("$host.address") == false);
BOOST_CHECK(Utility::ValidateMacroString("host.vars.test$") == false);
BOOST_CHECK(MacroProcessor::ValidateMacroString("$host.address") == false);
BOOST_CHECK(MacroProcessor::ValidateMacroString("host.vars.test$") == false);
BOOST_CHECK(Utility::ValidateMacroString("host.vars.test$") == false);
BOOST_CHECK(Utility::ValidateMacroString("$template::test$abc$") == false);
BOOST_CHECK(MacroProcessor::ValidateMacroString("host.vars.test$") == false);
BOOST_CHECK(MacroProcessor::ValidateMacroString("$template::test$abc$") == false);
BOOST_CHECK(Utility::ValidateMacroString("$$test $host.vars.test$") == true);
BOOST_CHECK(MacroProcessor::ValidateMacroString("$$test $host.vars.test$") == true);
BOOST_CHECK(Utility::ValidateMacroString("test $host.vars.test$") == true);
BOOST_CHECK(MacroProcessor::ValidateMacroString("test $host.vars.test$") == true);
}