Skip to content

Commit

Permalink
Merge pull request #57 from monarc-project/bugfix/339
Browse files Browse the repository at this point in the history
Bugfix/339
  • Loading branch information
Ruslan Baidan authored Oct 27, 2021
2 parents 24e0f2b + a552a29 commit 2e3aeb8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Model/Entity/InstanceRiskSuperClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ public function getInstance()
public function setInstance($instance): self
{
$this->instance = $instance;
$this->instance->addInstanceRisk($this);

return $this;
}
Expand Down
23 changes: 23 additions & 0 deletions src/Model/Entity/InstanceSuperClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ class InstanceSuperClass extends AbstractEntity
*/
protected $instanceConsequences;

/**
* @var InstanceRiskSuperClass[]|ArrayCollection
*
* @ORM\OneToMany(targetEntity="InstanceRisk", mappedBy="instance")
*/
protected $instanceRisks;

/**
* @var string
*
Expand Down Expand Up @@ -239,6 +246,7 @@ class InstanceSuperClass extends AbstractEntity
public function __construct($obj = null)
{
$this->instanceConsequences = new ArrayCollection();
$this->instanceRisks = new ArrayCollection();

parent::__construct($obj);
}
Expand Down Expand Up @@ -566,6 +574,21 @@ public function resetInstanceConsequences(): self
return $this;
}

public function getInstanceRisks()
{
return $this->instanceRisks;
}

public function addInstanceRisk(InstanceRiskSuperClass $instanceRisk): self
{
if (!$this->instanceRisks->contains($instanceRisk)) {
$this->instanceRisks->add($instanceRisk);
$instanceRisk->setInstance($this);
}

return $this;
}

/**
* Returns the instance hierarchy array ordered from it's root through all the children to the instance itself.
* Each element is a normalized array of instance properties.
Expand Down
46 changes: 42 additions & 4 deletions src/Service/InstanceRiskService.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,14 @@ public function getInstanceRisks(int $anrId, ?int $instanceId, array $params = [
$threat = $instanceRisk->getThreat();
$vulnerability = $instanceRisk->getVulnerability();
$key = 'r' . $instanceRisk->getId();
$isInstanceRiskHasToBeSet = true;
if ($object->isScopeGlobal()) {
$key = 'o' . $object->getUuid() . '-' . $threat->getUuid() . '-' . $vulnerability->getUuid();
if (isset($result[$key])) {
$isInstanceRiskHasToBeSet = $this->shouldInstanceRiskBeAddedToResults($instanceRisk, $result[$key]);
}
}
if (!isset($result[$key])
|| !$object->isScopeGlobal()
|| $result[$key]['max_risk'] < $instanceRisk->getCacheMaxRisk()
) {
if (!$object->isScopeGlobal() || $isInstanceRiskHasToBeSet) {
$result[$key] = $this->addCustomFieldsToInstanceRiskResult($instanceRisk, [
'id' => $instanceRisk->getId(),
'oid' => $object->getUuid(),
Expand Down Expand Up @@ -573,4 +574,41 @@ private function extractInstancesAndTheirChildrenIds(array $instances): array

return $instancesIds;
}

/**
* Determines whether the instance risk should be added to the list result in case. Only for global objects.
*
* @param InstanceRiskSuperClass $instanceRisk
* @param array $valuesToCompare
*
* @return bool
*/
private function shouldInstanceRiskBeAddedToResults(
InstanceRiskSuperClass $instanceRisk,
array $valuesToCompare
): bool {
$instance = $instanceRisk->getInstance();
$isMaxRiskSet = false;
foreach ($instance->getInstanceRisks() as $instanceRiskToValidate) {
if ($instanceRiskToValidate->getCacheMaxRisk() !== -1) {
$isMaxRiskSet = true;
break;
}
}
if ($isMaxRiskSet) {
return $valuesToCompare['max_risk'] < $instanceRisk->getCacheMaxRisk();
}

/* We compare CIA criteria in case if max risk value is not set. */
$maxExistedCia = max($valuesToCompare['c_impact'], $valuesToCompare['i_impact'], $valuesToCompare['d_impact']);
$maxCurrentCia = max($instance->getConfidentiality(), $instance->getIntegrity(), $instance->getAvailability());
if ($maxExistedCia === $maxCurrentCia) {
$sumExistedCia = $valuesToCompare['c_impact'] + $valuesToCompare['i_impact'] + $valuesToCompare['d_impact'];
$sumCurrentCia = $instance->getConfidentiality() + $instance->getIntegrity() + $instance->getAvailability();

return $sumExistedCia < $sumCurrentCia;
}

return $maxExistedCia < $maxCurrentCia;
}
}

0 comments on commit 2e3aeb8

Please sign in to comment.