8000 [FrameworkBundle][SodiumVault] Create secrets directory only when it … · symfony/symfony@dc94381 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc94381

Browse files
committed
[FrameworkBundle][SodiumVault] Create secrets directory only when it is used
1 parent 7f56758 commit dc94381

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

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

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

2931
/**
3032
* @param string|object|null $decryptionKey A string or a stringable object that defines the private key to use to decrypt the vault
3133
* or null to store generated keys in the provided $secretsDir
3234
*/
3335
public function __construct(string $secretsDir, $decryptionKey = null)
3436
{
35-
if (null !== $decryptionKey && !\is_string($decryptionKey) && !(\is_object($decryptionKey) && method_exists($decryptionKey, '__toString'))) {
36-
throw new \TypeError(sprintf('Decryption key should be a string or an object that implements the __toString() method, %s given.', \gettype($decryptionKey)));
37+
$this->secretsDir = $secretsDir;
38+
$this->decryptionKey = $decryptionKey;
39+
}
40+
41+
private function init(): void
42+
{
43 10000 +
if ($this->initialized) {
44+
return;
3745
}
3846

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));
47+
if (null !== $this->decryptionKey && !\is_string($this->decryptionKey) && !(\is_object($this->decryptionKey) && method_exists($this->decryptionKey, '__toString'))) {
48+
throw new \TypeError(sprintf('Decryption key should be a string or an object that implements the __toString() method, %s given.', \gettype($this->decryptionKey)));
4149
}
4250

43-
$this->pathPrefix = rtrim(strtr($secretsDir, '/', \DIRECTORY_SEPARATOR), \DIRECTORY_SEPARATOR).\DIRECTORY_SEPARATOR.basename($secretsDir).'.';
44-
$this->decryptionKey = $decryptionKey;
51+
if (!is_dir($this->secretsDir) && !@mkdir($this->secretsDir, 0777, true) && !is_dir($this->secretsDir)) {
52+
throw new \RuntimeException(sprintf('Unable to create the secrets directory (%s)', $this->secretsDir));
53+
}
54+
55+
$this->pathPrefix = rtrim(strtr($this->secretsDir, '/', \DIRECTORY_SEPARATOR), \DIRECTORY_SEPARATOR).\DIRECTORY_SEPARATOR.basename($this->secretsDir).'.';
56+
57+
$this->initialized = true;
4558
}
4659

4760
public function generateKeys(bool $override = false): bool
4861
{
62+
$this->init();
63+
4964
$this->lastMessage = null;
5065

5166
if (null === $this->encryptionKey && '' !== $this->decryptionKey = (string) $this->decryptionKey) {
@@ -81,6 +96,8 @@ public function generateKeys(bool $override = false): bool
8196

8297
public function seal(string $name, string $value): void
8398
{
99+
$this->init();
100+
84101
$this->lastMessage = null;
85102
$this->validateName($name);
86103
$this->loadKeys();
@@ -96,6 +113,8 @@ public function seal(string $name, string $value): void
96113

97114
public function reveal(string $name): ?string
98115
{
116+
$this->init();
117+
99118
$this->lastMessage = null;
100119
$this->validateName($name);
101120

@@ -130,6 +149,8 @@ public function reveal(string $name): ?string
130149

131150
public function remove(string $name): bool
132151
{
152+
$this->init();
153+
133154
$this->lastMessage = null;
134155
$this->validateName($name);
135156

@@ -150,6 +171,8 @@ public function remove(string $name): bool
150171

151172
public function list(bool $reveal = false): array
152173
{
174+
$this->init();
175+
153176
$this->lastMessage = null;
154177

155178
if (!file_exists($file = $this->pathPrefix.'list.php')) {

0 commit comments

Comments
 (0)
0