Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable30] chore: prevent create cfssl config path every time #4586

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading