Skip to content

Commit 2df4b07

Browse files
author
Matthew Guillot
committed
Add artisan encryptenv:console command to allow running console command that require decrypted env variables
1 parent bb443e3 commit 2df4b07

File tree

6 files changed

+115
-10
lines changed

6 files changed

+115
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
**v1.0.7** - Added artisan encryptenv:console command
2+
13
**v1.0.6** - Updated dependencies to support Laravel 9
24

35
**v1.0.5** - Updated dependencies to support Laravel 8

README.md

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ This publishes the following files:
4444
app/Helpers/secEnv.php
4545
config/encryptenv.php
4646
app/Console/Commands/EncryptEnvValues.php
47+
app/Console/Commands/SecEnvConsoleCommand.php
4748
```
4849

4950
#
@@ -59,7 +60,7 @@ Clear out and re-generate your autoload files, otherwise the new files entry you
5960
$ composer dump-autoload
6061
```
6162
#
62-
#### Add the new console command to the commands array in `app/Console/Kernel.php`
63+
#### Add the new console commands to the commands array in `app/Console/Kernel.php`
6364
```php
6465

6566
/**
@@ -69,7 +70,8 @@ $ composer dump-autoload
6970
*/
7071
protected $commands = [
7172
...
72-
'App\Console\Commands\EncryptEnvValues'
73+
'App\Console\Commands\EncryptEnvValues',
74+
'App\Console\Commands\SecEnvConsoleCommand'
7375
];
7476
```
7577
#
@@ -245,7 +247,7 @@ variables (or custom config file values).
245247

