Skip to content

Commit 8f71e6c

Browse files
committed
fix: use Nextcloud's instanceid instead of generating random ID
- Changed CaIdentifierService to use system's instanceid from config.php - Removed custom instance_id generation logic using ISecureRandom - Updated unit tests to mock IConfig and verify correct instanceid usage - This fixes race condition in certificate generation during integration tests Signed-off-by: Vitor Mattos <[email protected]>
1 parent 0bd1d33 commit 8f71e6c

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

lib/Service/CaIdentifierService.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
use OCA\Libresign\AppInfo\Application;
1212
use OCP\IAppConfig;
13-
use OCP\Security\ISecureRandom;
13+
use OCP\IConfig;
1414

1515
class CaIdentifierService {
1616
private const ENGINE_TYPES = [
@@ -20,17 +20,12 @@ class CaIdentifierService {
2020

2121
public function __construct(
2222
private IAppConfig $appConfig,
23+
private IConfig $config,
2324
) {
2425
}
2526

2627
public function getInstanceId(): string {
27-
$instanceId = $this->appConfig->getValueString(Application::APP_ID, 'instance_id', '');
28-
if (strlen($instanceId) === 10) {
29-
return $instanceId;
30-
}
31-
$instanceId = \OC::$server->get(ISecureRandom::class)->generate(10, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
32-
$this->appConfig->setValueString(Application::APP_ID, 'instance_id', $instanceId);
33-
return $instanceId;
28+
return $this->config->getSystemValueString('instanceid');
3429
}
3530

3631
public function generateCaId(string $engineName): string {

tests/php/Unit/Service/CaIdentifierServiceTest.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use OCA\Libresign\Service\CaIdentifierService;
1212
use OCP\IAppConfig;
13+
use OCP\IConfig;
1314
use PHPUnit\Framework\Attributes\DataProvider;
1415
use PHPUnit\Framework\MockObject\MockObject;
1516
use PHPUnit\Framework\TestCase;
@@ -19,17 +20,22 @@
1920
*/
2021
final class CaIdentifierServiceTest extends TestCase {
2122
private CaIdentifierService $service;
22-
private MockObject $appConfig;
23+
private IAppConfig&MockObject $appConfig;
24+
private IConfig&MockObject $config;
2325

2426
protected function setUp(): void {
25-
parent::setUp();
2627
$this->appConfig = $this->createMock(IAppConfig::class);
27-
/** @var IAppConfig $appConfig */
28-
$appConfig = $this->appConfig;
29-
$this->service = new CaIdentifierService($appConfig);
28+
$this->config = $this->createMock(IConfig::class);
29+
$this->service = new CaIdentifierService($this->appConfig, $this->config);
3030
}
3131

3232
public function testGenerateCaIdWithOpenSSL(): void {
33+
$this->config
34+
->expects($this->once())
35+
->method('getSystemValueString')
36+
->with('instanceid')
37+
->willReturn('abc1234567');
38+
3339
$this->appConfig
3440
->expects($this->once())
3541
->method('getValueInt')
@@ -43,10 +49,16 @@ public function testGenerateCaIdWithOpenSSL(): void {
4349

4450
$result = $this->service->generateCaId('openssl');
4551

46-
$this->assertMatchesRegularExpression('/^libresign-ca-id:[a-z0-9]{10}_g:\d+_e:o$/', $result);
52+
$this->assertEquals('libresign-ca-id:abc1234567_g:1_e:o', $result);
4753
}
4854

4955
public function testGenerateCaIdWithCFSSL(): void {
56+
$this->config
57+
->expects($this->once())
58+
->method('getSystemValueString')
59+
->with('instanceid')
60+
->willReturn('xyz9876543');
61+
5062
$this->appConfig
5163
->expects($this->once())
5264
->method('getValueInt')
@@ -60,7 +72,7 @@ public function testGenerateCaIdWithCFSSL(): void {
6072

6173
$result = $this->service->generateCaId('cfssl');
6274

63-
$this->assertMatchesRegularExpression('/^libresign-ca-id:[a-z0-9]{10}_g:\d+_e:c$/', $result);
75+
$this->assertEquals('libresign-ca-id:xyz9876543_g:3_e:c', $result);
6476
}
6577

6678
#[DataProvider('providerIsValidCaId')]

0 commit comments

Comments
 (0)