From 77f86c217b85a0142add4ad285dff2cf4c6d3b7a Mon Sep 17 00:00:00 2001 From: Luan Freitas <33601626+luanfreitasdev@users.noreply.github.com> Date: Fri, 12 May 2023 12:53:50 -0300 Subject: [PATCH] Hide environment keys / Add EnvironmentHasExceededLimit solution (#222) * Add deployment solution EnvironmentHasExceededLimit * Add test EnvironmentHasExceededLimit * Change str_contains to Str::contains * move message filter * formatting * update tests * formatting --------- Co-authored-by: Joe Dixon --- src/Commands/Output/DeploymentFailure.php | 4 +- src/Models/Deployment.php | 50 ++++++++++++------ .../EnvironmentVariableLimitReached.php | 51 +++++++++++++++++++ tests/SolutionsTest.php | 12 +++++ 4 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 src/Solutions/EnvironmentVariableLimitReached.php diff --git a/src/Commands/Output/DeploymentFailure.php b/src/Commands/Output/DeploymentFailure.php index cf408b56..8d50805c 100644 --- a/src/Commands/Output/DeploymentFailure.php +++ b/src/Commands/Output/DeploymentFailure.php @@ -12,7 +12,6 @@ class DeploymentFailure /** * Render the output. * - * @param \Laravel\VaporCli\Models\Deployment $deployment * @return void */ public function render(Deployment $deployment) @@ -22,8 +21,7 @@ public function render(Deployment $deployment) if ($deployment->status_message) { Helpers::line(''); - $message = $deployment->status_message; - Helpers::line(" $message"); + Helpers::line($deployment->formattedStatusMessage()); } $deployment->solutions()->whenNotEmpty(function ($solutions) { diff --git a/src/Models/Deployment.php b/src/Models/Deployment.php index 3e14f5c5..958c316d 100644 --- a/src/Models/Deployment.php +++ b/src/Models/Deployment.php @@ -14,10 +14,18 @@ class Deployment */ public $deployment; + /** + * Error messsages containing sensitive information. + * + * @var array + */ + protected $senstiveStatusMessages = [ + 'AWS: Lambda was unable to configure your environment variables because the environment variables you have provided exceeded the 4KB limit', + ]; + /** * Create a new model instance. * - * @param array $deployment * @return void */ public function __construct(array $deployment) @@ -28,26 +36,24 @@ public function __construct(array $deployment) /** * Get the names of the displayable steps. * - * @param array $displayedSteps * @return array */ public function displayableSteps(array $displayedSteps = []) { return collect($this->steps) - ->filter(function ($step) { - return $step['status'] !== 'pending' && - $step['status'] !== 'cancelled'; - })->map(function ($step) { - return $this->formatDeploymentStepName($step['name']); - })->filter(function ($step) use ($displayedSteps) { - return ! in_array($step, $displayedSteps); - })->all(); + ->filter(function ($step) { + return $step['status'] !== 'pending' && + $step['status'] !== 'cancelled'; + })->map(function ($step) { + return $this->formatDeploymentStepName($step['name']); + })->filter(function ($step) use ($displayedSteps) { + return ! in_array($step, $displayedSteps); + })->all(); } /** * Determine if the given deployment step should be displayed. * - * @param array $step * @return bool */ protected function stepShouldBeDisplayed(array $step) @@ -147,13 +153,27 @@ public function solutions() Solutions\FunctionExceedsMaximumAllowedSize::class, Solutions\ResourceUpdateInProgress::class, Solutions\RunDeploymentHooksTimedOut::class, + Solutions\EnvironmentVariableLimitReached::class, ])->map(function ($solutionsClass) { return new $solutionsClass($this); })->filter - ->applicable() - ->map - ->all() - ->flatten(); + ->applicable() + ->map + ->all() + ->flatten(); + } + + /** + * Format the deployment status message removing any sensitive information. + * + * @return string + */ + public function formattedStatusMessage() + { + return collect($this->senstiveStatusMessages) + ->first(function ($message) { + return Str::contains($this->status_message, $message); + }) ?: $this->status_message; } /** diff --git a/src/Solutions/EnvironmentVariableLimitReached.php b/src/Solutions/EnvironmentVariableLimitReached.php new file mode 100644 index 00000000..86ebf56c --- /dev/null +++ b/src/Solutions/EnvironmentVariableLimitReached.php @@ -0,0 +1,51 @@ +deployment = $deployment; + } + + /** + * Checks if the solution is applicable. + * + * @return bool + */ + public function applicable() + { + return Str::contains($this->deployment->status_message, [ + 'Lambda was unable to configure your environment variables because the environment variables you have provided exceeded the 4KB limit', + ]); + } + + /** + * Returns the list of solutions based on the deployment. + * + * @return array + */ + public function all() + { + return [ + 'Use encrypted environment files in place of or in addition to environment variables: https://docs.vapor.build/1.0/projects/environments.html#encrypted-environment-files', + ]; + } +} diff --git a/tests/SolutionsTest.php b/tests/SolutionsTest.php index 1bfb95c7..5b771485 100644 --- a/tests/SolutionsTest.php +++ b/tests/SolutionsTest.php @@ -67,4 +67,16 @@ public function test_run_deployment_hooks_timed_out() $this->assertCount(2, $deployment->solutions()); $this->assertStringContainsString('https://vapor.laravel.com/app/projects/1/environments/foo/logs', $deployment->solutions()[1]); } + + public function test_environment_has_exceeded_limit() + { + $deployment = new Deployment([ + 'project_id' => 1, + 'environment' => ['name' => 'foo'], + 'status_message' => 'Lambda was unable to configure your environment variables because the environment variables you have provided exceeded the 4KB limit', + ]); + + $this->assertCount(1, $deployment->solutions()); + $this->assertStringContainsString('Use encrypted environment files in place of or in addition to environment variables', $deployment->solutions()[0]); + } }