From a85724238eb0359c4772d4a3ce8c146ab7e6be3b Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Tue, 18 Aug 2026 18:01:42 -0400 Subject: [PATCH] Fixed one bad CMDB entry discarding every entry in its section CheckCMDBDataForUnexpandedVars() rejected a whole 'vars'/'variables'/ 'classes' section on the first entry containing an unresolved variable reference, discarding every other entry in that section along with it. Checked per entry instead, inside each section's own install loop, matching this file's existing skip-and-continue idiom for other per-entry problems (an invalid variable specification, a missing 'value' field). New acceptance test: one good and one bad entry in the same 'vars' section, asserting the bad entry is still rejected and named, and the good entry is installed anyway. Discrimination proved: fails on the unpatched function, passes with the fix; full 00_basics/06 suite still 14/14. Changelog: Title Ticket: CFE-4720 --- libpromises/cmdb.c | 133 +++++++++--------- ...variable-references-good-entry-survives.cf | 78 ++++++++++ ...ble-references-good-entry-survives.cf.json | 6 + 3 files changed, 147 insertions(+), 70 deletions(-) create mode 100644 tests/acceptance/00_basics/06_host_specific_data/16-variable-references-good-entry-survives.cf create mode 100644 tests/acceptance/00_basics/06_host_specific_data/16-variable-references-good-entry-survives.cf.json diff --git a/libpromises/cmdb.c b/libpromises/cmdb.c index a874071ab8b..32c460f92a6 100644 --- a/libpromises/cmdb.c +++ b/libpromises/cmdb.c @@ -116,73 +116,66 @@ static bool CheckObjectForUnexpandedVars(JsonElement *object, void *data) } /** - * Reject CMDB data containing unresolved variable references, naming the entry - * that was rejected. + * Reject a single CMDB entry containing unresolved variable references, + * naming the entry that was rejected. * - * Each top-level entry is walked separately so that the entry can be named, - * which is what an operator needs to find the offending line. A single walk of - * the whole section could only report the immediate property name, and in the - * 'variables' format (CFE-3633) that is the metadata key 'value' rather than - * the name of the variable holding it. + * Checked per entry, rather than once for the whole section, so that one + * offending entry does not take every other entry in the section down with + * it -- matching the skip-and-continue idiom the rest of this file already + * uses for other per-entry problems (an invalid variable specification, a + * missing "value" field). * - * @param data the CMDB section to check, which must be a JSON object + * @param entry the entry's value, as found under key in its section + * @param key the entry's key, as it appears in the CMDB file * @param section the section's key, as it appears in the CMDB file * @param file_path the CMDB file the data came from - * @return whether the data is free of variable references + * @return whether the entry is free of variable references */ -static bool CheckCMDBDataForUnexpandedVars(JsonElement *data, - const char *section, - const char *file_path) +static bool CheckCMDBEntryForUnexpandedVars(JsonElement *entry, + const char *key, + const char *section, + const char *file_path) { - assert(data != NULL); - assert(JsonGetType(data) == JSON_TYPE_OBJECT); + assert(entry != NULL); + assert(key != NULL); assert(section != NULL); assert(file_path != NULL); - JsonIterator iter = JsonIteratorInit(data); - while (JsonIteratorHasMore(&iter)) + if (StringContainsUnresolved(key)) { - JsonElement *child = JsonIteratorNextValue(&iter); - const char *entry = JsonGetPropertyAsString(child); - - if (StringContainsUnresolved(entry)) - { - Log(LOG_LEVEL_ERR, - "Invalid '%s' CMDB data in '%s', cannot contain variable" - " references (offending key: '%s')", - section, file_path, entry); - return false; - } + Log(LOG_LEVEL_ERR, + "Invalid '%s' CMDB data in '%s', cannot contain variable" + " references (offending key: '%s')", + section, file_path, key); + return false; + } - CMDBUnexpandedVar offender = {NULL, NULL}; - if (JsonWalk(child, CheckObjectForUnexpandedVars, - NULL, CheckPrimitiveForUnexpandedVars, &offender)) - { - continue; - } + CMDBUnexpandedVar offender = {NULL, NULL}; + if (JsonWalk(entry, CheckObjectForUnexpandedVars, + NULL, CheckPrimitiveForUnexpandedVars, &offender)) + { + return true; + } - /* The walk stops only when one of the two visitors above stops it, - * and each records what it found before doing so. */ - assert((offender.value != NULL) || (offender.key != NULL)); + /* The walk stops only when one of the two visitors above stops it, and + * each records what it found before doing so. */ + assert((offender.value != NULL) || (offender.key != NULL)); - if (offender.value != NULL) - { - Log(LOG_LEVEL_ERR, - "Invalid '%s' CMDB data in '%s', cannot contain variable" - " references (offending value in entry '%s': '%s')", - section, file_path, entry, offender.value); - } - else - { - Log(LOG_LEVEL_ERR, - "Invalid '%s' CMDB data in '%s', cannot contain variable" - " references (offending key in entry '%s': '%s')", - section, file_path, entry, offender.key); - } - return false; + if (offender.value != NULL) + { + Log(LOG_LEVEL_ERR, + "Invalid '%s' CMDB data in '%s', cannot contain variable" + " references (offending value in entry '%s': '%s')", + section, file_path, key, offender.value); } - - return true; + else + { + Log(LOG_LEVEL_ERR, + "Invalid '%s' CMDB data in '%s', cannot contain variable" + " references (offending key in entry '%s': '%s')", + section, file_path, key, offender.key); + } + return false; } static VarRef *GetCMDBVariableRef(const char *key) @@ -272,17 +265,17 @@ static bool ReadCMDBVars(EvalContext *ctx, JsonElement *vars, return false; } - if (!CheckCMDBDataForUnexpandedVars(vars, "vars", file_path)) - { - return false; - } - JsonIterator iter = JsonIteratorInit(vars); while (JsonIteratorHasMore(&iter)) { const char *key = JsonIteratorNextKey(&iter); JsonElement *data = JsonObjectGet(vars, key); + if (!CheckCMDBEntryForUnexpandedVars(data, key, "vars", file_path)) + { + continue; + } + VarRef *ref = GetCMDBVariableRef(key); if (ref == NULL) { @@ -375,15 +368,16 @@ static bool ReadCMDBVariables(EvalContext *ctx, JsonElement *variables, return false; } - if (!CheckCMDBDataForUnexpandedVars(variables, "variables", file_path)) - { - return false; - } - JsonIterator iter = JsonIteratorInit(variables); while (JsonIteratorHasMore(&iter)) { const char *key = JsonIteratorNextKey(&iter); + JsonElement *const var_info = JsonObjectGet(variables, key); + + if (!CheckCMDBEntryForUnexpandedVars(var_info, key, "variables", file_path)) + { + continue; + } VarRef *ref = GetCMDBVariableRef(key); if (ref == NULL) @@ -391,8 +385,6 @@ static bool ReadCMDBVariables(EvalContext *ctx, JsonElement *variables, continue; } - JsonElement *const var_info = JsonObjectGet(variables, key); - JsonElement *data; StringSet *tags; const char *comment = NULL; @@ -480,16 +472,17 @@ static bool ReadCMDBClasses(EvalContext *ctx, JsonElement *classes, return false; } - if (!CheckCMDBDataForUnexpandedVars(classes, "classes", file_path)) - { - return false; - } - JsonIterator iter = JsonIteratorInit(classes); while (JsonIteratorHasMore(&iter)) { const char *key = JsonIteratorNextKey(&iter); JsonElement *data = JsonObjectGet(classes, key); + + if (!CheckCMDBEntryForUnexpandedVars(data, key, "classes", file_path)) + { + continue; + } + if (JsonGetElementType(data) == JSON_ELEMENT_TYPE_PRIMITIVE) { const char *expr = JsonPrimitiveGetAsString(data); diff --git a/tests/acceptance/00_basics/06_host_specific_data/16-variable-references-good-entry-survives.cf b/tests/acceptance/00_basics/06_host_specific_data/16-variable-references-good-entry-survives.cf new file mode 100644 index 00000000000..9cc38dd9975 --- /dev/null +++ b/tests/acceptance/00_basics/06_host_specific_data/16-variable-references-good-entry-survives.cf @@ -0,0 +1,78 @@ +# test that one entry with an unresolved variable reference does not drop +# every other entry in the same section +body common control +{ + inputs => { "../../default.sub.cf" }; + bundlesequence => { default("$(this.promise_filename)") }; + version => "1.0"; +} + +####################################################### +bundle agent init +{ + files: + "$(sys.workdir)$(const.dirsep)data$(const.dirsep)." + create => "true", + perms => mog( + "0700", "$(sys.user_data[username])", "$(sys.user_data[username])" + ); + + methods: + "prepare_host_specific_data" + usebundle => file_copy( + "$(this.promise_filename).json", + "$(sys.workdir)$(const.dirsep)data$(const.dirsep)host_specific.json" + ), + handle => "prepare_host_specific_data"; + + "empty_promises_cf" + usebundle => file_make("$(sys.inputdir)/promises.cf", ''); +} + +####################################################### +bundle agent check +{ + vars: + "error_output" + string => execresult( + "$(sys.cf_promises) --show-classes -f $(sys.inputdir)/promises.cf", + "useshell" + ), + depends_on => { "prepare_host_specific_data" }; + + "vars_output" + string => execresult( + "$(sys.cf_promises) --show-vars -f $(sys.inputdir)/promises.cf | $(G.grep) good_key", + "useshell" + ), + depends_on => { "prepare_host_specific_data" }; + + classes: + # The offending entry is still rejected and named. + "offender_named" + expression => regcmp( + ".*error: Invalid 'vars' CMDB data in '.*host_specific\.json', cannot contain variable references \(offending value in entry 'key_with_unresolved_value': .*", + "$(error_output)" + ); + + # Its sibling entry in the same section is installed anyway -- a single + # bad entry must not take the rest of the section down with it. + "good_entry_survived" + expression => regcmp( + "data:variables\.good_key\s+a literal value\s+source=cmdb", + "$(vars_output)" + ); + + "ok" and => { "offender_named", "good_entry_survived" }; + + reports: + ok:: + "$(this.promise_filename) Pass"; + + !ok:: + "$(this.promise_filename) FAIL"; + + !ok.DEBUG:: + "error_output was: '$(error_output)'"; + "vars_output was: '$(vars_output)'"; +} diff --git a/tests/acceptance/00_basics/06_host_specific_data/16-variable-references-good-entry-survives.cf.json b/tests/acceptance/00_basics/06_host_specific_data/16-variable-references-good-entry-survives.cf.json new file mode 100644 index 00000000000..5f225ddcc86 --- /dev/null +++ b/tests/acceptance/00_basics/06_host_specific_data/16-variable-references-good-entry-survives.cf.json @@ -0,0 +1,6 @@ +{ + "vars": { + "good_key": "a literal value", + "key_with_unresolved_value": "$(sys.workdir)" + } +}