Check config file was written correctly by JasonMortonNZ · Pull Request #18024 · matomo-org/matomo · GitHub
Skip to content
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

Check config file was written correctly #18024

Merged
merged 10 commits into from
Sep 22, 2021
Merged

Check config file was written correctly #18024

merged 10 commits into from
Sep 22, 2021

Conversation

JasonMortonNZ
Copy link
Contributor

Description:

Issue: dev-2272

Whenever we save the config file, it be great to double check if the config file was written correctly. This PR adds a couple of small sanity checks on the config file upon writing its content and writes a log entry should the sanity checks not pass.

Review

@JasonMortonNZ JasonMortonNZ marked this pull request as ready for review September 20, 2021 21:33
@JasonMortonNZ JasonMortonNZ added the Needs Review PRs that need a code review label Sep 20, 2021
@JasonMortonNZ JasonMortonNZ added this to the 4.5.0 milestone Sep 20, 2021
Copy link
Member

@tsteur tsteur left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work 👍 , left a few comments as realised the check we use on Cloud might not work for On-Premise. Sorry about that.

core/Config.php Outdated Show resolved Hide resolved
core/Config.php Show resolved Hide resolved
core/Config.php Outdated Show resolved Hide resolved
core/Config.php Outdated Show resolved Hide resolved
core/Config.php Outdated
@@ -410,13 +410,15 @@ protected function writeConfig()
throw $this->getConfigNotWritableException();
}

if (!$this->sanityCheck($localPath, $output)) {
// If sanity check fails, try to write the contents once more before logging the issue.
if (@file_put_contents($localPath, $output, LOCK_EX === false)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JasonMortonNZ I think there's a typo re the === false which should be outside the function call?

Was also wondering if we should maybe check again the if to also do the sanity check again but not 100% sure. It may be useful I suppose but not sure if there could be any issues with that.

if (@file_put_contents($localPath, $output, LOCK_EX) === false
                    || !$this->sanityCheck($localPath, $output)) {

if (!$this->sanityCheck($localPath, $output)) {
// If sanity check fails, try to write the contents once more before logging the issue.
if (@file_put_contents($localPath, $output, LOCK_EX === false)) {
StaticContainer::get(LoggerInterface::class)->info("The configuration file {$localPath} did not write correctly.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we will want to post the event here before the log call in case we adjust the above if statement to also check for the sanityCheck. Otherwise we would post the event twice. I suppose putting the event here would make it harder to test if the event was triggered and it be no longer possible to properly test it I suppose.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsteur yes, I was thinking about sanity check in this above if too and moving the event to be posted within, but it was tricky for the tests. I have added an optional 3rd argument $notify to the sanityCheck() function which allows us to set this to true on the second try, and this will trigger the event to be posted. This should prevent the event being posted twice, and also keep testability.

core/Config.php Show resolved Hide resolved
core/Config.php Outdated
$content = @file_get_contents($localPath);

if (trim($content) !== trim($expectedContent)) {
Piwik::postEvent('Core.configFileSanityCheckFailed', [$localPath]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the event it would be great to also add a little bit of text through a comment. The event will then be documented automatically on https://developer.matomo.org/api-reference/events

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tsteur does the comment have to follow a specific format for it to be automatically documented?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JasonMortonNZ something similar to https://github.com/matomo-org/matomo/pull/18024/files#diff-8edb7d933e422f060b96277680eaada01d6ed1a7139e20963363b9973d625d12L415 where the comment is for the configFileChanged event. Basically, each variable in the array should be documented using @param and otherwise just regular comment AFAIK

@tsteur tsteur merged commit 40f0ce2 into 4.x-dev Sep 22, 2021
@tsteur tsteur deleted the dev-2272 branch September 22, 2021 21:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Review PRs that need a code review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants