2023-05-04 22:29:14 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2023-06-05 12:07:55 +02:00
|
|
|
use App\Models\EnvironmentVariable as ModelsEnvironmentVariable;
|
2023-05-04 22:29:14 +02:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2024-07-09 13:19:21 +02:00
|
|
|
use OpenApi\Attributes as OA;
|
2024-06-26 13:00:36 +02:00
|
|
|
use Visus\Cuid2\Cuid2;
|
2023-05-04 22:29:14 +02:00
|
|
|
|
2024-07-09 13:19:21 +02:00
|
|
|
#[OA\Schema(
|
|
|
|
description: 'Environment Variable model',
|
|
|
|
type: 'object',
|
|
|
|
properties: [
|
|
|
|
'id' => ['type' => 'integer'],
|
|
|
|
'uuid' => ['type' => 'string'],
|
|
|
|
'application_id' => ['type' => 'integer'],
|
|
|
|
'service_id' => ['type' => 'integer'],
|
|
|
|
'database_id' => ['type' => 'integer'],
|
|
|
|
'is_build_time' => ['type' => 'boolean'],
|
|
|
|
'is_literal' => ['type' => 'boolean'],
|
|
|
|
'is_multiline' => ['type' => 'boolean'],
|
|
|
|
'is_preview' => ['type' => 'boolean'],
|
|
|
|
'is_shared' => ['type' => 'boolean'],
|
|
|
|
'is_shown_once' => ['type' => 'boolean'],
|
|
|
|
'key' => ['type' => 'string'],
|
|
|
|
'value' => ['type' => 'string'],
|
|
|
|
'real_value' => ['type' => 'string'],
|
|
|
|
'version' => ['type' => 'string'],
|
|
|
|
'created_at' => ['type' => 'string'],
|
|
|
|
'updated_at' => ['type' => 'string'],
|
|
|
|
]
|
|
|
|
)]
|
2023-05-04 22:29:14 +02:00
|
|
|
class EnvironmentVariable extends Model
|
|
|
|
{
|
2023-08-07 22:14:21 +02:00
|
|
|
protected $guarded = [];
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2023-05-04 22:29:14 +02:00
|
|
|
protected $casts = [
|
2023-10-24 15:41:21 +02:00
|
|
|
'key' => 'string',
|
2023-05-04 22:29:14 +02:00
|
|
|
'value' => 'encrypted',
|
|
|
|
'is_build_time' => 'boolean',
|
2024-03-18 11:36:36 +01:00
|
|
|
'is_multiline' => 'boolean',
|
|
|
|
'is_preview' => 'boolean',
|
2024-06-10 22:43:34 +02:00
|
|
|
'version' => 'string',
|
2023-05-04 22:29:14 +02:00
|
|
|
];
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2024-01-23 17:13:23 +01:00
|
|
|
protected $appends = ['real_value', 'is_shared'];
|
2023-08-08 11:51:36 +02:00
|
|
|
|
2023-08-07 22:14:21 +02:00
|
|
|
protected static function booted()
|
|
|
|
{
|
2024-06-26 13:00:36 +02:00
|
|
|
static::creating(function (Model $model) {
|
|
|
|
if (! $model->uuid) {
|
2024-07-24 21:11:12 +02:00
|
|
|
$model->uuid = (string) new Cuid2;
|
2024-06-26 13:00:36 +02:00
|
|
|
}
|
|
|
|
});
|
2024-03-18 11:36:36 +01:00
|
|
|
static::created(function (EnvironmentVariable $environment_variable) {
|
2024-06-10 22:43:34 +02:00
|
|
|
if ($environment_variable->application_id && ! $environment_variable->is_preview) {
|
2023-09-11 22:29:47 +02:00
|
|
|
$found = ModelsEnvironmentVariable::where('key', $environment_variable->key)->where('application_id', $environment_variable->application_id)->where('is_preview', true)->first();
|
2024-06-10 22:43:34 +02:00
|
|
|
if (! $found) {
|
2024-04-02 15:40:19 +02:00
|
|
|
$application = Application::find($environment_variable->application_id);
|
|
|
|
if ($application->build_pack !== 'dockerfile') {
|
|
|
|
ModelsEnvironmentVariable::create([
|
|
|
|
'key' => $environment_variable->key,
|
|
|
|
'value' => $environment_variable->value,
|
|
|
|
'is_build_time' => $environment_variable->is_build_time,
|
2024-04-12 09:34:56 +02:00
|
|
|
'is_multiline' => $environment_variable->is_multiline ?? false,
|
2024-04-02 15:40:19 +02:00
|
|
|
'application_id' => $environment_variable->application_id,
|
2024-06-10 22:43:34 +02:00
|
|
|
'is_preview' => true,
|
2024-04-02 15:40:19 +02:00
|
|
|
]);
|
|
|
|
}
|
2023-09-08 16:16:59 +02:00
|
|
|
}
|
2023-08-08 11:51:36 +02:00
|
|
|
}
|
2024-03-18 11:36:36 +01:00
|
|
|
$environment_variable->update([
|
2024-06-10 22:43:34 +02:00
|
|
|
'version' => config('version'),
|
2024-03-18 11:36:36 +01:00
|
|
|
]);
|
2023-08-07 22:14:21 +02:00
|
|
|
});
|
|
|
|
}
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2023-10-24 15:41:21 +02:00
|
|
|
public function service()
|
|
|
|
{
|
2023-09-20 15:42:41 +02:00
|
|
|
return $this->belongsTo(Service::class);
|
|
|
|
}
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2023-08-08 11:51:36 +02:00
|
|
|
protected function value(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
2023-09-20 15:42:41 +02:00
|
|
|
get: fn (?string $value = null) => $this->get_environment_variables($value),
|
|
|
|
set: fn (?string $value = null) => $this->set_environment_variables($value),
|
2023-08-08 11:51:36 +02:00
|
|
|
);
|
|
|
|
}
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2024-03-15 22:02:37 +01:00
|
|
|
public function resource()
|
2024-01-23 17:13:23 +01:00
|
|
|
{
|
2024-01-24 15:54:55 +01:00
|
|
|
$resource = null;
|
|
|
|
if ($this->application_id) {
|
|
|
|
$resource = Application::find($this->application_id);
|
2024-06-10 22:43:34 +02:00
|
|
|
} elseif ($this->service_id) {
|
2024-01-24 15:54:55 +01:00
|
|
|
$resource = Service::find($this->service_id);
|
2024-08-13 14:25:24 +02:00
|
|
|
} elseif ($this->standalone_postgresql_id) {
|
|
|
|
$resource = StandalonePostgresql::find($this->standalone_postgresql_id);
|
|
|
|
} elseif ($this->standalone_redis_id) {
|
|
|
|
$resource = StandaloneRedis::find($this->standalone_redis_id);
|
|
|
|
} elseif ($this->standalone_mongodb_id) {
|
|
|
|
$resource = StandaloneMongodb::find($this->standalone_mongodb_id);
|
|
|
|
} elseif ($this->standalone_mysql_id) {
|
|
|
|
$resource = StandaloneMysql::find($this->standalone_mysql_id);
|
|
|
|
} elseif ($this->standalone_mariadb_id) {
|
|
|
|
$resource = StandaloneMariadb::find($this->standalone_mariadb_id);
|
|
|
|
} elseif ($this->standalone_keydb_id) {
|
|
|
|
$resource = StandaloneKeydb::find($this->standalone_keydb_id);
|
|
|
|
} elseif ($this->standalone_dragonfly_id) {
|
|
|
|
$resource = StandaloneDragonfly::find($this->standalone_dragonfly_id);
|
|
|
|
} elseif ($this->standalone_clickhouse_id) {
|
|
|
|
$resource = StandaloneClickhouse::find($this->standalone_clickhouse_id);
|
2024-01-24 15:54:55 +01:00
|
|
|
}
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2024-03-15 22:02:37 +01:00
|
|
|
return $resource;
|
|
|
|
}
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2024-03-15 22:02:37 +01:00
|
|
|
public function realValue(): Attribute
|
|
|
|
{
|
|
|
|
$resource = $this->resource();
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2024-01-23 17:13:23 +01:00
|
|
|
return Attribute::make(
|
2024-01-24 15:54:55 +01:00
|
|
|
get: function () use ($resource) {
|
2024-03-15 22:02:37 +01:00
|
|
|
$env = $this->get_real_environment_variables($this->value, $resource);
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2024-03-15 22:02:37 +01:00
|
|
|
return data_get($env, 'value', $env);
|
|
|
|
if (is_string($env)) {
|
|
|
|
return $env;
|
|
|
|
}
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2024-03-15 22:02:37 +01:00
|
|
|
return $env->value;
|
2024-01-24 15:54:55 +01:00
|
|
|
}
|
2024-01-23 17:13:23 +01:00
|
|
|
);
|
|
|
|
}
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2024-01-23 17:13:23 +01:00
|
|
|
protected function isShared(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
|
|
|
get: function () {
|
2024-06-10 22:43:34 +02:00
|
|
|
$type = str($this->value)->after('{{')->before('.')->value;
|
|
|
|
if (str($this->value)->startsWith('{{'.$type) && str($this->value)->endsWith('}}')) {
|
2024-01-23 17:13:23 +01:00
|
|
|
return true;
|
|
|
|
}
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2024-01-23 17:13:23 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2024-03-15 22:02:37 +01:00
|
|
|
private function get_real_environment_variables(?string $environment_variable = null, $resource = null)
|
2023-05-04 22:29:14 +02:00
|
|
|
{
|
2024-03-18 11:49:26 +01:00
|
|
|
if ((is_null($environment_variable) && $environment_variable == '') || is_null($resource)) {
|
2023-09-21 21:30:13 +02:00
|
|
|
return null;
|
|
|
|
}
|
2024-01-23 17:13:23 +01:00
|
|
|
$environment_variable = trim($environment_variable);
|
2024-07-26 13:22:44 +02:00
|
|
|
$sharedEnvsFound = str($environment_variable)->matchAll('/{{(.*?)}}/');
|
|
|
|
if ($sharedEnvsFound->isEmpty()) {
|
2024-08-13 14:25:24 +02:00
|
|
|
|
2024-07-26 13:22:44 +02:00
|
|
|
return $environment_variable;
|
|
|
|
}
|
2024-08-13 14:25:24 +02:00
|
|
|
|
2024-07-26 13:22:44 +02:00
|
|
|
foreach ($sharedEnvsFound as $sharedEnv) {
|
|
|
|
$type = str($sharedEnv)->match('/(.*?)\./');
|
2024-06-10 22:43:34 +02:00
|
|
|
if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) {
|
2024-07-26 13:22:44 +02:00
|
|
|
continue;
|
2024-01-31 13:40:15 +01:00
|
|
|
}
|
2024-07-26 13:22:44 +02:00
|
|
|
$variable = str($sharedEnv)->match('/\.(.*)/');
|
|
|
|
if ($type->value() === 'environment') {
|
2024-01-24 15:54:55 +01:00
|
|
|
$id = $resource->environment->id;
|
2024-07-26 13:22:44 +02:00
|
|
|
} elseif ($type->value() === 'project') {
|
2024-01-24 15:54:55 +01:00
|
|
|
$id = $resource->environment->project->id;
|
2024-07-26 13:22:44 +02:00
|
|
|
} elseif ($type->value() === 'team') {
|
2024-01-24 15:56:43 +01:00
|
|
|
$id = $resource->team()->id;
|
2024-01-24 15:54:55 +01:00
|
|
|
}
|
2024-07-26 13:22:44 +02:00
|
|
|
if (is_null($id)) {
|
|
|
|
continue;
|
|
|
|
}
|
2024-06-10 22:43:34 +02:00
|
|
|
$environment_variable_found = SharedEnvironmentVariable::where('type', $type)->where('key', $variable)->where('team_id', $resource->team()->id)->where("{$type}_id", $id)->first();
|
2024-01-23 17:13:23 +01:00
|
|
|
if ($environment_variable_found) {
|
2024-07-26 13:22:44 +02:00
|
|
|
$environment_variable = str($environment_variable)->replace("{{{$sharedEnv}}}", $environment_variable_found->value);
|
2024-01-23 17:13:23 +01:00
|
|
|
}
|
2023-05-04 22:29:14 +02:00
|
|
|
}
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2024-07-26 13:22:44 +02:00
|
|
|
return str($environment_variable)->value();
|
2023-05-04 22:29:14 +02:00
|
|
|
}
|
2024-06-10 22:43:34 +02:00
|
|
|
|
|
|
|
private function get_environment_variables(?string $environment_variable = null): ?string
|
2024-01-23 17:13:23 +01:00
|
|
|
{
|
2024-06-10 22:43:34 +02:00
|
|
|
if (! $environment_variable) {
|
2024-01-23 17:13:23 +01:00
|
|
|
return null;
|
|
|
|
}
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2024-01-23 17:13:23 +01:00
|
|
|
return trim(decrypt($environment_variable));
|
|
|
|
}
|
2023-08-08 11:51:36 +02:00
|
|
|
|
2024-06-10 22:43:34 +02:00
|
|
|
private function set_environment_variables(?string $environment_variable = null): ?string
|
2023-05-04 22:29:14 +02:00
|
|
|
{
|
2023-09-20 15:42:41 +02:00
|
|
|
if (is_null($environment_variable) && $environment_variable == '') {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-05 10:51:58 +02:00
|
|
|
$environment_variable = trim($environment_variable);
|
2024-06-10 22:43:34 +02:00
|
|
|
$type = str($environment_variable)->after('{{')->before('.')->value;
|
|
|
|
if (str($environment_variable)->startsWith('{{'.$type) && str($environment_variable)->endsWith('}}')) {
|
2024-01-23 17:13:23 +01:00
|
|
|
return encrypt((string) str($environment_variable)->replace(' ', ''));
|
|
|
|
}
|
2024-06-10 22:43:34 +02:00
|
|
|
|
2023-09-11 22:29:47 +02:00
|
|
|
return encrypt($environment_variable);
|
2023-05-04 22:29:14 +02:00
|
|
|
}
|
2023-08-08 11:51:36 +02:00
|
|
|
|
2023-05-05 10:51:58 +02:00
|
|
|
protected function key(): Attribute
|
|
|
|
{
|
|
|
|
return Attribute::make(
|
2024-06-26 13:00:36 +02:00
|
|
|
set: fn (string $value) => str($value)->trim()->replace(' ', '_')->value,
|
2023-05-05 10:51:58 +02:00
|
|
|
);
|
|
|
|
}
|
2023-05-04 22:29:14 +02:00
|
|
|
}
|