diff --git a/lib/Handler/CertificateEngine/CfsslHandler.php b/lib/Handler/CertificateEngine/CfsslHandler.php index eaad34cb3..fd7937b0f 100644 --- a/lib/Handler/CertificateEngine/CfsslHandler.php +++ b/lib/Handler/CertificateEngine/CfsslHandler.php @@ -48,9 +48,9 @@ public function __construct( protected ITempManager $tempManager, ) { parent::__construct($config, $appConfig, $appDataFactory, $dateTimeFormatter, $tempManager); - $this->cfsslServerHandler = new CfsslServerHandler( - $this->getConfigPath(), - ); + + $this->cfsslServerHandler = new CfsslServerHandler(); + $this->cfsslServerHandler->configCallback(fn () => $this->getConfigPath()); } public function generateRootCert( diff --git a/lib/Handler/CfsslServerHandler.php b/lib/Handler/CfsslServerHandler.php index 873bed97b..e4dbe2fe6 100644 --- a/lib/Handler/CfsslServerHandler.php +++ b/lib/Handler/CfsslServerHandler.php @@ -8,16 +8,31 @@ namespace OCA\Libresign\Handler; +use Closure; use OCA\Libresign\Exception\LibresignException; class CfsslServerHandler { - private string $csrServerFile; - private string $configServerFile; - private string $configServerFileHash; - public function __construct(string $configPath) { - $this->csrServerFile = $configPath . DIRECTORY_SEPARATOR . 'csr_server.json'; - $this->configServerFile = $configPath . DIRECTORY_SEPARATOR . 'config_server.json'; - $this->configServerFileHash = $configPath . DIRECTORY_SEPARATOR . 'hashes.sha256'; + private string $csrServerFile = ''; + private string $configServerFile = ''; + private string $configServerFileHash = ''; + private Closure $getConfigPath; + + /** + * Create a callback to get config path not at the constructor because + * getting at constructor, every time that the class is instantiated, will + * try to create the config path if not exists. + */ + public function configCallback(Closure $callback): void { + $this->getConfigPath = $callback; + $this->getConfigPath = function () use ($callback) { + if ($this->csrServerFile) { + return; + } + $configPath = $callback(); + $this->csrServerFile = $configPath . DIRECTORY_SEPARATOR . 'csr_server.json'; + $this->configServerFile = $configPath . DIRECTORY_SEPARATOR . 'config_server.json'; + $this->configServerFileHash = $configPath . DIRECTORY_SEPARATOR . 'hashes.sha256'; + }; } public function createConfigServer( @@ -47,6 +62,7 @@ private function putCsrServer( foreach ($names as $id => $name) { $content['names'][0][$id] = $name['value']; } + ($this->getConfigPath)(); $response = file_put_contents($this->csrServerFile, json_encode($content)); if ($response === false) { throw new LibresignException( @@ -87,6 +103,7 @@ private function saveNewConfig(string $key, int $expirity): void { private function saveConfig(array $config): void { $jsonConfig = json_encode($config); + ($this->getConfigPath)(); $response = file_put_contents($this->configServerFile, $jsonConfig); if ($response === false) { throw new LibresignException('Error while writing config server file!', 500); @@ -96,6 +113,7 @@ private function saveConfig(array $config): void { } public function updateExpirity(int $expirity): void { + ($this->getConfigPath)(); if (file_exists($this->configServerFileHash)) { $hashes = file_get_contents($this->configServerFileHash); preg_match('/(?\w*) +config_server.json/', $hashes, $matches);