Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions libpromises/cmdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,23 @@ static bool AddCMDBVariable(EvalContext *ctx, const char *key, const VarRef *ref
bool ret;
if (JsonGetElementType(data) == JSON_ELEMENT_TYPE_PRIMITIVE)
{
char *value = JsonPrimitiveToString(data);
Log(LOG_LEVEL_VERBOSE, "Installing CMDB variable '%s:%s.%s=%s'",
ref->ns, ref->scope, key, value);
ret = EvalContextVariablePutTagsSetWithComment(ctx, ref, value, CF_DATA_TYPE_STRING,
tags, comment);
free(value);
if (JsonGetPrimitiveType(data) == JSON_PRIMITIVE_TYPE_NULL)
{
/* JsonPrimitiveToString() returns NULL for JSON null and there is
* no scalar to install in its place. */
Log(LOG_LEVEL_ERR, "Invalid value in '%s' variable specification in CMDB data"
" (null is not a supported value)", key);
ret = false;
}
else
{
char *value = JsonPrimitiveToString(data);
Log(LOG_LEVEL_VERBOSE, "Installing CMDB variable '%s:%s.%s=%s'",
ref->ns, ref->scope, key, value);
ret = EvalContextVariablePutTagsSetWithComment(ctx, ref, value, CF_DATA_TYPE_STRING,
tags, comment);
free(value);
}
}
else if ((JsonGetType(data) == JSON_TYPE_ARRAY) &&
JsonArrayContainsOnlyPrimitives(data))
Expand Down
61 changes: 61 additions & 0 deletions tests/acceptance/00_basics/06_host_specific_data/13-null-values.cf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# test of the host_specific.json functionality: JSON null values are rejected
# per entry, the rest of the CMDB data is still installed (used to crash);
# lists containing null keep installing, with the nulls skipped
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:
"command"
string => "$(sys.cf_promises) --show-vars -f $(sys.inputdir)/promises.cf | $(G.grep) -E 'CMDB data|value_from_|null_'";

# The offending entries are reported in the order they appear in the CMDB
# data, the remaining variables are installed and shown by --show-vars;
# lists with a null still install with the nulls skipped (RlistFromContainer()
# drops them), exactly as before the null values were rejected.
"expected"
string => concat(
"\s*error: Invalid value in 'null_scalar'.*null is not a supported value.*",
"\s+error: Invalid value in 'null_value'.*null is not a supported value.*",
"\s+error: Invalid value in 'null_bare'.*null is not a supported value.*",
"\s+data:variables\.good_variables\s+value_from_variables\s+source=cmdb",
"\s+data:variables\.good_vars\s+value_from_vars\s+source=cmdb",
"\s+data:variables\.null_container\s+\{\s*\"nested\":\s*null\s*\}\s+source=cmdb",
"\s+data:variables\.null_list\s+\{\"a\"\}\s+source=cmdb",
"\s+data:variables\.null_value_list\s+\{\"a\"\}\s+source=cmdb\s*"
);

methods:
""
usebundle => dcs_passif_output(
$(expected), "", $(command), $(this.promise_filename)
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"vars": {
"null_scalar": null,
"null_list": ["a", null],
"good_vars": "value_from_vars",
"null_container": { "nested": null }
},
"variables": {
"null_value": { "value": null },
"null_bare": null,
"null_value_list": { "value": ["a", null] },
"good_variables": { "value": "value_from_variables" }
}
}
Loading