Skip to content

Commit 150da25

Browse files
committed
Remove booleans type from output and input
1 parent 660f945 commit 150da25

14 files changed

Lines changed: 160 additions & 65 deletions

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# PHPVault
22

3-
PHPVault is a PHP library that can create, read, encrypt (`.env`) and decrypt (`.env.enc`) environment files (dotenv
4-
files). Within your project you can automatically load these encrypted environment variables from `.env.enc` into
5-
`getenv()`, `$_ENV` and `$_SERVER`. The corresponding key-value pairs within these dotenv files are encrypted and
6-
decrypted using an asymmetric encryption method
3+
PHPVault is a PHP library that can create, read, encrypt and decrypt environment files (so-called dotenv files). For
4+
example is `.env`, an encrypted file `.env.enc`, etc. Within your project you can automatically load these encrypted
5+
environment variables from `.env.enc` into `getenv()`, `$_ENV` and `$_SERVER`. The corresponding key-value pairs within
6+
these dotenv files are encrypted and decrypted using an asymmetric encryption method
77
([Public-key cryptography](https://en.wikipedia.org/wiki/Public-key_cryptography)). Private keys are only available
88
on productive systems for decrypting dotenv values. The public key, on the other hand, can be safely checked into
99
the repository and is used everywhere to encrypt new values.

src/Command/BaseCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use Ixnode\PhpVault\PHPVault;
3535
use Ixnode\PhpVault\Logger\Logger;
3636
use Exception;
37+
use Ixnode\PhpVault\Vault\Reader;
3738
use ReflectionClass;
3839
use SodiumException;
3940

@@ -228,12 +229,12 @@ protected function loadPrivateOrPublicKey(PHPVault $core): bool
228229
*
229230
* @param PHPVault $core
230231
* @param string|null $envFile
231-
* @param bool $displayDecrypted
232+
* @param string $outputType
232233
* @param bool $ignoreExistingFile
233234
* @return void
234235
* @throws Exception
235236
*/
236-
protected function writeEnvVariables(PHPVault $core, ?string $envFile = null, bool $displayDecrypted = false, bool $ignoreExistingFile = false): void
237+
protected function writeEnvVariables(PHPVault $core, ?string $envFile = null, string $outputType = Reader::OUTPUT_TO_ENCRYPTED, bool $ignoreExistingFile = false): void
237238
{
238239
/* Check if option was given to write a file. */
239240
if (!$envFile) {
@@ -247,7 +248,7 @@ protected function writeEnvVariables(PHPVault $core, ?string $envFile = null, bo
247248
}
248249

249250
/* Write file */
250-
$envFileString = $core->getVault()->getWriter()->getEnvString($displayDecrypted, true);
251+
$envFileString = $core->getVault()->getWriter()->getEnvString($outputType, true);
251252
file_put_contents($envFile, $envFileString);
252253

253254
/* Check that the given env file was written. */

src/Command/DecryptFileCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ public function execute(): void
9898
}
9999

100100
/* Load env decrypted file */
101-
$core->getVault()->getReader()->addFileToVault($envFileEncrypted, Reader::LOAD_FROM_ENCRYPTED);
101+
$core->getVault()->getReader()->addFileToVault($envFileEncrypted, Reader::LOAD_FROM_ENCRYPTED, Reader::OUTPUT_TO_DECRYPTED);
102102

103103
/* Writes the vault */
104-
$this->writeEnvVariables($core, $envFileDecrypted, $displayDecrypted);
104+
$this->writeEnvVariables($core, $envFileDecrypted, Reader::OUTPUT_TO_DECRYPTED);
105105
}
106106
}

src/Command/DisplayCommand.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,16 @@ public function execute(): void
112112
}
113113

114114
/* Load env file */
115-
$core->getVault()->getReader()->addFileToVault($envFile, $loadEncrypted ? Reader::LOAD_FROM_ENCRYPTED : Reader::LOAD_FROM_DECRYPTED);
115+
$core->getVault()->getReader()->addFileToVault(
116+
$envFile,
117+
$loadEncrypted ? Reader::LOAD_FROM_ENCRYPTED : Reader::LOAD_FROM_DECRYPTED,
118+
$displayDecrypted ? Reader::OUTPUT_TO_DECRYPTED : Reader::OUTPUT_TO_ENCRYPTED
119+
);
116120

117121
/* Displays the vault */
118-
$this->logger->getDisplay()->envVariables($core, $displayDecrypted);
122+
$this->logger->getDisplay()->envVariables($core, Reader::OUTPUT_TO_DECRYPTED);
119123

120124
/* Writes the vault */
121-
$this->writeEnvVariables($core, $writeEnv, $displayDecrypted);
125+
$this->writeEnvVariables($core, $writeEnv, $displayDecrypted ? Reader::OUTPUT_TO_DECRYPTED : Reader::OUTPUT_TO_ENCRYPTED);
122126
}
123127
}

src/Command/EncryptFileCommand.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@ public function execute(): void
7272
$envFileDecrypted = $this->getArgument(self::ARGUMENT_ENV_FILE);
7373
$envFileEncrypted = sprintf('%s.enc', $envFileDecrypted);
7474

75-
/* Set options */
76-
$displayDecrypted = false;
77-
7875
/* Check if already encrypted */
7976
if (preg_match('~.+\.enc$~', $envFileDecrypted)) {
8077
$this->logger->getDisplay()->fileAlreadyEncrypted($envFileDecrypted);
@@ -96,9 +93,9 @@ public function execute(): void
9693
}
9794

9895
/* Load env decrypted file */
99-
$core->getVault()->getReader()->addFileToVault($envFileDecrypted, Reader::LOAD_FROM_DECRYPTED);
96+
$core->getVault()->getReader()->addFileToVault($envFileDecrypted, Reader::LOAD_FROM_DECRYPTED, Reader::OUTPUT_TO_ENCRYPTED);
10097

10198
/* Writes the vault */
102-
$this->writeEnvVariables($core, $envFileEncrypted, $displayDecrypted);
99+
$this->writeEnvVariables($core, $envFileEncrypted);
103100
}
104101
}

src/Command/SetCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,16 @@ public function execute(): void
109109

110110
/* Load env file */
111111
if (file_exists($envFile)) {
112-
$core->getVault()->getReader()->addFileToVault($envFile, Reader::LOAD_FROM_ENCRYPTED);
112+
$core->getVault()->getReader()->addFileToVault($envFile, Reader::LOAD_FROM_ENCRYPTED, Reader::OUTPUT_TO_ENCRYPTED);
113113
}
114114

115115
/* set new name value set */
116116
$core->getVault()->add($name, $value, $description);
117117

118118
/* Displays the vault */
119-
$this->logger->getDisplay()->envVariables($core, $displayDecrypted);
119+
$this->logger->getDisplay()->envVariables($core, Reader::OUTPUT_TO_DECRYPTED);
120120

121121
/* Writes the vault */
122-
$this->writeEnvVariables($core, $envFile, $displayDecrypted, true);
122+
$this->writeEnvVariables($core, $envFile, Reader::OUTPUT_TO_ENCRYPTED, true);
123123
}
124124
}

src/Logger/Display.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Ixnode\PhpVault\KeyPair;
3030
use Ixnode\PhpVault\PHPVault;
3131
use Exception;
32+
use Ixnode\PhpVault\Vault\Reader;
3233

3334
class Display
3435
{
@@ -217,20 +218,20 @@ public function privateAndPublicKeys(PHPVault $core): void
217218
* Displays all environment variables from given file.
218219
*
219220
* @param PHPVault $core
220-
* @param bool $displayDecrypted
221+
* @param string $outputType
221222
* @return void
222223
* @throws Exception
223224
*/
224-
public function envVariables(PHPVault $core, bool $displayDecrypted = false): void
225+
public function envVariables(PHPVault $core, string $outputType = Reader::OUTPUT_TO_ENCRYPTED): void
225226
{
226227
$table = array();
227228

228229
/* Collect all environment variables. */
229-
foreach ($core->getVault()->getAllObjects(true, $displayDecrypted) as $key => $data) {
230+
foreach ($core->getVault()->getAllObjectsRaw(true) as $key => $data) {
230231
$table[] = [
231232
'key' => $key,
232-
'value' => $displayDecrypted ? $data->getValueDecrypted() : $data->getValueEncrypted(),
233-
'description' => $displayDecrypted ? $data->getDescriptionDecrypted(): $data->getDescriptionEncrypted(),
233+
'value' => $outputType === Reader::OUTPUT_TO_DECRYPTED ? $data->getValueDecrypted() : $data->getValueEncrypted(),
234+
'description' => $outputType === Reader::OUTPUT_TO_DECRYPTED ? $data->getDescriptionDecrypted(): $data->getDescriptionEncrypted(),
234235
];
235236
}
236237

src/PHPVault.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public function loadPublicKeyFromFile(string $publicKey): void
176176
*/
177177
public function importEncryptedEnvFile(string $file): void
178178
{
179-
$this->getVault()->getReader()->addFileToVault($file, Reader::LOAD_FROM_ENCRYPTED);
179+
$this->getVault()->getReader()->addFileToVault($file, Reader::LOAD_FROM_ENCRYPTED, Reader::OUTPUT_TO_ENCRYPTED);
180180
$this->getVault()->getWriter()->saveToServer();
181181
$this->getVault()->getWriter()->saveToEnv();
182182
$this->getVault()->getWriter()->putEnv();

src/Vault/KeyValuePair.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,17 @@ public function setEncrypted(bool $encrypted): void
212212
* Decrypts this class.
213213
*
214214
* @param Decrypter $decrypter
215+
* @param bool $force
215216
* @return $this
216217
* @throws SodiumException
217218
*/
218-
public function decrypt(Decrypter $decrypter): KeyValuePair
219+
public function decrypt(Decrypter $decrypter, bool $force = false): KeyValuePair
219220
{
221+
/* Class is already decrypted */
222+
if ($this->isDecrypted() && !$force) {
223+
return $this;
224+
}
225+
220226
$this->setValueDecrypted(null);
221227
$this->setDescriptionDecrypted(null);
222228

@@ -237,11 +243,17 @@ public function decrypt(Decrypter $decrypter): KeyValuePair
237243
* Encrypts this class.
238244
*
239245
* @param Encrypter $encrypter
246+
* @param bool $force
240247
* @return $this
241248
* @throws SodiumException
242249
*/
243-
public function encrypt(Encrypter $encrypter): KeyValuePair
250+
public function encrypt(Encrypter $encrypter, bool $force = false): KeyValuePair
244251
{
252+
/* Class is already encrypted */
253+
if ($this->isEncrypted() && !$force) {
254+
return $this;
255+
}
256+
245257
$this->setValueEncrypted(null);
246258
$this->setDescriptionEncrypted(null);
247259

src/Vault/Reader.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class Reader
4545

4646
const OUTPUT_TO_DECRYPTED = 'SHOW_DECRYPTED';
4747

48+
const OUTPUT_TO_RAW = 'SHOW_RAW';
49+
4850
/**
4951
* Writer constructor.
5052
*
@@ -65,7 +67,7 @@ public function __construct(Vault $vault)
6567
* @return KeyValuePair[]
6668
* @throws SodiumException
6769
*/
68-
public function convertStreamToArray(string $stream, string $loadType = self::LOAD_FROM_ENCRYPTED, string $outputType = self::OUTPUT_TO_ENCRYPTED, string $nonce = null): array
70+
public function convertStreamToKeyPairArray(string $stream, string $loadType = self::LOAD_FROM_ENCRYPTED, string $outputType = self::OUTPUT_TO_ENCRYPTED, string $nonce = null): array
6971
{
7072
$lines = explode("\n", $stream);
7173
$return = array();
@@ -146,7 +148,7 @@ public function convertStreamToArray(string $stream, string $loadType = self::LO
146148
}
147149

148150
/**
149-
* Adds given array to vault.
151+
* Adds given array to vault (old).
150152
*
151153
* @param KeyValuePair[] $array
152154
* @param string|null $nonce
@@ -164,6 +166,19 @@ public function addArrayToVault(array $array, string $nonce = null): void
164166
}
165167
}
166168

169+
/**
170+
* Adds given key pair array to vault.
171+
*
172+
* @param KeyValuePair[] $array
173+
* @return void
174+
*/
175+
public function addKeyValuePairArrayToVault(array $array): void
176+
{
177+
foreach ($array as $name => $keyPair) {
178+
$this->vault->addKeyValuePair($name, $keyPair);
179+
}
180+
}
181+
167182
/**
168183
* Adds given stream to vault.
169184
*
@@ -175,9 +190,7 @@ public function addArrayToVault(array $array, string $nonce = null): void
175190
*/
176191
public function addStreamToVault(string $stream, string $loadType = self::LOAD_FROM_ENCRYPTED, string $outputType = self::OUTPUT_TO_ENCRYPTED): void
177192
{
178-
$array = $this->convertStreamToArray($stream, $loadType, Reader::OUTPUT_TO_ENCRYPTED);
179-
180-
$this->addArrayToVault($array, null);
193+
$this->addKeyValuePairArrayToVault($this->convertStreamToKeyPairArray($stream, $loadType, $outputType));
181194
}
182195

183196
/**
@@ -188,6 +201,7 @@ public function addStreamToVault(string $stream, string $loadType = self::LOAD_F
188201
* @param string $outputType
189202
* @return void
190203
* @throws SodiumException
204+
* @throws Exception
191205
*/
192206
public function addFileToVault(string $file, string $loadType = self::LOAD_FROM_ENCRYPTED, string $outputType = self::OUTPUT_TO_ENCRYPTED): void
193207
{

0 commit comments

Comments
 (0)