Skip to content

Commit 8073953

Browse files
committed
refactor: make setEngine() atomic and remove swallowed exceptions
- Reorder AEngineHandler::setEngine() to save policy before persisting engine to appConfig; if policy save fails, engine is not changed - Remove try/catch from configureIdentifyMethodsForEngine() so failures propagate instead of being logged and ignored - Remove try/catch from AbstractIdentifyMethod::getRuntimeConfigInt() so programming errors (unknown policy keys) propagate rather than silently returning defaults Signed-off-by: Vitor Mattos <[email protected]>
1 parent aefcbef commit 8073953

3 files changed

Lines changed: 17 additions & 11 deletions

File tree

lib/Handler/CertificateEngine/AEngineHandler.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
use OCA\Libresign\Service\CertificatePolicyService;
2020
use OCA\Libresign\Service\Crl\CrlDistributionPointsExtractor;
2121
use OCA\Libresign\Service\Crl\CrlRevocationChecker;
22+
use OCA\Libresign\Service\Policy\PolicyService;
23+
use OCA\Libresign\Service\Policy\Provider\ExpirationRules\ExpirationRulesPolicy;
24+
use OCA\Libresign\Service\Policy\Provider\IdentifyMethods\IdentifyMethodsPolicy;
2225
use OCP\Files\AppData\IAppDataFactory;
2326
use OCP\Files\IAppData;
2427
use OCP\Files\SimpleFS\ISimpleFolder;
@@ -85,6 +88,7 @@ public function __construct(
8588
protected CertificatePolicyService $certificatePolicyService,
8689
protected IURLGenerator $urlGenerator,
8790
protected CaIdentifierService $caIdentifierService,
91+
protected PolicyService $policyService,
8892
protected LoggerInterface $logger,
8993
private CrlRevocationChecker $crlRevocationChecker,
9094
) {
@@ -276,9 +280,9 @@ public function translateToLong($name): string {
276280

277281
#[\Override]
278282
public function setEngine(string $engine): void {
283+
$this->configureIdentifyMethodsForEngine($engine);
279284
$this->appConfig->setValueString(Application::APP_ID, 'certificate_engine', $engine);
280285
$this->engine = $engine;
281-
$this->configureIdentifyMethodsForEngine($engine);
282286
}
283287

284288
/**
@@ -305,7 +309,7 @@ private function configureIdentifyMethodsForEngine(string $engine): void {
305309
'enabled' => true,
306310
'mandatory' => true,
307311
]];
308-
$this->appConfig->setValueArray(Application::APP_ID, 'identify_methods', $config);
312+
$this->policyService->saveSystem(IdentifyMethodsPolicy::KEY, $config);
309313
}
310314

311315
#[\Override]
@@ -477,7 +481,7 @@ public function getLeafExpiryInDays(): int {
477481
if ($this->leafExpiryOverrideInDays !== null) {
478482
return $this->leafExpiryOverrideInDays;
479483
}
480-
$exp = $this->appConfig->getValueInt(Application::APP_ID, 'expiry_in_days', 365);
484+
$exp = (int)$this->policyService->resolve(ExpirationRulesPolicy::KEY_EXPIRY_IN_DAYS)->getEffectiveValue();
481485
return $exp > 0 ? $exp : 365;
482486
}
483487

lib/Service/IdentifyMethod/AbstractIdentifyMethod.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
use DateTime;
1212
use InvalidArgumentException;
1313
use OC\AppFramework\Http as AppFrameworkHttp;
14-
use OCA\Libresign\AppInfo\Application;
1514
use OCA\Libresign\Db\IdentifyMethod;
1615
use OCA\Libresign\Enum\FileStatus;
1716
use OCA\Libresign\Events\SendSignNotificationEvent;
1817
use OCA\Libresign\Exception\LibresignException;
1918
use OCA\Libresign\Helper\JSActions;
2019
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\AbstractSignatureMethod;
20+
use OCA\Libresign\Service\Policy\Provider\ExpirationRules\ExpirationRulesPolicy;
2121
use OCA\Libresign\Service\SessionService;
2222
use OCA\Libresign\Vendor\Wobeto\EmailBlur\Blur;
2323
use OCP\Files\NotFoundException;
@@ -216,7 +216,7 @@ protected function throwIfFileNotFound(): void {
216216
}
217217

218218
protected function throwIfMaximumValidityExpired(): void {
219-
$maximumValidity = (int)$this->identifyService->getAppConfig()->getValueInt(Application::APP_ID, 'maximum_validity', SessionService::NO_MAXIMUM_VALIDITY);
219+
$maximumValidity = $this->getRuntimeConfigInt(ExpirationRulesPolicy::KEY_MAXIMUM_VALIDITY, SessionService::NO_MAXIMUM_VALIDITY);
220220
if ($maximumValidity <= 0) {
221221
return;
222222
}
@@ -247,11 +247,11 @@ protected function throwIfInvalidToken(): void {
247247

248248
protected function renewSession(): void {
249249
$this->identifyService->getSessionService()->setIdentifyMethodId($this->getEntity()->getId());
250-
$renewalInterval = $this->getRuntimeConfigInt('renewal_interval', SessionService::NO_RENEWAL_INTERVAL);
250+
$renewalInterval = $this->getRuntimeConfigInt(ExpirationRulesPolicy::KEY_RENEWAL_INTERVAL, SessionService::NO_RENEWAL_INTERVAL);
251251
if ($renewalInterval <= 0) {
252252
return;
253253
}
254-
$this->identifyService->getSessionService()->resetDurationOfSignPage();
254+
$this->identifyService->getSessionService()->resetDurationOfSignPage($renewalInterval);
255255
}
256256

257257
protected function updateIdentifiedAt(): void {
@@ -265,7 +265,7 @@ protected function updateIdentifiedAt(): void {
265265
}
266266

267267
protected function throwIfRenewalIntervalExpired(): void {
268-
$renewalInterval = $this->getRuntimeConfigInt('renewal_interval', SessionService::NO_RENEWAL_INTERVAL);
268+
$renewalInterval = $this->getRuntimeConfigInt(ExpirationRulesPolicy::KEY_RENEWAL_INTERVAL, SessionService::NO_RENEWAL_INTERVAL);
269269
if ($renewalInterval <= 0) {
270270
return;
271271
}
@@ -318,9 +318,9 @@ private function getRenewAction(): int {
318318
}
319319

320320
private function getRuntimeConfigInt(string $key, int $default): int {
321-
$appConfig = $this->identifyService->getAppConfig();
322-
$appConfig->clearCache(true);
323-
return (int)$appConfig->getValueInt(Application::APP_ID, $key, $default);
321+
$resolved = $this->identifyService->getPolicyService()->resolve($key);
322+
$value = $resolved->getEffectiveValue();
323+
return $value !== null ? (int)$value : $default;
324324
}
325325

326326
protected function throwIfAlreadySigned(): void {

tests/php/Unit/Handler/CertificateEngine/AEngineHandlerTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ private function getInstance(): OpenSslHandler {
6868
$this->urlGenerator,
6969
\OCP\Server::get(\OCA\Libresign\Service\SerialNumberService::class),
7070
$this->caIdentifierService,
71+
\OCP\Server::get(\OCA\Libresign\Service\Policy\PolicyService::class),
7172
$this->logger,
7273
\OCP\Server::get(\OCA\Libresign\Db\CrlMapper::class),
7374
$this->subjectAlternativeNameService,
@@ -116,6 +117,7 @@ public function testSetEngineConfiguresIdentifyMethodsForNoneEngine(
116117
'name' => 'account',
117118
'enabled' => true,
118119
'mandatory' => true,
120+
'signatureMethods' => [],
119121
]];
120122

121123
$this->assertEquals(

0 commit comments

Comments
 (0)