Skip to content

Commit

Permalink
Improved the export and extended import, removed the objects position…
Browse files Browse the repository at this point in the history
… field, recommendations fixes, added relation of rolf tags and objects.
  • Loading branch information
ruslanbaidan committed Jun 10, 2024
1 parent 493062a commit 9e634d7
Show file tree
Hide file tree
Showing 53 changed files with 1,442 additions and 815 deletions.
26 changes: 17 additions & 9 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -1571,15 +1571,23 @@
Service\SoaScaleCommentService::class => AutowireFactory::class,
CronTask\Service\CronTaskService::class => AutowireFactory::class,
/* Export services. */
Export\Service\AnrExportService::class => ReflectionBasedAbstractFactory::class,
Export\Service\AssetExportService::class => AutowireFactory::class,
Export\Service\AnrExportService::class => AutowireFactory::class,
Export\Service\ObjectExportService::class => AutowireFactory::class,
Export\Service\SoaScaleCommentExportService::class => AutowireFactory::class,
Export\Service\OperationalRiskScalesExportService::class => AutowireFactory::class,
Export\Service\InstanceExportService::class => AutowireFactory::class,
/* Import services. */
Import\Service\ObjectImportService::class => AutowireFactory::class,
Import\Service\AssetImportService::class => AutowireFactory::class,
Import\Service\InstanceImportService::class => AutowireFactory::class,
Import\Processor\AssetImportProcessor::class => AutowireFactory::class,
Import\Processor\InformationRiskImportProcessor::class => AutowireFactory::class,
Import\Processor\ObjectCategoryImportProcessor::class => AutowireFactory::class,
Import\Processor\ObjectImportProcessor::class => AutowireFactory::class,
Import\Processor\OperationalRisksImportProcessor::class => AutowireFactory::class,
Import\Processor\RecommendationImportProcessor::class => AutowireFactory::class,
Import\Processor\ReferentialImportProcessor::class => AutowireFactory::class,
Import\Processor\RolfTagImportProcessor::class => AutowireFactory::class,
Import\Processor\ThreatImportProcessor::class => AutowireFactory::class,
Import\Processor\VulnerabilityImportProcessor::class => AutowireFactory::class,

// Helpers
Import\Helper\ImportCacheHelper::class => AutowireFactory::class,
Expand Down Expand Up @@ -1684,10 +1692,8 @@
},

