Skip to content

Commit c861570

Browse files
committed
[FrameworkBundle][SodiumVault] Create secrets directory only when needed
1 parent 7f56758 commit c861570

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/Secrets/SodiumVault.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class SodiumVault extends AbstractVault implements EnvVarLoaderInterface
2525
private $encryptionKey;
2626
private $decryptionKey;
2727
private $pathPrefix;
28+
private $secretsDir;
2829

2930
/**
3031
* @param string|object|null $decryptionKey A string or a stringable object that defines the private key to use to decrypt the vault
@@ -36,12 +37,9 @@ public function __construct(string $secretsDir, $decryptionKey = null)
3637
throw new \TypeError(sprintf('Decryption key should be a string or an object that implements the __toString() method, %s given.', \gettype($decryptionKey)));
3738
}
3839

39-
if (!is_dir($secretsDir) && !@mkdir($secretsDir, 0777, true) && !is_dir($secretsDir)) {
40-
throw new \RuntimeException(sprintf('Unable to create the secrets directory (%s)', $secretsDir));
41-
}
42-
4340
$this->pathPrefix = rtrim(strtr($secretsDir, '/', \DIRECTORY_SEPARATOR), \DIRECTORY_SEPARATOR).\DIRECTORY_SEPARATOR.basename($secretsDir).'.';
4441
$this->decryptionKey = $decryptionKey;
42+
$this->secretsDir = $secretsDir;
4543
}
4644

4745
public function generateKeys(bool $override = false): bool
@@ -203,9 +201,20 @@ private function export(string $file, string $data): void
203201
$data = str_replace('%', '\x', rawurlencode($data));
204202
$data = sprintf("<?php // %s on %s\n\nreturn \"%s\";\n", $name, date('r'), $data);
205203

204+
$this->createSecretsDir();
205+
206206
if (false === file_put_contents($this->pathPrefix.$file.'.php', $data, LOCK_EX)) {
207207
$e = error_get_last();
208208
throw new \ErrorException($e['message'] ?? 'Failed to write secrets data.', 0, $e['type'] ?? E_USER_WARNING);
209209
}
210210
}
211+
212+
private function createSecretsDir(): void
213+
{
214+
if ($this->secretsDir && !is_dir($this->secretsDir) && !@mkdir($this->secretsDir, 0777, true) && !is_dir($this->secretsDir)) {
215+
throw new \RuntimeException(sprintf('Unable to create the secrets directory (%s)', $this->secretsDir));
216+
}
217+
218+
$this->secretsDir = null;
219+
}
211220
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy