11
11
12
12
namespace Symfony \Bundle \FrameworkBundle \Secrets ;
13
13
14
+ use Symfony \Component \DependencyInjection \EnvVarLoaderInterface ;
15
+
14
16
/**
15
17
* @author Tobias Schultze <http://tobion.de>
16
18
* @author Jérémy Derussé <jeremy@derusse.com>
17
19
* @author Nicolas Grekas <p@tchwork.com>
18
20
*
19
21
* @internal
20
22
*/
21
- class SodiumVault extends AbstractVault
23
+ class SodiumVault extends AbstractVault implements EnvVarLoaderInterface
22
24
{
23
25
private $ encryptionKey ;
24
26
private $ decryptionKey ;
@@ -56,8 +58,8 @@ public function generateKeys(bool $override = false): bool
56
58
// ignore failures to load keys
57
59
}
58
60
59
- if ('' !== $ this ->decryptionKey && !file_exists ($ this ->pathPrefix .'sodium. encrypt.public ' )) {
60
- $ this ->export ('sodium. encrypt.public ' , $ this ->encryptionKey );
61
+ if ('' !== $ this ->decryptionKey && !file_exists ($ this ->pathPrefix .'encrypt.public.php ' )) {
62
+ $ this ->export ('encrypt.public ' , $ this ->encryptionKey );
61
63
}
62
64
63
65
if (!$ override && null !== $ this ->encryptionKey ) {
@@ -69,10 +71,10 @@ public function generateKeys(bool $override = false): bool
69
71
$ this ->decryptionKey = sodium_crypto_box_keypair ();
70
72
$ this ->encryptionKey = sodium_crypto_box_publickey ($ this ->decryptionKey );
71
73
72
- $ this ->export ('sodium. encrypt.public ' , $ this ->encryptionKey );
73
- $ this ->export ('sodium. decrypt.private ' , $ this ->decryptionKey );
74
+ $ this ->export ('encrypt.public ' , $ this ->encryptionKey );
75
+ $ this ->export ('decrypt.private ' , $ this ->decryptionKey );
74
76
75
- $ this ->lastMessage = sprintf ('Sodium keys have been generated at "%s*.{ public, private} ". ' , $ this ->getPrettyPath ($ this ->pathPrefix ));
77
+ $ this ->lastMessage = sprintf ('Sodium keys have been generated at "%s*.public/ private.php ". ' , $ this ->getPrettyPath ($ this ->pathPrefix ));
76
78
77
79
return true ;
78
80
}
@@ -82,12 +84,12 @@ public function seal(string $name, string $value): void
82
84
$ this ->lastMessage = null ;
83
85
$ this ->validateName ($ name );
84
86
$ this ->loadKeys ();
85
- $ this ->export ($ name .'. ' .substr_replace (md5 ($ name ), ' .sodium ' , - 26 ), sodium_crypto_box_seal ($ value , $ this ->encryptionKey ?? sodium_crypto_box_publickey ($ this ->decryptionKey )));
87
+ $ this ->export ($ name .'. ' .substr (md5 ($ name ), 0 , 6 ), sodium_crypto_box_seal ($ value , $ this ->encryptionKey ?? sodium_crypto_box_publickey ($ this ->decryptionKey )));
86
88
87
89
$ list = $ this ->list ();
88
90
$ list [$ name ] = null ;
89
91
uksort ($ list , 'strnatcmp ' );
90
- file_put_contents ($ this ->pathPrefix .'sodium. list ' , sprintf ("<?php \n\nreturn %s; \n" , var_export ($ list , true ), LOCK_EX ));
92
+ file_put_contents ($ this ->pathPrefix .'list.php ' , sprintf ("<?php \n\nreturn %s; \n" , var_export ($ list , true ), LOCK_EX ));
91
93
92
94
$ this ->lastMessage = sprintf ('Secret "%s" encrypted in "%s"; you can commit it. ' , $ name , $ this ->getPrettyPath (\dirname ($ this ->pathPrefix ).\DIRECTORY_SEPARATOR ));
93
95
}
@@ -97,7 +99,7 @@ public function reveal(string $name): ?string
97
99
$ this ->lastMessage = null ;
98
100
$ this ->validateName ($ name );
99
101
100
- if (!file_exists ($ file = $ this ->pathPrefix .$ name .'. ' .substr_replace (md5 ($ name ), '.sodium ' , -26 ))) {
102
+ if (!file_exists ($ file = $ this ->pathPrefix .$ name .'. ' .substr_replace (md5 ($ name ), '.php ' , -26 ))) {
101
103
$ this ->lastMessage = sprintf ('Secret "%s" not found in "%s". ' , $ name , $ this ->getPrettyPath (\dirname ($ this ->pathPrefix ).\DIRECTORY_SEPARATOR ));
102
104
103
105
return null ;
@@ -131,15 +133,15 @@ public function remove(string $name): bool
131
133
$ this ->lastMessage = null ;
132
134
$ this ->validateName ($ name );
133
135
134
- if (!file_exists ($ file = $ this ->pathPrefix .$ name .'. ' .substr_replace (md5 ($ name ), '.sodium ' , -26 ))) {
136
+ if (!file_exists ($ file = $ this ->pathPrefix .$ name .'. ' .substr_replace (md5 ($ name ), '.php ' , -26 ))) {
135
137
$ this ->lastMessage = sprintf ('Secret "%s" not found in "%s". ' , $ name , $ this ->getPrettyPath (\dirname ($ this ->pathPrefix ).\DIRECTORY_SEPARATOR ));
136
138
137
139
return false ;
138
140
}
139
141
140
142
$ list = $ this ->list ();
141
143
unset($ list [$ name ]);
142
- file_put_contents ($ this ->pathPrefix .'sodium. list ' , sprintf ("<?php \n\nreturn %s; \n" , var_export ($ list , true ), LOCK_EX ));
144
+ file_put_contents ($ this ->pathPrefix .'list.php ' , sprintf ("<?php \n\nreturn %s; \n" , var_export ($ list , true ), LOCK_EX ));
143
145
144
146
$ this ->lastMessage = sprintf ('Secret "%s" removed from "%s". ' , $ name , $ this ->getPrettyPath (\dirname ($ this ->pathPrefix ).\DIRECTORY_SEPARATOR ));
145
147
@@ -150,7 +152,7 @@ public function list(bool $reveal = false): array
150
152
{
151
153
$ this ->lastMessage = null ;
152
154
153
- if (!file_exists ($ file = $ this ->pathPrefix .'sodium. list ' )) {
155
+ if (!file_exists ($ file = $ this ->pathPrefix .'list.php ' )) {
154
156
return [];
155
157
}
156
158
@@ -167,6 +169,11 @@ public function list(bool $reveal = false): array
167
169
return $ secrets ;
168
170
}
169
171
172
+ public function loadEnvVars (): array
173
+ {
174
+ return $ this ->list (true );
175
+ }
176
+
170
177
private function loadKeys (): void
171
178
{
172
179
if (!\function_exists ('sodium_crypto_box_seal ' )) {
@@ -177,12 +184,12 @@ private function loadKeys(): void
177
184
return ;
178
185
}
179
186
180
- if (file_exists ($ this ->pathPrefix .'sodium. decrypt.private ' )) {
181
- $ this ->decryptionKey = (string ) include $ this ->pathPrefix .'sodium. decrypt.private ' ;
187
+ if (file_exists ($ this ->pathPrefix .'decrypt.private.php ' )) {
188
+ $ this ->decryptionKey = (string ) include $ this ->pathPrefix .'decrypt.private.php ' ;
182
189
}
183
190
184
- if (file_exists ($ this ->pathPrefix .'sodium. encrypt.public ' )) {
185
- $ this ->encryptionKey = (string ) include $ this ->pathPrefix .'sodium. encrypt.public ' ;
191
+ if (file_exists ($ this ->pathPrefix .'encrypt.public.php ' )) {
192
+ $ this ->encryptionKey = (string ) include $ this ->pathPrefix .'encrypt.public.php ' ;
186
193
} elseif ('' !== $ this ->decryptionKey ) {
187
194
$ this ->encryptionKey = sodium_crypto_box_publickey ($ this ->decryptionKey );
188
195
} else {
@@ -196,7 +203,7 @@ private function export(string $file, string $data): void
196
203
$ data = str_replace ('% ' , '\x ' , rawurlencode ($ data ));
197
204
$ data = sprintf ("<?php // %s on %s \n\nreturn \"%s \"; \n" , $ name , date ('r ' ), $ data );
198
205
199
- if (false === file_put_contents ($ this ->pathPrefix .$ file , $ data , LOCK_EX )) {
206
+ if (false === file_put_contents ($ this ->pathPrefix .$ file. ' .php ' , $ data , LOCK_EX )) {
200
207
$ e = error_get_last ();
201
208
throw new \ErrorException ($ e ['message ' ] ?? 'Failed to write secrets data. ' , 0 , $ e ['type ' ] ?? E_USER_WARNING );
202
209
}
0 commit comments