From 0232cf5b4c4b80e1d90fc0cbb95f04a941e810b2 Mon Sep 17 00:00:00 2001
From: Andras Bacsai
Date: Tue, 24 Oct 2023 15:41:21 +0200
Subject: [PATCH] feat: lock environment variables
---
.../Shared/EnvironmentVariable/All.php | 24 ++++++--
.../Shared/EnvironmentVariable/Show.php | 33 +++++++---
app/Models/EnvironmentVariable.php | 16 +++--
...wn_once_to_environment_variables_table.php | 28 +++++++++
.../shared/environment-variable/all.blade.php | 3 +-
.../environment-variable/show.blade.php | 60 ++++++++++++-------
6 files changed, 121 insertions(+), 43 deletions(-)
create mode 100644 database/migrations/2023_10_24_124934_add_is_shown_once_to_environment_variables_table.php
diff --git a/app/Http/Livewire/Project/Shared/EnvironmentVariable/All.php b/app/Http/Livewire/Project/Shared/EnvironmentVariable/All.php
index 9b714a590..b1fa237e0 100644
--- a/app/Http/Livewire/Project/Shared/EnvironmentVariable/All.php
+++ b/app/Http/Livewire/Project/Shared/EnvironmentVariable/All.php
@@ -31,11 +31,17 @@ public function mount()
public function getDevView()
{
$this->variables = $this->resource->environment_variables->map(function ($item) {
+ if ($item->is_shown_once) {
+ return "$item->key=(locked secret)";
+ }
return "$item->key=$item->value";
})->sort()->join('
');
if ($this->showPreview) {
$this->variablesPreview = $this->resource->environment_variables_preview->map(function ($item) {
+ if ($item->is_shown_once) {
+ return "$item->key=(locked secret)";
+ }
return "$item->key=$item->value";
})->sort()->join('
');
@@ -49,19 +55,27 @@ public function saveVariables($isPreview)
{
if ($isPreview) {
$variables = parseEnvFormatToArray($this->variablesPreview);
- $existingVariables = $this->resource->environment_variables_preview();
- $this->resource->environment_variables_preview()->delete();
} else {
$variables = parseEnvFormatToArray($this->variables);
- $existingVariables = $this->resource->environment_variables();
- $this->resource->environment_variables()->delete();
}
foreach ($variables as $key => $variable) {
- $found = $existingVariables->where('key', $key)->first();
+ $found = $this->resource->environment_variables()->where('key', $key)->first();
+ $foundPreview = $this->resource->environment_variables_preview()->where('key', $key)->first();
if ($found) {
+ if ($found->is_shown_once) {
+ continue;
+ }
$found->value = $variable;
$found->save();
continue;
+ }
+ if ($foundPreview) {
+ if ($foundPreview->is_shown_once) {
+ continue;
+ }
+ $foundPreview->value = $variable;
+ $foundPreview->save();
+ continue;
} else {
$environment = new EnvironmentVariable();
$environment->key = $key;
diff --git a/app/Http/Livewire/Project/Shared/EnvironmentVariable/Show.php b/app/Http/Livewire/Project/Shared/EnvironmentVariable/Show.php
index 0ad197f1a..eed0f7052 100644
--- a/app/Http/Livewire/Project/Shared/EnvironmentVariable/Show.php
+++ b/app/Http/Livewire/Project/Shared/EnvironmentVariable/Show.php
@@ -5,7 +5,6 @@
use App\Models\EnvironmentVariable as ModelsEnvironmentVariable;
use Livewire\Component;
use Visus\Cuid2\Cuid2;
-use Illuminate\Support\Str;
class Show extends Component
{
@@ -13,29 +12,45 @@ class Show extends Component
public ModelsEnvironmentVariable $env;
public ?string $modalId = null;
public bool $isDisabled = false;
+ public bool $isLocked = false;
public string $type;
protected $rules = [
'env.key' => 'required|string',
'env.value' => 'nullable',
'env.is_build_time' => 'required|boolean',
+ 'env.is_shown_once' => 'required|boolean',
];
protected $validationAttributes = [
- 'key' => 'key',
- 'value' => 'value',
- 'is_build_time' => 'build',
+ 'key' => 'Key',
+ 'value' => 'Value',
+ 'is_build_time' => 'Build Time',
+ 'is_shown_once' => 'Shown Once',
];
public function mount()
{
- $this->isDisabled = false;
- if (Str::of($this->env->key)->startsWith('SERVICE_FQDN') || Str::of($this->env->key)->startsWith('SERVICE_URL')) {
- $this->isDisabled = true;
- }
$this->modalId = new Cuid2(7);
$this->parameters = get_route_parameters();
+ $this->checkEnvs();
+ }
+ public function checkEnvs()
+ {
+ $this->isDisabled = false;
+ if (str($this->env->key)->startsWith('SERVICE_FQDN') || str($this->env->key)->startsWith('SERVICE_URL')) {
+ $this->isDisabled = true;
+ }
+ if ($this->env->is_shown_once) {
+ $this->isLocked = true;
+ }
+ }
+ public function lock()
+ {
+ $this->env->is_shown_once = true;
+ $this->env->save();
+ $this->checkEnvs();
+ $this->emit('refreshEnvs');
}
-
public function instantSave()
{
$this->submit();
diff --git a/app/Models/EnvironmentVariable.php b/app/Models/EnvironmentVariable.php
index 37619d190..5450f0127 100644
--- a/app/Models/EnvironmentVariable.php
+++ b/app/Models/EnvironmentVariable.php
@@ -11,7 +11,7 @@ class EnvironmentVariable extends Model
{
protected $guarded = [];
protected $casts = [
- "key" => 'string',
+ 'key' => 'string',
'value' => 'encrypted',
'is_build_time' => 'boolean',
];
@@ -21,6 +21,10 @@ protected static function booted()
static::created(function ($environment_variable) {
if ($environment_variable->application_id && !$environment_variable->is_preview) {
$found = ModelsEnvironmentVariable::where('key', $environment_variable->key)->where('application_id', $environment_variable->application_id)->where('is_preview', true)->first();
+ $application = Application::find($environment_variable->application_id);
+ if ($application->build_pack === 'dockerfile') {
+ return;
+ }
if (!$found) {
ModelsEnvironmentVariable::create([
'key' => $environment_variable->key,
@@ -33,7 +37,8 @@ protected static function booted()
}
});
}
- public function service() {
+ public function service()
+ {
return $this->belongsTo(Service::class);
}
protected function value(): Attribute
@@ -55,9 +60,9 @@ private function get_environment_variables(?string $environment_variable = null)
$variable = Str::after($environment_variable, 'global.');
$variable = Str::before($variable, '}}');
$variable = Str::of($variable)->trim()->value;
- // $environment_variable = GlobalEnvironmentVariable::where('name', $environment_variable)->where('team_id', $team_id)->first()?->value;
- ray('global env variable');
- return $environment_variable;
+ // $environment_variable = GlobalEnvironmentVariable::where('name', $environment_variable)->where('team_id', $team_id)->first()?->value;
+ ray('global env variable');
+ return $environment_variable;
}
return $environment_variable;
}
@@ -77,5 +82,4 @@ protected function key(): Attribute
set: fn (string $value) => Str::of($value)->trim(),
);
}
-
}
diff --git a/database/migrations/2023_10_24_124934_add_is_shown_once_to_environment_variables_table.php b/database/migrations/2023_10_24_124934_add_is_shown_once_to_environment_variables_table.php
new file mode 100644
index 000000000..e0df21186
--- /dev/null
+++ b/database/migrations/2023_10_24_124934_add_is_shown_once_to_environment_variables_table.php
@@ -0,0 +1,28 @@
+boolean('is_shown_once')->default(false);
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void
+ {
+ Schema::table('environment_variables', function (Blueprint $table) {
+ $table->dropColumn('is_shown_once');
+ });
+ }
+};
diff --git a/resources/views/livewire/project/shared/environment-variable/all.blade.php b/resources/views/livewire/project/shared/environment-variable/all.blade.php
index 6297f3822..ec1480e05 100644
--- a/resources/views/livewire/project/shared/environment-variable/all.blade.php
+++ b/resources/views/livewire/project/shared/environment-variable/all.blade.php
@@ -28,8 +28,7 @@
@endif
@else
@if ($showPreview)
diff --git a/resources/views/livewire/project/shared/environment-variable/show.blade.php b/resources/views/livewire/project/shared/environment-variable/show.blade.php
index 6663dc12a..f41cf8bef 100644
--- a/resources/views/livewire/project/shared/environment-variable/show.blade.php
+++ b/resources/views/livewire/project/shared/environment-variable/show.blade.php
@@ -6,36 +6,54 @@ class="font-bold text-warning">({{ $env->key }})?