246248
1. [Preparing for Encryption](#preparing-for-encryption)
247249
2. [Using the Encryption flag](#using-the-encryption-flag)
248-
3. [Running the Console Command](#running-the-console-command)
250+
3. [Running the Console Commands](#running-the-console-commands)
249251
4. [File Permissions](#file-permissions)
250252

251253

@@ -350,16 +352,20 @@ return [
350352
In both examples (above) the values for APP_KEY, MYSQL_USER, MYSQL_PASS, and SERVICE_API_KEY are flagged for encryption
351353
and will be replaced with the encrypted string when running the console command (below).
352354

353-
### Running the Console Command
355+
### Running the Console Commands
354356

355-
To run the encryption sequence in your environment variables file, execute the artisan console command included with this package
357+
#### Encrypting your environment variables
356358

357-
The artisan console command encryptenv:encrypt has one optional argument `configkey`. Having the config key as an optional
359+
`php artisan encryptenv:encrypt`
360+
361+
This command will run the encryption sequence in your environment variables file, execute the artisan console command included with this package
362+
363+
There is one optional argument `configkey`. Having the config key as an optional
358364
argument allows you to add this console command to your own scripts for things like automation in your deployment process.
359365
If you do use the configkey argument, it is recommended that you include safeguards to prevent this console command from
360-
being recorded in your shell's history (to protect your Config Key).
366+
being recorded in your shell's history (to protect your Config Key).
361367

362-
More on this here:
368+
More on protecting your config key here:
363369
https://stackoverflow.com/questions/6475524/how-do-i-prevent-commands-from-showing-up-in-bash-history
364370

365371
##### Generating a new CONFIGKEY (encryption key)
@@ -382,7 +388,7 @@ You will need to update your web service configuration file with this new CONFIG
382388
Refer to the Install [Configure your web service] section in the README for more info
383389
```
384390

385-
##### Running Console Command With An Existing CONFIGKEY
391+
##### Running This Command With An Existing CONFIGKEY
386392

387393
If you already have a CONFIGKEY set up and configured for your web service, simply run the encryptenv:encrypt artisan
388394
command as follows:
@@ -430,6 +436,20 @@ If you set everything up correctly, Laravel should now be working with your encr
430436

431437
Note: You should run `php artisan config:clear` to clear your config cache just to be sure everything is truly working.
432438

439+
### Running console commands that require encrypted environment variables
440+
`php artisan encryptenv:console`
441+
442+
This command exists to allow you to run console commands that require your environment variables to be decrypted during execution.
443+
444+
For example `php artisan encryptenv:console 'php artisan migrate'`
445+
446+
The first required argument `console_command` which must be wrapped in single quotes or regular quotes
447+
448+
The second optional argument is `configkey`. This allows you to add console commands that require the CONFIGKEY to deployment scripts or cron jobs.
449+
450+
As noted above, you should do your due diligence to protect your config key from being saved in your shell's history.
451+
452+
433453
### File Permissions
434454

435455
You should make `public/index.php` read-only to non-privileged users. This prevents a malicious user from adding code to
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php namespace App\Console\Commands;
2+
3+
use Illuminate\Console\Command;
4+
use mrgswift\EncryptEnv\Action\Encrypt;
5+
use Symfony\Component\Process\Process;
6+
use Symfony\Component\Process\Exception\ProcessFailedException;
7+
8+
class SecEnvConsoleCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'encryptenv:console {console_command} {configkey?}';
16+
17+
/**
18+
* The console command description.
19+
*
20+
* @var string
21+
*/
22+
protected $description = 'Runs a console command using the user-provided CONFIGKEY';
23+
24+
/**
25+
* Create a new command instance.
26+
*
27+
* @return void
28+
*/
29+
public function __construct()
30+
{
31+
parent::__construct();
32+
}
33+
34+
/**
35+
* Execute the console command.
36+
*
37+
* @return boolean
38+
*/
39+
public function handle()
40+
{
41+
$encrypter = new Encrypt;
42+
43+
$configkey = $this->argument('configkey');
44+
45+
while (empty($configkey)) {
46+
$configkey = $this->ask('Config Key ('.$encrypter->getKeySize().' char key)');
47+
}
48+
49+
$cmdarr = explode(' ', $this->argument('console_command'));
50+
51+
$process = new Process($cmdarr, null,[
52+
'APP_CONFIGKEY' => $configkey
53+
]);
54+
55+
unset($configkey);
56+
57+
$process->run();
58+
59+
// executes after the command finishes
60+
if (!$process->isSuccessful()) {
61+
throw new ProcessFailedException($process);
62+
}
63+
64+
echo $process->getOutput();
65+
66+
unset($process);
67+
68+
return true;
69+
}
70+
}

src/Entity/ConfigFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function __construct()
4141
$this->configarr = !empty($this->configpath) &&
4242
file_exists($this->configpath) ?
4343
require $this->configpath :
44-
$_ENV;
44+
(!empty($_ENV) ? $_ENV : getenv());
4545

4646
!empty($this->configpath) && !empty($encenv_config['custom_config_output']) ?
4747
$this->configoutput = $encenv_config['custom_config_output'] :

src/Entity/ConfigKey.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,19 @@ class ConfigKey
77

88
function __construct()
99
{
10+
1011
if (!empty($_SERVER['CONFIGKEY'])) {
1112

1213
$this->configkey = $_SERVER['CONFIGKEY'];
1314

15+
} else {
16+
$configkey = getenv('APP_CONFIGKEY');
17+
18+
if ($configkey !== false) {
19+
20+
$this->configkey = $configkey;
21+
22+
}
1423
}
1524
}
1625
/**

src/Provider/EncryptEnvServiceProvider.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ public function boot()
2020
$this->publishes([
2121
__DIR__.'/../Console/EncryptEnvValues.php' => app_path('Console/Commands/EncryptEnvValues.php')
2222
], 'console');
23+
24+
$this->publishes([
25+
__DIR__.'/../Console/SecEnvConsoleCommand.php' => app_path('Console/Commands/SecEnvConsoleCommand.php')
26+
], 'console');
2327
}
2428

2529
public function register()

0 commit comments

Comments
 (0)