Skip to content

Made CMDB class insertion fail closed and rollback restore state - #98

Merged
djbclark merged 4 commits into
cmdb-fail-closedfrom
cmdb-fail-closed-a1
Nov 14, 2026
Merged

djbclark merged 4 commits into
cmdb-fail-closedfrom
cmdb-fail-closed-a1

Conversation

@djbclark

@djbclark djbclark commented Nov 14, 2026 •

Copy link
Copy Markdown
Owner

A class the insertion API cannot insert was treated as one that already exists, so a file with a whitespace-only class name still installed its variables. Stacked on cmdb-fail-closed; found in review of frdminc/tendcf#41.

  • A class name with whitespace at either end is invalid. The API chops trailing whitespace for its emptiness test but files the name as given ("x " becomes x_).
  • Existence is decided by the canonical identity (namespace, canonified name) before inserting, so pre-class no longer replaces pre_class. Existing classes are skipped and never rolled back (the benign case of ENT-10336: Fixed misleading error msg when adding class to CMDB cfengine/core#5263). Any other failed insertion rolls back everything this call installed and LoadCMDBData() returns false.
  • A variable that exists already makes the file invalid, so removing on rollback is restoring. The loader runs on a cleared context, before augments (generic_agent.c), so nothing legitimate is refused.
  • CMDBTestFailInsertion also covers classes (no change in AddCMDBVariable(), which Fixed crash on JSON null values in host_specific.json cfengine/core#6507 touches).

Tests (tests/unit/cmdb_test.c): whitespace-only and edge-whitespace class names next to a good variable and class install nothing; an existing class and an aliased one stay as they were while the rest loads; an existing variable is refused and keeps its value; a forced failure removes dash-class (stored as dash_class) and leaves an aliased pre_class alone; a forced class failure rolls back earlier classes and installs no variable. Not run locally; fork CI and the Linux gate run them.

Not changed: CMDBTestFailInsertion stays a production global (cmdb_test links libpromises.la).

Phase 2 (A2 of the same review): the loader read the file with JsonParseFile(), so a repeated object member replaced the earlier one before validation saw it ({"vars":{"good":null,"good":"ok"}} loaded) and anything after the first document was ignored. libntech is bumped to djbclark/libntech cc892efae (JsonParseFileStrict()) and LoadCMDBData() uses it; ReadJsonFile() keeps its lenient behaviour for the augments callers. Unit tests cover repeated members (top level, classes, variables, nested) and trailing input, plus whitespace after the document still loading; acceptance 17 and 18 cover a repeated member and trailing input. Not run locally.

Closes #97

Prior art / docs checked: libntech JsonParseStrict() docs at the pin; cfengine#5263 (ENT-10336) made "class exists" benign on purpose, kept; searched cfengine/core and djbclark/core issues and PRs for CMDB class validation, found no other change.

🤖 Generated with Claude Code

A class the insertion API could not insert was treated as one that
already existed, so {"vars":{"good":"x"},"classes":{"default: ":"any::"}}
installed the variable and returned true. EvalContextClassPutTagsSet()
chops trailing whitespace off a copy of the name for its emptiness test,
refusing a default-namespace class of whitespace only, but files the
class under the name as given.

- A class name with whitespace at either end is invalid.
- A class that exists is found by its canonical identity (namespace,
  canonified name) before inserting, not by the raw name, so 'pre-class'
  no longer replaces 'pre_class'. Existing classes stay benign and are
  left alone and never rolled back.
- Any other failed class insertion rolls back what the call installed and
  makes LoadCMDBData() return false. The CMDBTestFailInsertion seam now
  covers classes too.
- A variable that exists already makes the file invalid, so rollback only
  ever removes what the load itself added. The loader runs on a cleared
  context, before augments, so nothing legitimate is refused.

Changelog: None
Ticket: none

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@greptile-apps

greptile-apps Bot commented Nov 14, 2026

Copy link
Copy Markdown

Automatic reviews are disabled for this repository. Configure in Code Review Settings.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T20:38:13.649824Z ba4b4c4 PR opened
🔒 Security Review ✅ Completed 2026-11-14T20:39:53.570732Z ba4b4c4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

djbclark and others added 3 commits November 14, 2026 15:57
Brings in JsonParseStrict() and JsonParseFileStrict() (djbclark/libntech
tendcf-integration cc892efae, from fix/json-strict-parse), which refuse a
duplicate object member and trailing input after the document. The
commits are on the fork's tendcf-integration, not upstream
NorthernTechHQ/libntech.

Changelog: None
Ticket: none

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
host_specific.json was parsed with JsonParseFile(): a repeated object
member replaced the earlier one before validation saw it ({"vars":{
"good":null,"good":"ok"}} loaded), and anything after the first complete
document was ignored. LoadCMDBData() now reads the file with
JsonParseFileStrict() and rejects either, installing nothing.

ReadJsonFile() keeps its behaviour for the augments callers; both share
a static helper that takes the strict flag. Also spells out the headers
cmdb.c and cmdb_test.c used through others (class.h, ctype.h, set.h,
string_lib.h, var_expressions.h).

Unit tests: a repeated member at the top level, in classes, in variables
and in a nested object, with an invalid earlier duplicate; trailing {},
a letter and a bracket; whitespace after the document still loads.

Changelog: None
Ticket: none

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…B data

Each file has a good variable and class beside the offending text; the
agent run must report the parse refusal and define neither.

Changelog: None
Ticket: none

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@djbclark
djbclark merged commit 8b2e1ba into cmdb-fail-closed Nov 14, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant