Skip to content

Commit

Permalink
[9.x] Allow to pass base64 key to env:encrypt command (#45157)
Browse files Browse the repository at this point in the history
* Encrypt .env file when base64 key given

* Display passed base64 in a console output

* Parse all keys

Co-authored-by: Joe Dixon <hello@joedixon.co.uk>
  • Loading branch information
ekateiva and joedixon authored Dec 1, 2022
1 parent d828da6 commit eb19ed8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Illuminate/Foundation/Console/EnvironmentEncryptCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Console\Command;
use Illuminate\Encryption\Encrypter;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;

#[AsCommand(name: 'env:encrypt')]
Expand Down Expand Up @@ -96,7 +97,7 @@ public function handle()
}

try {
$encrypter = new Encrypter($key, $cipher);
$encrypter = new Encrypter($this->parseKey($key), $cipher);

$this->files->put(
$encryptedFile,
Expand All @@ -116,4 +117,19 @@ public function handle()

$this->newLine();
}

/**
* Parse the encryption key.
*
* @param string $key
* @return string
*/
protected function parseKey(string $key)
{
if (Str::startsWith($key, $prefix = 'base64:')) {
$key = base64_decode(Str::after($key, $prefix));
}

return $key;
}
}
35 changes: 35 additions & 0 deletions tests/Integration/Console/EnvironmentEncryptCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Illuminate\Tests\Integration\Console;

use Illuminate\Encryption\Encrypter;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\File;
use Mockery as m;
Expand Down Expand Up @@ -120,4 +121,38 @@ public function testItGeneratesTheEncryptionFileWhenForcing()
$this->filesystem->shouldHaveReceived('put')
->with(base_path('.env.encrypted'), m::any());
}

public function testItEncryptsWithGivenKeyAndDisplaysIt()
{
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(false);

$this->artisan('env:encrypt', ['--key' => $key = 'ANvVbPbE0tWMHpUySh6liY4WaCmAYKXP'])
->expectsOutputToContain('Environment successfully encrypted')
->expectsOutputToContain($key)
->expectsOutputToContain('.env.encrypted')
->assertExitCode(0);
}

public function testItEncryptsWithGivenGeneratedBase64KeyAndDisplaysIt()
{
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(false);

$key = Encrypter::generateKey('AES-256-CBC');

$this->artisan('env:encrypt', ['--key' => 'base64:'.base64_encode($key)])
->expectsOutputToContain('Environment successfully encrypted')
->expectsOutputToContain('base64:'.base64_encode($key))
->expectsOutputToContain('.env.encrypted')
->assertExitCode(0);
}
}

0 comments on commit eb19ed8

Please sign in to comment.