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

Handle antivirus updates from partial inventory #17665

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions phpunit/functional/Glpi/Inventory/Assets/AntivirusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,5 +350,175 @@ public function testInventoryUpdate()

$this->assertTrue($antivirus->getFromDB($antivirus_3_id));
$this->assertSame(0, $antivirus->fields['is_dynamic']);

//remove all antiviruses from inventory
$xml_source = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
<REQUEST>
<CONTENT>
<HARDWARE>
<NAME>pc002</NAME>
</HARDWARE>
<BIOS>
<SSN>ggheb7ne7</SSN>
</BIOS>
<VERSIONCLIENT>FusionInventory-Agent_v2.3.19</VERSIONCLIENT>
</CONTENT>
<DEVICEID>test-pc002</DEVICEID>
<QUERY>INVENTORY</QUERY>
</REQUEST>";

$this->doInventory($xml_source, true);

//we now have 1 antivirus only
$results = $antivirus->find(['computers_id' => $computers_id]);
$this->assertCount(1, $results);

trasher marked this conversation as resolved.
Show resolved Hide resolved
}

public function testPartialUpdate()
{
$computer = new \Computer();
$antivirus = new \ComputerAntivirus();

//create manually a computer, with 3 antivirus
$computers_id = $computer->add([
'name' => 'pc002',
'serial' => 'ggheb7ne7',
'entities_id' => 0
]);
$this->assertGreaterThan(0, $computers_id);

$antivirus_1_id = $antivirus->add([
'computers_id' => $computers_id,
'name' => 'Kaspersky Endpoint Security 10 for Windows',
'antivirus_version' => '2021 21.3.10.391',
'is_active' => 1
]);
$this->assertGreaterThan(0, $antivirus_1_id);

$antivirus_2_id = $antivirus->add([
'computers_id' => $computers_id,
'name' => 'Microsoft Security Essentials',
'antivirus_version' => '4.3.216.0',
'is_active' => 1
]);
$this->assertGreaterThan(0, $antivirus_2_id);

$antivirus_3_id = $antivirus->add([
'computers_id' => $computers_id,
'name' => 'Avast Antivirus',
'antivirus_version' => '19',
'is_active' => 1
]);
$this->assertGreaterThan(0, $antivirus_3_id);

$results = $antivirus->find(['computers_id' => $computers_id]);
$this->assertCount(3, $results);
foreach ($results as $result) {
$this->assertEquals(0, $result['is_dynamic']);
}

$source = '{
"action": "inventory",
"content": {
"hardware": {
"name": "pc002"
},
"antivirus": [
{
"base_version": "20200310.007",
"company": "Kaspersky",
"enabled": true,
"guid": "{B41C7598-35F6-4D89-7D0E-7ADE69B4047B}",
"name": "Kaspersky Endpoint Security 10 for Windows",
"uptodate": true,
"version": "2021 21.3.10.391"
},
{
"company": "Microsoft Corporation",
"enabled": true,
"guid": "{641105E6-77ED-3F35-A304-765193BCB75F}",
"name": "Microsoft Security Essentials",
"uptodate": true,
"version": "4.3.216.0"
}
],
"versionclient": "GLPI-Agent_v1.4"
},
"deviceid": "pc.site.ru-2023-01-20-11-41-00",
"itemtype": "Computer"
}';

//computer inventory knows only 2 antivirus: Microsoft and Kaspersky
$this->doInventory(json_decode($source));

//we still have 3 antivirus linked to the computer
$results = $antivirus->find(['computers_id' => $computers_id]);
$this->assertCount(3, $results);

//antivirus present in the inventory source are now dynamic
$results = $antivirus->find(['computers_id' => $computers_id, 'is_dynamic' => 1]);
$this->assertCount(2, $results);

$this->assertTrue($antivirus->getFromDB($antivirus_1_id));
$this->assertSame(1, $antivirus->fields['is_dynamic']);

$this->assertTrue($antivirus->getFromDB($antivirus_2_id));
$this->assertSame(1, $antivirus->fields['is_dynamic']);

//antivirus not present in the inventory is still not dynamic
$results = $antivirus->find(['computers_id' => $computers_id, 'is_dynamic' => 0]);
$this->assertCount(1, $results);

$this->assertTrue($antivirus->getFromDB($antivirus_3_id));
$this->assertSame(0, $antivirus->fields['is_dynamic']);

//Redo a partial inventory, with removed microsoft antivirus
$source = '{
"action": "inventory",
"content": {
"hardware": {
"name": "pc002"
},
"antivirus": [
{
"base_version": "20200310.007",
"company": "Kaspersky",
"enabled": true,
"guid": "{B41C7598-35F6-4D89-7D0E-7ADE69B4047B}",
"name": "Kaspersky Endpoint Security 10 for Windows",
"uptodate": true,
"version": "2021 21.3.10.391"
}
],
"versionclient": "GLPI-Agent_v1.4"
},
"deviceid": "pc.site.ru-2023-01-20-11-41-00",
"itemtype": "Computer",
"partial": true
}';

$this->doInventory(json_decode($source));

//we now have 2 antivirus only
$results = $antivirus->find(['computers_id' => $computers_id]);
$this->assertCount(2, $results);

//antivirus present in the inventory source are still dynamic
$results = $antivirus->find(['computers_id' => $computers_id, 'is_dynamic' => 1]);
$this->assertCount(1, $results);

$this->assertTrue($antivirus->getFromDB($antivirus_1_id));
$this->assertSame(1, $antivirus->fields['is_dynamic']);

//microsoft has been removed
$this->assertFalse($antivirus->getFromDB($antivirus_2_id));

//antivirus not present in the inventory is still not dynamic
$results = $antivirus->find(['computers_id' => $computers_id, 'is_dynamic' => 0]);
$this->assertCount(1, $results);

$this->assertTrue($antivirus->getFromDB($antivirus_3_id));
$this->assertSame(0, $antivirus->fields['is_dynamic']);
}
}
4 changes: 2 additions & 2 deletions src/Inventory/Asset/Antivirus.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function handle()
$value = $this->data;
$computerAntivirus = new ComputerAntivirus();

//check for existing
//check for existing
foreach ($value as $k => $val) {
$compare = ['name' => $val->name, 'antivirus_version' => $val->antivirus_version];
$compare = array_map('strtolower', $compare);
Expand All @@ -135,7 +135,7 @@ public function handle()
}
}

if ((!$this->main_asset || !$this->main_asset->isPartial()) && count($db_antivirus) !== 0) {
if (count($db_antivirus) !== 0) {
foreach ($db_antivirus as $idtmp => $data) {
if ($data['is_dynamic'] == 1) {
$computerAntivirus->delete(['id' => $idtmp], true);
Expand Down
2 changes: 1 addition & 1 deletion src/Inventory/Asset/MainAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ public function rulepassed($items_id, $itemtype, $rules_id, $ports_id = 0)
$input = $this->handleInput($val, $this->item);

if ($this->isNew()) {
// ONADD were already exececuted, and we want to skip rules that are only ONUPDATE
// ONADD were already executed, and we want to skip rules that are only ONUPDATE
$input['_skip_rules'] = true;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Inventory/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,8 @@ function ($property_name) {
$empty_props = [
'virtualmachines',
'remote_mgmt',
'monitors'
'monitors',
'antivirus',
];
}

Expand Down
Loading