Skip to content

Commit eb19ed8

Browse files
ekateivajoedixon
andauthored
[9.x] Allow to pass base64 key to env:encrypt command (#45157)
* Encrypt .env file when base64 key given * Display passed base64 in a console output * Parse all keys Co-authored-by: Joe Dixon <[email protected]>
1 parent d828da6 commit eb19ed8

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

src/Illuminate/Foundation/Console/EnvironmentEncryptCommand.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Console\Command;
77
use Illuminate\Encryption\Encrypter;
88
use Illuminate\Filesystem\Filesystem;
9+
use Illuminate\Support\Str;
910
use Symfony\Component\Console\Attribute\AsCommand;
1011

1112
#[AsCommand(name: 'env:encrypt')]
@@ -96,7 +97,7 @@ public function handle()
9697
}
9798

9899
try {
99-
$encrypter = new Encrypter($key, $cipher);
100+
$encrypter = new Encrypter($this->parseKey($key), $cipher);
100101

101102
$this->files->put(
102103
$encryptedFile,
@@ -116,4 +117,19 @@ public function handle()
116117

117118
$this->newLine();
118119
}
120+
121+
/**
122+
* Parse the encryption key.
123+
*
124+
* @param string $key
125+
* @return string
126+
*/
127+
protected function parseKey(string $key)
128+
{
129+
if (Str::startsWith($key, $prefix = 'base64:')) {
130+
$key = base64_decode(Str::after($key, $prefix));
131+
}
132+
133+
return $key;
134+
}
119135
}

tests/Integration/Console/EnvironmentEncryptCommandTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Illuminate\Tests\Integration\Console;
44

5+
use Illuminate\Encryption\Encrypter;
56
use Illuminate\Filesystem\Filesystem;
67
use Illuminate\Support\Facades\File;
78
use Mockery as m;
@@ -120,4 +121,38 @@ public function testItGeneratesTheEncryptionFileWhenForcing()
120121
$this->filesystem->shouldHaveReceived('put')
121122
->with(base_path('.env.encrypted'), m::any());
122123
}
124+
125+
public function testItEncryptsWithGivenKeyAndDisplaysIt()
126+
{
127+
$this->filesystem->shouldReceive('exists')
128+
->once()
129+
->andReturn(true)
130+
->shouldReceive('exists')
131+
->once()
132+
->andReturn(false);
133+
134+
$this->artisan('env:encrypt', ['--key' => $key = 'ANvVbPbE0tWMHpUySh6liY4WaCmAYKXP'])
135+
->expectsOutputToContain('Environment successfully encrypted')
136+
->expectsOutputToContain($key)
137+
->expectsOutputToContain('.env.encrypted')
138+
->assertExitCode(0);
139+
}
140+
141+
public function testItEncryptsWithGivenGeneratedBase64KeyAndDisplaysIt()
142+
{
143+
$this->filesystem->shouldReceive('exists')
144+
->once()
145+
->andReturn(true)
146+
->shouldReceive('exists')
147+
->once()
148+
->andReturn(false);
149+
150+
$key = Encrypter::generateKey('AES-256-CBC');
151+
152+
$this->artisan('env:encrypt', ['--key' => 'base64:'.base64_encode($key)])
153+
->expectsOutputToContain('Environment successfully encrypted')
154+
->expectsOutputToContain('base64:'.base64_encode($key))
155+
->expectsOutputToContain('.env.encrypted')
156+
->assertExitCode(0);
157+
}
123158
}

0 commit comments

Comments
 (0)