|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of laravel-env-security. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Signature Tech Studio, Inc <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace STS\EnvSecurity\Console; |
|
12
|
|
|
|
|
13
|
|
|
use STS\EnvSecurity\Console\Concerns\HandlesEnvFiles; |
|
14
|
|
|
use Illuminate\Console\Command; |
|
15
|
|
|
use STS\EnvSecurity\EnvSecurityManager; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class Decrypt |
|
20
|
|
|
* @package STS\EnvSecurity\Console |
|
21
|
|
|
*/ |
|
22
|
|
|
class Decrypt extends Command |
|
23
|
|
|
{ |
|
24
|
|
|
use HandlesEnvFiles; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The name and signature of the console command. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $signature = 'env:decrypt |
|
32
|
|
|
{environment? : Which environment file you wish to decrypt} |
|
33
|
|
|
{--o|out= : Saves the decrypted file to an alternate location}'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The console command description. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $description = 'Decrypt a .env file. Tries to deduce the environment if none provided.'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var EnvSecurityManager |
|
44
|
|
|
*/ |
|
45
|
|
|
protected $envSecurity; |
|
46
|
|
|
|
|
47
|
|
|
public function __construct(EnvSecurityManager $envSecurity) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->envSecurity = $envSecurity; |
|
50
|
|
|
|
|
51
|
|
|
parent::__construct(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Execute the console command. |
|
56
|
|
|
* |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
|
|
public function handle() |
|
60
|
|
|
{ |
|
61
|
|
|
if (!$environment = $this->getEnvironment()) { |
|
62
|
|
|
$this->error("No environment specified, and we couldn't resolve it on our own"); |
|
63
|
|
|
|
|
64
|
|
|
return 1; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
if (!$ciphertext = $this->loadEncrypted($environment)) { |
|
68
|
|
|
$this->error("Unable to load encrypted .env file for environment [$environment]"); |
|
69
|
|
|
|
|
70
|
|
|
return 1; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$plaintext = $this->envSecurity->decrypt($ciphertext); |
|
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
if (!$this->saveDecrypted($plaintext, $this->option('out'))) { |
|
76
|
|
|
$this->error("Unable to save decrypted .env file"); |
|
77
|
|
|
|
|
78
|
|
|
return 1; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
$this->info("Successfully decrypted .env for environment [$environment]"); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return array|string |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function getEnvironment() |
|
88
|
|
|
{ |
|
89
|
|
|
return is_null($this->argument('environment')) |
|
90
|
|
|
? $this->envSecurity->resolveEnvironment() |
|
91
|
|
|
: $this->argument('environment'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|