From a5a6b6b6a51a40db2085073af6ddf96854ce4cfc Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Wed, 30 Sep 2026 12:29:08 -0400 Subject: [PATCH 1/6] Fixed simulate mode swapping package version and architecture RecordPkgOperationInChroot() wrote the architecture before the version, the reverse of the order its readers expect. Ticket: none Changelog: Title Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Fable 5.1 --- libpromises/changes_chroot.c | 3 ++- tests/unit/simulate_mode_test.c | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/libpromises/changes_chroot.c b/libpromises/changes_chroot.c index 82fd1ff30bc..bb81cf19bea 100644 --- a/libpromises/changes_chroot.c +++ b/libpromises/changes_chroot.c @@ -297,10 +297,11 @@ bool RecordPkgOperationInChroot(const char *op, const char *name, const char *ve return false; } + /* Record format: op,name,version,architecture */ CsvWriterField(csv_writer, op); CsvWriterField(csv_writer, name); - CsvWriterField(csv_writer, NULL_TO_EMPTY_STRING(arch)); CsvWriterField(csv_writer, NULL_TO_EMPTY_STRING(version)); + CsvWriterField(csv_writer, NULL_TO_EMPTY_STRING(arch)); CsvWriterNewRecord(csv_writer); CsvWriterClose(csv_writer); diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index 662e2fe0e57..eb0aa4eb093 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -87,6 +87,21 @@ static void test_diff_install_cancels_removal(void) unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } +/* A package recorded with both a version and an architecture must be + * reported with each in its own place. */ +static void test_diff_version_and_architecture(void) +{ + assert_true(RecordPkgOperationInChroot(CHROOT_PKG_OPERATION_INSTALL, + "foo", "1.2.3", "x86_64")); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(strstr(output, "Package 'foo-x86_64 [1.2.3]' would be installed") != NULL); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + static void test_manifest_install_cancels_removal(void) { write_pkgs_ops("r,foo,,\r\n" @@ -111,6 +126,7 @@ int main() const UnitTest tests[] = { unit_test(test_diff_install_cancels_removal), + unit_test(test_diff_version_and_architecture), unit_test(test_manifest_install_cancels_removal), }; From 2cf0b678d0938b88028efbbaa55cdd851ba0fffc Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Wed, 30 Sep 2026 14:40:26 -0400 Subject: [PATCH 2/6] Fixed simulate diff mode not reporting a removal with a version A recorded removal with a version was only reported when it cancelled a recorded installation of the same version. Ticket: none Changelog: Title Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Fable 5.1 --- cf-agent/simulate_mode.c | 2 +- tests/unit/simulate_mode_test.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/cf-agent/simulate_mode.c b/cf-agent/simulate_mode.c index 77ed2c84c23..b70274a9c13 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -865,7 +865,7 @@ bool DiffPkgOperations() { MapRemove(installed, name_arch); } - else + else if (inst_record != NULL) { /* Keeping the install message, the removal would make no change. */ insert_new_msg = false; diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index eb0aa4eb093..5212959ec55 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -102,6 +102,36 @@ static void test_diff_version_and_architecture(void) unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } +/* A removal with a version and no install record to cancel is a removal to + * report. */ +static void test_diff_removal_with_version(void) +{ + write_pkgs_ops("r,foo,1.0,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(strstr(output, "Package 'foo [1.0]' would be removed") != NULL); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* A removal with a version that differs from the recorded installation would + * fail, so only the installation is reported. */ +static void test_diff_removal_with_other_version_keeps_install(void) +{ + write_pkgs_ops("i,foo,2.0,\r\n" + "r,foo,1.0,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(strstr(output, "Package 'foo [2.0]' would be installed") != NULL); + assert_true(strstr(output, "would be removed") == NULL); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + static void test_manifest_install_cancels_removal(void) { write_pkgs_ops("r,foo,,\r\n" @@ -127,6 +157,8 @@ int main() { unit_test(test_diff_install_cancels_removal), unit_test(test_diff_version_and_architecture), + unit_test(test_diff_removal_with_version), + unit_test(test_diff_removal_with_other_version_keeps_install), unit_test(test_manifest_install_cancels_removal), }; From e383c766543a4e8cd6130f83d54f2973e9bedf6f Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Thu, 8 Oct 2026 23:29:11 -0400 Subject: [PATCH 3/6] Fixed simulate diff mode aborting on repeated unversioned package installs DiffPkgOperations() compared the versions of two recorded installations of the same package without checking that both were set, which PkgVersionIsGreater() asserts. The diff report now keeps the record the way the manifest report does: a versioned record over an unversioned one, otherwise the higher version, otherwise the first one recorded. Ticket: none Changelog: Title Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Fable 5.1 --- cf-agent/simulate_mode.c | 5 ++++- tests/unit/simulate_mode_test.c | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/cf-agent/simulate_mode.c b/cf-agent/simulate_mode.c index b70274a9c13..bf5343bd449 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -827,7 +827,10 @@ bool DiffPkgOperations() MapRemove(removed, name_arch); PkgOperationRecord *prev_record = MapGet(installed, name_arch); - if ((prev_record == NULL) || PkgVersionIsGreater(pkg_ver, prev_record->pkg_ver)) + if ((prev_record == NULL) || + (NULL_OR_EMPTY(prev_record->pkg_ver) && !NULL_OR_EMPTY(pkg_ver)) || + (!NULL_OR_EMPTY(pkg_ver) && !NULL_OR_EMPTY(prev_record->pkg_ver) && + PkgVersionIsGreater(pkg_ver, prev_record->pkg_ver))) { char *msg = GetPkgOperationMsg(CHROOT_PKG_OPERATION_CODE_INSTALL, pkg_name, pkg_arch, pkg_ver); diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index 5212959ec55..1cfb070174c 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -146,6 +146,40 @@ static void test_manifest_install_cancels_removal(void) unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); } +/* Two recorded installations of the same package without a version must be + * reported once, not abort on comparing the empty versions. */ +static void test_diff_repeated_unversioned_install(void) +{ + write_pkgs_ops("i,foo,,\r\n" + "i,foo,,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + const char *msg = "Package 'foo' would be installed"; + const char *first = strstr(output, msg); + assert_true(first != NULL); + assert_true(strstr(first + strlen(msg), "would be installed") == NULL); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + +/* A versioned installation record is more specific than an unversioned one + * of the same package, so it is the one to report. */ +static void test_diff_unversioned_then_versioned_install(void) +{ + write_pkgs_ops("i,foo,,\r\n" + "i,foo,1.2.3,\r\n"); + + char output[4096]; + assert_true(call_with_captured_stdout(&DiffPkgOperations, output, sizeof(output))); + + assert_true(strstr(output, "Package 'foo [1.2.3]' would be installed") != NULL); + assert_true(strstr(output, "Package 'foo' would be installed") == NULL); + + unlink(ToChangesChroot(CHROOT_PKGS_OPS_FILE)); +} + int main() { PRINT_TEST_BANNER(); @@ -160,6 +194,8 @@ int main() unit_test(test_diff_removal_with_version), unit_test(test_diff_removal_with_other_version_keeps_install), unit_test(test_manifest_install_cancels_removal), + unit_test(test_diff_repeated_unversioned_install), + unit_test(test_diff_unversioned_then_versioned_install), }; int ret = run_tests(tests); From 94d325e88a1317f1ccaea59fd9f867015147e6be Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Wed, 30 Sep 2026 12:38:28 -0400 Subject: [PATCH 4/6] Added --simulate-json option to write the simulated change set as JSON With --simulate-json=FILE, cf-agent also writes the files and renames of a --simulate run to FILE as one JSON document, together with whether the run fell back to the failsafe policy or was aborted. Under manifest-full the JSON lists only the changed files, not the kept ones. Ticket: none Changelog: Title Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Fable 5.1 --- cf-agent/cf-agent.c | 39 ++ cf-agent/simulate_mode.c | 290 +++++++++++++++ cf-agent/simulate_mode.h | 3 + libpromises/generic_agent.c | 1 + libpromises/generic_agent.h | 1 + .../29_simulate_mode/simulate_json.cf | 59 +++ .../simulate_json.cf.expected | 48 +++ .../29_simulate_mode/simulate_json.cf.sub | 28 ++ tests/unit/simulate_mode_test.c | 352 ++++++++++++++++++ 9 files changed, 821 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 a015224bffb..53faf21f06f 100644 --- a/cf-agent/cf-agent.c +++ b/cf-agent/cf-agent.c @@ -185,6 +185,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'} }; @@ -221,6 +222,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 }; @@ -338,6 +340,23 @@ int main(int argc, char *argv[]) Nova_NoteAgentExecutionPerformance(config->input_file, start); + /* Before GenericAgentFinalize(), which deinitializes the crypto library + * needed for the file digests. */ + if (config->agent_specific.agent.simulate_json_file != NULL) + { + const Class *failsafe_class = + EvalContextClassGet(ctx, NULL, "failsafe_fallback"); + const bool failsafe_fallback = + ((failsafe_class != NULL) && !failsafe_class->is_soft); + if (!WriteChangesJson(config->agent_specific.agent.simulate_json_file, + failsafe_fallback, EvalAborted(ctx)) && + (ret == 0)) + { + ret = EXIT_FAILURE; + } + free(config->agent_specific.agent.simulate_json_file); + } + GenericAgentFinalize(ctx, config); StringSetDestroy(SINGLE_COPY_CACHE); @@ -748,6 +767,18 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv) DoCleanupAndExit(EXIT_FAILURE); } } + else if (StringEqual(option_name, "simulate-json")) + { + if (!IsAbsoluteFileName(optarg)) + { + Log(LOG_LEVEL_ERR, + "Invalid argument for --simulate-json, an absolute path is required, not '%s'", + optarg); + DoCleanupAndExit(EXIT_FAILURE); + } + free(config->agent_specific.agent.simulate_json_file); + config->agent_specific.agent.simulate_json_file = xstrdup(optarg); + } break; } default: @@ -775,6 +806,14 @@ static GenericAgentConfig *CheckOpts(int argc, char **argv) DoCleanupAndExit(EXIT_FAILURE); } + if ((config->agent_specific.agent.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 bf5343bd449..0d61b342013 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -41,13 +41,23 @@ #include /* StringMap */ #include /* GetCsvLineNext() */ #include /* GetGroupName(), GetUserName() */ +#include /* JsonElement */ +#include /* StringWriter() */ +#include /* HashFile(), HashPrintSafe() */ +#include /* CF_PERMS_DEFAULT */ +#include /* RAND_bytes() */ #include #define DELIM_CHAR '=' +#define CHANGES_JSON_FORMAT_VERSION 1 /* Taken from coreutils/lib/stat-macros.h. */ +#ifndef __MINGW32__ #define CHMOD_MODE_BITS (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO) +#else +#define CHMOD_MODE_BITS (S_IRWXU | S_IRWXG | S_IRWXO) +#endif static inline void PrintDelimiter() { @@ -1054,3 +1064,283 @@ bool ManifestPkgOperations() return true; } + + +/* Returns NULL for a path that exists neither in the changes chroot nor + * outside of it, e.g. a file created and deleted by the same run. */ +static JsonElement *ChangedFileAsJson(const char *path) +{ + assert(path != NULL); + + const char *chrooted_path = ToChangesChroot(path); + struct stat st_orig; + struct stat st; + const bool existed = (lstat(path, &st_orig) == 0); + if (lstat(chrooted_path, &st) == -1) + { + if (!existed) + { + return NULL; + } + JsonElement *file_info = JsonObjectCreate(2); + JsonObjectAppendString(file_info, "path", path); + JsonObjectAppendString(file_info, "change", "deleted"); + return file_info; + } + + JsonElement *file_info = JsonObjectCreate(9); + JsonObjectAppendString(file_info, "path", path); + JsonObjectAppendString(file_info, "change", + existed ? "modified" : "created"); + JsonObjectAppendString(file_info, "type", + GetFileTypeDescription(st.st_mode)); + if (existed && ((st_orig.st_mode & S_IFMT) != (st.st_mode & S_IFMT))) + { + JsonObjectAppendString(file_info, "previous_type", + GetFileTypeDescription(st_orig.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); + + /* HashFile() cannot report failure, it leaves the digest zeroed + * and logs the cause at the INFO level. */ + static const unsigned char no_digest[EVP_MAX_MD_SIZE + 1] = {0}; + unsigned char digest[EVP_MAX_MD_SIZE + 1]; + HashFile(chrooted_path, digest, HASH_METHOD_SHA256, false); + if (memcmp(digest, no_digest, sizeof(digest)) != 0) + { + 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}; + if (readlink(chrooted_path, target, sizeof(target) - 1) > 0) + { + const char *real_target = target; + if (IsAbsoluteFileName(target)) + { + real_target = ToNormalRoot(target); + } + JsonObjectAppendString(file_info, "target", real_target); + } + } +#endif /* !__MINGW32__ */ + + return file_info; +} + +/* Appends the length-prefixed strings in #list_file (in the changes chroot) + * to #records. A missing #list_file means no records. #file_category is + * "changed" or "renamed", for the log messages. */ +static bool ReadChrootRecords(const char *list_file, const char *file_category, + Seq *records) +{ + assert(list_file != NULL); + assert(file_category != NULL); + assert(records != NULL); + + const char *path = ToChangesChroot(list_file); + if (access(path, F_OK) != 0) + { + return true; + } + + int fd = safe_open(path, O_RDONLY); + if (fd == -1) + { + Log(LOG_LEVEL_ERR, "Failed to open the file with list of %s files: %s", + file_category, GetErrorStr()); + return false; + } + + char *record; + int ret; + while ((ret = ReadLenPrefixedString(fd, &record)) > 0) + { + SeqAppend(records, record); + } + close(fd); + + if (ret < 0) + { + Log(LOG_LEVEL_ERR, "Failed to read the list of %s files", + file_category); + return false; + } + return true; +} + +static bool AddChangedFilesToJson(JsonElement *files) +{ + assert(files != NULL); + + Seq *paths = SeqNew(16, free); + const bool success = + ReadChrootRecords(CHROOT_CHANGES_LIST_FILE, "changed", paths); + if (success) + { + /* Each file should only be reported once. */ + StringSet *reported = StringSetNew(); + const size_t n_paths = SeqLength(paths); + for (size_t i = 0; i < n_paths; i++) + { + const char *path = SeqAt(paths, i); + if (!StringSetContains(reported, path)) + { + JsonElement *file_info = ChangedFileAsJson(path); + if (file_info != NULL) + { + JsonArrayAppendObject(files, file_info); + } + StringSetAdd(reported, xstrdup(path)); + } + } + StringSetDestroy(reported); + } + SeqDestroy(paths); + return success; +} + +static bool AddRenamedFilesToJson(JsonElement *renames) +{ + assert(renames != NULL); + + Seq *names = SeqNew(16, free); + bool success = + ReadChrootRecords(CHROOT_RENAMES_LIST_FILE, "renamed", names); + const size_t n_names = SeqLength(names); + if (success && ((n_names % 2) != 0)) + { + /* Pairs of the original and the new name, see + * RecordFileRenamedInChroot(). */ + Log(LOG_LEVEL_ERR, "Invalid data about renamed files"); + success = false; + } + for (size_t i = 0; success && (i < n_names); i += 2) + { + JsonElement *rename = JsonObjectCreate(2); + JsonObjectAppendString(rename, "old_name", SeqAt(names, i)); + JsonObjectAppendString(rename, "new_name", SeqAt(names, i + 1)); + JsonArrayAppendObject(renames, rename); + } + SeqDestroy(names); + return success; +} + +bool WriteChangesJson(const char *output_file, bool failsafe_fallback, + bool aborted) +{ + assert(output_file != NULL); + + JsonElement *json = JsonObjectCreate(5); + JsonObjectAppendInteger(json, "format_version", + CHANGES_JSON_FORMAT_VERSION); + JsonObjectAppendBool(json, "failsafe_fallback", failsafe_fallback); + JsonObjectAppendBool(json, "aborted", aborted); + JsonElement *files = JsonArrayCreate(16); + JsonObjectAppendArray(json, "files", files); + JsonElement *renames = JsonArrayCreate(16); + JsonObjectAppendArray(json, "renames", renames); + if (!AddChangedFilesToJson(files) || !AddRenamedFilesToJson(renames)) + { + 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); + + /* Written to a new file which is then renamed into place, so that a + * failed write leaves an existing #output_file intact and a symbolic + * link at #output_file is replaced rather than followed. */ + unsigned char random_bytes[8]; + if (RAND_bytes(random_bytes, sizeof(random_bytes)) != 1) + { + Log(LOG_LEVEL_ERR, "Failed to generate a temporary name for '%s'", + output_file); + free(document); + return false; + } + char random_hex[2 * sizeof(random_bytes) + 1]; + StringBytesToHex(random_hex, sizeof(random_hex), + random_bytes, sizeof(random_bytes)); + char *tmp_file; + xasprintf(&tmp_file, "%s.%s" CF_NEW, output_file, random_hex); + + bool success = false; + 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 create '%s' for the simulated change set: %s", + tmp_file, GetErrorStr()); + } + else + { + const size_t document_len = strlen(document); + if (FullWrite(fd, document, document_len) != (ssize_t) document_len) + { + Log(LOG_LEVEL_ERR, + "Failed to write the simulated change set to '%s': %s", + tmp_file, GetErrorStr()); + close(fd); + } + else if (close(fd) == -1) + { + Log(LOG_LEVEL_ERR, + "Failed to close the simulated change set '%s': %s", + tmp_file, GetErrorStr()); + } + else if (rename(tmp_file, output_file) == -1) + { + Log(LOG_LEVEL_ERR, "Failed to rename '%s' to '%s': %s", + tmp_file, output_file, GetErrorStr()); + } + else + { + success = true; + } + + if (!success) + { + unlink(tmp_file); + } + } + free(tmp_file); + free(document); + return success; +} diff --git a/cf-agent/simulate_mode.h b/cf-agent/simulate_mode.h index 4e855813015..f6a54f152c2 100644 --- a/cf-agent/simulate_mode.h +++ b/cf-agent/simulate_mode.h @@ -50,4 +50,7 @@ bool DiffChangedFiles(StringSet **audited_files); bool DiffPkgOperations(); bool ManifestPkgOperations(); +bool WriteChangesJson(const char *output_file, bool failsafe_fallback, + bool aborted); + #endif /* _SIMULATE_H_ */ diff --git a/libpromises/generic_agent.c b/libpromises/generic_agent.c index 6cc9f6897c0..cab12473829 100644 --- a/libpromises/generic_agent.c +++ b/libpromises/generic_agent.c @@ -2638,6 +2638,7 @@ GenericAgentConfig *GenericAgentConfigNewDefault(AgentType agent_type, bool tty_ case AGENT_TYPE_AGENT: config->agent_specific.agent.show_evaluated_classes = NULL; config->agent_specific.agent.show_evaluated_variables = NULL; + config->agent_specific.agent.simulate_json_file = NULL; break; default: diff --git a/libpromises/generic_agent.h b/libpromises/generic_agent.h index 58f792ab763..0e181ed0e73 100644 --- a/libpromises/generic_agent.h +++ b/libpromises/generic_agent.h @@ -94,6 +94,7 @@ typedef struct bool skip_bootstrap_service_start; char *show_evaluated_classes; char *show_evaluated_variables; + char *simulate_json_file; // BODY AGENT CONTROL bool report_class_log; 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..b4ca8fce93d --- /dev/null +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf @@ -0,0 +1,59 @@ +body common control +{ + inputs => { "../default.sub.cf" }; + bundlesequence => { default("$(this.promise_filename)") }; + version => "1.0"; +} + +bundle agent init +{ + files: + "$(G.testroot)$(const.dirsep)modify-me" + create => "true", + content => "before"; + + "$(G.testroot)$(const.dirsep)delete-me" create => "true"; + "$(G.testroot)$(const.dirsep)rename-me" create => "true"; +} + +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; + + # The option requires --simulate and an absolute path. + "$(sys.cf_agent) -Kf $(this.promise_filename).sub --simulate-json=$(this.promise_filename).unused.temp > /dev/null 2>&1; echo \"exit status $? without --simulate\" >> $(this.promise_filename).actual" + contain => in_shell; + + "$(sys.cf_agent) -Kf $(this.promise_filename).sub --simulate=manifest --simulate-json=relative.json > /dev/null 2>&1; echo \"exit status $? with a relative path\" >> $(this.promise_filename).actual" + contain => in_shell; + + # A failed write of the JSON document fails the run. + "$(sys.cf_agent) -Kf $(this.promise_filename).sub --simulate=manifest --simulate-json=$(G.testroot)$(const.dirsep)no-such-dir$(const.dirsep)changes.json > /dev/null 2>&1; echo \"exit status $? with an output path in a missing directory\" >> $(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..ceba4900c59 --- /dev/null +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf.expected @@ -0,0 +1,48 @@ +{ + "aborted": false, + "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, + "renames": [ + { + "new_name": "WORKDIR/tmp/renamed", + "old_name": "WORKDIR/tmp/rename-me" + } + ] +} +exit status 1 without --simulate +exit status 1 with a relative path +exit status 1 with an output path in a missing directory 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..c395b5edbec --- /dev/null +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf.sub @@ -0,0 +1,28 @@ +body common control +{ + inputs => { "../default.sub.cf" }; +} + +bundle agent main +{ + files: + "$(G.testroot)$(const.dirsep)create-me" + create => "true", + content => "created"; + + "$(G.testroot)$(const.dirsep)modify-me" content => "after"; + "$(G.testroot)$(const.dirsep)delete-me" delete => tidy; + "$(G.testroot)$(const.dirsep)rename-me" rename => to("renamed"); + "$(G.testroot)$(const.dirsep)link" link_from => symlink("$(G.testroot)$(const.dirsep)modify-me"); +} + +body rename to(name) +{ + newname => "$(name)"; +} + +body link_from symlink(target) +{ + source => "$(target)"; + link_type => "symlink"; +} diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index 1cfb070174c..0077ded8150 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -27,10 +27,16 @@ #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]; /* #csv contains "op,name,version,architecture" records terminated by "\r\n", * just like the records written by RecordPkgOperationInChroot(). */ @@ -71,6 +77,92 @@ static bool call_with_captured_stdout(bool (*fn)(void), char *output, size_t out return ret; } +static void reset_records(void) +{ + unlink(ToChangesChroot(CHROOT_CHANGES_LIST_FILE)); + unlink(ToChangesChroot(CHROOT_RENAMES_LIST_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_file(const char *path, const char *content) +{ + FILE *file = fopen(path, "w"); + assert_true(file != NULL); + assert_true(fputs(content, file) >= 0); + fclose(file); +} + +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(chrooted, true, NULL)); + + create_file(chrooted, content); + assert_int_equal(chmod(chrooted, mode), 0); +} + +static JsonElement *parse_changes(void) +{ + JsonElement *json = NULL; + assert_int_equal(JsonParseFile(OUTPUT_FILE, 1024 * 1024, &json), + JSON_PARSE_OK); + assert_true(json != NULL); + return json; +} + +static JsonElement *write_and_parse_changes(void) +{ + assert_true(WriteChangesJson(OUTPUT_FILE, false, false)); + return parse_changes(); +} + +/* The JsonObjectGetAs*() functions assert that #key exists, which would + * abort the test binary instead of failing the one test. */ +static JsonElement *get_key(const JsonElement *object, const char *key) +{ + JsonElement *element = JsonObjectGet(object, key); + assert_true(element != NULL); + return element; +} + +static const char *get_string(const JsonElement *object, const char *key) +{ + return JsonPrimitiveGetAsString(get_key(object, key)); +} + +static long get_integer(const JsonElement *object, const char *key) +{ + return JsonPrimitiveGetAsInteger(get_key(object, key)); +} + +static bool get_bool(const JsonElement *object, const char *key) +{ + return JsonPrimitiveGetAsBool(get_key(object, key)); +} + +/* Returns the only element of the #key array in #json. */ +static JsonElement *get_single(const JsonElement *json, const char *key) +{ + JsonElement *array = get_key(json, key); + assert_int_equal(JsonLength(array), 1); + return JsonArrayGet(array, 0); +} + /* A recorded removal followed by a recorded installation of the same package * is a net installation, so the removal must not be reported. */ static void test_diff_install_cancels_removal(void) @@ -180,12 +272,252 @@ static void test_diff_unversioned_then_versioned_install(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(get_integer(json, "format_version"), 1); + assert_int_equal(JsonLength(get_key(json, "files")), 0); + assert_int_equal(JsonLength(get_key(json, "renames")), 0); + assert_false(get_bool(json, "failsafe_fallback")); + assert_false(get_bool(json, "aborted")); + JsonDestroy(json); + + assert_true(WriteChangesJson(OUTPUT_FILE, true, false)); + json = parse_changes(); + assert_true(get_bool(json, "failsafe_fallback")); + assert_false(get_bool(json, "aborted")); + JsonDestroy(json); + + assert_true(WriteChangesJson(OUTPUT_FILE, false, true)); + json = parse_changes(); + assert_false(get_bool(json, "failsafe_fallback")); + assert_true(get_bool(json, "aborted")); + 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(get_string(file_info, "path"), path); + assert_string_equal(get_string(file_info, "change"), "created"); + assert_string_equal(get_string(file_info, "type"), "regular file"); + assert_true(JsonObjectGet(file_info, "previous_type") == NULL); +#ifndef __MINGW32__ + assert_string_equal(get_string(file_info, "permissions"), "0640"); +#endif + assert_int_equal(get_integer(file_info, "uid"), (long) st.st_uid); + assert_int_equal(get_integer(file_info, "gid"), (long) st.st_gid); + assert_int_equal(get_integer(file_info, "size"), 17); + assert_string_equal( + get_string(file_info, "sha256"), + "9be7023e1f91bae9d1f734b49c579cc2091c71924ae7494c9a5a3a8006527615"); + JsonDestroy(json); +} + +#ifndef __MINGW32__ +/* Characters that JSON escapes and Windows file names cannot contain. */ +static void test_special_characters_in_path(void) +{ + reset_records(); + + const char *const path = "/simulate-test/created \"file\" \\"; + create_chroot_file(path, "", 0644); + write_records(CHROOT_CHANGES_LIST_FILE, &path, 1); + + JsonElement *json = write_and_parse_changes(); + assert_string_equal(get_string(get_single(json, "files"), "path"), path); + JsonDestroy(json); +} +#endif /* !__MINGW32__ */ + +static void test_deleted_file(void) +{ + reset_records(); + + /* Exists outside of the chroot, but not in it. */ + char path[PATH_MAX]; + xsnprintf(path, sizeof(path), "%s/deleted-file", ORIG_DIR); + create_file(path, ""); + const char *const paths[] = {path}; + write_records(CHROOT_CHANGES_LIST_FILE, paths, 1); + + JsonElement *json = write_and_parse_changes(); + JsonElement *file_info = get_single(json, "files"); + assert_string_equal(get_string(file_info, "path"), path); + assert_string_equal(get_string(file_info, "change"), "deleted"); + assert_true(JsonObjectGet(file_info, "type") == NULL); + assert_true(JsonObjectGet(file_info, "sha256") == NULL); + JsonDestroy(json); +} + +static void test_created_and_deleted_file_not_reported(void) +{ + reset_records(); + + /* Exists neither in the chroot nor outside of it. */ + const char *const path = "/simulate-test/created-and-deleted"; + write_records(CHROOT_CHANGES_LIST_FILE, &path, 1); + + JsonElement *json = write_and_parse_changes(); + assert_int_equal(JsonLength(get_key(json, "files")), 0); + JsonDestroy(json); +} + +static void test_modified_file(void) +{ + reset_records(); + + char path[PATH_MAX]; + xsnprintf(path, sizeof(path), "%s/modified-file", ORIG_DIR); + create_file(path, "contents before the run\n"); + 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(get_string(file_info, "change"), "modified"); + assert_true(JsonObjectGet(file_info, "previous_type") == NULL); + assert_string_equal( + get_string(file_info, "sha256"), + "6ba024f3c03f13f9a8c1bb444829640c68553009facb418bb4552dd7ddebe427"); + JsonDestroy(json); +} + +static void test_changed_file_type(void) +{ + reset_records(); + + /* A directory outside of the chroot, a regular file in it. */ + char path[PATH_MAX]; + xsnprintf(path, sizeof(path), "%s/was-a-directory", ORIG_DIR); + assert_int_equal(mkdir(path, 0755), 0); + create_chroot_file(path, "", 0644); + const char *const paths[] = {path}; + write_records(CHROOT_CHANGES_LIST_FILE, paths, 1); + + JsonElement *json = write_and_parse_changes(); + JsonElement *file_info = get_single(json, "files"); + assert_string_equal(get_string(file_info, "change"), "modified"); + assert_string_equal(get_string(file_info, "type"), "regular file"); + assert_string_equal(get_string(file_info, "previous_type"), "directory"); + 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(get_string(file_info, "type"), "symbolic link"); + assert_string_equal(get_string(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(get_string(rename, "old_name"), names[0]); + assert_string_equal(get_string(rename, "new_name"), names[1]); + JsonDestroy(json); + + /* An original name without the new name is an error. */ + write_records(CHROOT_RENAMES_LIST_FILE, names, 1); + assert_false(WriteChangesJson(OUTPUT_FILE, false, false)); +} + +static void test_truncated_records(void) +{ + reset_records(); + + /* A length prefix promising more than the file holds. */ + FILE *file = fopen(ToChangesChroot(CHROOT_CHANGES_LIST_FILE), "w"); + assert_true(file != NULL); + assert_true(fprintf(file, "%-10zu%s", (size_t) 100, "/short") > 0); + fclose(file); + + assert_false(WriteChangesJson(OUTPUT_FILE, false, false)); +} + +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, 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); + create_file(link_target, "do not overwrite\n"); + 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 *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); const UnitTest tests[] = { @@ -196,12 +528,32 @@ int main() unit_test(test_manifest_install_cancels_removal), unit_test(test_diff_repeated_unversioned_install), unit_test(test_diff_unversioned_then_versioned_install), + unit_test(test_empty_change_set), + unit_test(test_created_file), +#ifndef __MINGW32__ + unit_test(test_special_characters_in_path), +#endif + unit_test(test_deleted_file), + unit_test(test_created_and_deleted_file_not_reported), + unit_test(test_modified_file), + unit_test(test_changed_file_type), +#ifndef __MINGW32__ + unit_test(test_created_symlink), +#endif + unit_test(test_renamed_file), + unit_test(test_truncated_records), + 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; } From 19f1ff672de94431201ba6752d3679471933d89c Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Wed, 30 Sep 2026 12:43:05 -0400 Subject: [PATCH 5/6] Refactored the reduction of recorded package operations PkgOperationRecord now stores the package fields and the simulate modes format their messages when printing, so that the reduction can be reused by other renderers. Ticket: none Changelog: None Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Fable 5.1 --- cf-agent/simulate_mode.c | 146 ++++++++++++++++++++++----------------- 1 file changed, 83 insertions(+), 63 deletions(-) diff --git a/cf-agent/simulate_mode.c b/cf-agent/simulate_mode.c index 0d61b342013..ca95f98022a 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -656,15 +656,19 @@ bool DiffChangedFiles(StringSet **audited_files) typedef struct PkgOperationRecord_ { - char *msg; + char *pkg_name; + char *pkg_arch; char *pkg_ver; } PkgOperationRecord; -static PkgOperationRecord *PkgOperationRecordNew(char *msg, char *pkg_ver) +static PkgOperationRecord *PkgOperationRecordNew(const char *pkg_name, + const char *pkg_arch, + const char *pkg_ver) { PkgOperationRecord *ret = xmalloc(sizeof(PkgOperationRecord)); - ret->msg = msg; - ret->pkg_ver = pkg_ver; + ret->pkg_name = xstrdup(pkg_name); + ret->pkg_arch = SafeStringDuplicate(pkg_arch); + ret->pkg_ver = SafeStringDuplicate(pkg_ver); return ret; } @@ -673,7 +677,8 @@ static void PkgOperationRecordDestroy(PkgOperationRecord *pkg_op) { if (pkg_op != NULL) { - free(pkg_op->msg); + free(pkg_op->pkg_name); + free(pkg_op->pkg_arch); free(pkg_op->pkg_ver); free(pkg_op); } @@ -737,12 +742,36 @@ static inline char *GetPkgOperationMsg(ChrootPkgOperationCode op, const char *pk return msg; } -bool DiffPkgOperations() +static void PrintPkgOperations(Map *pkg_ops, ChrootPkgOperationCode op) +{ + assert(pkg_ops != NULL); + + MapIterator i = MapIteratorInit(pkg_ops); + MapKeyValue *item; + while ((item = MapIteratorNext(&i))) + { + const PkgOperationRecord *record = item->value; + char *msg = GetPkgOperationMsg(op, record->pkg_name, + record->pkg_arch, record->pkg_ver); + puts(msg); + free(msg); + } +} + +/* Reduces the recorded package operations to the net sets of packages that + * would be installed and removed (maps of PkgOperationRecord), 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; } @@ -795,7 +824,7 @@ bool DiffPkgOperations() * and so the package is still seen as present in the system (package cache). * * This means that a 'present' operation after 'remove' operation results in no - * difference (the package would be installed back) so the potential message about the + * difference (the package would be installed back) so the potential record of the * removal should be removed. */ MapRemove(removed, name_arch); } @@ -803,7 +832,7 @@ bool DiffPkgOperations() { /* The same logic as above applies here for an originally absent package that is * installed and then reported as absent again. No diff to report, just remove the - * message about the package installation. */ + * record of the package installation. */ /* However, if a different specific version is reported as absent than the version that * would have been installed, this removal would not remove the installed package @@ -842,9 +871,8 @@ bool DiffPkgOperations() (!NULL_OR_EMPTY(pkg_ver) && !NULL_OR_EMPTY(prev_record->pkg_ver) && 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)); + PkgOperationRecord *record = + PkgOperationRecordNew(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) */ } @@ -854,10 +882,10 @@ bool DiffPkgOperations() assert(*op == CHROOT_PKG_OPERATION_CODE_REMOVE); /* The only option not covered above. */ /* 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 + * record over a new record 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); - bool insert_new_msg = ((prev_record == NULL) || (NULL_OR_EMPTY(prev_record->pkg_ver))); + bool insert_new_record = ((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 * means that the package was initially present, then updated by the 'install' operation @@ -868,7 +896,7 @@ bool DiffPkgOperations() * would remove the installed package. */ if (NULL_OR_EMPTY(pkg_ver)) { - /* No version specified, remove the installation message (if any). */ + /* No version specified, remove the installation record (if any). */ MapRemove(installed, name_arch); } else @@ -880,16 +908,15 @@ bool DiffPkgOperations() } else if (inst_record != NULL) { - /* Keeping the install message, the removal would make no change. */ - insert_new_msg = false; + /* Keeping the install record, the removal would make no change. */ + insert_new_record = false; } } - if (insert_new_msg) + if (insert_new_record) { - char *msg = GetPkgOperationMsg(CHROOT_PKG_OPERATION_CODE_REMOVE, - pkg_name, pkg_arch, pkg_ver); - PkgOperationRecord *record = PkgOperationRecordNew(msg, SafeStringDuplicate(pkg_ver)); + PkgOperationRecord *record = + PkgOperationRecordNew(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) */ } @@ -899,6 +926,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"); @@ -910,21 +958,8 @@ bool DiffPkgOperations() } Log(LOG_LEVEL_INFO, "Showing differences in installed packages"); - MapIterator i = MapIteratorInit(installed); - MapKeyValue *item; - while ((item = MapIteratorNext(&i))) - { - PkgOperationRecord *value = item->value; - const char *msg = value->msg; - puts(msg); - } - i = MapIteratorInit(removed); - while ((item = MapIteratorNext(&i))) - { - PkgOperationRecord *value = item->value; - const char *msg = value->msg; - puts(msg); - } + PrintPkgOperations(installed, CHROOT_PKG_OPERATION_CODE_INSTALL); + PrintPkgOperations(removed, CHROOT_PKG_OPERATION_CODE_REMOVE); MapDestroy(installed); MapDestroy(removed); @@ -983,10 +1018,10 @@ bool ManifestPkgOperations() if ((*op == CHROOT_PKG_OPERATION_CODE_INSTALL) || (*op == CHROOT_PKG_OPERATION_CODE_PRESENT)) { - /* If there is a previous install/present operation, we want to choose the message with - * the higher version or the message which has a specific version (if any). */ + /* If there is a previous install/present operation, we want to choose the record with + * the higher version or the record which has a specific version (if any). */ - /* Cancels any previous remove/absent message. */ + /* Cancels any previous remove/absent record. */ MapRemove(absent, name_arch); PkgOperationRecord *prev_record = MapGet(present, name_arch); @@ -995,9 +1030,8 @@ bool ManifestPkgOperations() (!NULL_OR_EMPTY(pkg_ver) && !NULL_OR_EMPTY(prev_record->pkg_ver) && PkgVersionIsGreater(pkg_ver, prev_record->pkg_ver))) { - char *msg = GetPkgOperationMsg(CHROOT_PKG_OPERATION_CODE_PRESENT, - pkg_name, pkg_arch, pkg_ver); - PkgOperationRecord *record = PkgOperationRecordNew(msg, SafeStringDuplicate(pkg_ver)); + PkgOperationRecord *record = + PkgOperationRecordNew(pkg_name, pkg_arch, pkg_ver); MapInsert(present, name_arch, record); name_arch = NULL; /* name_arch is now owned by the map (as a key) */ } @@ -1007,7 +1041,7 @@ bool ManifestPkgOperations() assert((*op == CHROOT_PKG_OPERATION_CODE_REMOVE) || (*op == CHROOT_PKG_OPERATION_CODE_ABSENT)); - /* If there is a 'present' message with a different version than the version specified + /* If there is a 'present' record with a different version than the version specified * here (if any), we know that the removal would fail and so it should not be * reported. */ PkgOperationRecord *present_record = MapGet(present, name_arch); @@ -1015,18 +1049,17 @@ bool ManifestPkgOperations() NULL_OR_EMPTY(present_record->pkg_ver) || NULL_OR_EMPTY(pkg_ver) || StringEqual(present_record->pkg_ver, pkg_ver)) { - /* Remove the 'present' message now that we know the removal would/should work. */ + /* Remove the 'present' record now that we know the removal would/should work. */ MapRemove(present, name_arch); - /* If there is a previous 'absent' message, we want to use the message with no + /* If there is a previous 'absent' record, we want to use the record with no * version specification (if any) because it's more generic. */ PkgOperationRecord *prev_record = MapGet(absent, name_arch); if ((prev_record == NULL) || (!NULL_OR_EMPTY(prev_record->pkg_ver) && NULL_OR_EMPTY(pkg_ver))) { - char *msg = GetPkgOperationMsg(CHROOT_PKG_OPERATION_CODE_ABSENT, - pkg_name, pkg_arch, pkg_ver); - PkgOperationRecord *record = PkgOperationRecordNew(msg, SafeStringDuplicate(pkg_ver)); + PkgOperationRecord *record = + PkgOperationRecordNew(pkg_name, pkg_arch, pkg_ver); MapInsert(absent, name_arch, record); name_arch = NULL; /* name_arch is now owned by the map (as a key) */ } @@ -1043,21 +1076,8 @@ bool ManifestPkgOperations() assert((MapSize(present) != 0) || (MapSize(absent) != 0)); Log(LOG_LEVEL_INFO, "Manifesting present and absent packages"); - MapIterator i = MapIteratorInit(present); - MapKeyValue *item; - while ((item = MapIteratorNext(&i))) - { - PkgOperationRecord *value = item->value; - const char *msg = value->msg; - puts(msg); - } - i = MapIteratorInit(absent); - while ((item = MapIteratorNext(&i))) - { - PkgOperationRecord *value = item->value; - const char *msg = value->msg; - puts(msg); - } + PrintPkgOperations(present, CHROOT_PKG_OPERATION_CODE_PRESENT); + PrintPkgOperations(absent, CHROOT_PKG_OPERATION_CODE_ABSENT); MapDestroy(present); MapDestroy(absent); From e00b8990bf850312b6cc2ded63258bdfcfbd8638 Mon Sep 17 00:00:00 2001 From: Daniel JB Clark Date: Wed, 30 Sep 2026 12:47:50 -0400 Subject: [PATCH 6/6] Added the net package operations to the --simulate-json change set The "packages" array lists each package that a --simulate run would install or remove, with its version and architecture when known. It always carries that install/remove reduction, under --simulate=manifest as well. Ticket: none Changelog: Title Signed-off-by: Daniel JB Clark Co-Authored-By: Claude Fable 5.1 --- cf-agent/simulate_mode.c | 70 ++++++++++- .../29_simulate_mode/simulate_json.cf | 5 + .../simulate_json.cf.expected | 6 + .../29_simulate_mode/simulate_json.cf.module | 20 +++ .../29_simulate_mode/simulate_json.cf.sub | 13 ++ tests/unit/simulate_mode_test.c | 115 ++++++++++++++++++ 6 files changed, 228 insertions(+), 1 deletion(-) create mode 100755 tests/acceptance/29_simulate_mode/simulate_json.cf.module diff --git a/cf-agent/simulate_mode.c b/cf-agent/simulate_mode.c index ca95f98022a..a36b64d10ec 100644 --- a/cf-agent/simulate_mode.c +++ b/cf-agent/simulate_mode.c @@ -1274,6 +1274,71 @@ static bool AddRenamedFilesToJson(JsonElement *renames) 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 PkgOperationRecord *record = item->value; + JsonElement *op_info = JsonObjectCreate(4); + JsonObjectAppendString(op_info, "operation", operation); + JsonObjectAppendString(op_info, "name", record->pkg_name); + if (!NULL_OR_EMPTY(record->pkg_ver)) + { + JsonObjectAppendString(op_info, "version", record->pkg_ver); + } + if (!NULL_OR_EMPTY(record->pkg_arch)) + { + JsonObjectAppendString(op_info, "architecture", record->pkg_arch); + } + JsonArrayAppendObject(packages, op_info); + } +} + +/* Orders the array by package name and architecture. That is a total order + * because CollectPkgOperations() keeps each package in at most one of its two + * maps: an installation record cancels any removal record and vice versa. */ +static int ComparePkgOperations(const JsonElement *a, const JsonElement *b, + ARG_UNUSED void *user_data) +{ + int ret = StringSafeCompare(JsonObjectGetAsString(a, "name"), + JsonObjectGetAsString(b, "name")); + if (ret == 0) + { + ret = StringSafeCompare(JsonObjectGetAsString(a, "architecture"), + JsonObjectGetAsString(b, "architecture")); + } + return ret; +} + +static bool AddPkgOperationsToJson(JsonElement *packages) +{ + assert(packages != NULL); + + Map *installed; + Map *removed; + if (!CollectPkgOperations(&installed, &removed)) + { + return false; + } + if (installed != NULL) + { + JsonArrayAppendPkgOperations(packages, installed, "install"); + JsonArrayAppendPkgOperations(packages, removed, "remove"); + MapDestroy(installed); + MapDestroy(removed); + + /* The maps have no stable order. */ + JsonSort(packages, ComparePkgOperations, NULL); + } + return true; +} + bool WriteChangesJson(const char *output_file, bool failsafe_fallback, bool aborted) { @@ -1288,7 +1353,10 @@ bool WriteChangesJson(const char *output_file, bool failsafe_fallback, JsonObjectAppendArray(json, "files", files); JsonElement *renames = JsonArrayCreate(16); JsonObjectAppendArray(json, "renames", renames); - if (!AddChangedFilesToJson(files) || !AddRenamedFilesToJson(renames)) + JsonElement *packages = JsonArrayCreate(16); + JsonObjectAppendArray(json, "packages", packages); + if (!AddChangedFilesToJson(files) || !AddRenamedFilesToJson(renames) || + !AddPkgOperationsToJson(packages)) { JsonDestroy(json); return false; diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf b/tests/acceptance/29_simulate_mode/simulate_json.cf index b4ca8fce93d..6aaa96b5a34 100644 --- a/tests/acceptance/29_simulate_mode/simulate_json.cf +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf @@ -14,6 +14,11 @@ bundle agent init "$(G.testroot)$(const.dirsep)delete-me" create => "true"; "$(G.testroot)$(const.dirsep)rename-me" create => "true"; + "$(sys.workdir)/modules/packages/." create => "true"; + + "$(sys.workdir)/modules/packages/simulate_json" + copy_from => local_cp("$(this.promise_filename).module"), + perms => m("ugo+x"); } bundle agent test diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf.expected b/tests/acceptance/29_simulate_mode/simulate_json.cf.expected index ceba4900c59..928e03c0ba3 100644 --- a/tests/acceptance/29_simulate_mode/simulate_json.cf.expected +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf.expected @@ -36,6 +36,12 @@ } ], "format_version": 1, + "packages": [ + { + "name": "simulate-json-pkg", + "operation": "install" + } + ], "renames": [ { "new_name": "WORKDIR/tmp/renamed", diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf.module b/tests/acceptance/29_simulate_mode/simulate_json.cf.module new file mode 100755 index 00000000000..8498c548c9e --- /dev/null +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf.module @@ -0,0 +1,20 @@ +#!/bin/sh +# A package module that knows one package type, so that the simulated run +# records a package operation. Nothing is installed. + +case "$1" in + supports-api-version) + echo 1 + ;; + get-package-data) + echo PackageType=repo + # The "File=" line, whatever else precedes it. + sed -n 's/^File=/Name=/p' + ;; + *) + # Drain input. + cat > /dev/null + ;; +esac + +exit 0 diff --git a/tests/acceptance/29_simulate_mode/simulate_json.cf.sub b/tests/acceptance/29_simulate_mode/simulate_json.cf.sub index c395b5edbec..043cc0faf08 100644 --- a/tests/acceptance/29_simulate_mode/simulate_json.cf.sub +++ b/tests/acceptance/29_simulate_mode/simulate_json.cf.sub @@ -14,6 +14,13 @@ bundle agent main "$(G.testroot)$(const.dirsep)delete-me" delete => tidy; "$(G.testroot)$(const.dirsep)rename-me" rename => to("renamed"); "$(G.testroot)$(const.dirsep)link" link_from => symlink("$(G.testroot)$(const.dirsep)modify-me"); + + packages: + # Logs "Error installing package": the simulated installation is validated + # against the installed packages, where it is not. The record is the point. + "simulate-json-pkg" + policy => "present", + package_module => simulate_json; } body rename to(name) @@ -26,3 +33,9 @@ body link_from symlink(target) source => "$(target)"; link_type => "symlink"; } + +body package_module simulate_json +{ + query_installed_ifelapsed => "0"; + query_updates_ifelapsed => "0"; +} diff --git a/tests/unit/simulate_mode_test.c b/tests/unit/simulate_mode_test.c index 0077ded8150..34d079d8ce1 100644 --- a/tests/unit/simulate_mode_test.c +++ b/tests/unit/simulate_mode_test.c @@ -81,6 +81,7 @@ 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); } @@ -280,6 +281,7 @@ static void test_empty_change_set(void) assert_int_equal(get_integer(json, "format_version"), 1); assert_int_equal(JsonLength(get_key(json, "files")), 0); assert_int_equal(JsonLength(get_key(json, "renames")), 0); + assert_int_equal(JsonLength(get_key(json, "packages")), 0); assert_false(get_bool(json, "failsafe_fallback")); assert_false(get_bool(json, "aborted")); JsonDestroy(json); @@ -475,6 +477,117 @@ static void test_truncated_records(void) assert_false(WriteChangesJson(OUTPUT_FILE, false, false)); } +/* Checks that the package operations recorded in #csv reduce to the single + * #operation on the package #name, #version and #arch (NULL for omitted), + * or to no operation at all if #operation is NULL. */ +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(); + if (operation == NULL) + { + assert_int_equal(JsonLength(get_key(json, "packages")), 0); + JsonDestroy(json); + return; + } + + JsonElement *op_info = get_single(json, "packages"); + assert_string_equal(get_string(op_info, "operation"), operation); + assert_string_equal(get_string(op_info, "name"), name); + if (version == NULL) + { + assert_true(JsonObjectGet(op_info, "version") == NULL); + } + else + { + assert_string_equal(get_string(op_info, "version"), version); + } + if (arch == NULL) + { + assert_true(JsonObjectGet(op_info, "architecture") == NULL); + } + else + { + assert_string_equal(get_string(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("i,pkg,,x86_64\r\n", + "install", "pkg", NULL, "x86_64"); + check_pkg_operations("r,pkg,,x86_64\r\n", + "remove", "pkg", NULL, "x86_64"); + check_pkg_operations("r,pkg,,\r\n", "remove", "pkg", NULL, NULL); + check_pkg_operations("r,pkg,1.0,\r\n", "remove", "pkg", "1.0", NULL); + + /* Net operations only: cancelled ones are not reported and a newer + * version wins. */ + 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); + check_pkg_operations("i,pkg,1.0,\r\ni,pkg,2.0,\r\n", + "install", "pkg", "2.0", NULL); + check_pkg_operations("i,pkg,v2,\r\ni,pkg,latest,\r\n", + "install", "pkg", "latest", NULL); + + /* A removal after an installation removes the package that was present + * before the run, unless it names another version than the one that + * would be installed. A versioned removal is more specific than an + * unversioned one. */ + check_pkg_operations("i,pkg,1.0,\r\nr,pkg,,\r\n", + "remove", "pkg", NULL, NULL); + check_pkg_operations("i,pkg,1.0,\r\nr,pkg,1.0,\r\n", + "remove", "pkg", "1.0", NULL); + check_pkg_operations("i,pkg,2.0,\r\na,pkg,1.0,\r\n", + "install", "pkg", "2.0", NULL); + check_pkg_operations("r,pkg,1.0,\r\nr,pkg,,\r\n", + "remove", "pkg", "1.0", NULL); + + /* A malformed record is skipped. */ + check_pkg_operations("garbage\r\ni,pkg,1.0,\r\n", + "install", "pkg", "1.0", NULL); + + /* The record written by RecordPkgOperationInChroot() itself. */ + reset_records(); + assert_true(RecordPkgOperationInChroot(CHROOT_PKG_OPERATION_INSTALL, + "pkg", "1.0", "x86_64")); + JsonElement *json = write_and_parse_changes(); + JsonElement *op_info = get_single(json, "packages"); + assert_string_equal(get_string(op_info, "operation"), "install"); + assert_string_equal(get_string(op_info, "version"), "1.0"); + assert_string_equal(get_string(op_info, "architecture"), "x86_64"); + JsonDestroy(json); +} + +static void test_pkg_operations_sorted(void) +{ + reset_records(); + write_pkgs_ops("i,zeta,1.0,x86_64\r\n" + "r,alpha,,\r\n" + "i,zeta,1.0,arm64\r\n"); + + JsonElement *json = write_and_parse_changes(); + JsonElement *packages = get_key(json, "packages"); + assert_int_equal(JsonLength(packages), 3); + assert_string_equal(get_string(JsonArrayGet(packages, 0), "name"), + "alpha"); + assert_string_equal(get_string(JsonArrayGet(packages, 1), "architecture"), + "arm64"); + assert_string_equal(get_string(JsonArrayGet(packages, 2), "architecture"), + "x86_64"); + JsonDestroy(json); +} + static void test_write_failure(void) { reset_records(); @@ -542,6 +655,8 @@ int main() #endif unit_test(test_renamed_file), unit_test(test_truncated_records), + unit_test(test_pkg_operations), + unit_test(test_pkg_operations_sorted), unit_test(test_write_failure), #ifndef __MINGW32__ unit_test(test_output_symlink_not_followed),