Repository navigation
ENT-14463: Made cf-reactor evaluate the reactor bundles #6361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4397c4d
Changed files_deleted to file_deleted in mod_reactor
victormlg 47757ae
Made cf-reactor watcher thread safe
victormlg a03caa9
Evaluate reactor and events promises
victormlg cfeb23b
Allowed parameterized bundles in 'then' attribute
victormlg 597a514
Updated watcher_test.c
victormlg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| /* | ||
| Copyright 2026 Northern.tech AS | ||
|
|
||
| This file is part of CFEngine 3 - written and maintained by Northern.tech AS. | ||
|
|
||
| This program is free software; you can redistribute it and/or modify it | ||
| under the terms of the GNU General Public License as published by the | ||
| Free Software Foundation; version 3. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program; if not, write to the Free Software | ||
| Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
|
|
||
| To the extent this program is licensed as part of the Enterprise | ||
| versions of CFEngine, the applicable Commercial Open Source License | ||
| (COSL) may apply to this file if you as a licensee so wish it. See | ||
| included file COSL.txt. | ||
| */ | ||
|
|
||
| #include <reactor_transform.h> | ||
|
|
||
| #include <logging.h> | ||
| #include <expand.h> | ||
| #include <fncall.h> | ||
| #include <ornaments.h> | ||
| #include <rlist.h> | ||
| #include <sequence.h> | ||
| #include <verify_classes.h> | ||
| #include <verify_vars.h> | ||
| #include <watcher.h> | ||
| #include <file_watcher.h> | ||
|
|
||
| /* Promise types evaluated within `bundle reactor NAME { ... }`. */ | ||
| static const char *const REACTOR_TYPESEQUENCE[] = | ||
| { | ||
| "meta", | ||
| "vars", | ||
| "classes", | ||
| "events", | ||
| NULL | ||
| }; | ||
|
|
||
| /* Checks that the bundle referred to by an events promise's 'then' attribute, | ||
| * either `then => "name"` or `then => name(args)`, is defined as an agent or | ||
| * common bundle. Logs an error and returns false if it is not. */ | ||
| static bool ThenBundleExists(const EvalContext *ctx, const Promise *pp, Rval then_rval, const char *key) | ||
| { | ||
| const char *bundle_name = NULL; | ||
| switch (then_rval.type) | ||
| { | ||
| case RVAL_TYPE_SCALAR: | ||
| bundle_name = RvalScalarValue(then_rval); | ||
| break; | ||
| case RVAL_TYPE_FNCALL: | ||
| bundle_name = RvalFnCallValue(then_rval)->name; | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
|
|
||
| const Bundle *bundle = NULL; | ||
| if (bundle_name != NULL) | ||
| { | ||
| bundle = EvalContextResolveBundleExpression(ctx, PromiseGetPolicy(pp), bundle_name, "agent"); | ||
| if (bundle == NULL) | ||
| { | ||
| bundle = EvalContextResolveBundleExpression(ctx, PromiseGetPolicy(pp), bundle_name, "common"); | ||
| } | ||
| } | ||
| if (bundle == NULL) | ||
| { | ||
| Log(LOG_LEVEL_ERR, "Reactor events promise '%s' refers to unknown bundle '%s', ignoring", | ||
| key, (bundle_name != NULL) ? bundle_name : "(invalid)"); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| // Temporary limitations: | ||
| // - 'when' body: only a single constraint, 'file_deleted', is supported (no OR-ing of constraints yet) | ||
| // - 'then': only a single bundle is supported, not a list of bundles yet | ||
| // TODO: support multiple 'when' constraints and a list of 'then' bundles | ||
| static PromiseResult KeepEventsPromise(EvalContext *ctx, const Promise *pp) | ||
| { | ||
| assert(pp != NULL); | ||
|
|
||
| const Bundle *bp = PromiseGetBundle(pp); | ||
| char *key = StringFormat("%s:%s:%s", bp->ns, bp->name, pp->promiser); | ||
|
|
||
| const char *path = PromiseGetConstraintAsRval(pp, "file_deleted", RVAL_TYPE_SCALAR); | ||
| if (path == NULL) | ||
| { | ||
| Log(LOG_LEVEL_WARNING, | ||
| "Reactor events promise '%s' must specify exactly one file to watch in its 'when' body, ignoring", | ||
| key); | ||
| free(key); | ||
| return PROMISE_RESULT_FAIL; | ||
| } | ||
|
|
||
| const Constraint *then_constraint = PromiseGetConstraint(pp, "then"); | ||
| if (then_constraint == NULL) | ||
| { | ||
| Log(LOG_LEVEL_ERR, "Reactor events promise '%s' does not specify a 'then' bundle, ignoring", key); | ||
| free(key); | ||
| return PROMISE_RESULT_FAIL; | ||
| } | ||
|
|
||
| if (!ThenBundleExists(ctx, pp, then_constraint->rval, key)) | ||
| { | ||
| free(key); | ||
| return PROMISE_RESULT_FAIL; | ||
| } | ||
|
|
||
| // register watcher | ||
| Log(LOG_LEVEL_INFO, "Registering a file_deleted watcher with key '%s', on file '%s'", key, path); | ||
| bool kept = WatcherRegister(key, EVENT_FILE_DELETED, FileWatcherStateNew(path), then_constraint->rval, 1); | ||
| free(key); | ||
|
|
||
| return (kept) ? PROMISE_RESULT_NOOP : PROMISE_RESULT_FAIL; | ||
|
craigcomstock marked this conversation as resolved.
|
||
| } | ||
|
|
||
| static PromiseResult KeepReactorPromise(EvalContext *ctx, const Promise *pp, ARG_UNUSED void *param) | ||
| { | ||
| assert(param == NULL); | ||
| PromiseBanner(ctx, pp); | ||
|
|
||
| if (StringEqual(PromiseGetPromiseType(pp), "vars") || | ||
| StringEqual(PromiseGetPromiseType(pp), "meta")) | ||
| { | ||
| return VerifyVarPromise(ctx, pp, NULL); | ||
| } | ||
|
|
||
| if (StringEqual(PromiseGetPromiseType(pp), "classes")) | ||
| { | ||
| return VerifyClassPromise(ctx, pp, NULL); | ||
| } | ||
|
|
||
| if (StringEqual(PromiseGetPromiseType(pp), "events")) | ||
| { | ||
| return KeepEventsPromise(ctx, pp); | ||
| } | ||
|
|
||
| return PROMISE_RESULT_NOOP; | ||
| } | ||
|
|
||
| static void EvaluateReactorBundle(EvalContext *ctx, const Bundle *bp) | ||
| { | ||
| assert(bp != NULL); | ||
| EvalContextStackPushBundleFrame(ctx, bp, NULL, false, NULL); | ||
|
|
||
| for (int i = 0; REACTOR_TYPESEQUENCE[i] != NULL; i++) | ||
| { | ||
| const BundleSection *sp = BundleGetSection(bp, REACTOR_TYPESEQUENCE[i]); | ||
| if (sp == NULL || SeqLength(sp->promises) == 0) | ||
| { | ||
| Log(LOG_LEVEL_DEBUG, "No promise type %s in bundle %s", | ||
| REACTOR_TYPESEQUENCE[i], bp->name); | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| continue; | ||
| } | ||
|
|
||
| EvalContextStackPushBundleSectionFrame(ctx, sp); | ||
| for (size_t j = 0; j < SeqLength(sp->promises); j++) | ||
| { | ||
| const Promise *pp = SeqAt(sp->promises, j); | ||
| ExpandPromise(ctx, pp, KeepReactorPromise, NULL); | ||
| } | ||
| EvalContextStackPopFrame(ctx); | ||
| } | ||
|
|
||
| EvalContextStackPopFrame(ctx); | ||
| } | ||
|
|
||
| void KeepReactorPromises(EvalContext *ctx, const Policy *policy) | ||
| { | ||
| assert(policy != NULL); | ||
| WatcherRegistryClear(); | ||
|
|
||
| for (size_t i = 0; i < SeqLength(policy->bundles); i++) | ||
|
github-advanced-security[bot] marked this conversation as resolved.
Fixed
|
||
| { | ||
| const Bundle *bp = SeqAt(policy->bundles, i); | ||
| if (!StringEqual(bp->type, CF_AGENTTYPES[AGENT_TYPE_REACTOR])) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| if (RlistLen(bp->args) > 0) | ||
| { | ||
| Log(LOG_LEVEL_WARNING, | ||
| "Cannot implicitly evaluate bundle '%s %s', as this bundle takes arguments.", | ||
| bp->type, bp->name); | ||
| continue; | ||
| } | ||
|
|
||
| EvaluateReactorBundle(ctx, bp); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| Copyright 2026 Northern.tech AS | ||
|
|
||
| This file is part of CFEngine 3 - written and maintained by Northern.tech AS. | ||
|
|
||
| This program is free software; you can redistribute it and/or modify it | ||
| under the terms of the GNU General Public License as published by the | ||
| Free Software Foundation; version 3. | ||
|
|
||
| This program is distributed in the hope that it will be useful, | ||
| but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| GNU General Public License for more details. | ||
|
|
||
| You should have received a copy of the GNU General Public License | ||
| along with this program; if not, write to the Free Software | ||
| Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA | ||
|
|
||
| To the extent this program is licensed as part of the Enterprise | ||
| versions of CFEngine, the applicable Commercial Open Source License | ||
| (COSL) may apply to this file if you as a licensee so wish it. See | ||
| included file COSL.txt. | ||
| */ | ||
|
|
||
| #ifndef CFENGINE_REACTOR_TRANSFORM_H | ||
| #define CFENGINE_REACTOR_TRANSFORM_H | ||
|
|
||
| #include <eval_context.h> | ||
| #include <policy.h> | ||
|
|
||
| /** | ||
| * @brief Evaluate every `bundle reactor NAME { ... }` in the given policy: | ||
| * keep its "meta", "vars" and "classes" promises, and register a watcher | ||
| * (see watcher.h) for each of its "events" promises. | ||
| * | ||
| * Safe to call again for every re-read of the policy: it first discards all | ||
| * previously registered watchers (see WatcherRegistryClear()), so the | ||
| * result always reflects only the given policy, not a stale prior one. | ||
| */ | ||
| void KeepReactorPromises(EvalContext *ctx, const Policy *policy); | ||
|
|
||
| #endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.