Add strict validation for Docker network names using a regex pattern that matches Docker's naming rules (alphanumeric start, followed by alphanumeric, dots, hyphens, underscores). Changes: - Add DOCKER_NETWORK_PATTERN to ValidationPatterns with helper methods - Validate network field in Destination creation and update Livewire components - Add setNetworkAttribute mutator on StandaloneDocker and SwarmDocker models - Apply escapeshellarg() to all network field usages in shell commands across ApplicationDeploymentJob, DatabaseBackupJob, StartService, Init command, proxy helpers, and Destination/Show - Add comprehensive tests for pattern validation and model mutator Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
115 lines
3 KiB
PHP
115 lines
3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Support\ValidationPatterns;
|
|
|
|
class SwarmDocker extends BaseModel
|
|
{
|
|
protected $guarded = [];
|
|
|
|
public function setNetworkAttribute(string $value): void
|
|
{
|
|
if (! ValidationPatterns::isValidDockerNetwork($value)) {
|
|
throw new \InvalidArgumentException('Invalid Docker network name. Must start with alphanumeric and contain only alphanumeric characters, dots, hyphens, and underscores.');
|
|
}
|
|
|
|
$this->attributes['network'] = $value;
|
|
}
|
|
|
|
public function applications()
|
|
{
|
|
return $this->morphMany(Application::class, 'destination');
|
|
}
|
|
|
|
public function postgresqls()
|
|
{
|
|
return $this->morphMany(StandalonePostgresql::class, 'destination');
|
|
}
|
|
|
|
public function redis()
|
|
{
|
|
return $this->morphMany(StandaloneRedis::class, 'destination');
|
|
}
|
|
|
|
public function keydbs()
|
|
{
|
|
return $this->morphMany(StandaloneKeydb::class, 'destination');
|
|
}
|
|
|
|
public function dragonflies()
|
|
{
|
|
return $this->morphMany(StandaloneDragonfly::class, 'destination');
|
|
}
|
|
|
|
public function clickhouses()
|
|
{
|
|
return $this->morphMany(StandaloneClickhouse::class, 'destination');
|
|
}
|
|
|
|
public function mongodbs()
|
|
{
|
|
return $this->morphMany(StandaloneMongodb::class, 'destination');
|
|
}
|
|
|
|
public function mysqls()
|
|
{
|
|
return $this->morphMany(StandaloneMysql::class, 'destination');
|
|
}
|
|
|
|
public function mariadbs()
|
|
{
|
|
return $this->morphMany(StandaloneMariadb::class, 'destination');
|
|
}
|
|
|
|
public function server()
|
|
{
|
|
return $this->belongsTo(Server::class);
|
|
}
|
|
|
|
/**
|
|
* Get the server attribute using identity map caching.
|
|
* This intercepts lazy-loading to use cached Server lookups.
|
|
*/
|
|
public function getServerAttribute(): ?Server
|
|
{
|
|
// Use eager loaded data if available
|
|
if ($this->relationLoaded('server')) {
|
|
return $this->getRelation('server');
|
|
}
|
|
|
|
// Use identity map for lazy loading
|
|
$server = Server::findCached($this->server_id);
|
|
|
|
// Cache in relation for future access on this instance
|
|
if ($server) {
|
|
$this->setRelation('server', $server);
|
|
}
|
|
|
|
return $server;
|
|
}
|
|
|
|
public function services()
|
|
{
|
|
return $this->morphMany(Service::class, 'destination');
|
|
}
|
|
|
|
public function databases()
|
|
{
|
|
$postgresqls = $this->postgresqls;
|
|
$redis = $this->redis;
|
|
$mongodbs = $this->mongodbs;
|
|
$mysqls = $this->mysqls;
|
|
$mariadbs = $this->mariadbs;
|
|
$keydbs = $this->keydbs;
|
|
$dragonflies = $this->dragonflies;
|
|
$clickhouses = $this->clickhouses;
|
|
|
|
return $postgresqls->concat($redis)->concat($mongodbs)->concat($mysqls)->concat($mariadbs)->concat($keydbs)->concat($dragonflies)->concat($clickhouses);
|
|
}
|
|
|
|
public function attachedTo()
|
|
{
|
|
return $this->applications?->count() > 0 || $this->databases()->count() > 0;
|
|
}
|
|
}
|