1111
1212namespace Symfony \Bundle \FrameworkBundle \Secrets ;
1313
14+ use Symfony \Component \DependencyInjection \EnvVarLoaderInterface ;
15+
1416/**
1517 * @author Tobias Schultze <http://tobion.de>
1618 * @author Jérémy Derussé <jeremy@derusse.com>
1719 * @author Nicolas Grekas <p@tchwork.com>
1820 *
1921 * @internal
2022 */
21- class SodiumVault extends AbstractVault
23+ class SodiumVault extends AbstractVault implements EnvVarLoaderInterface
2224{
2325 private $ encryptionKey ;
2426 private $ decryptionKey ;
@@ -56,8 +58,8 @@ public function generateKeys(bool $override = false): bool
5658 // ignore failures to load keys
5759 }
5860
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 );
6163 }
6264
6365 if (!$ override && null !== $ this ->encryptionKey ) {
@@ -69,10 +71,10 @@ public function generateKeys(bool $override = false): bool
6971 $ this ->decryptionKey = sodium_crypto_box_keypair ();
7072 $ this ->encryptionKey = sodium_crypto_box_publickey ($ this ->decryptionKey );
7173
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 );
7476
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 ));
7678
7779 return true ;
7880 }
@@ -82,12 +84,12 @@ public function seal(string $name, string $value): void
8284 $ this ->lastMessage = null ;
8385 $ this ->validateName ($ name );
8486 $ 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 )));
8688
8789 $ list = $ this ->list ();
8890 $ list [$ name ] = null ;
8991 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 ));
9193
9294 $ this ->lastMessage = sprintf ('Secret "%s" encrypted in "%s"; you can commit it. ' , $ name , $ this ->getPrettyPath (\dirname ($ this ->pathPrefix ).\DIRECTORY_SEPARATOR ));
9395 }
@@ -97,7 +99,7 @@ public function reveal(string $name): ?string
9799 $ this ->lastMessage = null ;
98100 $ this ->validateName ($ name );
99101
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 ))) {
101103 $ this ->lastMessage = sprintf ('Secret "%s" not found in "%s". ' , $ name , $ this ->getPrettyPath (\dirname ($ this ->pathPrefix ).\DIRECTORY_SEPARATOR ));
102104
103105 return null ;
@@ -131,15 +133,15 @@ public function remove(string $name): bool
131133 $ this ->lastMessage = null ;
132134 $ this ->validateName ($ name );
133135
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 ))) {
135137 $ this ->lastMessage = sprintf ('Secret "%s" not found in "%s". ' , $ name , $ this ->getPrettyPath (\dirname ($ this ->pathPrefix ).\DIRECTORY_SEPARATOR ));
136138
137139 return false ;
138140 }
139141
140142 $ list = $ this ->list ();
141143 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 ));
143145
144146 $ this ->lastMessage = sprintf ('Secret "%s" removed from "%s". ' , $ name , $ this ->getPrettyPath (\dirname ($ this ->pathPrefix ).\DIRECTORY_SEPARATOR ));
145147
@@ -150,7 +152,7 @@ public function list(bool $reveal = false): array
150152 {
151153 $ this ->lastMessage = null ;
152154
153- if (!file_exists ($ file = $ this ->pathPrefix .'sodium. list ' )) {
155+ if (!file_exists ($ file = $ this ->pathPrefix .'list.php ' )) {
154156 return [];
155157 }
156158
@@ -167,6 +169,11 @@ public function list(bool $reveal = false): array
167169 return $ secrets ;
168170 }
169171
172+ public function loadEnvVars (): array
173+ {
174+ return $ this ->list (true );
175+ }
176+
170177 private function loadKeys (): void
171178 {
172179 if (!\function_exists ('sodium_crypto_box_seal ' )) {
@@ -177,12 +184,12 @@ private function loadKeys(): void
177184 return ;
178185 }
179186
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 ' ;
182189 }
183190
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 ' ;
186193 } elseif ('' !== $ this ->decryptionKey ) {
187194 $ this ->encryptionKey = sodium_crypto_box_publickey ($ this ->decryptionKey );
188195 } else {
@@ -196,7 +203,7 @@ private function export(string $file, string $data): void
196203 $ data = str_replace ('% ' , '\x ' , rawurlencode ($ data ));
197204 $ data = sprintf ("<?php // %s on %s \n\nreturn \"%s \"; \n" , $ name , date ('r ' ), $ data );
198205
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 )) {
200207 $ e = error_get_last ();
201208 throw new \ErrorException ($ e ['message ' ] ?? 'Failed to write secrets data. ' , 0 , $ e ['type ' ] ?? E_USER_WARNING );
202209 }
0 commit comments