Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

refacto(remote-server): optimize execution time of export/import #7749

Merged
merged 68 commits into from
Sep 19, 2019

Conversation

cgagnaire
Copy link

@cgagnaire cgagnaire commented Aug 1, 2019

Description

Refactoring export/import routine of remote server.
This PR also include @lpinsivy insertBulk function of PR #7714 even though it's not used.

(Unit tests have to be reworked as classes have been removed).
(Some code can also be remove from Repository classes).

MySQL user 'centreon' needs "FILE" privilege to use "LOAD DATA INFILE" statement :
GRANT FILE on *.* to 'centreon'@'localhost';

Refs: MON-3998

Type of change

  • Patch fixing an issue (non-breaking change)
  • New functionality (non-breaking change)
  • Breaking change (patch or feature) that might cause side effects breaking part of the Software
  • Updating documentation (missing information, typo...)

Target serie

  • 2.8.x
  • 18.10.x
  • 19.04.x
  • 19.10.x (master)

How this pull request can be tested ?

Use the source files in a platform with one remote server and multiple pollers behind the remote server.

Checklist

Community contributors & Centreon team

  • I followed the coding style guidelines provided by Centreon
  • I have commented my code, especially new classes, functions or any legacy code modified. (docblock)
  • I have commented my code, especially hard-to-understand areas of the PR.
  • I have made corresponding changes to the documentation.
  • I have rebased my development branch on the base branch (master, maintenance).

Centreon team only

  • I have made sure that the unit tests related to the story are successful.
  • I have made sure that unit tests cover 80% of the code written for the story.
  • I have made sure that acceptance tests related to the story are successful (local and CI)

@kduret kduret changed the title Refacto remote export import refacto(remose-server): optimize execution time of export/import Aug 5, 2019
@kduret kduret changed the title refacto(remose-server): optimize execution time of export/import refacto(remote-server): optimize execution time of export/import Aug 5, 2019
// (`col_name`, `col_name`,...)

// Construct SQL statement
$sql = "LOAD DATA INFILE '$file'";
Copy link
Contributor

Choose a reason for hiding this comment

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

as you didn't bound the values, you should sanitize them before sending them to the DB.

* Copy directory recusively
*/
private function recursive_copy($src, $dst) {
$dir = opendir($src);
Copy link
Contributor

Choose a reason for hiding this comment

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

please could you sanitize $src and $dst to avoid RCE (using escapeShellArg maybe)

return 0;
}

protected function getHostTemplates(&$host)
Copy link
Contributor

Choose a reason for hiding this comment

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

a getSomething method need to send back something

}
$loop[$host_id] = 1;
if ($host_id == $host_tpl_id) {
return 1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return 1;
return true;

$stack = array_merge($hosts_tpl[$host_id]['htpl'], $stack);
}

return 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return 0;
return false;

Copy link
Contributor

Choose a reason for hiding this comment

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

(used in an if condition in config-generate/servicetemplate.class.php +133 and config-generate-remote/servicetemplate.class.php +101)


protected function getHostTimezone(&$host)
{
# not needed
Copy link
Contributor

Choose a reason for hiding this comment

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

then remove it


protected function createFile($dir)
{
$full_file = $dir . '/' . $this->subdir . '/' . $this->generate_filename;
Copy link
Contributor

Choose a reason for hiding this comment

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

sanitize $this->generate_filename to avoid RCE exploit

Copy link
Contributor

Choose a reason for hiding this comment

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

or use escapeshellarg on $full_file variable

@sc979 sc979 force-pushed the refacto-remote-export-import branch 2 times, most recently from e2da8ca to 356c2b1 Compare August 21, 2019 13:31
@centreon centreon deleted a comment from cgagnaire Aug 21, 2019
@centreon centreon deleted a comment from cgagnaire Aug 28, 2019
@centreon centreon deleted a comment from cgagnaire Aug 28, 2019
@centreon centreon deleted a comment from cgagnaire Aug 28, 2019
@centreon centreon deleted a comment from cgagnaire Aug 28, 2019
@sc979 sc979 force-pushed the refacto-remote-export-import branch from 8fa444f to 9a02592 Compare August 28, 2019 07:30
@centreon centreon deleted a comment from cgagnaire Aug 28, 2019
@sc979 sc979 requested review from kduret and callapa August 28, 2019 08:43
@sc979
Copy link
Contributor

sc979 commented Aug 28, 2019

I removed "request changes" taken in account and the outdated comments.
The branch have been rebased

@sc979
Copy link
Contributor

sc979 commented Aug 28, 2019

@cgagnaire -> About : "MySQL user 'centreon' needs "FILE" privilege to use "LOAD DATA INFILE" statement :
GRANT FILE on . to 'centreon'@'localhost';" -> On which page should this sentence be added to the doc ?

Copy link
Contributor

@callapa callapa left a comment

Choose a reason for hiding this comment

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

Always the sames comments:
Le first letter of class name must be capitalized.
Le class name must be equal to his file.
Remove the '.class' in the name of a class file.
Use a boolean value instead of integer


// insert data
$exportPathFile = $this->getFile($data[filename]);
echo date("Y-m-d H:i:s") . " - INFO - Loading '" . $exportPathFile . "'.\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not printing message directly from the method.
For example you can inject a logger class instance and use it to log message.

} catch (\ErrorException $e) {
// rollback changes
$db->rollBack();
echo date("Y-m-d H:i:s") . " - ERROR - Loading failed.\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as above

if (is_dir($src . '/' . $file)) {
$this->recursiveCopy($src . '/' . $file, $dst . '/' . $file);
} else {
echo date("Y-m-d H:i:s") . " - INFO - Copying '" . $src . "/" . $file . "'.\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

Do not printing message directly from the method.
For example you can inject a logger class instance and use it to log message.

www/class/config-generate-remote/serviceCategory.class.php Outdated Show resolved Hide resolved
www/class/config-generate-remote/servicegroup.class.php Outdated Show resolved Hide resolved
www/class/config-generate-remote/servicetemplate.class.php Outdated Show resolved Hide resolved
www/class/config-generate-remote/timeperiod.class.php Outdated Show resolved Hide resolved
www/class/config-generate-remote/trap.class.php Outdated Show resolved Hide resolved
Copy link
Contributor

@callapa callapa left a comment

Choose a reason for hiding this comment

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

Always the same comments:

  • The first letter of class name must be capitalized.
  • The class name must be equal to his file.
  • Remove the '.class' in the name of a class file.
  • Use a boolean value instead of integer

@sc979 sc979 force-pushed the refacto-remote-export-import branch 2 times, most recently from 26100fc to 0ca1f8f Compare September 3, 2019 09:55
{
$severityId = HostCategory::getInstance($this->dependencyInjector)->getHostSeverityByHostId($hostIdArg);
if (!is_null($severityId)) {
Relations\HostCategoriesRelation::getInstance($this->dependencyInjector)->addRelation($severityId, $hostIdArg);

Choose a reason for hiding this comment

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

WARN: Line exceeds 120 characters; contains 123 characters

@kduret kduret merged commit 21c80f0 into master Sep 19, 2019
@kduret kduret deleted the refacto-remote-export-import branch September 19, 2019 12:58
cgagnaire added a commit that referenced this pull request Sep 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants