From e71f66d3bf06ac600f0fd665c2bd604b2872fc23 Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Wed, 10 May 2023 12:52:56 -0300 Subject: [PATCH 1/7] Add deployment solution EnvironmentHasExceededLimit --- src/Commands/Output/DeploymentFailure.php | 13 +++-- src/Models/Deployment.php | 1 + src/Solutions/EnvironmentHasExceededLimit.php | 52 +++++++++++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 src/Solutions/EnvironmentHasExceededLimit.php diff --git a/src/Commands/Output/DeploymentFailure.php b/src/Commands/Output/DeploymentFailure.php index cf408b56..6f705c72 100644 --- a/src/Commands/Output/DeploymentFailure.php +++ b/src/Commands/Output/DeploymentFailure.php @@ -20,10 +20,17 @@ public function render(Deployment $deployment) Helpers::line(''); Helpers::danger(' Deployment Failed '); - if ($deployment->status_message) { + if ($message = $deployment->status_message) { Helpers::line(''); - $message = $deployment->status_message; - Helpers::line(" $message"); + + $limitEnvironmentMessage = 'AWS: Lambda was unable to configure your environment variables because the environment variables you have provided exceeded the 4KB limit'; + + $output = str_contains($message, $limitEnvironmentMessage) + ? " $limitEnvironmentMessage" + : " $message"; + + Helpers::line(''); + Helpers::line($output); } $deployment->solutions()->whenNotEmpty(function ($solutions) { diff --git a/src/Models/Deployment.php b/src/Models/Deployment.php index 3e14f5c5..72066bdf 100644 --- a/src/Models/Deployment.php +++ b/src/Models/Deployment.php @@ -147,6 +147,7 @@ public function solutions() Solutions\FunctionExceedsMaximumAllowedSize::class, Solutions\ResourceUpdateInProgress::class, Solutions\RunDeploymentHooksTimedOut::class, + Solutions\EnvironmentHasExceededLimit::class, ])->map(function ($solutionsClass) { return new $solutionsClass($this); })->filter diff --git a/src/Solutions/EnvironmentHasExceededLimit.php b/src/Solutions/EnvironmentHasExceededLimit.php new file mode 100644 index 00000000..685f488f --- /dev/null +++ b/src/Solutions/EnvironmentHasExceededLimit.php @@ -0,0 +1,52 @@ +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 [ + 'You can 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', + 'You can also use the "Secrets" feature in conjunction with "Keys": https://vapor.laravel.com/app/projects/'.$this->deployment->project_id.'/environments/'.$this->deployment->environment['name'].'/secrets', + ]; + } +} From eb570423ebae8d8bac4d7540419f5d7d357960fd Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Wed, 10 May 2023 12:53:05 -0300 Subject: [PATCH 2/7] Add test EnvironmentHasExceededLimit --- tests/SolutionsTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/SolutionsTest.php b/tests/SolutionsTest.php index 1bfb95c7..be82b2e0 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(2, $deployment->solutions()); + $this->assertStringContainsString('https://vapor.laravel.com/app/projects/1/environments/foo/secrets', $deployment->solutions()[1]); + } } From 61e91819a925503594d30db8a9871ac1fd97dc8e Mon Sep 17 00:00:00 2001 From: luanfreitasdev Date: Wed, 10 May 2023 13:09:05 -0300 Subject: [PATCH 3/7] Change str_contains to Str::contains --- src/Commands/Output/DeploymentFailure.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Commands/Output/DeploymentFailure.php b/src/Commands/Output/DeploymentFailure.php index 6f705c72..fa1185f7 100644 --- a/src/Commands/Output/DeploymentFailure.php +++ b/src/Commands/Output/DeploymentFailure.php @@ -2,6 +2,7 @@ namespace Laravel\VaporCli\Commands\Output; +use Illuminate\Support\Str; use Laravel\VaporCli\Commands\HookOutputCommand; use Laravel\VaporCli\ConsoleVaporClient; use Laravel\VaporCli\Helpers; @@ -25,7 +26,7 @@ public function render(Deployment $deployment) $limitEnvironmentMessage = 'AWS: Lambda was unable to configure your environment variables because the environment variables you have provided exceeded the 4KB limit'; - $output = str_contains($message, $limitEnvironmentMessage) + $output = Str::contains($message, $limitEnvironmentMessage) ? " $limitEnvironmentMessage" : " $message"; From 09ce1615f935cc35b8238140b86ed29770731512 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Thu, 11 May 2023 15:23:53 +0100 Subject: [PATCH 4/7] move message filter --- src/Commands/Output/DeploymentFailure.php | 14 ++------ src/Models/Deployment.php | 41 +++++++++++++++++------ 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/Commands/Output/DeploymentFailure.php b/src/Commands/Output/DeploymentFailure.php index fa1185f7..8d50805c 100644 --- a/src/Commands/Output/DeploymentFailure.php +++ b/src/Commands/Output/DeploymentFailure.php @@ -2,7 +2,6 @@ namespace Laravel\VaporCli\Commands\Output; -use Illuminate\Support\Str; use Laravel\VaporCli\Commands\HookOutputCommand; use Laravel\VaporCli\ConsoleVaporClient; use Laravel\VaporCli\Helpers; @@ -13,7 +12,6 @@ class DeploymentFailure /** * Render the output. * - * @param \Laravel\VaporCli\Models\Deployment $deployment * @return void */ public function render(Deployment $deployment) @@ -21,17 +19,9 @@ public function render(Deployment $deployment) Helpers::line(''); Helpers::danger(' Deployment Failed '); - if ($message = $deployment->status_message) { + if ($deployment->status_message) { Helpers::line(''); - - $limitEnvironmentMessage = 'AWS: Lambda was unable to configure your environment variables because the environment variables you have provided exceeded the 4KB limit'; - - $output = Str::contains($message, $limitEnvironmentMessage) - ? " $limitEnvironmentMessage" - : " $message"; - - Helpers::line(''); - Helpers::line($output); + Helpers::line($deployment->formattedStatusMessage()); } $deployment->solutions()->whenNotEmpty(function ($solutions) { diff --git a/src/Models/Deployment.php b/src/Models/Deployment.php index 72066bdf..9da88118 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,16 +36,15 @@ 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) { + ->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); @@ -47,7 +54,6 @@ public function displayableSteps(array $displayedSteps = []) /** * Determine if the given deployment step should be displayed. * - * @param array $step * @return bool */ protected function stepShouldBeDisplayed(array $step) @@ -151,10 +157,23 @@ public function solutions() ])->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; } /** From b119309904e6155aba7b9b64f97a19ff4791be13 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Thu, 11 May 2023 15:31:06 +0100 Subject: [PATCH 5/7] formatting --- src/Models/Deployment.php | 2 +- ...ExceededLimit.php => EnvironmentVariableLimitReached.php} | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) rename src/Solutions/{EnvironmentHasExceededLimit.php => EnvironmentVariableLimitReached.php} (69%) diff --git a/src/Models/Deployment.php b/src/Models/Deployment.php index 9da88118..bd3276b3 100644 --- a/src/Models/Deployment.php +++ b/src/Models/Deployment.php @@ -153,7 +153,7 @@ public function solutions() Solutions\FunctionExceedsMaximumAllowedSize::class, Solutions\ResourceUpdateInProgress::class, Solutions\RunDeploymentHooksTimedOut::class, - Solutions\EnvironmentHasExceededLimit::class, + Solutions\EnvironmentVariableLimitReached::class, ])->map(function ($solutionsClass) { return new $solutionsClass($this); })->filter diff --git a/src/Solutions/EnvironmentHasExceededLimit.php b/src/Solutions/EnvironmentVariableLimitReached.php similarity index 69% rename from src/Solutions/EnvironmentHasExceededLimit.php rename to src/Solutions/EnvironmentVariableLimitReached.php index 685f488f..86ebf56c 100644 --- a/src/Solutions/EnvironmentHasExceededLimit.php +++ b/src/Solutions/EnvironmentVariableLimitReached.php @@ -5,7 +5,7 @@ use Illuminate\Support\Str; use Laravel\VaporCli\Deployment; -class EnvironmentHasExceededLimit +class EnvironmentVariableLimitReached { /** * The deployment that have failed. @@ -45,8 +45,7 @@ public function applicable() public function all() { return [ - 'You can 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', - 'You can also use the "Secrets" feature in conjunction with "Keys": https://vapor.laravel.com/app/projects/'.$this->deployment->project_id.'/environments/'.$this->deployment->environment['name'].'/secrets', + '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', ]; } } From d064ec8e39b901f46fbdd323686c644f60e8395c Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Thu, 11 May 2023 15:34:09 +0100 Subject: [PATCH 6/7] update tests --- tests/SolutionsTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/SolutionsTest.php b/tests/SolutionsTest.php index be82b2e0..5b771485 100644 --- a/tests/SolutionsTest.php +++ b/tests/SolutionsTest.php @@ -76,7 +76,7 @@ public function test_environment_has_exceeded_limit() 'status_message' => 'Lambda was unable to configure your environment variables because the environment variables you have provided exceeded the 4KB limit', ]); - $this->assertCount(2, $deployment->solutions()); - $this->assertStringContainsString('https://vapor.laravel.com/app/projects/1/environments/foo/secrets', $deployment->solutions()[1]); + $this->assertCount(1, $deployment->solutions()); + $this->assertStringContainsString('Use encrypted environment files in place of or in addition to environment variables', $deployment->solutions()[0]); } } From 2f61a253adaf935b2723cdec2bec7ead5100a77d Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Thu, 11 May 2023 15:37:25 +0100 Subject: [PATCH 7/7] formatting --- src/Models/Deployment.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Models/Deployment.php b/src/Models/Deployment.php index bd3276b3..958c316d 100644 --- a/src/Models/Deployment.php +++ b/src/Models/Deployment.php @@ -45,10 +45,10 @@ public function displayableSteps(array $displayedSteps = []) 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(); + return $this->formatDeploymentStepName($step['name']); + })->filter(function ($step) use ($displayedSteps) { + return ! in_array($step, $displayedSteps); + })->all(); } /**