Skip to content

Commit

Permalink
[9.x] Prevents booting providers when running env:decrypt (#44654)
Browse files Browse the repository at this point in the history
* Prevent provider boot

* wip

* wip

* Support custom file path

* Remove argument

* Add reboot state

* Wording

* Revert reboot state

* Update force check

* Remove duplicate

* Update tests

* Format output filename
  • Loading branch information
joedixon authored Oct 20, 2022
1 parent 1661857 commit 6319837
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 12 deletions.
36 changes: 24 additions & 12 deletions src/Illuminate/Foundation/Console/EnvironmentDecryptCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class EnvironmentDecryptCommand extends Command
{--cipher= : The encryption cipher}
{--env= : The environment to be decrypted}
{--force : Overwrite the existing environment file}
{--filename= : Where to write the decrypted file contents}';
{--path= : Path to write the decrypted file}
{--filename= : Filename of the decrypted file}';

/**
* The name of the console command.
Expand Down Expand Up @@ -82,17 +83,13 @@ public function handle()

$key = $this->parseKey($key);

$environmentFile = $this->option('env')
$encryptedFile = ($this->option('env')
? base_path('.env').'.'.$this->option('env')
: $this->laravel->environmentFilePath();
: $this->laravel->environmentFilePath()).'.encrypted';

$encryptedFile = $environmentFile.'.encrypted';
$outputFile = $this->outputFilePath();

$filename = $this->option('filename')
? base_path($this->option('filename'))
: $environmentFile;

if (Str::endsWith($filename, '.encrypted')) {
if (Str::endsWith($outputFile, '.encrypted')) {
$this->components->error('Invalid filename.');

return Command::FAILURE;
Expand All @@ -104,7 +101,7 @@ public function handle()
return Command::FAILURE;
}

if ($this->files->exists($environmentFile) && ! $this->option('force')) {
if ($this->files->exists($outputFile) && ! $this->option('force')) {
$this->components->error('Environment file already exists.');

return Command::FAILURE;
Expand All @@ -114,7 +111,7 @@ public function handle()
$encrypter = new Encrypter($key, $cipher);

$this->files->put(
$filename,
$outputFile,
$encrypter->decrypt($this->files->get($encryptedFile))
);
} catch (Exception $e) {
Expand All @@ -125,7 +122,7 @@ public function handle()

$this->components->info('Environment successfully decrypted.');

$this->components->twoColumnDetail('Decrypted file', $filename);
$this->components->twoColumnDetail('Decrypted file', $outputFile);

$this->newLine();
}
Expand All @@ -144,4 +141,19 @@ protected function parseKey(string $key)

return $key;
}

/**
* Get the output file path that should be used for the command.
*
* @return string
*/
protected function outputFilePath()
{
$path = Str::finish($this->option('path') ?: base_path(), DIRECTORY_SEPARATOR);

$outputFile = $this->option('filename') ?: ('.env'.($this->option('env') ? '.'.$this->option('env') : ''));
$outputFile = ltrim($outputFile, DIRECTORY_SEPARATOR);

return $path.$outputFile;
}
}
22 changes: 22 additions & 0 deletions src/Illuminate/Foundation/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ public function handle($input, $output = null)
$this->commandStartedAt = Carbon::now();

try {
if ($input->getFirstArgument() === 'env:decrypt') {
$this->bootstrapWithoutBootingProviders();
}

$this->bootstrap();

return $this->getArtisan()->run($input, $output);
Expand Down Expand Up @@ -323,6 +327,10 @@ public function registerCommand($command)
*/
public function call($command, array $parameters = [], $outputBuffer = null)
{
if ($command === 'env:decrypt') {
$this->bootstrapWithoutBootingProviders();
}

$this->bootstrap();

return $this->getArtisan()->call($command, $parameters, $outputBuffer);
Expand Down Expand Up @@ -384,6 +392,20 @@ public function bootstrap()
}
}

/**
* Bootstrap the application without booting service providers.
*
* @return void
*/
public function bootstrapWithoutBootingProviders()
{
$this->app->bootstrapWith(
collect($this->bootstrappers())->reject(function ($bootstrapper) {
return $bootstrapper === \Illuminate\Foundation\Bootstrap\BootProviders::class;
})->all()
);
}

/**
* Get the Artisan application instance.
*
Expand Down
46 changes: 46 additions & 0 deletions tests/Integration/Console/EnvironmentDecryptCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,52 @@ public function testItWritesTheEnvironmentFileCustomFilename()
->with(base_path('.env'), 'APP_NAME="Laravel Two"');
}

public function testItWritesTheEnvironmentFileCustomPath()
{
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(false)
->shouldReceive('get')
->once()
->andReturn(
(new Encrypter('abcdefghijklmnopabcdefghijklmnop', 'AES-256-CBC'))
->encrypt('APP_NAME="Laravel Two"')
);

$this->artisan('env:decrypt', ['--env' => 'production', '--key' => 'abcdefghijklmnopabcdefghijklmnop', '--path' => '/tmp'])
->expectsOutputToContain('Environment successfully decrypted.')
->assertExitCode(0);

$this->filesystem->shouldHaveReceived('put')
->with('/tmp'.DIRECTORY_SEPARATOR.'.env.production', 'APP_NAME="Laravel Two"');
}

public function testItWritesTheEnvironmentFileCustomPathAndFilename()
{
$this->filesystem->shouldReceive('exists')
->once()
->andReturn(true)
->shouldReceive('exists')
->once()
->andReturn(false)
->shouldReceive('get')
->once()
->andReturn(
(new Encrypter('abcdefghijklmnopabcdefghijklmnop', 'AES-256-CBC'))
->encrypt('APP_NAME="Laravel Two"')
);

$this->artisan('env:decrypt', ['--env' => 'production', '--key' => 'abcdefghijklmnopabcdefghijklmnop', '--filename' => '.env', '--path' => '/tmp'])
->expectsOutputToContain('Environment successfully decrypted.')
->assertExitCode(0);

$this->filesystem->shouldHaveReceived('put')
->with('/tmp'.DIRECTORY_SEPARATOR.'.env', 'APP_NAME="Laravel Two"');
}

public function testItCannotOverwriteEncryptedFiles()
{
$this->artisan('env:decrypt', ['--env' => 'production', '--key' => 'abcdefghijklmnop', '--filename' => '.env.production.encrypted'])
Expand Down

0 comments on commit 6319837

Please sign in to comment.