Repository navigation
Avoid inconsistent modified attributes state after object updates were rejected due to validation errors - #10939
julianbrost wants to merge 1 commit into
Conversation
Otherwise, the value may be rejected, leaving original_attributes in the already changed but now inconsistent state.
jschmidt-icinga
left a comment
There was a problem hiding this comment.
I've looked at the test commands and confirmed the problem and solution.
One small issue I had with reproducing this is that a podman compose restart on my container setup seems to wipe modified-attributes.conf but not the object in api, but that's very likely unrelated to this PR and with the API restart I could reproduce it immediately.
Two small suggestions below:
| void ConfigObject::ModifyAttribute(const String& attr, const Value& value, bool updateVersion) | ||
| { | ||
| Dictionary::Ptr original_attributes = GetOriginalAttributes(); | ||
| std::vector<std::pair<String, Value>> orig_attr_updates; |
There was a problem hiding this comment.
Maybe this declaration can be moved a little closer to where it's used.
There was a problem hiding this comment.
Also using snake_case as a variable name in 2026 :).
| String key = prefix + "." + kv.first; | ||
| if (!original_attributes->Contains(key)) | ||
| original_attributes->Set(key, kv.second); | ||
| orig_attr_updates.emplace_back(prefix + "." + kv.first, kv.second); |
There was a problem hiding this comment.
Maybe std::move(kv.second) here and std::move(oldValue) in the two cases below. Probably doesn't matter much unless the Value contains a really big string, but still...
Update
original_attributesonly after validation of the new value. Otherwise, the value may be rejected, leaving original_attributes in the already changed but now inconsistent state.I noticed that issue while working on #10940, but I've created this as a separate PR as this can also be viewed as an independent issue. As shown by the tests below, this can already be triggered even without that PR.
Tests
The following test creates an object, then tries to modify a custom variable with an invalid value and then observes the resulting behavior.
Before (master as of 8ff3d58)
Create an object for testing (having non-empty
varsseems to be important for reproducing this):Attempt to modify its custom variables without success (as expected, it fails):
Now query the object, in particular the
original_attributes, you'll see that even though the update was rejected,vars.oopswas added there:Now restart Icinga 2:
And perform the very same query again, where you can observe, that now the object has a mysterious
vars.oopswith valuenull:This PR (as of 0332ead)
If the object is still there from the previous test, make sure to delete it.
Creating (PUT) and attempting to update (POST) produces the same responses, the difference only starts to show with the following GET request, where nothing was added to
original_attributes:Which - in contrast to the previous behavior - also stays the same across a restart of Icinga 2.
Additional Information
The value of
original_attributesis important here as this is used to create/var/lib/icinga2/modified-attributes.confwhich then contains something like this with the unfixed version, explaining why this suddenly appears withinvarsafter the restart: