Skip to content

Commit 259e0dc

Browse files
committed
fix conflicts
2 parents ed4d751 + 1ce080b commit 259e0dc

File tree

5 files changed

+632
-0
lines changed

5 files changed

+632
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Console;
4+
5+
use Exception;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Encryption\Encrypter;
8+
use Illuminate\Filesystem\Filesystem;
9+
use Illuminate\Support\Env;
10+
use Illuminate\Support\Str;
11+
use Symfony\Component\Console\Attribute\AsCommand;
12+
13+
#[AsCommand(name: 'env:decrypt')]
14+
class EnvironmentDecryptCommand extends Command
15+
{
16+
/**
17+
* The name and signature of the console command.
18+
*
19+
* @var string
20+
*/
21+
protected $signature = 'env:decrypt
22+
{--key= : The encryption key}
23+
{--cipher= : The encryption cipher}
24+
{--env= : The environment to be decrypted}
25+
{--force : Overwrite the existing environment file}
26+
{--filename= : Where to write the decrypted file contents}';
27+
28+
/**
29+
* The name of the console command.
30+
*
31+
* This name is used to identify the command during lazy loading.
32+
*
33+
* @var string|null
34+
*
35+
* @deprecated
36+
*/
37+
protected static $defaultName = 'env:decrypt';
38+
39+
/**
40+
* The console command description.
41+
*
42+
* @var string
43+
*/
44+
protected $description = 'Decrypt an environment file';
45+
46+
/**
47+
* The filesystem instance.
48+
*
49+
* @var \Illuminate\Filesystem\Filesystem
50+
*/
51+
protected $files;
52+
53+
/**
54+
* Create a new command instance.
55+
*
56+
* @param \Illuminate\Filesystem\Filesystem $files
57+
* @return void
58+
*/
59+
public function __construct(Filesystem $files)
60+
{
61+
parent::__construct();
62+
63+
$this->files = $files;
64+
}
65+
66+
/**
67+
* Execute the console command.
68+
*
69+
* @return void
70+
*/
71+
public function handle()
72+
{
73+
$key = $this->option('key') ?: Env::get('LARAVEL_ENV_ENCRYPTION_KEY');
74+
75+
if (! $key) {
76+
$this->components->error('A decryption key is required.');
77+
78+
return Command::FAILURE;
79+
}
80+
81+
$cipher = $this->option('cipher') ?: 'AES-256-CBC';
82+
83+
$key = $this->parseKey($key);
84+
85+
$environmentFile = $this->option('env')
86+
? base_path('.env').'.'.$this->option('env')
87+
: $this->laravel->environmentFilePath();
88+
89+
$encryptedFile = $environmentFile.'.encrypted';
90+
91+
$filename = $this->option('filename')
92+
? base_path($this->option('filename'))
93+
: $environmentFile;
94+
95+
if (Str::endsWith($filename, '.encrypted')) {
96+
$this->components->error('Invalid filename.');
97+
98+
return Command::FAILURE;
99+
}
100+
101+
if (! $this->files->exists($encryptedFile)) {
102+
$this->components->error('Encrypted environment file not found.');
103+
104+
return Command::FAILURE;
105+
}
106+
107+
if ($this->files->exists($environmentFile) && ! $this->option('force')) {
108+
$this->components->error('Environment file already exists.');
109+
110+
return Command::FAILURE;
111+
}
112+
113+
try {
114+
$encrypter = new Encrypter($key, $cipher);
115+
116+
$this->files->put(
117+
$filename,
118+
$encrypter->decrypt($this->files->get($encryptedFile))
119+
);
120+
} catch (Exception $e) {
121+
$this->components->error($e->getMessage());
122+
123+
return Command::FAILURE;
124+
}
125+
126+
$this->components->info('Environment successfully decrypted.');
127+
128+
$this->components->twoColumnDetail('Decrypted file', $filename);
129+
130+
$this->newLine();
131+
}
132+
133+
/**
134+
* Parse the encryption key.
135+
*
136+
* @param string $key
137+
* @return string
138+
*/
139+
protected function parseKey(string $key)
140+
{
141+
if (Str::startsWith($key, $prefix = 'base64:')) {
142+
$key = base64_decode(Str::after($key, $prefix));
143+
}
144+
145+
return $key;
146+
}
147+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
3+
namespace Illuminate\Foundation\Console;
4+
5+
use Exception;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Encryption\Encrypter;
8+
use Illuminate\Filesystem\Filesystem;
9+
use Symfony\Component\Console\Attribute\AsCommand;
10+
11+
#[AsCommand(name: 'env:encrypt')]
12+
class EnvironmentEncryptCommand extends Command
13+
{
14+
/**
15+
* The name and signature of the console command.
16+
*
17+
* @var string
18+
*/
19+
protected $signature = 'env:encrypt
20+
{--key= : The encryption key}
21+
{--cipher= : The encryption cipher}
22+
{--env= : The environment to be encrypted}
23+
{--force : Overwrite the existing encrypted environment file}';
24+
25+
/**
26+
* The name of the console command.
27+
*
28+
* This name is used to identify the command during lazy loading.
29+
*
30+
* @var string|null
31+
*
32+
* @deprecated
33+
*/
34+
protected static $defaultName = 'env:encrypt';
35+
36+
/**
37+
* The console command description.
38+
*
39+
* @var string
40+
*/
41+
protected $description = 'Encrypt an environment file';
42+
43+
/**
44+
* The filesystem instance.
45+
*
46+
* @var \Illuminate\Filesystem\Filesystem
47+
*/
48+
protected $files;
49+
50+
/**
51+
* Create a new command instance.
52+
*
53+
* @param \Illuminate\Filesystem\Filesystem $files
54+
* @return void
55+
*/
56+
public function __construct(Filesystem $files)
57+
{
58+
parent::__construct();
59+
60+
$this->files = $files;
61+
}
62+
63+
/**
64+
* Execute the console command.
65+
*
66+
* @return void
67+
*/
68+
public function handle()
69+
{
70+
$cipher = $this->option('cipher') ?: 'AES-256-CBC';
71+
72+
$key = $this->option('key');
73+
74+
$keyPassed = $key !== null;
75+
76+
$environmentFile = $this->option('env')
77+
? base_path('.env').'.'.$this->option('env')
78+
: $this->laravel->environmentFilePath();
79+
80+
$encryptedFile = $environmentFile.'.encrypted';
81+
82+
if (! $keyPassed) {
83+
$key = Encrypter::generateKey($cipher);
84+
}
85+
86+
if (! $this->files->exists($environmentFile)) {
87+
$this->components->error('Environment file not found.');
88+
89+
return Command::FAILURE;
90+
}
91+
92+
if ($this->files->exists($encryptedFile) && ! $this->option('force')) {
93+
$this->components->error('Encrypted environment file already exists.');
94+
95+
return Command::FAILURE;
96+
}
97+
98+
try {
99+
$encrypter = new Encrypter($key, $cipher);
100+
101+
$this->files->put(
102+
$encryptedFile,
103+
$encrypter->encrypt($this->files->get($environmentFile))
104+
);
105+
} catch (Exception $e) {
106+
$this->components->error($e->getMessage());
107+
108+
return Command::FAILURE;
109+
}
110+
111+
$this->components->info('Environment successfully encrypted.');
112+
113+
$this->components->twoColumnDetail('Key', ($keyPassed ? $key : 'base64:'.base64_encode($key)));
114+
$this->components->twoColumnDetail('Cipher', $cipher);
115+
$this->components->twoColumnDetail('Encrypted file', $encryptedFile);
116+
117+
$this->newLine();
118+
}
119+
}

src/Illuminate/Foundation/Providers/ArtisanServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
use Illuminate\Foundation\Console\DocsCommand;
3636
use Illuminate\Foundation\Console\DownCommand;
3737
use Illuminate\Foundation\Console\EnvironmentCommand;
38+
use Illuminate\Foundation\Console\EnvironmentDecryptCommand;
39+
use Illuminate\Foundation\Console\EnvironmentEncryptCommand;
3840
use Illuminate\Foundation\Console\EventCacheCommand;
3941
use Illuminate\Foundation\Console\EventClearCommand;
4042
use Illuminate\Foundation\Console\EventGenerateCommand;
@@ -113,6 +115,8 @@ class ArtisanServiceProvider extends ServiceProvider implements DeferrableProvid
113115
'DbWipe' => WipeCommand::class,
114116
'Down' => DownCommand::class,
115117
'Environment' => EnvironmentCommand::class,
118+
'EnvironmentDecrypt' => EnvironmentDecryptCommand::class,
119+
'EnvironmentEncrypt' => EnvironmentEncryptCommand::class,
116120
'EventCache' => EventCacheCommand::class,
117121
'EventClear' => EventClearCommand::class,
118122
'EventList' => EventListCommand::class,

0 commit comments

Comments
 (0)