-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds commands for tagging and scanning
this commit adds commands for tagging and scanning so the test don't have to rely on a cron run.
- Loading branch information
1 parent
1f1638d
commit 2efc873
Showing
7 changed files
with
189 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace OCA\GDataVaas\Command; | ||
|
||
use Exception; | ||
use OCA\GDataVaas\Service\TagService; | ||
use OCA\GDataVaas\Service\VerdictService; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\BackgroundJob\TimedJob; | ||
use OCP\IConfig; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class ScanCommand extends Command { | ||
private const APP_ID = "gdatavaas"; | ||
|
||
private TagService $tagService; | ||
private VerdictService $scanService; | ||
private IConfig $appConfig; | ||
private LoggerInterface $logger; | ||
|
||
public function __construct(LoggerInterface $logger, TagService $tagService, VerdictService $scanService, IConfig $appConfig) { | ||
parent::__construct(); | ||
|
||
$this->logger = $logger; | ||
$this->tagService = $tagService; | ||
$this->scanService = $scanService; | ||
$this->appConfig = $appConfig; | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function configure() { | ||
$this->setName('gdatavaas:scan'); | ||
$this->setDescription('scan files for malware'); | ||
} | ||
|
||
/** | ||
* @param $argument | ||
* @return void | ||
* @throws \OCP\DB\Exception if the database platform is not supported | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): int { | ||
$unscannedTagIsDisabled = $this->appConfig->getAppValue(self::APP_ID, 'disableUnscannedTag'); | ||
$quantity = $this->appConfig->getAppValue(self::APP_ID, 'scanQueueLength'); | ||
try { | ||
$quantity = intval($quantity); | ||
} catch (Exception) { | ||
$quantity = 5; | ||
} | ||
|
||
$maliciousTag = $this->tagService->getTag(TagService::MALICIOUS); | ||
$pupTag = $this->tagService->getTag(TagService::PUP); | ||
$cleanTag = $this->tagService->getTag(TagService::CLEAN); | ||
$unscannedTag = $this->tagService->getTag(TagService::UNSCANNED); | ||
$wontScanTag = $this->tagService->getTag(TagService::WONT_SCAN); | ||
|
||
if ($unscannedTagIsDisabled) { | ||
$excludedTagIds = [$unscannedTag->getId(), $maliciousTag->getId(), $cleanTag->getId(), $pupTag->getId(), $wontScanTag->getId()]; | ||
$fileIds = $this->tagService->getFileIdsWithoutTags($excludedTagIds, $quantity); | ||
} else { | ||
$fileIds = $this->tagService->getFileIdsWithTag(TagService::UNSCANNED, $quantity, 0); | ||
} | ||
|
||
$this->logger->debug("Scanning files"); | ||
|
||
foreach ($fileIds as $fileId) { | ||
try { | ||
$this->scanService->scanFileById($fileId); | ||
} catch (Exception $e) { | ||
$output->writeln("Failed to scan file with id " . $fileId . ": " . $e->getMessage()); | ||
} | ||
} | ||
|
||
$this->logger->debug("Scanned " . count($fileIds) . " files"); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace OCA\GDataVaas\Command; | ||
|
||
use OCA\GDataVaas\Service\TagService; | ||
use OCP\AppFramework\Utility\ITimeFactory; | ||
use OCP\DB\Exception; | ||
use OCP\IConfig; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
|
||
class TagUnscannedCommand extends Command { | ||
private const APP_ID = "gdatavaas"; | ||
|
||
private TagService $tagService; | ||
private IConfig $appConfig; | ||
private LoggerInterface $logger; | ||
|
||
public function __construct(IConfig $appConfig, TagService $tagService, LoggerInterface $logger) { | ||
parent::__construct(); | ||
|
||
$this->appConfig = $appConfig; | ||
$this->tagService = $tagService; | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
protected function configure() { | ||
$this->setName('gdatavaas:tag-unscanned'); | ||
$this->setDescription('tags all files without tag from this app as unscanned'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int { | ||
$unscannedTagIsDisabled = $this->appConfig->getAppValue(self::APP_ID, 'disableUnscannedTag'); | ||
if ($unscannedTagIsDisabled) { | ||
$this->tagService->removeTag(TagService::UNSCANNED); | ||
$this->logger->info("Unscanned Tag is disabled, exiting."); | ||
return 0; | ||
} | ||
|
||
$this->logger->debug("Tagging unscanned files"); | ||
|
||
$unscannedTag = $this->tagService->getTag(TagService::UNSCANNED); | ||
$maliciousTag = $this->tagService->getTag(TagService::MALICIOUS); | ||
$pupTag = $this->tagService->getTag(TagService::PUP); | ||
$cleanTag = $this->tagService->getTag(TagService::CLEAN); | ||
$wontScanTag = $this->tagService->getTag(TagService::WONT_SCAN); | ||
|
||
$excludedTagIds = [$unscannedTag->getId(), $maliciousTag->getId(), $cleanTag->getId(), $pupTag->getId(), $wontScanTag->getId()]; | ||
|
||
$fileIds = $this->tagService->getFileIdsWithoutTags($excludedTagIds, 10000); | ||
$this->logger->info("Found " . count($fileIds) . " files without tags from this app"); | ||
|
||
foreach ($fileIds as $fileId) { | ||
if ($this->tagService->hasAnyButUnscannedTag($fileId)) { | ||
continue; | ||
} | ||
$this->tagService->setTag($fileId, TagService::UNSCANNED); | ||
} | ||
|
||
$this->logger->debug("Tagged " . count($fileIds) . " unscanned files"); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters