Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 19 additions & 7 deletions lib/Command/Predict.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

namespace OCA\SuspiciousLogin\Command;

use OCA\SuspiciousLogin\Exception\ModelNotFoundException;
use OCA\SuspiciousLogin\Exception\ServiceException;
use OCA\SuspiciousLogin\Service\EstimatorService;
use OCA\SuspiciousLogin\Service\Ipv4Strategy;
use OCA\SuspiciousLogin\Service\IpV6Strategy;
Expand Down Expand Up @@ -55,14 +57,24 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$uid = $input->getArgument('uid');
$ip = $input->getArgument('ip');
$modelId = $input->getArgument('model');
if (!$this->estimatorService->predict(
$uid,
$ip,
$input->getOption('v6') ? new IpV6Strategy() : new Ipv4Strategy(),
$modelId ? (int)$modelId : null)) {
$output->writeln("WARN: IP $ip is suspicious");
return 1;

try {
if (!$this->estimatorService->predict(
$uid,
$ip,
$input->getOption('v6') ? new IpV6Strategy() : new Ipv4Strategy(),
$modelId ? (int)$modelId : null)) {
$output->writeln("WARN: IP $ip is suspicious");
return 1;
}
} catch (ModelNotFoundException $ex) {
$output->writeln('<error>Could not predict suspiciousness: ' . $ex->getMessage() . '</error>');
return 2;
} catch (ServiceException $ex) {
$output->writeln('<error>Could not predict suspiciousness: ' . $ex->getMessage() . '</error>');
return 3;
}

$output->writeln("OK: IP $ip is not suspicious");
return 0;
}
Expand Down
13 changes: 13 additions & 0 deletions lib/Exception/ModelNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\SuspiciousLogin\Exception;

class ModelNotFoundException extends ServiceException {
}
2 changes: 2 additions & 0 deletions lib/Service/EstimatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\SuspiciousLogin\Service;

use OCA\SuspiciousLogin\Exception\ModelNotFoundException;
use OCA\SuspiciousLogin\Exception\ServiceException;
use Psr\Log\LoggerInterface;
use Rubix\ML\Datasets\Unlabeled;
Expand All @@ -28,6 +29,7 @@ public function __construct(
* @param int|null $modelId
*
* @return bool
* @throws ModelNotFoundException
* @throws ServiceException
*/
public function predict(string $uid, string $ip, AClassificationStrategy $strategy, ?int $modelId = null): bool {
Expand Down
9 changes: 7 additions & 2 deletions lib/Service/LoginClassifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OCA\SuspiciousLogin\Db\SuspiciousLogin;
use OCA\SuspiciousLogin\Db\SuspiciousLoginMapper;
use OCA\SuspiciousLogin\Event\SuspiciousLoginEvent;
use OCA\SuspiciousLogin\Exception\ModelNotFoundException;
use OCA\SuspiciousLogin\Exception\ServiceException;
use OCA\SuspiciousLogin\Util\AddressClassifier;
use OCP\AppFramework\Utility\ITimeFactory;
Expand Down Expand Up @@ -75,10 +76,14 @@ public function process(string $uid, string $ip) {
// All good, carry on!
return;
}
} catch (ServiceException $ex) {
$this->logger->debug("Could not predict suspiciousness: " . $ex->getMessage());
} catch (ModelNotFoundException $ex) {
$this->logger->debug('Could not predict suspiciousness: ' . $ex->getMessage());
// This most likely means there is no trained model yet, so we return early here
return;
} catch (ServiceException $ex) {
$this->logger->warning("Could not predict suspiciousness: " . $ex->getMessage());
// There was an error loading the model, so we return early here
return;
}

$this->logger->info("Detected a login from a suspicious login. user=$uid ip=$ip strategy=" . $strategy::getTypeName());
Expand Down
13 changes: 9 additions & 4 deletions lib/Service/ModelStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use OCA\SuspiciousLogin\AppInfo\Application;
use OCA\SuspiciousLogin\Db\Model;
use OCA\SuspiciousLogin\Db\ModelMapper;
use OCA\SuspiciousLogin\Exception\ServiceException;
use OCA\SuspiciousLogin\Exception\ModelNotFoundException;
use OCP\App\IAppManager;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\Files\IAppData;
Expand Down Expand Up @@ -47,14 +47,14 @@ public function __construct(
/**
* @return Estimator
* @throws RuntimeException
* @throws ServiceException
* @throws ModelNotFoundException
*/
public function loadLatest(AClassificationStrategy $strategy): Estimator {
try {
$latestModel = $this->modelMapper->findLatest($strategy::getTypeName());
} catch (DoesNotExistException $e) {
$this->logger->debug("No models found. Can't load latest");
throw new ServiceException("No models found", 0, $e);
throw new ModelNotFoundException("No models found", 0, $e);
}
return $this->load($latestModel->getId());
}
Expand Down Expand Up @@ -82,6 +82,11 @@ private function cache(int $id, string $serialized): void {
$cache->set($this->getCacheKey($id), $serialized);
}

/**
* @return Estimator
* @throws RuntimeException
* @throws ModelNotFoundException
*/
public function load(int $id): Estimator {
$cached = $this->getCached($id);
if ($cached !== null) {
Expand All @@ -96,7 +101,7 @@ public function load(int $id): Estimator {
$modelFile = $modelsFolder->getFile((string)$id);
} catch (NotFoundException $e) {
$this->logger->error("Could not load classifier model $id: " . $e->getMessage());
throw new ServiceException("Could not load model $id", 0, $e);
throw new ModelNotFoundException("Could not load model $id", 0, $e);
}

$serialized = $modelFile->getContent();
Expand Down
Loading