From 09d8d9f1f4b3c22d2131a5d0e1b109cb6367fdbd Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Wed, 30 Sep 2026 08:36:23 -0400 Subject: [PATCH 1/2] Shared the reduction of recorded package operations DiffPkgOperations() now renders its messages from the reduced records at print time, so that the reduction can be reused by other renderers. Changelog: None Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Opus 5.5 --- cf-agent/simulate_mode.c | 98 ++++++++++++++++++++++++++++++++-------- 1 file changed, 80 insertions(+), 18 deletions(-) diff --git a/cf-agent/simulate_mode.c b/cf-agent/simulate_mode.c index 77ed2c84c23..b8b2937e3e7 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -669,6 +669,35 @@ static void PkgOperationRecordDestroy(PkgOperationRecord *pkg_op) } } +typedef struct PkgOperation_ { + char *pkg_name; + char *pkg_arch; + char *pkg_ver; +} PkgOperation; + +static PkgOperation *PkgOperationNew(const char *pkg_name, + const char *pkg_arch, + const char *pkg_ver) +{ + PkgOperation *ret = xmalloc(sizeof(PkgOperation)); + ret->pkg_name = SafeStringDuplicate(pkg_name); + ret->pkg_arch = SafeStringDuplicate(pkg_arch); + ret->pkg_ver = SafeStringDuplicate(pkg_ver); + + return ret; +} + +static void PkgOperationDestroy(PkgOperation *pkg_op) +{ + if (pkg_op != NULL) + { + free(pkg_op->pkg_name); + free(pkg_op->pkg_arch); + free(pkg_op->pkg_ver); + free(pkg_op); + } +} + static inline bool PkgVersionIsGreater(const char *ver1, const char *ver2) { /* Empty/missing versions should be handled separately based on the @@ -727,12 +756,20 @@ static inline char *GetPkgOperationMsg(ChrootPkgOperationCode op, const char *pk return msg; } -bool DiffPkgOperations() +/* Reduces the recorded package operations to the net sets of packages that + * would be installed and removed (maps of PkgOperation), or sets both to NULL + * if no package operations were recorded. */ +static bool CollectPkgOperations(Map **installed_out, Map **removed_out) { + assert(installed_out != NULL); + assert(removed_out != NULL); + + *installed_out = NULL; + *removed_out = NULL; + const char *pkgs_ops_csv_file = ToChangesChroot(CHROOT_PKGS_OPS_FILE); if (access(pkgs_ops_csv_file, F_OK) != 0) { - Log(LOG_LEVEL_INFO, "No package operations done by the agent run"); return true; } @@ -744,8 +781,8 @@ bool DiffPkgOperations() return false; } - Map *installed = MapNew(StringHash_untyped, StringEqual_untyped, free, (MapDestroyDataFn) PkgOperationRecordDestroy); - Map *removed = MapNew(StringHash_untyped, StringEqual_untyped, free, (MapDestroyDataFn) PkgOperationRecordDestroy); + Map *installed = MapNew(StringHash_untyped, StringEqual_untyped, free, (MapDestroyDataFn) PkgOperationDestroy); + Map *removed = MapNew(StringHash_untyped, StringEqual_untyped, free, (MapDestroyDataFn) PkgOperationDestroy); char *line; while ((line = GetCsvLineNext(csv_file)) != NULL) { @@ -804,7 +841,7 @@ bool DiffPkgOperations() } else { - PkgOperationRecord *record = MapGet(installed, name_arch); + PkgOperation *record = MapGet(installed, name_arch); if ((record != NULL) && StringEqual(pkg_ver, record->pkg_ver)) { /* Matching version being removed -> cancel the installation */ @@ -826,12 +863,11 @@ bool DiffPkgOperations() /* Package installation cancels a previous removal (if any). */ MapRemove(removed, name_arch); - PkgOperationRecord *prev_record = MapGet(installed, name_arch); + PkgOperation *prev_record = MapGet(installed, name_arch); if ((prev_record == NULL) || PkgVersionIsGreater(pkg_ver, prev_record->pkg_ver)) { - char *msg = GetPkgOperationMsg(CHROOT_PKG_OPERATION_CODE_INSTALL, - pkg_name, pkg_arch, pkg_ver); - PkgOperationRecord *record = PkgOperationRecordNew(msg, SafeStringDuplicate(pkg_ver)); + PkgOperation *record = PkgOperationNew(pkg_name, pkg_arch, + pkg_ver); MapInsert(installed, name_arch, record); name_arch = NULL; /* name_arch is now owned by the map (as a key) */ } @@ -843,7 +879,7 @@ bool DiffPkgOperations() /* If there is a previous 'remove' operation record with version specified, prefer that * message over a new message without version specification as the net result would be * the package being removed, in the version that was pressent. */ - PkgOperationRecord *prev_record = MapGet(removed, name_arch); + PkgOperation *prev_record = MapGet(removed, name_arch); bool insert_new_msg = ((prev_record == NULL) || (NULL_OR_EMPTY(prev_record->pkg_ver))); /* If there is a previous 'install' operation and now there is a 'remove' operation it @@ -860,7 +896,7 @@ bool DiffPkgOperations() } else { - PkgOperationRecord *inst_record = MapGet(installed, name_arch); + PkgOperation *inst_record = MapGet(installed, name_arch); if ((inst_record != NULL) && (StringEqual(pkg_ver, inst_record->pkg_ver))) { MapRemove(installed, name_arch); @@ -874,9 +910,8 @@ bool DiffPkgOperations() if (insert_new_msg) { - char *msg = GetPkgOperationMsg(CHROOT_PKG_OPERATION_CODE_REMOVE, - pkg_name, pkg_arch, pkg_ver); - PkgOperationRecord *record = PkgOperationRecordNew(msg, SafeStringDuplicate(pkg_ver)); + PkgOperation *record = PkgOperationNew(pkg_name, pkg_arch, + pkg_ver); MapInsert(removed, name_arch, record); name_arch = NULL; /* name_arch is now owned by the map (as a key) */ } @@ -886,6 +921,27 @@ bool DiffPkgOperations() } fclose(csv_file); + *installed_out = installed; + *removed_out = removed; + + return true; +} + +bool DiffPkgOperations() +{ + Map *installed; + Map *removed; + if (!CollectPkgOperations(&installed, &removed)) + { + return false; + } + + if (installed == NULL) + { + Log(LOG_LEVEL_INFO, "No package operations done by the agent run"); + return true; + } + if ((MapSize(installed) == 0) && (MapSize(removed) == 0)) { Log(LOG_LEVEL_INFO, "No differences in installed packages to report"); @@ -901,16 +957,22 @@ bool DiffPkgOperations() MapKeyValue *item; while ((item = MapIteratorNext(&i))) { - PkgOperationRecord *value = item->value; - const char *msg = value->msg; + PkgOperation *value = item->value; + char *msg = GetPkgOperationMsg(CHROOT_PKG_OPERATION_CODE_INSTALL, + value->pkg_name, value->pkg_arch, + value->pkg_ver); puts(msg); + free(msg); } i = MapIteratorInit(removed); while ((item = MapIteratorNext(&i))) { - PkgOperationRecord *value = item->value; - const char *msg = value->msg; + PkgOperation *value = item->value; + char *msg = GetPkgOperationMsg(CHROOT_PKG_OPERATION_CODE_REMOVE, + value->pkg_name, value->pkg_arch, + value->pkg_ver); puts(msg); + free(msg); } MapDestroy(installed); From 480acaf52748e37c17fe5ae180d41b4c10153438 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Wed, 30 Sep 2026 08:42:34 -0400 Subject: [PATCH 2/2] Added --simulate-json option to write the simulated change set as JSON With --simulate-json=FILE, cf-agent also writes the files, renames and net package operations of a --simulate run to FILE as one JSON document. Changelog: Title Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Opus 5.5 --- cf-agent/cf-agent.c | 44 +++ cf-agent/simulate_mode.c | 371 ++++++++++++++++++ cf-agent/simulate_mode.h | 2 + .../29_simulate_mode/simulate_json.cf | 58 +++ .../simulate_json.cf.expected | 51 +++ .../29_simulate_mode/simulate_json.cf.sub | 41 ++ tests/unit/simulate_mode_test.c | 313 +++++++++++++++ 7 files changed, 880 insertions(+) create mode 100644 tests/acceptance/29_simulate_mode/simulate_json.cf create mode 100644 tests/acceptance/29_simulate_mode/simulate_json.cf.expected create mode 100644 tests/acceptance/29_simulate_mode/simulate_json.cf.sub diff --git a/cf-agent/cf-agent.c b/cf-agent/cf-agent.c index 4ffe508e72f..af7636887f2 100644 --- a/cf-agent/cf-agent.c +++ b/cf-agent/cf-agent.c @@ -112,6 +112,7 @@ static bool ALLCLASSESREPORT = false; /* GLOBAL_P */ static bool ALWAYS_VALIDATE = false; /* GLOBAL_P */ static bool CFPARANOID = false; /* GLOBAL_P */ static bool PERFORM_DB_CHECK = false; +static char *SIMULATE_JSON_FILE = NULL; static const Rlist *ACCESSLIST = NULL; /* GLOBAL_P */ @@ -221,6 +222,7 @@ static const struct option OPTIONS[] = {"skip-bootstrap-service-start", no_argument, 0, 0 }, {"skip-db-check", optional_argument, 0, 0 }, {"simulate", required_argument, 0, 0}, + {"simulate-json", required_argument, 0, 0}, {NULL, 0, 0, '\0'} }; @@ -257,6 +259,7 @@ static const char *const HINTS[] = "Do not start CFEngine services as part of the bootstrap process", "Do not run database integrity checks and repairs at startup", "Run in simulate mode, either 'manifest', 'manifest-full' or 'diff'", + "Write the change set of a --simulate run to the given file as JSON", NULL }; @@ -385,6 +388,19 @@ int main(int argc, char *argv[]) Nova_NoteAgentExecutionPerformance(config->input_file, start); + /* Before GenericAgentFinalize(), which deinitializes the crypto library + * needed for the file digests. */ + const bool failsafe_fallback = + (EvalContextClassGet(ctx, NULL, "failsafe_fallback") != NULL); + if ((SIMULATE_JSON_FILE != NULL) && + !WriteChangesJson(SIMULATE_JSON_FILE, failsafe_fallback)) + { + if (ret == 0) + { + ret = EXIT_FAILURE; + } + } + GenericAgentFinalize(ctx, config); StringSetDestroy(SINGLE_COPY_CACHE); @@ -795,6 +811,27 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv) DoCleanupAndExit(EXIT_FAILURE); } } + else if (StringEqual(option_name, "simulate-json")) + { + if (optarg == NULL) + { + Log(LOG_LEVEL_ERR, + "Missing argument for --simulate-json, a file path required"); + DoCleanupAndExit(EXIT_FAILURE); + } + else if (!IsAbsPath(optarg)) + { + Log(LOG_LEVEL_ERR, + "Invalid argument for --simulate-json, an absolute path required, not '%s'", + optarg); + DoCleanupAndExit(EXIT_FAILURE); + } + else + { + free(SIMULATE_JSON_FILE); + SIMULATE_JSON_FILE = xstrdup(optarg); + } + } break; } default: @@ -822,6 +859,13 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv) DoCleanupAndExit(EXIT_FAILURE); } + if ((SIMULATE_JSON_FILE != NULL) && !ChrootChanges()) + { + Log(LOG_LEVEL_ERR, + "Option --simulate-json can only be used together with --simulate"); + DoCleanupAndExit(EXIT_FAILURE); + } + FreeFixedStringArray(argc_new, argv_new); return config; diff --git a/cf-agent/simulate_mode.c b/cf-agent/simulate_mode.c index b8b2937e3e7..b6efbf34a26 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -41,6 +41,10 @@ #include /* StringMap */ #include /* GetCsvLineNext() */ #include /* GetGroupName(), GetUserName() */ +#include /* JsonElement */ +#include /* StringWriter() */ +#include /* HashFile(), HashPrintSafe() */ +#include /* CF_PERMS_DEFAULT */ #include @@ -1113,3 +1117,370 @@ bool ManifestPkgOperations() return true; } + + +#define CHANGES_JSON_FORMAT_VERSION 1 + +static JsonElement *ChangedFileAsJson(const char *path) +{ + assert(path != NULL); + + JsonElement *file_info = JsonObjectCreate(8); + JsonObjectAppendString(file_info, "path", path); + + const char *chrooted_path = ToChangesChroot(path); + struct stat st; + if (lstat(chrooted_path, &st) == -1) + { + JsonObjectAppendString(file_info, "change", "deleted"); + return file_info; + } + + struct stat st_orig; + if (lstat(path, &st_orig) == -1) + { + JsonObjectAppendString(file_info, "change", "created"); + } + else + { + JsonObjectAppendString(file_info, "change", "modified"); + } + + JsonObjectAppendString(file_info, "type", + GetFileTypeDescription(st.st_mode)); + + /* The permissions of a symbolic link are meaningless and differ between + * platforms. */ + if (!S_ISLNK(st.st_mode)) + { + char perms[5]; + xsnprintf(perms, sizeof(perms), "%04o", + st.st_mode & CHMOD_MODE_BITS); + JsonObjectAppendString(file_info, "permissions", perms); + } + + /* 64-bit, so that e.g. uid 4294967294 is not written as -2. */ + JsonObjectAppendInteger64(file_info, "uid", (int64_t) st.st_uid); + JsonObjectAppendInteger64(file_info, "gid", (int64_t) st.st_gid); + + if (S_ISREG(st.st_mode)) + { + JsonObjectAppendInteger64(file_info, "size", (int64_t) st.st_size); + + unsigned char digest[EVP_MAX_MD_SIZE + 1] = {0}; + HashFile(chrooted_path, digest, HASH_METHOD_SHA256, false); + + /* HashFile() cannot report failure, it leaves the digest zeroed. */ + const size_t digest_len = HashSizeFromId(HASH_METHOD_SHA256); + bool have_digest = false; + for (size_t i = 0; !have_digest && (i < digest_len); i++) + { + have_digest = (digest[i] != 0); + } + if (have_digest) + { + char digest_str[CF_HOSTKEY_STRING_SIZE]; + HashPrintSafe(digest_str, sizeof(digest_str), digest, + HASH_METHOD_SHA256, false); + JsonObjectAppendString(file_info, "sha256", digest_str); + } + else + { + Log(LOG_LEVEL_ERR, + "Failed to compute the SHA-256 digest of '%s'", + chrooted_path); + } + } +#ifndef __MINGW32__ + else if (S_ISLNK(st.st_mode)) + { + char target[PATH_MAX] = {0}; + ssize_t target_len = readlink(chrooted_path, target, + sizeof(target) - 1); + + /* Omit a possibly truncated target. */ + if ((target_len > 0) && ((size_t) target_len < (sizeof(target) - 1))) + { + const char *real_target = target; + if (IsAbsoluteFileName(target)) + { + real_target = ToNormalRoot(target); + } + JsonObjectAppendString(file_info, "target", real_target); + } + } +#endif /* !__MINGW32__ */ + + return file_info; +} + +static bool AddChangedFilesToJson(JsonElement *files) +{ + assert(files != NULL); + + const char *files_list_file = ToChangesChroot(CHROOT_CHANGES_LIST_FILE); + if (access(files_list_file, F_OK) != 0) + { + return true; + } + + int fd = safe_open(files_list_file, O_RDONLY); + if (fd == -1) + { + Log(LOG_LEVEL_ERR, + "Failed to open the file with list of changed files: %s", + GetErrorStr()); + return false; + } + + StringSet *recorded_files = StringSetNew(); + bool success = true; + bool done = false; + while (!done) + { + char *path; + int ret = ReadLenPrefixedString(fd, &path); + if (ret > 0) + { + if (!StringSetContains(recorded_files, path)) + { + JsonArrayAppendObject(files, ChangedFileAsJson(path)); + StringSetAdd(recorded_files, path); + } + else + { + free(path); + } + } + else if (ret == 0) + { + done = true; + } + else + { + Log(LOG_LEVEL_ERR, "Failed to read the list of changed files"); + success = false; + done = true; + } + } + close(fd); + StringSetDestroy(recorded_files); + return success; +} + +static bool AddRenamedFilesToJson(JsonElement *renames) +{ + assert(renames != NULL); + + const char *renames_file = ToChangesChroot(CHROOT_RENAMES_LIST_FILE); + if (access(renames_file, F_OK) != 0) + { + return true; + } + + int fd = safe_open(renames_file, O_RDONLY); + if (fd == -1) + { + Log(LOG_LEVEL_ERR, + "Failed to open the file with list of renamed files: %s", + GetErrorStr()); + return false; + } + + bool success = true; + bool done = false; + while (!done) + { + /* Pairs of the original and the new name, see + * RecordFileRenamedInChroot(). */ + char *orig_name; + int ret = ReadLenPrefixedString(fd, &orig_name); + if (ret > 0) + { + char *new_name; + ret = ReadLenPrefixedString(fd, &new_name); + if (ret > 0) + { + JsonElement *rename = JsonObjectCreate(2); + JsonObjectAppendString(rename, "old_name", orig_name); + JsonObjectAppendString(rename, "new_name", new_name); + JsonArrayAppendObject(renames, rename); + free(new_name); + } + else + { + Log(LOG_LEVEL_ERR, "Invalid data about renamed files"); + success = false; + done = true; + } + free(orig_name); + } + else if (ret == 0) + { + done = true; + } + else + { + Log(LOG_LEVEL_ERR, "Failed to read the list of renamed files"); + success = false; + done = true; + } + } + close(fd); + return success; +} + +static void JsonArrayAppendPkgOperations(JsonElement *packages, Map *pkg_ops, + const char *operation) +{ + assert(packages != NULL); + assert(pkg_ops != NULL); + + MapIterator i = MapIteratorInit(pkg_ops); + MapKeyValue *item; + while ((item = MapIteratorNext(&i))) + { + const PkgOperation *pkg_op = item->value; + JsonElement *op_info = JsonObjectCreate(4); + JsonObjectAppendString(op_info, "operation", operation); + JsonObjectAppendString(op_info, "name", pkg_op->pkg_name); + if (!NULL_OR_EMPTY(pkg_op->pkg_arch)) + { + JsonObjectAppendString(op_info, "architecture", pkg_op->pkg_arch); + } + if (!NULL_OR_EMPTY(pkg_op->pkg_ver)) + { + JsonObjectAppendString(op_info, "version", pkg_op->pkg_ver); + } + JsonArrayAppendObject(packages, op_info); + } +} + +static bool AddPkgOperationsToJson(JsonElement *packages) +{ + assert(packages != NULL); + + Map *installed; + Map *removed; + if (!CollectPkgOperations(&installed, &removed)) + { + return false; + } + + if (installed == NULL) + { + return true; + } + + JsonArrayAppendPkgOperations(packages, installed, "install"); + JsonArrayAppendPkgOperations(packages, removed, "remove"); + + MapDestroy(installed); + MapDestroy(removed); + + return true; +} + +bool WriteChangesJson(const char *output_file, bool failsafe_fallback) +{ + assert(output_file != NULL); + + const char *mode_str; + switch (EVAL_MODE) + { + case EVAL_MODE_SIMULATE_MANIFEST: + mode_str = "manifest"; + break; + case EVAL_MODE_SIMULATE_MANIFEST_FULL: + mode_str = "manifest-full"; + break; + case EVAL_MODE_SIMULATE_DIFF: + mode_str = "diff"; + break; + default: + debug_abort_if_reached(); + mode_str = "unknown"; + break; + } + + JsonElement *json = JsonObjectCreate(6); + JsonObjectAppendInteger(json, "format_version", + CHANGES_JSON_FORMAT_VERSION); + JsonObjectAppendString(json, "simulate_mode", mode_str); + JsonObjectAppendBool(json, "failsafe_fallback", failsafe_fallback); + + JsonElement *files = JsonArrayCreate(10); + bool success = AddChangedFilesToJson(files); + JsonObjectAppendArray(json, "files", files); + + JsonElement *renames = JsonArrayCreate(10); + success = success && AddRenamedFilesToJson(renames); + JsonObjectAppendArray(json, "renames", renames); + + JsonElement *packages = JsonArrayCreate(10); + success = success && AddPkgOperationsToJson(packages); + JsonObjectAppendArray(json, "packages", packages); + + if (!success) + { + JsonDestroy(json); + return false; + } + + Log(LOG_LEVEL_INFO, "Writing the simulated change set to '%s'", + output_file); + + Writer *writer = StringWriter(); + JsonWrite(writer, json, 0); + WriterWrite(writer, "\n"); + JsonDestroy(json); + char *document = StringWriterClose(writer); + + /* Write to a new file and rename() it into place, so that a failed write + * leaves any existing #output_file intact and a symbolic link at + * #output_file is replaced rather than followed. */ + char *tmp_file; + xasprintf(&tmp_file, "%s.%06d" CF_NEW, output_file, rand() % 1000000); + int fd = safe_open_create_perms(tmp_file, O_WRONLY | O_CREAT | O_EXCL, + CF_PERMS_DEFAULT); + if (fd == -1) + { + Log(LOG_LEVEL_ERR, + "Failed to open '%s' for writing the simulated change set (open: %s)", + tmp_file, GetErrorStr()); + free(tmp_file); + free(document); + return false; + } + + const size_t document_len = strlen(document); + success = (FullWrite(fd, document, document_len) + == (ssize_t) document_len); + free(document); + if (close(fd) == -1) + { + success = false; + } + if (!success) + { + Log(LOG_LEVEL_ERR, + "Failed to write the simulated change set to '%s' (write: %s)", + tmp_file, GetErrorStr()); + unlink(tmp_file); + free(tmp_file); + return false; + } + + if (rename(tmp_file, output_file) == -1) + { + Log(LOG_LEVEL_ERR, + "Failed to move the simulated change set to '%s' (rename: %s)", + output_file, GetErrorStr()); + unlink(tmp_file); + free(tmp_file); + return false; + } + free(tmp_file); + + return true; +} diff --git a/cf-agent/simulate_mode.h b/cf-agent/simulate_mode.h index 4e855813015..8eca34a9852 100644 --- a/cf-agent/simulate_mode.h +++ b/cf-agent/simulate_mode.h @@ -50,4 +50,6 @@ bool DiffChangedFiles(StringSet **audited_files); bool DiffPkgOperations(); bool ManifestPkgOperations(); +bool WriteChangesJson(const char *output_file, bool failsafe_fallback); + #endif /* _SIMULATE_H_ */ diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf b/tests/acceptance/29_simulate_mode/simulate_json.cf new file mode 100644 index 00000000000..e737e7817e3 --- /dev/null +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf @@ -0,0 +1,58 @@ +body common control +{ + inputs => { "../default.sub.cf" }; + bundlesequence => { default("$(this.promise_filename)") }; + version => "1.0"; +} + +bundle agent init +{ + files: + "$(G.testroot)/modify-me" + create => "true", + content => "before"; + + "$(G.testroot)/delete-me" create => "true"; + "$(G.testroot)/rename-me" create => "true"; + + "$(sys.workdir)/modules/packages/simulate_json" + create => "true", + perms => m("755"), + content => '#!/bin/sh +case "$1" in + supports-api-version) echo 1 ;; + get-package-data) echo PackageType=repo; echo Name=`cut -c6-` ;; + *) cat > /dev/null ;; +esac'; +} + +bundle agent test +{ + meta: + "test_soft_fail" + string => "(solaris|aix|hpux|windows)", + # ENT-6540 exotics fail to delete chroot + # ENT-10254 tests fail on Windows due to CRLF + meta => { "ENT-6540,ENT-10254" }; + + "description" + string => "Test that --simulate-json writes the change set as JSON"; + + commands: + "$(sys.cf_agent) -Kf $(this.promise_filename).sub --simulate=manifest --simulate-json=$(this.promise_filename).json.temp > $(this.promise_filename).prose.temp 2>&1" + contain => in_shell; + + "$(G.sed) -e 's,$(sys.workdir),WORKDIR,g' -e 's/\"uid\": [0-9]*/\"uid\": UID/' -e 's/\"gid\": [0-9]*/\"gid\": GID/' $(this.promise_filename).json.temp > $(this.promise_filename).actual" + contain => in_shell; +} + +bundle agent check +{ + methods: + "check" + usebundle => dcs_check_diff( + "$(this.promise_filename).actual", + "$(this.promise_filename).expected", + "$(this.promise_filename)" + ); +} diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf.expected b/tests/acceptance/29_simulate_mode/simulate_json.cf.expected new file mode 100644 index 00000000000..f36e00a75d6 --- /dev/null +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf.expected @@ -0,0 +1,51 @@ +{ + "failsafe_fallback": false, + "files": [ + { + "change": "created", + "gid": GID, + "path": "WORKDIR/tmp/create-me", + "permissions": "0600", + "sha256": "406effb1e9c59672c66a598c2b21e331b23b16c54024e96d6df3e7c173549791", + "size": 7, + "type": "regular file", + "uid": UID + }, + { + "change": "modified", + "gid": GID, + "path": "WORKDIR/tmp/modify-me", + "permissions": "0600", + "sha256": "f39592393ef0859cb196a52693d2cea00fb2df784b3c04ae54aa7cadb8e562f8", + "size": 5, + "type": "regular file", + "uid": UID + }, + { + "change": "deleted", + "path": "WORKDIR/tmp/delete-me" + }, + { + "change": "created", + "gid": GID, + "path": "WORKDIR/tmp/link", + "target": "WORKDIR/tmp/modify-me", + "type": "symbolic link", + "uid": UID + } + ], + "format_version": 1, + "packages": [ + { + "name": "simulate-json-pkg", + "operation": "install" + } + ], + "renames": [ + { + "new_name": "WORKDIR/tmp/renamed", + "old_name": "WORKDIR/tmp/rename-me" + } + ], + "simulate_mode": "manifest" +} diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf.sub b/tests/acceptance/29_simulate_mode/simulate_json.cf.sub new file mode 100644 index 00000000000..2557c37cf50 --- /dev/null +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf.sub @@ -0,0 +1,41 @@ +body common control +{ + inputs => { "../default.sub.cf" }; +} + +bundle agent main +{ + files: + "$(G.testroot)/create-me" + create => "true", + content => "created"; + "$(G.testroot)/modify-me" + content => "after"; + "$(G.testroot)/delete-me" + delete => tidy; + "$(G.testroot)/rename-me" + rename => to("renamed"); + "$(G.testroot)/link" + link_from => symlink("$(G.testroot)/modify-me"); + + packages: + "simulate-json-pkg" + policy => "present", + package_module => simulate_json; +} + +body rename to(name) +{ + newname => "$(name)"; +} + +body link_from symlink(target) +{ + source => "$(target)"; + link_type => "symlink"; +} + +body package_module simulate_json +{ + query_installed_ifelapsed => "0"; +} diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index 662e2fe0e57..308a5e619ab 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -27,10 +27,70 @@ #include /* SetChangesChroot(), ToChangesChroot() */ #include /* CHROOT_PKGS_OPS_FILE */ #include /* DeleteDirectoryTree() */ +#include /* MakeParentDirectory() */ +#include /* JsonParseFile() */ +#include /* WriteLenPrefixedString() */ +#include /* FileWriter() */ #include static char CHROOT_DIR[] = "/tmp/simulate_mode_test_chroot.XXXXXX"; +static char ORIG_DIR[] = "/tmp/simulate_mode_test_orig.XXXXXX"; +static char OUTPUT_FILE[PATH_MAX]; + +static void reset_records(void) +{ + unlink(ToChangesChroot(CHROOT_CHANGES_LIST_FILE)); + unlink(ToChangesChroot(CHROOT_RENAMES_LIST_FILE)); + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); + unlink(OUTPUT_FILE); +} + +static void write_records(const char *records_file, + const char *const records[], size_t n) +{ + FILE *file = fopen(ToChangesChroot(records_file), "w"); + assert_true(file != NULL); + Writer *writer = FileWriter(file); + for (size_t i = 0; i < n; i++) + { + assert_true(WriteLenPrefixedString(writer, records[i])); + } + WriterClose(writer); +} + +static void create_chroot_file(const char *path, const char *content, + mode_t mode) +{ + char chrooted[PATH_MAX]; + strlcpy(chrooted, ToChangesChroot(path), sizeof(chrooted)); + assert_true(MakeParentDirectory(path, true, NULL)); + + FILE *file = fopen(chrooted, "w"); + assert_true(file != NULL); + assert_true(fputs(content, file) >= 0); + fclose(file); + assert_int_equal(chmod(chrooted, mode), 0); +} + +static JsonElement *write_and_parse_changes(void) +{ + assert_true(WriteChangesJson(OUTPUT_FILE, false)); + + JsonElement *json = NULL; + assert_int_equal(JsonParseFile(OUTPUT_FILE, 1024 * 1024, &json), + JSON_PARSE_OK); + assert_true(json != NULL); + return json; +} + +/* Returns the only element of the #key array in #json. */ +static JsonElement *get_single(JsonElement *json, const char *key) +{ + JsonElement *array = JsonObjectGetAsArray(json, key); + assert_int_equal(JsonLength(array), 1); + return JsonArrayGetAsObject(array, 0); +} /* #csv contains "op,name,version,architecture" records terminated by "\r\n", * just like the records written by RecordPkgOperationInChroot(). */ @@ -101,23 +161,276 @@ static void test_manifest_install_cancels_removal(void) unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } +static void test_empty_change_set(void) +{ + reset_records(); + + JsonElement *json = write_and_parse_changes(); + assert_int_equal( + JsonPrimitiveGetAsInteger(JsonObjectGet(json, "format_version")), 1); + assert_string_equal(JsonObjectGetAsString(json, "simulate_mode"), + "manifest"); + assert_int_equal(JsonLength(JsonObjectGetAsArray(json, "files")), 0); + assert_int_equal(JsonLength(JsonObjectGetAsArray(json, "renames")), 0); + assert_int_equal(JsonLength(JsonObjectGetAsArray(json, "packages")), 0); + assert_true(JsonObjectGet(json, "failsafe_fallback") != NULL); + assert_false(JsonObjectGetAsBool(json, "failsafe_fallback")); + JsonDestroy(json); + + assert_true(WriteChangesJson(OUTPUT_FILE, true)); + assert_int_equal(JsonParseFile(OUTPUT_FILE, 1024 * 1024, &json), + JSON_PARSE_OK); + assert_true(JsonObjectGetAsBool(json, "failsafe_fallback")); + JsonDestroy(json); +} + +static void test_created_file(void) +{ + reset_records(); + + /* Recorded three times, reported once. */ + const char *const path = "/simulate-test/created \"file\" \\"; + const char *const paths[] = {path, path, path}; + create_chroot_file(path, "Hello, CFEngine!\n", 0640); + write_records(CHROOT_CHANGES_LIST_FILE, paths, 3); + + struct stat st; + assert_int_equal(lstat(ToChangesChroot(path), &st), 0); + + JsonElement *json = write_and_parse_changes(); + JsonElement *file_info = get_single(json, "files"); + assert_string_equal(JsonObjectGetAsString(file_info, "path"), path); + assert_string_equal(JsonObjectGetAsString(file_info, "change"), + "created"); + assert_string_equal(JsonObjectGetAsString(file_info, "type"), + "regular file"); + assert_string_equal(JsonObjectGetAsString(file_info, "permissions"), + "0640"); + assert_int_equal( + JsonPrimitiveGetAsInteger(JsonObjectGet(file_info, "uid")), + (long) st.st_uid); + assert_int_equal( + JsonPrimitiveGetAsInteger(JsonObjectGet(file_info, "gid")), + (long) st.st_gid); + assert_int_equal( + JsonPrimitiveGetAsInteger(JsonObjectGet(file_info, "size")), 17); + assert_string_equal( + JsonObjectGetAsString(file_info, "sha256"), + "9be7023e1f91bae9d1f734b49c579cc2091c71924ae7494c9a5a3a8006527615"); + JsonDestroy(json); +} + +static void test_deleted_file(void) +{ + reset_records(); + + const char *const path = "/simulate-test/deleted-file"; + write_records(CHROOT_CHANGES_LIST_FILE, &path, 1); + + JsonElement *json = write_and_parse_changes(); + JsonElement *file_info = get_single(json, "files"); + assert_string_equal(JsonObjectGetAsString(file_info, "path"), path); + assert_string_equal(JsonObjectGetAsString(file_info, "change"), + "deleted"); + assert_true(JsonObjectGet(file_info, "type") == NULL); + assert_true(JsonObjectGet(file_info, "sha256") == NULL); + JsonDestroy(json); +} + +static void test_modified_file(void) +{ + reset_records(); + + char path[PATH_MAX]; + xsnprintf(path, sizeof(path), "%s/modified-file", ORIG_DIR); + FILE *file = fopen(path, "w"); + assert_true(file != NULL); + assert_true(fputs("contents before the run\n", file) >= 0); + fclose(file); + + create_chroot_file(path, "new contents after the run\n", 0644); + const char *const paths[] = {path}; + write_records(CHROOT_CHANGES_LIST_FILE, paths, 1); + + /* The digest is of the contents after the run. */ + JsonElement *json = write_and_parse_changes(); + JsonElement *file_info = get_single(json, "files"); + assert_string_equal(JsonObjectGetAsString(file_info, "change"), + "modified"); + assert_string_equal( + JsonObjectGetAsString(file_info, "sha256"), + "6ba024f3c03f13f9a8c1bb444829640c68553009facb418bb4552dd7ddebe427"); + JsonDestroy(json); +} + +#ifndef __MINGW32__ +static void test_created_symlink(void) +{ + reset_records(); + + const char *const target = "/simulate-test/link-target"; + const char *const path = "/simulate-test/created-link"; + create_chroot_file(target, "", 0644); + + /* Links in the chroot point to chrooted paths. */ + char chrooted_target[PATH_MAX]; + strlcpy(chrooted_target, ToChangesChroot(target), + sizeof(chrooted_target)); + assert_int_equal(symlink(chrooted_target, ToChangesChroot(path)), 0); + write_records(CHROOT_CHANGES_LIST_FILE, &path, 1); + + JsonElement *json = write_and_parse_changes(); + JsonElement *file_info = get_single(json, "files"); + assert_string_equal(JsonObjectGetAsString(file_info, "type"), + "symbolic link"); + assert_string_equal(JsonObjectGetAsString(file_info, "target"), target); + assert_true(JsonObjectGet(file_info, "permissions") == NULL); + JsonDestroy(json); +} +#endif /* !__MINGW32__ */ + +static void test_renamed_file(void) +{ + reset_records(); + + const char *const names[] = {"/simulate-test/old", "/simulate-test/new"}; + write_records(CHROOT_RENAMES_LIST_FILE, names, 2); + + JsonElement *json = write_and_parse_changes(); + JsonElement *rename = get_single(json, "renames"); + assert_string_equal(JsonObjectGetAsString(rename, "old_name"), names[0]); + assert_string_equal(JsonObjectGetAsString(rename, "new_name"), names[1]); + JsonDestroy(json); +} + +static void check_pkg_operations(const char *csv, const char *operation, + const char *name, const char *version, + const char *arch) +{ + reset_records(); + write_pkgs_ops(csv); + + JsonElement *json = write_and_parse_changes(); + JsonElement *packages = JsonObjectGetAsArray(json, "packages"); + if (operation == NULL) + { + assert_int_equal(JsonLength(packages), 0); + JsonDestroy(json); + return; + } + + JsonElement *op_info = get_single(json, "packages"); + assert_string_equal(JsonObjectGetAsString(op_info, "operation"), + operation); + assert_string_equal(JsonObjectGetAsString(op_info, "name"), name); + if (version == NULL) + { + assert_true(JsonObjectGet(op_info, "version") == NULL); + } + else + { + assert_string_equal(JsonObjectGetAsString(op_info, "version"), + version); + } + if (arch == NULL) + { + assert_true(JsonObjectGet(op_info, "architecture") == NULL); + } + else + { + assert_string_equal(JsonObjectGetAsString(op_info, "architecture"), + arch); + } + JsonDestroy(json); +} + +static void test_pkg_operations(void) +{ + check_pkg_operations("i,pkg,1.0,x86_64\r\n", + "install", "pkg", "1.0", "x86_64"); + check_pkg_operations("r,pkg,,\r\n", "remove", "pkg", NULL, NULL); + + /* Net operations only. */ + check_pkg_operations("r,pkg,,\r\np,pkg,,\r\n", NULL, NULL, NULL, NULL); + check_pkg_operations("i,pkg,2.0,\r\na,pkg,2.0,\r\n", + NULL, NULL, NULL, NULL); + check_pkg_operations("r,pkg,,\r\ni,pkg,1.0,\r\n", + "install", "pkg", "1.0", NULL); +} + +static void test_write_failure(void) +{ + reset_records(); + + char bad_output[PATH_MAX]; + xsnprintf(bad_output, sizeof(bad_output), "%s/no-such-dir/out.json", + ORIG_DIR); + assert_false(WriteChangesJson(bad_output, false)); +} + +#ifndef __MINGW32__ +static void test_output_symlink_not_followed(void) +{ + reset_records(); + + char link_target[PATH_MAX]; + xsnprintf(link_target, sizeof(link_target), "%s/link-target", ORIG_DIR); + FILE *file = fopen(link_target, "w"); + assert_true(file != NULL); + assert_true(fputs("do not overwrite\n", file) >= 0); + fclose(file); + assert_int_equal(symlink(link_target, OUTPUT_FILE), 0); + + JsonDestroy(write_and_parse_changes()); + + struct stat st; + assert_int_equal(lstat(OUTPUT_FILE, &st), 0); + assert_true(S_ISREG(st.st_mode)); + + char buf[64] = {0}; + file = fopen(link_target, "r"); + assert_true(file != NULL); + assert_true(fread(buf, 1, sizeof(buf) - 1, file) > 0); + fclose(file); + assert_string_equal(buf, "do not overwrite\n"); +} +#endif /* !__MINGW32__ */ + int main() { PRINT_TEST_BANNER(); assert_true(mkdtemp(CHROOT_DIR) != NULL); SetChangesChroot(CHROOT_DIR); + assert_true(mkdtemp(ORIG_DIR) != NULL); + xsnprintf(OUTPUT_FILE, sizeof(OUTPUT_FILE), "%s/out.json", ORIG_DIR); + EVAL_MODE = EVAL_MODE_SIMULATE_MANIFEST; const UnitTest tests[] = { unit_test(test_diff_install_cancels_removal), unit_test(test_manifest_install_cancels_removal), + unit_test(test_empty_change_set), + unit_test(test_created_file), + unit_test(test_deleted_file), + unit_test(test_modified_file), +#ifndef __MINGW32__ + unit_test(test_created_symlink), +#endif + unit_test(test_renamed_file), + unit_test(test_pkg_operations), + unit_test(test_write_failure), +#ifndef __MINGW32__ + unit_test(test_output_symlink_not_followed), +#endif }; int ret = run_tests(tests); DeleteDirectoryTree(CHROOT_DIR); rmdir(CHROOT_DIR); + DeleteDirectoryTree(ORIG_DIR); + rmdir(ORIG_DIR); return ret; }