Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4858dac

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

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
class SodiumVault extends AbstractVault implements EnvVarLoaderInterface
2424
{
2525
private $encryptionKey;
26+
private $secretsDir;
2627
private $decryptionKey;
2728
private $pathPrefix;
2829

@@ -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-
43-
$this->pathPrefix = rtrim(strtr($secretsDir, '/', \DIRECTORY_SEPARATOR), \DIRECTORY_SEPARATOR).\DIRECTORY_SEPARATOR.basename($secretsDir).'.';
40+
$this->secretsDir = $secretsDir;
4441
$this->decryptionKey = $decryptionKey;
42+
$this->pathPrefix = rtrim(strtr($secretsDir, '/', \DIRECTORY_SEPARATOR), \DIRECTORY_SEPARATOR).\DIRECTORY_SEPARATOR.basename($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)