Skip to content

Commit ef7cf8c

Browse files
authored
Merge pull request #4540 from LibreSign/chore/prevent-create-cfssl-config-path-every-time
chore: prevent create cfssl config path every time
2 parents 65fbcd4 + c35ca94 commit ef7cf8c

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

lib/Handler/CertificateEngine/CfsslHandler.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public function __construct(
4848
protected ITempManager $tempManager,
4949
) {
5050
parent::__construct($config, $appConfig, $appDataFactory, $dateTimeFormatter, $tempManager);
51-
$this->cfsslServerHandler = new CfsslServerHandler(
52-
$this->getConfigPath(),
53-
);
51+
52+
$this->cfsslServerHandler = new CfsslServerHandler();
53+
$this->cfsslServerHandler->configCallback(fn () => $this->getConfigPath());
5454
}
5555

5656
public function generateRootCert(

lib/Handler/CfsslServerHandler.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,31 @@
88

99
namespace OCA\Libresign\Handler;
1010

11+
use Closure;
1112
use OCA\Libresign\Exception\LibresignException;
1213

1314
class CfsslServerHandler {
14-
private string $csrServerFile;
15-
private string $configServerFile;
16-
private string $configServerFileHash;
17-
public function __construct(string $configPath) {
18-
$this->csrServerFile = $configPath . DIRECTORY_SEPARATOR . 'csr_server.json';
19-
$this->configServerFile = $configPath . DIRECTORY_SEPARATOR . 'config_server.json';
20-
$this->configServerFileHash = $configPath . DIRECTORY_SEPARATOR . 'hashes.sha256';
15+
private string $csrServerFile = '';
16+
private string $configServerFile = '';
17+
private string $configServerFileHash = '';
18+
private Closure $getConfigPath;
19+
20+
/**
21+
* Create a callback to get config path not at the constructor because
22+
* getting at constructor, every time that the class is instantiated, will
23+
* try to create the config path if not exists.
24+
*/
25+
public function configCallback(Closure $callback): void {
26+
$this->getConfigPath = $callback;
27+
$this->getConfigPath = function () use ($callback) {
28+
if ($this->csrServerFile) {
29+
return;
30+
}
31+
$configPath = $callback();
32+
$this->csrServerFile = $configPath . DIRECTORY_SEPARATOR . 'csr_server.json';
33+
$this->configServerFile = $configPath . DIRECTORY_SEPARATOR . 'config_server.json';
34+
$this->configServerFileHash = $configPath . DIRECTORY_SEPARATOR . 'hashes.sha256';
35+
};
2136
}
2237

2338
public function createConfigServer(
@@ -47,6 +62,7 @@ private function putCsrServer(
4762
foreach ($names as $id => $name) {
4863
$content['names'][0][$id] = $name['value'];
4964
}
65+
($this->getConfigPath)();
5066
$response = file_put_contents($this->csrServerFile, json_encode($content));
5167
if ($response === false) {
5268
throw new LibresignException(
@@ -87,6 +103,7 @@ private function saveNewConfig(string $key, int $expirity): void {
87103

88104
private function saveConfig(array $config): void {
89105
$jsonConfig = json_encode($config);
106+
($this->getConfigPath)();
90107
$response = file_put_contents($this->configServerFile, $jsonConfig);
91108
if ($response === false) {
92109
throw new LibresignException('Error while writing config server file!', 500);
@@ -96,6 +113,7 @@ private function saveConfig(array $config): void {
96113
}
97114

98115
public function updateExpirity(int $expirity): void {
116+
($this->getConfigPath)();
99117
if (file_exists($this->configServerFileHash)) {
100118
$hashes = file_get_contents($this->configServerFileHash);
101119
preg_match('/(?<hash>\w*) +config_server.json/', $hashes, $matches);

0 commit comments

Comments
 (0)