|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Laravel\VaporCli\Commands; |
| 4 | + |
| 5 | +use Dotenv\Dotenv; |
| 6 | +use Dotenv\Exception\ExceptionInterface; |
| 7 | +use Laravel\VaporCli\Helpers; |
| 8 | +use Symfony\Component\Console\Input\InputArgument; |
| 9 | + |
| 10 | +class EnvPassportCommand extends Command |
| 11 | +{ |
| 12 | + /** |
| 13 | + * Configure the command options. |
| 14 | + * |
| 15 | + * @return void |
| 16 | + */ |
| 17 | + protected function configure() |
| 18 | + { |
| 19 | + $this |
| 20 | + ->setName('env:passport') |
| 21 | + ->addArgument('environment', InputArgument::OPTIONAL, 'The environment name') |
| 22 | + ->setDescription('Store the application\'s Passport keys in the given environment file'); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Execute the command. |
| 27 | + * |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + public function handle() |
| 31 | + { |
| 32 | + if (! Helpers::confirm("Passport keys are too large to be stored as AWS Lambda environment variables. You should only use this command when using Laravel's encrypted environment files. Would you like to proceed?", false)) { |
| 33 | + return static::FAILURE; |
| 34 | + } |
| 35 | + |
| 36 | + $environment = $this->argument('environment'); |
| 37 | + $environmentFile = '.env.'.$environment; |
| 38 | + |
| 39 | + if (! file_exists(getcwd().'/'.$environmentFile)) { |
| 40 | + Helpers::abort('Unable to find .env file for environment: '.$environment); |
| 41 | + } |
| 42 | + |
| 43 | + if (! file_exists(getcwd().'/storage/oauth-private.key') || |
| 44 | + ! file_exists(getcwd().'/storage/oauth-public.key')) { |
| 45 | + Helpers::abort('Unable to find Passport keys in [storage] directory.'); |
| 46 | + } |
| 47 | + |
| 48 | + $contents = file_get_contents(getcwd().'/'.$environmentFile); |
| 49 | + |
| 50 | + try { |
| 51 | + $variables = Dotenv::parse($contents); |
| 52 | + } catch (ExceptionInterface $e) { |
| 53 | + Helpers::abort('Unable to parse environment file: '.$environmentFile); |
| 54 | + } |
| 55 | + |
| 56 | + if (array_key_exists('PASSPORT_PRIVATE_KEY', $variables) || array_key_exists('PASSPORT_PRIVATE_KEY', $variables)) { |
| 57 | + Helpers::abort('The environment file already contains Passport keys.'); |
| 58 | + } |
| 59 | + |
| 60 | + $contents .= PHP_EOL.'PASSPORT_PRIVATE_KEY="'.file_get_contents(getcwd().'/storage/oauth-private.key').'"'; |
| 61 | + $contents .= PHP_EOL.'PASSPORT_PUBLIC_KEY="'.file_get_contents(getcwd().'/storage/oauth-public.key').'"'; |
| 62 | + |
| 63 | + file_put_contents(getcwd().'/'.$environmentFile, $contents); |
| 64 | + |
| 65 | + Helpers::info('Keys successfully added to '.$environmentFile.'.'); |
| 66 | + Helpers::line('You should now encrypt the environment file using the "env:encrypt" command before redeploying your application.'); |
| 67 | + } |
| 68 | +} |
0 commit comments