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
12 changes: 12 additions & 0 deletions lib/Exception/CrlGenerationInProgressException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Libresign\Exception;

class CrlGenerationInProgressException extends LibresignException {
}
3 changes: 2 additions & 1 deletion lib/Service/Crl/CrlRevocationChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OCA\Libresign\Service\Crl;

use OCA\Libresign\Enum\CrlValidationStatus;
use OCA\Libresign\Exception\CrlGenerationInProgressException;
use OCA\Libresign\Service\Crl\Ldap\LdapCrlDownloader;
use OCA\Libresign\Service\Policy\PolicyService;
use OCA\Libresign\Service\Policy\Provider\CrlValidation\CrlValidationPolicy;
Expand Down Expand Up @@ -228,7 +229,7 @@ protected function generateLocalCrl(string $crlUrl): ?string {
$this->logger->debug('Skipping local CRL generation because source PKI config path is missing', [
'reason' => $e->getMessage(),
]);
} elseif ($e instanceof \RuntimeException && str_contains($e->getMessage(), 'already in progress')) {
} elseif ($e instanceof CrlGenerationInProgressException) {
$this->logger->debug('Local CRL generation is already in progress', [
'reason' => $e->getMessage(),
]);
Expand Down
3 changes: 2 additions & 1 deletion lib/Service/Crl/CrlService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCA\Libresign\Enum\CertificateEngineType;
use OCA\Libresign\Enum\CRLReason;
use OCA\Libresign\Enum\CRLStatus;
use OCA\Libresign\Exception\CrlGenerationInProgressException;
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
use OCA\Libresign\Service\Certificate\FileService;
use OCP\ICacheFactory;
Expand Down Expand Up @@ -414,7 +415,7 @@ private function resolveGeneratedCrlDer(string $instanceId, int $generation, str

$lockAcquired = $this->lockCache->add($lockKey, $lockToken, self::GENERATED_CRL_LOCK_TTL);
if (!$lockAcquired) {
throw new \RuntimeException(sprintf(
throw new CrlGenerationInProgressException(sprintf(
'CRL %s is already in progress for %s/%d/%s',
$forceRefresh ? 'refresh' : 'generation',
$instanceId,
Expand Down
26 changes: 23 additions & 3 deletions tests/php/Unit/Service/Crl/CrlRevocationCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace OCA\Libresign\Tests\Unit\Service\Crl;

use OCA\Libresign\Enum\CrlValidationStatus;
use OCA\Libresign\Exception\CrlGenerationInProgressException;
use OCA\Libresign\Service\Crl\CrlRevocationChecker;
use OCA\Libresign\Service\Crl\CrlService;
use OCA\Libresign\Service\Crl\Ldap\LdapCrlDownloader;
Expand Down Expand Up @@ -543,10 +544,11 @@ public function testBuildValidationCacheKeyDiffersForDifferentCrlContent(): void
public function testGenerateLocalCrlLogsDebugWhenAlreadyInProgress(): void {
$this->mockCrlRouteUrlGenerator();

$exception = new CrlGenerationInProgressException('CRL generation is already in progress for abc123/4/o');
$crlService = $this->createMock(CrlService::class);
$crlService->expects($this->once())
->method('generateCrlDer')
->willThrowException(new \RuntimeException('CRL generation is already in progress for abc123/4/o'));
->willThrowException($exception);

$this->checker->setCrlService($crlService);

Expand All @@ -555,8 +557,7 @@ public function testGenerateLocalCrlLogsDebugWhenAlreadyInProgress(): void {
->method('debug')
->with(
'Local CRL generation is already in progress',
$this->callback(fn (array $context): bool => isset($context['reason'])
&& str_contains($context['reason'], 'already in progress'))
['reason' => $exception->getMessage()]
);

$url = 'https://cloud.example.com/apps/libresign/crl/libresign_abc123_4_o.crl';
Expand All @@ -565,4 +566,23 @@ public function testGenerateLocalCrlLogsDebugWhenAlreadyInProgress(): void {
$this->assertNull($result);
}

public function testGenerateLocalCrlLogsWarningForUntypedExceptionMentioningInProgress(): void {
$this->mockCrlRouteUrlGenerator();

$crlService = $this->createMock(CrlService::class);
$crlService->expects($this->once())
->method('generateCrlDer')
->willThrowException(new \RuntimeException('Upstream job is already in progress'));

$this->checker->setCrlService($crlService);

$this->logger->expects($this->never())->method('debug');
$this->logger->expects($this->once())->method('warning');

$url = 'https://cloud.example.com/apps/libresign/crl/libresign_abc123_4_o.crl';
$result = $this->checker->publicGenerateLocalCrl($url);

$this->assertNull($result);
}

}
7 changes: 3 additions & 4 deletions tests/php/Unit/Service/Crl/CrlServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OCA\Libresign\Db\CrlMapper;
use OCA\Libresign\Enum\CertificateEngineType;
use OCA\Libresign\Enum\CRLReason;
use OCA\Libresign\Exception\CrlGenerationInProgressException;
use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
use OCA\Libresign\Handler\CertificateEngine\IEngineHandler;
use OCA\Libresign\Service\Certificate\FileService;
Expand Down Expand Up @@ -559,8 +560,7 @@ public function testRefreshGeneratedCrlDerThrowsWhenLockHeldAndOnlyReusablePersi
$this->certificateEngineFactory->expects($this->never())->method('getEngine');
$this->generatedCrlStorage->expects($this->never())->method('write');

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('CRL refresh is already in progress');
$this->expectException(CrlGenerationInProgressException::class);

$this->service->refreshGeneratedCrlDer('test-instance', 1, 'o');
}
Expand Down Expand Up @@ -594,8 +594,7 @@ public function testGenerateCrlDerThrowsWhenLockRemainsHeldAndNoReusablePersiste
$this->generatedCrlStorage->expects($this->never())
->method('write');

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('CRL generation is already in progress');
$this->expectException(CrlGenerationInProgressException::class);

$this->service->generateCrlDer('test-instance', 1, 'o');
}
Expand Down
Loading