1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
|
/*
Copyright 2024 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <platform.h>
#include <unistd.h>
#include <json.h>
#include <set.h> /* StringSet */
#include <string_lib.h>
#include <known_dirs.h> /* GetDataDir() */
#include <var_expressions.h> /* VarRef, StringContainsUnresolved() */
#include <eval_context.h> /* EvalContext*() */
#include <files_names.h> /* JoinPaths() */
#include <cmdb.h>
#define HOST_SPECIFIC_DATA_FILE "host_specific.json"
#define HOST_SPECIFIC_DATA_MAX_SIZE (5 * 1024 * 1024) /* maximum size of the host-specific.json file */
#define CMDB_NAMESPACE "data"
#define CMDB_VARIABLES_TAGS "tags"
#define CMDB_VARIABLES_DATA "value"
#define CMDB_CLASSES_TAGS "tags"
#define CMDB_CLASSES_CLASS_EXPRESSIONS "class_expressions"
#define CMDB_CLASSES_REGULAR_EXPRESSIONS "regular_expressions"
#define CMDB_COMMENT_KEY "comment"
JsonElement *ReadJsonFile(const char *filename, LogLevel log_level, size_t size_max)
{
assert(filename != NULL);
JsonElement *doc = NULL;
JsonParseError err = JsonParseFile(filename, size_max, &doc);
if (err == JSON_PARSE_ERROR_NO_SUCH_FILE)
{
Log(log_level, "Could not open JSON file %s", filename);
return NULL;
}
if (err != JSON_PARSE_OK ||
doc == NULL)
{
Log(log_level, "Could not parse JSON file %s: %s", filename, JsonParseErrorToString(err));
}
return doc;
}
static bool CheckPrimitiveForUnexpandedVars(JsonElement *primitive, ARG_UNUSED void *data)
{
assert(JsonGetElementType(primitive) == JSON_ELEMENT_TYPE_PRIMITIVE);
/* Stop the iteration if a variable expression is found. */
return (!StringContainsUnresolved(JsonPrimitiveGetAsString(primitive)));
}
static bool CheckObjectForUnexpandedVars(JsonElement *object, ARG_UNUSED void *data)
{
assert(JsonGetType(object) == JSON_TYPE_OBJECT);
/* Stop the iteration if a variable expression is found among children
* keys. (elements inside the object are checked separately) */
JsonIterator iter = JsonIteratorInit(object);
while (JsonIteratorHasMore(&iter))
{
const char *key = JsonIteratorNextKey(&iter);
if (StringContainsUnresolved(key))
{
return false;
}
}
return true;
}
static VarRef *GetCMDBVariableRef(const char *key)
{
VarRef *ref = VarRefParse(key);
if (ref->ns == NULL)
{
ref->ns = xstrdup(CMDB_NAMESPACE);
}
else
{
if (ref->scope == NULL)
{
Log(LOG_LEVEL_ERR, "Invalid variable specification in CMDB data: '%s'"
" (bundle name has to be specified if namespace is specified)", key);
VarRefDestroy(ref);
return NULL;
}
}
if (ref->scope == NULL)
{
ref->scope = xstrdup("variables");
}
return ref;
}
static bool AddCMDBVariable(EvalContext *ctx, const char *key, const VarRef *ref,
JsonElement *data, StringSet *tags, const char *comment)
{
assert(ctx != NULL);
assert(key != NULL);
assert(ref != NULL);
assert(data != NULL);
assert(tags != NULL);
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);
}
else if ((JsonGetType(data) == JSON_TYPE_ARRAY) &&
JsonArrayContainsOnlyPrimitives(data))
{
// map to slist if the data only has primitives
Log(LOG_LEVEL_VERBOSE, "Installing CMDB slist variable '%s:%s.%s'",
ref->ns, ref->scope, key);
Rlist *data_rlist = RlistFromContainer(data);
ret = EvalContextVariablePutTagsSetWithComment(ctx, ref,
data_rlist, CF_DATA_TYPE_STRING_LIST,
tags, comment);
RlistDestroy(data_rlist);
}
else
{
// install as a data container
Log(LOG_LEVEL_VERBOSE, "Installing CMDB data container variable '%s:%s.%s'",
ref->ns, ref->scope, key);
ret = EvalContextVariablePutTagsSetWithComment(ctx, ref,
data, CF_DATA_TYPE_CONTAINER,
tags, comment);
}
if (!ret)
{
/* On success, EvalContextVariablePutTagsSet() consumes the tags set,
* otherwise, we shall destroy it. */
StringSetDestroy(tags);
}
return ret;
}
static bool ReadCMDBVars(EvalContext *ctx, JsonElement *vars)
{
assert(vars != NULL);
if (JsonGetType(vars) != JSON_TYPE_OBJECT)
{
Log(LOG_LEVEL_ERR, "Invalid 'vars' CMDB data, must be a JSON object");
return false;
}
if (!JsonWalk(vars, CheckObjectForUnexpandedVars, NULL, CheckPrimitiveForUnexpandedVars, NULL))
{
Log(LOG_LEVEL_ERR, "Invalid 'vars' CMDB data, cannot contain variable references");
return false;
}
JsonIterator iter = JsonIteratorInit(vars);
while (JsonIteratorHasMore(&iter))
{
const char *key = JsonIteratorNextKey(&iter);
JsonElement *data = JsonObjectGet(vars, key);
VarRef *ref = GetCMDBVariableRef(key);
if (ref == NULL)
{
continue;
}
StringSet *tags = StringSetNew();
StringSetAdd(tags, xstrdup(CMDB_SOURCE_TAG));
bool ret = AddCMDBVariable(ctx, key, ref, data, tags, NULL);
VarRefDestroy(ref);
if (!ret)
{
/* Details should have been logged already. */
Log(LOG_LEVEL_ERR, "Failed to add CMDB variable '%s'", key);
}
}
return true;
}
static StringSet *GetTagsFromJsonTags(const char *item_type,
const char *key,
const JsonElement *json_tags,
const char *default_tag)
{
StringSet *tags = NULL;
if (JSON_NOT_NULL(json_tags))
{
if ((JsonGetType(json_tags) != JSON_TYPE_ARRAY) ||
(!JsonArrayContainsOnlyPrimitives((JsonElement*) json_tags)))
{
Log(LOG_LEVEL_ERR,
"Invalid json_tags information for %s '%s' in CMDB data:"
" must be a JSON array of strings",
item_type, key);
}
else
{
tags = JsonArrayToStringSet(json_tags);
if (tags == NULL)
{
Log(LOG_LEVEL_ERR,
"Invalid json_tags information %s '%s' in CMDB data:"
" must be a JSON array of strings",
item_type, key);
}
}
}
if (tags == NULL)
{
tags = StringSetNew();
}
StringSetAdd(tags, xstrdup(default_tag));
return tags;
}
static inline const char *GetCMDBComment(const char *item_type, const char *identifier,
const JsonElement *json_object)
{
assert(JsonGetType(json_object) == JSON_TYPE_OBJECT);
JsonElement *json_comment = JsonObjectGet(json_object, CMDB_COMMENT_KEY);
if (NULL_JSON(json_comment))
{
return NULL;
}
if (JsonGetType(json_comment) != JSON_TYPE_STRING)
{
Log(LOG_LEVEL_ERR,
"Invalid type of the 'comment' field for the '%s' %s in CMDB data, must be a string",
identifier, item_type);
return NULL;
}
return JsonPrimitiveGetAsString(json_comment);
}
/** Uses the new format allowing metadata (CFE-3633) */
static bool ReadCMDBVariables(EvalContext *ctx, JsonElement *variables)
{
assert(variables != NULL);
if (JsonGetType(variables) != JSON_TYPE_OBJECT)
{
Log(LOG_LEVEL_ERR, "Invalid 'variables' CMDB data, must be a JSON object");
return false;
}
if (!JsonWalk(variables, CheckObjectForUnexpandedVars, NULL, CheckPrimitiveForUnexpandedVars, NULL))
{
Log(LOG_LEVEL_ERR, "Invalid 'variables' CMDB data, cannot contain variable references");
return false;
}
JsonIterator iter = JsonIteratorInit(variables);
while (JsonIteratorHasMore(&iter))
{
const char *key = JsonIteratorNextKey(&iter);
VarRef *ref = GetCMDBVariableRef(key);
if (ref == NULL)
{
continue;
}
JsonElement *const var_info = JsonObjectGet(variables, key);
JsonElement *data;
StringSet *tags;
const char *comment = NULL;
if (JsonGetType(var_info) == JSON_TYPE_OBJECT)
{
data = JsonObjectGet(var_info, CMDB_VARIABLES_DATA);
if (data == NULL)
{
Log(LOG_LEVEL_ERR, "Missing value in '%s' variable specification in CMDB data (value field is required)", key);
VarRefDestroy(ref);
continue;
}
JsonElement *json_tags = JsonObjectGet(var_info, CMDB_VARIABLES_TAGS);
tags = GetTagsFromJsonTags("variable", key, json_tags, CMDB_SOURCE_TAG);
comment = GetCMDBComment("variable", key, var_info);
}
else
{
// Just a bare value, like in "vars", no metadata
data = var_info;
tags = GetTagsFromJsonTags("variable", key, NULL, CMDB_SOURCE_TAG);
}
assert(tags != NULL);
assert(data != NULL);
bool ret = AddCMDBVariable(ctx, key, ref, data, tags, comment);
VarRefDestroy(ref);
if (!ret)
{
/* Details should have been logged already. */
Log(LOG_LEVEL_ERR, "Failed to add CMDB variable '%s'", key);
}
}
return true;
}
static bool AddCMDBClass(EvalContext *ctx, const char *key, StringSet *tags, const char *comment)
{
assert(ctx != NULL);
assert(key != NULL);
assert(tags != NULL);
bool ret;
Log(LOG_LEVEL_VERBOSE, "Installing CMDB class '%s'", key);
if (strchr(key, ':') != NULL)
{
char *ns_class_name = xstrdup(key);
char *sep = strchr(ns_class_name, ':');
*sep = '\0';
key = sep + 1;
ret = EvalContextClassPutSoftNSTagsSetWithComment(ctx, ns_class_name, key,
CONTEXT_SCOPE_NAMESPACE, tags, comment);
free(ns_class_name);
}
else
{
ret = EvalContextClassPutSoftNSTagsSetWithComment(ctx, CMDB_NAMESPACE, key,
CONTEXT_SCOPE_NAMESPACE, tags, comment);
}
if (!ret)
{
/* On success, EvalContextClassPutSoftNSTagsSetWithComment() consumes
* the tags set, otherwise, we shall destroy it. */
StringSetDestroy(tags);
}
return ret;
}
static bool ReadCMDBClasses(EvalContext *ctx, JsonElement *classes)
{
assert(classes != NULL);
if (JsonGetType(classes) != JSON_TYPE_OBJECT)
{
Log(LOG_LEVEL_ERR, "Invalid 'classes' CMDB data, must be a JSON object");
return false;
}
if (!JsonWalk(classes, CheckObjectForUnexpandedVars, NULL, CheckPrimitiveForUnexpandedVars, NULL))
{
Log(LOG_LEVEL_ERR, "Invalid 'classes' CMDB data, cannot contain variable references");
return false;
}
JsonIterator iter = JsonIteratorInit(classes);
while (JsonIteratorHasMore(&iter))
{
const char *key = JsonIteratorNextKey(&iter);
JsonElement *data = JsonObjectGet(classes, key);
if (JsonGetElementType(data) == JSON_ELEMENT_TYPE_PRIMITIVE)
{
const char *expr = JsonPrimitiveGetAsString(data);
if (!StringEqual(expr, "any::"))
{
Log(LOG_LEVEL_ERR,
"Invalid class specification '%s' in CMDB data, only \"any::\" allowed", expr);
continue;
}
StringSet *default_tags = StringSetNew();
StringSetAdd(default_tags, xstrdup(CMDB_SOURCE_TAG));
bool ret = AddCMDBClass(ctx, key, default_tags, NULL);
if (!ret)
{
/* It does not have to be and error, it could be a result of
* the class already existing. Anyways, details about
* potential errors should have been logged already. */
Log(LOG_LEVEL_DEBUG, "Did not add CMDB class '%s'", key);
}
}
else if (JsonGetContainerType(data) == JSON_CONTAINER_TYPE_ARRAY &&
JsonArrayContainsOnlyPrimitives(data))
{
switch (JsonLength(data))
{
case 0:
Log(LOG_LEVEL_ERR,
"Empty class specification '[]' in CMDB data not allowed, only '[\"any::\"]'");
continue;;
case 1:
if (!StringEqual(JsonPrimitiveGetAsString(JsonArrayGet(data, 0)), "any::"))
{
Log(LOG_LEVEL_ERR,
"Invalid class specification '[\"%s\"]' in CMDB data, only '[\"any::\"]' allowed",
JsonPrimitiveGetAsString(JsonArrayGet(data, 0)));
continue;
}
// All good :)
break;
default:
{
Writer *const str = StringWriter();
JsonWriteCompact(str, data);
Log(LOG_LEVEL_ERR,
"Too many elements in class specification '%s' in CMDB data, only '[\"any::\"]' allowed",
StringWriterData(str));
WriterClose(str);
}
continue;
}
StringSet *default_tags = StringSetNew();
StringSetAdd(default_tags, xstrdup(CMDB_SOURCE_TAG));
bool ret = AddCMDBClass(ctx, key, default_tags, NULL);
if (!ret)
{
/* It does not have to be and error, it could be a result of
* the class already existing. Anyways, details about
* potential errors should have been logged already. */
Log(LOG_LEVEL_DEBUG, "Did not add CMDB class '%s'", key);
}
}
else if (JsonGetContainerType(data) == JSON_CONTAINER_TYPE_OBJECT)
{
const JsonElement *class_exprs = JsonObjectGet(data, CMDB_CLASSES_CLASS_EXPRESSIONS);
const JsonElement *reg_exprs = JsonObjectGet(data, CMDB_CLASSES_REGULAR_EXPRESSIONS);
const JsonElement *json_tags = JsonObjectGet(data, CMDB_CLASSES_TAGS);
if (JSON_NOT_NULL(class_exprs) &&
(JsonGetType(class_exprs) != JSON_TYPE_ARRAY ||
JsonLength(class_exprs) > 1 ||
(JsonLength(class_exprs) == 1 &&
!StringEqual(JsonPrimitiveGetAsString(JsonArrayGet(class_exprs, 0)), "any::"))))
{
Log(LOG_LEVEL_ERR,
"Invalid class expression rules for class '%s' in CMDB data,"
" only '[]' or '[\"any::\"]' allowed", key);
continue;
}
if (JSON_NOT_NULL(reg_exprs) &&
(JsonGetType(reg_exprs) != JSON_TYPE_ARRAY ||
JsonLength(reg_exprs) > 1 ||
(JsonLength(reg_exprs) == 1 &&
!StringEqual(JsonPrimitiveGetAsString(JsonArrayGet(reg_exprs, 0)), "any"))))
{
Log(LOG_LEVEL_ERR,
"Invalid regular expression rules for class '%s' in CMDB data,"
" only '[]' or '[\"any\"]' allowed", key);
continue;
}
StringSet *tags = GetTagsFromJsonTags("class", key, json_tags, CMDB_SOURCE_TAG);
const char *comment = GetCMDBComment("class", key, data);
bool ret = AddCMDBClass(ctx, key, tags, comment);
if (!ret)
{
/* It does not have to be and error, it could be a result of
* the class already existing. Anyways, details about
* potential errors should have been logged already. */
Log(LOG_LEVEL_DEBUG, "Did not add CMDB class '%s'", key);
}
}
else
{
Log(LOG_LEVEL_ERR, "Invalid CMDB class data for class '%s'", key);
}
}
return true;
}
bool LoadCMDBData(EvalContext *ctx)
{
char file_path[PATH_MAX] = {0};
strncpy(file_path, GetDataDir(), sizeof(file_path) - 1);
JoinPaths(file_path, sizeof(file_path), HOST_SPECIFIC_DATA_FILE);
if (access(file_path, F_OK) != 0)
{
Log(LOG_LEVEL_VERBOSE, "No host-specific JSON data available at '%s'", file_path);
return true; /* not an error */
}
if (access(file_path, R_OK) != 0)
{
Log(LOG_LEVEL_ERR, "Cannot read host-spefic JSON data from '%s'",
file_path);
return false;
}
JsonElement *data = ReadJsonFile(file_path, LOG_LEVEL_ERR, HOST_SPECIFIC_DATA_MAX_SIZE);
if (data == NULL)
{
/* Details are logged by ReadJsonFile() */
return false;
}
if (JsonGetType(data) != JSON_TYPE_OBJECT)
{
Log(LOG_LEVEL_ERR, "Invalid CMDB contents in '%s', must be a JSON object", file_path);
JsonDestroy(data);
return false;
}
Log(LOG_LEVEL_VERBOSE, "Loaded CMDB data file '%s', installing contents", file_path);
JsonIterator iter = JsonIteratorInit(data);
while (JsonIteratorHasMore(&iter))
{
const char *key = JsonIteratorNextKey(&iter);
/* Only vars and classes allowed in CMDB data */
if (!IsStrIn(key, (const char*[4]){"vars", "classes", "variables", NULL}))
{
Log(LOG_LEVEL_WARNING, "Invalid key '%s' in the CMDB data file '%s', skipping it",
key, file_path);
}
}
bool success = true;
JsonElement *vars = JsonObjectGet(data, "vars");
if (JSON_NOT_NULL(vars) && !ReadCMDBVars(ctx, vars))
{
success = false;
}
/* Uses the new format allowing metadata (CFE-3633) */
JsonElement *variables = JsonObjectGet(data, "variables");
if (JSON_NOT_NULL(variables) && !ReadCMDBVariables(ctx, variables))
{
success = false;
}
JsonElement *classes = JsonObjectGet(data, "classes");
if (JSON_NOT_NULL(classes) && !ReadCMDBClasses(ctx, classes))
{
success = false;
}
JsonDestroy(data);
return success;
}
|