Skip to content

Commit 0a4d9ca

Browse files
Revert "fix: use Nextcloud's instanceid instead of generating random ID"
This reverts commit 8f71e6c. Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent 75c741a commit 0a4d9ca

2 files changed

Lines changed: 15 additions & 22 deletions

File tree

lib/Service/CaIdentifierService.php

Lines changed: 8 additions & 3 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\IConfig;
13+
use OCP\Security\ISecureRandom;
1414

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

2121
public function __construct(
2222
private IAppConfig $appConfig,
23-
private IConfig $config,
2423
) {
2524
}
2625

2726
public function getInstanceId(): string {
28-
return $this->config->getSystemValueString('instanceid');
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;
2934
}
3035

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

tests/php/Unit/Service/CaIdentifierServiceTest.php

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

1111
use OCA\Libresign\Service\CaIdentifierService;
1212
use OCP\IAppConfig;
13-
use OCP\IConfig;
1413
use PHPUnit\Framework\Attributes\DataProvider;
1514
use PHPUnit\Framework\MockObject\MockObject;
1615
use PHPUnit\Framework\TestCase;
@@ -20,22 +19,17 @@
2019
*/
2120
final class CaIdentifierServiceTest extends TestCase {
2221
private CaIdentifierService $service;
23-
private IAppConfig&MockObject $appConfig;
24-
private IConfig&MockObject $config;
22+
private MockObject $appConfig;
2523

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

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

5044
$result = $this->service->generateCaId('openssl');
5145

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

5549
public function testGenerateCaIdWithCFSSL(): void {
56-
$this->config
57-
->expects($this->once())
58-
->method('getSystemValueString')
59-
->with('instanceid')
60-
->willReturn('xyz9876543');
61-
6250
$this->appConfig
6351
->expects($this->once())
6452
->method('getValueInt')
@@ -72,7 +60,7 @@ public function testGenerateCaIdWithCFSSL(): void {
7260

7361
$result = $this->service->generateCaId('cfssl');
7462

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

7866
#[DataProvider('providerIsValidCaId')]

0 commit comments

Comments
 (0)