2023-05-04 20:29:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2023-06-05 10:07:55 +00:00
|
|
|
use App\Models\EnvironmentVariable as ModelsEnvironmentVariable;
|
2023-05-04 20:29:14 +00:00
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2024-07-09 11:19:21 +00:00
|
|
|
use OpenApi\Attributes as OA;
|
2023-05-04 20:29:14 +00:00
|
|
|
|
2024-07-09 11:19:21 +00:00
|
|
|
#[OA\Schema(
|
|
|
|
|
description: 'Environment Variable model',
|
|
|
|
|
type: 'object',
|
|
|
|
|
properties: [
|
|
|
|
|
'id' => ['type' => 'integer'],
|
|
|
|
|
'uuid' => ['type' => 'string'],
|
2024-12-17 09:38:32 +00:00
|
|
|
'resourceable_type' => ['type' => 'string'],
|
|
|
|
|
'resourceable_id' => ['type' => 'integer'],
|
2024-07-09 11:19:21 +00:00
|
|
|
'is_literal' => ['type' => 'boolean'],
|
|
|
|
|
'is_multiline' => ['type' => 'boolean'],
|
|
|
|
|
'is_preview' => ['type' => 'boolean'],
|
2025-09-18 16:14:54 +00:00
|
|
|
'is_runtime' => ['type' => 'boolean'],
|
|
|
|
|
'is_buildtime' => ['type' => 'boolean'],
|
2024-07-09 11:19:21 +00:00
|
|
|
'is_shared' => ['type' => 'boolean'],
|
|
|
|
|
'is_shown_once' => ['type' => 'boolean'],
|
|
|
|
|
'key' => ['type' => 'string'],
|
|
|
|
|
'value' => ['type' => 'string'],
|
|
|
|
|
'real_value' => ['type' => 'string'],
|
2025-11-18 09:10:29 +00:00
|
|
|
'comment' => ['type' => 'string', 'nullable' => true],
|
2024-07-09 11:19:21 +00:00
|
|
|
'version' => ['type' => 'string'],
|
|
|
|
|
'created_at' => ['type' => 'string'],
|
|
|
|
|
'updated_at' => ['type' => 'string'],
|
|
|
|
|
]
|
|
|
|
|
)]
|
2025-01-22 19:14:44 +00:00
|
|
|
class EnvironmentVariable extends BaseModel
|
2023-05-04 20:29:14 +00:00
|
|
|
{
|
refactor: add explicit fillable array to EnvironmentVariable model
Replace permissive $guarded = [] with explicit $fillable array for better security and clarity. The fillable array includes all 13 fields that are legitimately mass-assignable:
- Core: key, value, comment
- Polymorphic relationship: resourceable_type, resourceable_id
- Boolean flags: is_preview, is_multiline, is_literal, is_runtime, is_buildtime, is_shown_once, is_shared
- Metadata: version, order
Also adds comprehensive test suite (EnvironmentVariableMassAssignmentTest) with 12 test cases covering:
- Mass assignment of all fillable fields
- Comment field edge cases (null, empty, long text)
- Value encryption verification
- Key mutation (trim and space replacement)
- Protection of auto-managed fields (id, uuid, timestamps)
- Update method compatibility
All tests passing (12 passed, 33 assertions).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 12:47:11 +00:00
|
|
|
protected $fillable = [
|
|
|
|
|
// Core identification
|
|
|
|
|
'key',
|
|
|
|
|
'value',
|
|
|
|
|
'comment',
|
|
|
|
|
|
|
|
|
|
// Polymorphic relationship
|
|
|
|
|
'resourceable_type',
|
|
|
|
|
'resourceable_id',
|
|
|
|
|
|
|
|
|
|
// Boolean flags
|
|
|
|
|
'is_preview',
|
|
|
|
|
'is_multiline',
|
|
|
|
|
'is_literal',
|
|
|
|
|
'is_runtime',
|
|
|
|
|
'is_buildtime',
|
|
|
|
|
'is_shown_once',
|
|
|
|
|
'is_shared',
|
2026-03-02 11:34:30 +00:00
|
|
|
'is_required',
|
refactor: add explicit fillable array to EnvironmentVariable model
Replace permissive $guarded = [] with explicit $fillable array for better security and clarity. The fillable array includes all 13 fields that are legitimately mass-assignable:
- Core: key, value, comment
- Polymorphic relationship: resourceable_type, resourceable_id
- Boolean flags: is_preview, is_multiline, is_literal, is_runtime, is_buildtime, is_shown_once, is_shared
- Metadata: version, order
Also adds comprehensive test suite (EnvironmentVariableMassAssignmentTest) with 12 test cases covering:
- Mass assignment of all fillable fields
- Comment field edge cases (null, empty, long text)
- Value encryption verification
- Key mutation (trim and space replacement)
- Protection of auto-managed fields (id, uuid, timestamps)
- Update method compatibility
All tests passing (12 passed, 33 assertions).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-18 12:47:11 +00:00
|
|
|
|
|
|
|
|
// Metadata
|
|
|
|
|
'version',
|
|
|
|
|
'order',
|
|
|
|
|
];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-05-04 20:29:14 +00:00
|
|
|
protected $casts = [
|
2023-10-24 13:41:21 +00:00
|
|
|
'key' => 'string',
|
2023-05-04 20:29:14 +00:00
|
|
|
'value' => 'encrypted',
|
2024-03-18 10:36:36 +00:00
|
|
|
'is_multiline' => 'boolean',
|
|
|
|
|
'is_preview' => 'boolean',
|
2025-09-18 16:14:54 +00:00
|
|
|
'is_runtime' => 'boolean',
|
|
|
|
|
'is_buildtime' => 'boolean',
|
2024-06-10 20:43:34 +00:00
|
|
|
'version' => 'string',
|
2024-12-17 09:38:32 +00:00
|
|
|
'resourceable_type' => 'string',
|
|
|
|
|
'resourceable_id' => 'integer',
|
2023-05-04 20:29:14 +00:00
|
|
|
];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-09-18 16:14:54 +00:00
|
|
|
protected $appends = ['real_value', 'is_shared', 'is_really_required', 'is_nixpacks', 'is_coolify'];
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-08-07 20:14:21 +00:00
|
|
|
protected static function booted()
|
|
|
|
|
{
|
2025-01-07 14:31:43 +00:00
|
|
|
static::created(function (EnvironmentVariable $environment_variable) {
|
|
|
|
|
if ($environment_variable->resourceable_type === Application::class && ! $environment_variable->is_preview) {
|
|
|
|
|
$found = ModelsEnvironmentVariable::where('key', $environment_variable->key)
|
2024-12-17 09:38:32 +00:00
|
|
|
->where('resourceable_type', Application::class)
|
2025-01-07 14:31:43 +00:00
|
|
|
->where('resourceable_id', $environment_variable->resourceable_id)
|
2024-12-17 09:38:32 +00:00
|
|
|
->where('is_preview', true)
|
|
|
|
|
->first();
|
|
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! $found) {
|
2025-01-07 14:31:43 +00:00
|
|
|
$application = Application::find($environment_variable->resourceable_id);
|
2025-05-30 07:11:54 +00:00
|
|
|
if ($application) {
|
2024-04-02 13:40:19 +00:00
|
|
|
ModelsEnvironmentVariable::create([
|
2025-01-07 14:31:43 +00:00
|
|
|
'key' => $environment_variable->key,
|
|
|
|
|
'value' => $environment_variable->value,
|
|
|
|
|
'is_multiline' => $environment_variable->is_multiline ?? false,
|
2025-09-11 13:25:44 +00:00
|
|
|
'is_literal' => $environment_variable->is_literal ?? false,
|
2025-12-03 09:25:38 +00:00
|
|
|
'is_runtime' => $environment_variable->is_runtime ?? false,
|
|
|
|
|
'is_buildtime' => $environment_variable->is_buildtime ?? false,
|
2025-11-18 09:10:29 +00:00
|
|
|
'comment' => $environment_variable->comment,
|
2024-12-17 09:38:32 +00:00
|
|
|
'resourceable_type' => Application::class,
|
2025-01-07 14:31:43 +00:00
|
|
|
'resourceable_id' => $environment_variable->resourceable_id,
|
2024-06-10 20:43:34 +00:00
|
|
|
'is_preview' => true,
|
2024-04-02 13:40:19 +00:00
|
|
|
]);
|
|
|
|
|
}
|
2023-09-08 14:16:59 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|
2025-01-07 14:31:43 +00:00
|
|
|
$environment_variable->update([
|
2024-11-25 10:28:08 +00:00
|
|
|
'version' => config('constants.coolify.version'),
|
2024-03-18 10:36:36 +00:00
|
|
|
]);
|
2023-08-07 20:14:21 +00:00
|
|
|
});
|
2024-12-17 09:38:32 +00:00
|
|
|
|
2024-10-16 11:20:26 +00:00
|
|
|
static::saving(function (EnvironmentVariable $environmentVariable) {
|
|
|
|
|
$environmentVariable->updateIsShared();
|
|
|
|
|
});
|
2023-08-07 20:14:21 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-10-24 13:41:21 +00:00
|
|
|
public function service()
|
|
|
|
|
{
|
2023-09-20 13:42:41 +00:00
|
|
|
return $this->belongsTo(Service::class);
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
protected function value(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
2023-09-20 13:42:41 +00:00
|
|
|
get: fn (?string $value = null) => $this->get_environment_variables($value),
|
|
|
|
|
set: fn (?string $value = null) => $this->set_environment_variables($value),
|
2023-08-08 09:51:36 +00:00
|
|
|
);
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-12-17 09:38:32 +00:00
|
|
|
/**
|
|
|
|
|
* Get the parent resourceable model.
|
|
|
|
|
*/
|
|
|
|
|
public function resourceable()
|
2024-01-23 16:13:23 +00:00
|
|
|
{
|
2024-12-17 09:38:32 +00:00
|
|
|
return $this->morphTo();
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-12-17 09:38:32 +00:00
|
|
|
public function resource()
|
|
|
|
|
{
|
|
|
|
|
return $this->resourceable;
|
2024-03-15 21:02:37 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-15 21:02:37 +00:00
|
|
|
public function realValue(): Attribute
|
|
|
|
|
{
|
2024-01-23 16:13:23 +00:00
|
|
|
return Attribute::make(
|
2024-12-17 09:38:32 +00:00
|
|
|
get: function () {
|
|
|
|
|
if (! $this->relationLoaded('resourceable')) {
|
|
|
|
|
$this->load('resourceable');
|
|
|
|
|
}
|
|
|
|
|
$resource = $this->resourceable;
|
|
|
|
|
if (! $resource) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-07-07 10:30:44 +00:00
|
|
|
$real_value = $this->get_real_environment_variables($this->value, $resource);
|
2026-01-28 09:59:00 +00:00
|
|
|
|
|
|
|
|
// Skip escaping for valid JSON objects/arrays to prevent quote corruption (see #6160)
|
|
|
|
|
if (json_validate($real_value) && (str_starts_with($real_value, '{') || str_starts_with($real_value, '['))) {
|
|
|
|
|
return $real_value;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-07 10:30:44 +00:00
|
|
|
if ($this->is_literal || $this->is_multiline) {
|
|
|
|
|
$real_value = '\''.$real_value.'\'';
|
|
|
|
|
} else {
|
|
|
|
|
$real_value = escapeEnvVariables($real_value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $real_value;
|
2024-01-24 14:54:55 +00:00
|
|
|
}
|
2024-01-23 16:13:23 +00:00
|
|
|
);
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-10-11 12:38:22 +00:00
|
|
|
protected function isReallyRequired(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
|
|
|
|
get: fn () => $this->is_required && str($this->real_value)->isEmpty(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-18 16:14:54 +00:00
|
|
|
protected function isNixpacks(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
|
|
|
|
get: function () {
|
|
|
|
|
if (str($this->key)->startsWith('NIXPACKS_')) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function isCoolify(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
|
|
|
|
get: function () {
|
|
|
|
|
if (str($this->key)->startsWith('SERVICE_')) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-23 16:13:23 +00:00
|
|
|
protected function isShared(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
|
|
|
|
get: function () {
|
2024-06-10 20:43:34 +00:00
|
|
|
$type = str($this->value)->after('{{')->before('.')->value;
|
2025-01-07 14:31:43 +00:00
|
|
|
if (str($this->value)->startsWith('{{'.$type) && str($this->value)->endsWith('}}')) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-01-07 14:31:43 +00:00
|
|
|
return false;
|
2024-01-23 16:13:23 +00:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-03-15 21:02:37 +00:00
|
|
|
private function get_real_environment_variables(?string $environment_variable = null, $resource = null)
|
2023-05-04 20:29:14 +00:00
|
|
|
{
|
2025-01-07 14:31:43 +00:00
|
|
|
if ((is_null($environment_variable) && $environment_variable === '') || is_null($resource)) {
|
2023-09-21 19:30:13 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2024-01-23 16:13:23 +00:00
|
|
|
$environment_variable = trim($environment_variable);
|
2024-07-26 11:22:44 +00:00
|
|
|
$sharedEnvsFound = str($environment_variable)->matchAll('/{{(.*?)}}/');
|
|
|
|
|
if ($sharedEnvsFound->isEmpty()) {
|
|
|
|
|
return $environment_variable;
|
|
|
|
|
}
|
2025-01-07 14:31:43 +00:00
|
|
|
foreach ($sharedEnvsFound as $sharedEnv) {
|
2025-11-27 09:45:39 +00:00
|
|
|
$type = str($sharedEnv)->trim()->match('/(.*?)\./');
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! collect(SHARED_VARIABLE_TYPES)->contains($type)) {
|
2024-07-26 11:22:44 +00:00
|
|
|
continue;
|
2024-01-31 12:40:15 +00:00
|
|
|
}
|
2025-11-27 09:45:39 +00:00
|
|
|
$variable = str($sharedEnv)->trim()->match('/\.(.*)/');
|
2024-07-26 11:22:44 +00:00
|
|
|
if ($type->value() === 'environment') {
|
2024-01-24 14:54:55 +00:00
|
|
|
$id = $resource->environment->id;
|
2024-07-26 11:22:44 +00:00
|
|
|
} elseif ($type->value() === 'project') {
|
2024-01-24 14:54:55 +00:00
|
|
|
$id = $resource->environment->project->id;
|
2024-07-26 11:22:44 +00:00
|
|
|
} elseif ($type->value() === 'team') {
|
2024-01-24 14:56:43 +00:00
|
|
|
$id = $resource->team()->id;
|
2024-01-24 14:54:55 +00:00
|
|
|
}
|
2024-07-26 11:22:44 +00:00
|
|
|
if (is_null($id)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-01-07 14:31:43 +00: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 16:13:23 +00:00
|
|
|
if ($environment_variable_found) {
|
2025-01-07 14:31:43 +00:00
|
|
|
$environment_variable = str($environment_variable)->replace("{{{$sharedEnv}}}", $environment_variable_found->value);
|
2024-01-23 16:13:23 +00:00
|
|
|
}
|
2023-05-04 20:29:14 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-07-26 11:22:44 +00:00
|
|
|
return str($environment_variable)->value();
|
2023-05-04 20:29:14 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
|
private function get_environment_variables(?string $environment_variable = null): ?string
|
2024-01-23 16:13:23 +00:00
|
|
|
{
|
2024-06-10 20:43:34 +00:00
|
|
|
if (! $environment_variable) {
|
2024-01-23 16:13:23 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-01-23 16:13:23 +00:00
|
|
|
return trim(decrypt($environment_variable));
|
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
private function set_environment_variables(?string $environment_variable = null): ?string
|
2023-05-04 20:29:14 +00:00
|
|
|
{
|
2024-10-31 14:23:19 +00:00
|
|
|
if (is_null($environment_variable) && $environment_variable === '') {
|
2023-09-20 13:42:41 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2023-05-05 08:51:58 +00:00
|
|
|
$environment_variable = trim($environment_variable);
|
2024-06-10 20:43:34 +00:00
|
|
|
$type = str($environment_variable)->after('{{')->before('.')->value;
|
|
|
|
|
if (str($environment_variable)->startsWith('{{'.$type) && str($environment_variable)->endsWith('}}')) {
|
2025-11-27 09:45:39 +00:00
|
|
|
return encrypt($environment_variable);
|
2024-01-23 16:13:23 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-09-11 20:29:47 +00:00
|
|
|
return encrypt($environment_variable);
|
2023-05-04 20:29:14 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-05-05 08:51:58 +00:00
|
|
|
protected function key(): Attribute
|
|
|
|
|
{
|
|
|
|
|
return Attribute::make(
|
2024-06-26 11:00:36 +00:00
|
|
|
set: fn (string $value) => str($value)->trim()->replace(' ', '_')->value,
|
2023-05-05 08:51:58 +00:00
|
|
|
);
|
|
|
|
|
}
|
2024-10-16 11:20:26 +00:00
|
|
|
|
|
|
|
|
protected function updateIsShared(): void
|
|
|
|
|
{
|
|
|
|
|
$type = str($this->value)->after('{{')->before('.')->value;
|
|
|
|
|
$isShared = str($this->value)->startsWith('{{'.$type) && str($this->value)->endsWith('}}');
|
|
|
|
|
$this->is_shared = $isShared;
|
|
|
|
|
}
|
2023-05-04 20:29:14 +00:00
|
|
|
}
|