// Commands
Import\Command\ImportAnalysesCommand::class => static function (
ContainerInterface $container,
$serviceName
) {
Import\Command\ImportAnalysesCommand::class => static function (ContainerInterface $container)
{

Check failure on line 1696 in config/module.config.php

View workflow job for this annotation

GitHub Actions / build

Opening brace should be on the same line as the declaration
/** @var ConnectedUserService $connectedUserService */
$connectedUserService = $container->get(ConnectedUserService::class);
$connectedUserService->setConnectedUser(new Entity\User([
Expand All @@ -1711,7 +1717,9 @@
'lazy_services' => [
'class_map' => [
Table\UserTokenTable::class => Table\UserTokenTable::class,
Service\AnrInstanceService::class => Service\AnrInstanceService::class
Service\AnrInstanceService::class => Service\AnrInstanceService::class,
Import\Processor\ObjectCategoryImportProcessor::class =>
Import\Processor\ObjectCategoryImportProcessor::class,
],
'proxies_target_dir' => $dataPath . '/LazyServices/Proxy',
'write_proxy_files' => $env === 'production',
Expand Down
42 changes: 7 additions & 35 deletions migrations/db/20230901112005_fix_positions_cleanup_db.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,41 +49,6 @@ public function change()
$previousAnrId = $amvData['anr_id'];
}

/* Fix the objects positions. */
$objectsQuery = $this->query(
'SELECT uuid, anr_id, object_category_id, position
FROM objects
ORDER BY anr_id, object_category_id, position'
);
$previousObjectCategoryId = null;
$previousAnrId = null;
$expectedObjectPosition = 1;
foreach ($objectsQuery->fetchAll() as $objectData) {
if ($previousObjectCategoryId === null) {
$previousObjectCategoryId = $objectData['object_category_id'];
$previousAnrId = $objectData['anr_id'];
}
if ($objectData['object_category_id'] !== $previousObjectCategoryId
|| $previousAnrId !== $objectData['anr_id']
) {
$expectedObjectPosition = 1;
}

if ($expectedObjectPosition !== $objectData['position']) {
$this->execute(
sprintf(
'UPDATE objects SET position = %d WHERE uuid = "%s"',
$expectedObjectPosition,
$objectData['uuid']
)
);
}

$expectedObjectPosition++;
$previousObjectCategoryId = $objectData['object_category_id'];
$previousAnrId = $objectData['anr_id'];
}

/* Fix the objects compositions positions. */
$objectsQuery = $this->query(
'SELECT id, anr_id, father_id, position FROM objects_objects ORDER BY anr_id, father_id, position'
Expand Down Expand Up @@ -183,6 +148,7 @@ public function change()
->removeColumn('disponibility')
->removeColumn('token_import')
->removeColumn('original_name')
->removeColumn('position')
->update();
$this->table('instances_consequences')->removeColumn('object_id')->removeColumn('locally_touched')->update();

Expand Down Expand Up @@ -282,6 +248,12 @@ public function change()
$this->table('measures_amvs')
->changePrimaryKey(['measure_id', 'amv_id', 'anr_id'])
->addForeignKey('measure_id', 'measures', 'id', ['delete' => 'CASCADE', 'update' => 'RESTRICT'])
->addForeignKey(
['amv_id', 'anr_id'],
'amvs',
['uuid', 'anr_id'],
['delete' => 'CASCADE', 'update' => 'RESTRICT']
)
->update();
$this->table('measures_amvs')->removeColumn('measure_uuid')->update();
/* Apply measures relation to measures_rolf_risks. */
Expand Down
7 changes: 7 additions & 0 deletions src/Entity/Recommendation.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,13 @@ public function setDueDate(?DateTime $date): self
return $this;
}

public function setDueDateFromString(string $dateString): self
{
$this->dueDate = new DateTime($dateString);

return $this;
}

public function getRecommendationSet(): RecommendationSet
{
return $this->recommendationSet;
Expand Down
60 changes: 35 additions & 25 deletions src/Export/Service/AnrExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,13 @@ private function prepareExportData(Entity\Anr $anr, array $exportParams): array
'withSoas' => $withSoas,
'withRecords' => $withRecords,
'withLibrary' => $withLibrary,
'withKnowledge' => $withKnowledgeBase,
'withKnowledgeBase' => $withKnowledgeBase,
'languageCode' => $anr->getLanguageCode(),
'languageIndex' => $anr->getLanguage(),
'knowledgeBase' => $withKnowledgeBase
? $this->prepareKnowledgeBaseData($anr, $withEval, $withControls, $withRecommendations)
: [],
'library' => $withLibrary ? $this->prepareLibraryData($anr) : [],
'library' => $withLibrary ? $this->prepareLibraryData($anr, !$withKnowledgeBase) : [],
'instances' => $this
->prepareInstancesData($anr, !$withLibrary, $withEval, $withControls, $withRecommendations),
'anrInstanceMetadataFields' => $this->prepareAnrInstanceMetadataFieldsData($anr),
Expand Down Expand Up @@ -141,9 +141,9 @@ private function prepareKnowledgeBaseData(
'threats' => $this->prepareThreatsData($anr, $withEval),
'vulnerabilities' => $this->prepareVulnerabilitiesData($anr),
'referentials' => $withControls ? $this->prepareReferentialsData($anr) : [],
'informationRisks' => $this->prepareInformationRisksData($anr, $withEval),
'tags' => $this->prepareTagsData($anr),
'operationalRisks' => $this->prepareOperationalRisksData($anr),
'informationRisks' => $this->prepareInformationRisksData($anr, $withEval, $withControls),
'rolfTags' => $this->prepareRolfTagsData($anr),
'operationalRisks' => $this->prepareOperationalRisksData($anr, $withControls),
'recommendationSets' => $withRecommendations ? $this->prepareRecommendationSetsData($anr) : [],
];
}
Expand Down Expand Up @@ -184,18 +184,18 @@ private function prepareVulnerabilitiesData(Entity\Anr $anr): array
return $result;
}

private function prepareInformationRisksData(Entity\Anr $anr, bool $withEval): array
private function prepareInformationRisksData(Entity\Anr $anr, bool $withEval, bool $withControls): array
{
$result = [];
/** @var Entity\Amv $amv */
foreach ($this->amvTable->findByAnr($anr) as $amv) {
$result[] = $this->prepareInformationRiskData($amv);
$result[] = $this->prepareInformationRiskData($amv, $withEval, $withControls, false);
}

return $result;
}

private function prepareTagsData(Entity\Anr $anr): array
private function prepareRolfTagsData(Entity\Anr $anr): array
{
$result = [];
$languageIndex = $anr->getLanguage();
Expand All @@ -211,13 +211,13 @@ private function prepareTagsData(Entity\Anr $anr): array
return $result;
}

private function prepareOperationalRisksData(Entity\Anr $anr): array
private function prepareOperationalRisksData(Entity\Anr $anr, bool $withControls): array
{
$result = [];
$languageIndex = $anr->getLanguage();
/** @var Entity\RolfRisk $rolfRisk */
foreach ($this->rolfRiskTable->findByAnr($anr) as $rolfRisk) {
$result[] = $this->prepareOperationalRiskData($rolfRisk, $languageIndex);
$result[] = $this->prepareOperationalRiskData($rolfRisk, $languageIndex, $withControls, false);
}

return $result;
Expand Down Expand Up @@ -266,51 +266,61 @@ private function prepareRecommendationSetsData(Entity\Anr $anr): array
return $result;
}

private function prepareLibraryData(Entity\Anr $anr): array
private function prepareLibraryData(Entity\Anr $anr, bool $addRolfRisksInObjects): array
{
return [
'categories' => $this->prepareCategoriesAndObjects($anr),
'categories' => $this->prepareCategoriesAndObjects($anr, $addRolfRisksInObjects),
];
}

private function prepareCategoriesAndObjects(Entity\Anr $anr): array
private function prepareCategoriesAndObjects(Entity\Anr $anr, bool $addRolfRisksInObjects): array
{
$result = [];
$languageIndex = $anr->getLanguage();
foreach ($this->objectCategoryTable->findRootCategoriesByAnrOrderedByPosition($anr) as $objectCategory) {
$result[] = $this->prepareCategoryData($objectCategory, $languageIndex, true);
$result[] = $this->prepareCategoryData($objectCategory, $languageIndex, true, $addRolfRisksInObjects);
}

return $result;
}

private function prepareChildrenCategoriesData(Entity\ObjectCategory $objectCategory, int $languageIndex): array
{
private function prepareChildrenCategoriesData(
Entity\ObjectCategory $objectCategory,
int $languageIndex,
bool $addRolfRisksInObjects
): array {
$result = [];
foreach ($objectCategory->getChildren() as $childObjectCategory) {
$result[] = $this
->prepareCategoryData($childObjectCategory, $languageIndex, false);
$result[] = $this->prepareCategoryData($childObjectCategory, $languageIndex, false, $addRolfRisksInObjects);
}

return $result;
}

private function prepareObjectsDataOfCategory(Entity\ObjectCategory $objectCategory, int $languageIndex): array
{
private function prepareObjectsDataOfCategory(
Entity\ObjectCategory $objectCategory,
int $languageIndex,
bool $addRolfRisksInObjects
): array {
$result = [];
foreach ($objectCategory->getObjects() as $object) {
$result[] = $this->prepareObjectData($object, $languageIndex, false);
$result[] = $this->prepareObjectData($object, $languageIndex, false, false, $addRolfRisksInObjects);
}

return $result;
}

private function prepareCategoryData(Entity\ObjectCategory $objectCategory, int $languageIndex, bool $isRoot): array
{
private function prepareCategoryData(
Entity\ObjectCategory $objectCategory,
int $languageIndex,
bool $isRoot,
bool $addRolfRisksInObjects
): array {
return [
'label' => $objectCategory->getLabel($languageIndex),
'children' => $this->prepareChildrenCategoriesData($objectCategory, $languageIndex),
'objects' => $this->prepareObjectsDataOfCategory($objectCategory, $languageIndex),
'children' => $this->prepareChildrenCategoriesData($objectCategory, $languageIndex, $addRolfRisksInObjects),
'objects' => $this->prepareObjectsDataOfCategory($objectCategory, $languageIndex, $addRolfRisksInObjects),
'position' => $objectCategory->getPosition(),
'isRoot' => $isRoot,
];
}
Expand Down
6 changes: 3 additions & 3 deletions src/Export/Service/InstanceExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ private function prepareExportData(Entity\Instance $instance, array $exportParam
'export_datetime' => (new \DateTime())->format('Y-m-d H:i:s'),
'languageCode' => $anr->getLanguageCode(),
'languageIndex' => $anr->getLanguage(),
'with_eval' => $withEval,
'with_controls' => $withControls,
'with_recommendations' => $withRecommendations,
'withEval' => $withEval,
'withControls' => $withControls,
'withRecommendations' => $withRecommendations,
'instance' => $this->prepareInstanceData(
$instance,
$languageIndex,
Expand Down
11 changes: 9 additions & 2 deletions src/Export/Service/Traits/AssetExportTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,15 @@ trait AssetExportTrait
'description' => "string",
'type' => "int",
'status' => "int"
])] private function prepareAssetData(Entity\Asset $asset, int $languageIndex): array
{
])] private function prepareAssetData(
Entity\Asset $asset,
int $languageIndex,
bool $includeCompleteData = true
): array {
if (!$includeCompleteData) {
return ['uuid' => $asset->getUuid()];
}

return [
'uuid' => $asset->getUuid(),
'code' => $asset->getCode(),
Expand Down
17 changes: 12 additions & 5 deletions src/Export/Service/Traits/InformationRiskExportTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ trait InformationRiskExportTrait
])] private function prepareInformationRiskData(
Entity\Amv $amv,
bool $withEval = false,
bool $withControls = true
bool $withControls = true,
bool $includeCompleteRelationData = true
): array {
/** @var Entity\Asset $asset */
$asset = $amv->getAsset();
Expand All @@ -40,15 +41,21 @@ trait InformationRiskExportTrait
$measuresData = [];
if ($withControls) {
foreach ($amv->getMeasures() as $measure) {
$measuresData[] = $this->prepareMeasureData($measure, $languageIndex);
$measuresData[] = $this->prepareMeasureData(
$measure,
$languageIndex,
false,
$includeCompleteRelationData
);
}
}

return [
'uuid' => $amv->getUuid(),
'asset' => $this->prepareAssetData($asset, $languageIndex),
'threat' => $this->prepareThreatData($threat, $languageIndex, $withEval),
'vulnerability' => $this->prepareVulnerabilityData($vulnerability, $languageIndex),
'asset' => $this->prepareAssetData($asset, $languageIndex, $includeCompleteRelationData),
'threat' => $this->prepareThreatData($threat, $languageIndex, $withEval, $includeCompleteRelationData),
'vulnerability' => $this
->prepareVulnerabilityData($vulnerability, $languageIndex, $includeCompleteRelationData),
'measures' => $measuresData,
'status' => $amv->getStatus(),
];
Expand Down
2 changes: 1 addition & 1 deletion src/Export/Service/Traits/InstanceExportTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private function prepareInstanceData(
'integrityInherited' => $withEval ? (int)$instance->isIntegrityInherited() : 1,
'availabilityInherited' => $withEval ? (int)$instance->isAvailabilityInherited() : 1,
'asset' => $this->prepareAssetData($asset, $languageIndex),
/* For Anr and Instance export instanceRisks are added to the instance, so not needed in asset. */
/* For Anr and Instance export instanceRisks are added to the instance, so not needed in AMVs in asset. */
'object' => $includeCompleteObjectData
? $this->prepareObjectData($object, $languageIndex, false)
: ['uuid' => $instance->getObject()->getUuid()],
Expand Down
7 changes: 6 additions & 1 deletion src/Export/Service/Traits/MeasureExportTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ trait MeasureExportTrait
])] private function prepareMeasureData(
Entity\Measure $measure,
int $languageIndex,
bool $includeLinks = false
bool $includeLinks = false,
bool $includeCompleteData = true
): array {
if (!$includeCompleteData) {
return ['uuid' => $measure->getUuid()];
}

$result = [
'uuid' => $measure->getUuid(),
'code' => $measure->getCode(),
Expand Down
Loading

0 comments on commit 9e634d7

Please sign in to comment.