Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds env:passport command #206

Merged
merged 4 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/Commands/EnvPassportCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Laravel\VaporCli\Commands;

use Dotenv\Dotenv;
use Dotenv\Exception\ExceptionInterface;
use Laravel\VaporCli\Helpers;
use Symfony\Component\Console\Input\InputArgument;

class EnvPassportCommand extends Command
{
/**
* Configure the command options.
*
* @return void
*/
protected function configure()
{
$this
->setName('env:passport')
->addArgument('environment', InputArgument::OPTIONAL, 'The environment name')
->setDescription('Store the application\'s Passport keys in the given environment file');
}

/**
* Execute the command.
*
* @return void
*/
public function handle()
{
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)) {
return static::FAILURE;
}

$environment = $this->argument('environment');
$environmentFile = '.env.'.$environment;

if (! file_exists(getcwd().'/'.$environmentFile)) {
Helpers::abort('Unable to find .env file for environment: '.$environment);
}

if (! file_exists(getcwd().'/storage/oauth-private.key') ||
! file_exists(getcwd().'/storage/oauth-public.key')) {
Helpers::abort('Unable to find Passport keys in [storage] directory.');
}

$contents = file_get_contents(getcwd().'/'.$environmentFile);

try {
$variables = Dotenv::parse($contents);
} catch (ExceptionInterface $e) {
Helpers::abort('Unable to parse environment file: '.$environmentFile);
}

if (array_key_exists('PASSPORT_PRIVATE_KEY', $variables) || array_key_exists('PASSPORT_PRIVATE_KEY', $variables)) {
Helpers::abort('The environment file already contains Passport keys.');
}

$contents .= PHP_EOL.'PASSPORT_PRIVATE_KEY="'.file_get_contents(getcwd().'/storage/oauth-private.key').'"';
$contents .= PHP_EOL.'PASSPORT_PUBLIC_KEY="'.file_get_contents(getcwd().'/storage/oauth-public.key').'"';

file_put_contents(getcwd().'/'.$environmentFile, $contents);

Helpers::info('Keys successfully added to '.$environmentFile.'.');
Helpers::line('You should now encrypt the environment file using the "env:encrypt" command before redeploying your application.');
}
}
1 change: 1 addition & 0 deletions vapor
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ $app->add(new Commands\ProjectListCommand);
$app->add(new Commands\EnvListCommand);
$app->add(new Commands\EnvCommand);
$app->add(new Commands\EnvDescribeCommand);
$app->add(new Commands\EnvPassportCommand);
$app->add(new Commands\EnvPullCommand);
$app->add(new Commands\EnvPushCommand);
$app->add(new Commands\EnvCloneCommand);
Expand Down