Skip to content

Commit

Permalink
Merge pull request #4586 from LibreSign/backport/4540/stable30
Browse files Browse the repository at this point in the history
[stable30] chore: prevent create cfssl config path every time
  • Loading branch information
vitormattos authored Feb 1, 2025
2 parents e3cf8a5 + 0ddacb2 commit cf25a74
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/Handler/CertificateEngine/CfsslHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
32 changes: 25 additions & 7 deletions lib/Handler/CfsslServerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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('/(?<hash>\w*) +config_server.json/', $hashes, $matches);
Expand Down

0 comments on commit cf25a74

Please sign in to comment.