refactor: replace direct SslCertificate queries with server relationship methods for consistency

This commit is contained in:
Andras Bacsai 2025-10-09 17:00:05 +02:00
parent bf5c08d071
commit f4e5c195fe
19 changed files with 27 additions and 36 deletions

View file

@ -55,11 +55,11 @@ public function handle(StandaloneDragonfly $database)
$this->commands[] = "mkdir -p $this->configuration_dir/ssl"; $this->commands[] = "mkdir -p $this->configuration_dir/ssl";
$server = $this->database->destination->server; $server = $this->database->destination->server;
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
if (! $caCert) { if (! $caCert) {
$server->generateCaCertificate(); $server->generateCaCertificate();
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
} }
if (! $caCert) { if (! $caCert) {

View file

@ -56,11 +56,11 @@ public function handle(StandaloneKeydb $database)
$this->commands[] = "mkdir -p $this->configuration_dir/ssl"; $this->commands[] = "mkdir -p $this->configuration_dir/ssl";
$server = $this->database->destination->server; $server = $this->database->destination->server;
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
if (! $caCert) { if (! $caCert) {
$server->generateCaCertificate(); $server->generateCaCertificate();
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
} }
if (! $caCert) { if (! $caCert) {

View file

@ -57,11 +57,11 @@ public function handle(StandaloneMariadb $database)
$this->commands[] = "mkdir -p $this->configuration_dir/ssl"; $this->commands[] = "mkdir -p $this->configuration_dir/ssl";
$server = $this->database->destination->server; $server = $this->database->destination->server;
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
if (! $caCert) { if (! $caCert) {
$server->generateCaCertificate(); $server->generateCaCertificate();
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
} }
if (! $caCert) { if (! $caCert) {

View file

@ -61,11 +61,11 @@ public function handle(StandaloneMongodb $database)
$this->commands[] = "mkdir -p $this->configuration_dir/ssl"; $this->commands[] = "mkdir -p $this->configuration_dir/ssl";
$server = $this->database->destination->server; $server = $this->database->destination->server;
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
if (! $caCert) { if (! $caCert) {
$server->generateCaCertificate(); $server->generateCaCertificate();
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
} }
if (! $caCert) { if (! $caCert) {

View file

@ -57,11 +57,11 @@ public function handle(StandaloneMysql $database)
$this->commands[] = "mkdir -p $this->configuration_dir/ssl"; $this->commands[] = "mkdir -p $this->configuration_dir/ssl";
$server = $this->database->destination->server; $server = $this->database->destination->server;
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
if (! $caCert) { if (! $caCert) {
$server->generateCaCertificate(); $server->generateCaCertificate();
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
} }
if (! $caCert) { if (! $caCert) {

View file

@ -62,11 +62,11 @@ public function handle(StandalonePostgresql $database)
$this->commands[] = "mkdir -p $this->configuration_dir/ssl"; $this->commands[] = "mkdir -p $this->configuration_dir/ssl";
$server = $this->database->destination->server; $server = $this->database->destination->server;
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
if (! $caCert) { if (! $caCert) {
$server->generateCaCertificate(); $server->generateCaCertificate();
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
} }
if (! $caCert) { if (! $caCert) {

View file

@ -56,11 +56,11 @@ public function handle(StandaloneRedis $database)
$this->commands[] = "mkdir -p $this->configuration_dir/ssl"; $this->commands[] = "mkdir -p $this->configuration_dir/ssl";
$server = $this->database->destination->server; $server = $this->database->destination->server;
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
if (! $caCert) { if (! $caCert) {
$server->generateCaCertificate(); $server->generateCaCertificate();
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
} }
if (! $caCert) { if (! $caCert) {

View file

@ -4,7 +4,6 @@
use App\Helpers\SslHelper; use App\Helpers\SslHelper;
use App\Models\Server; use App\Models\Server;
use App\Models\SslCertificate;
use App\Models\StandaloneDocker; use App\Models\StandaloneDocker;
use Lorisleiva\Actions\Concerns\AsAction; use Lorisleiva\Actions\Concerns\AsAction;
@ -20,7 +19,7 @@ public function handle(Server $server)
throw new \Exception('Server OS type is not supported for automated installation. Please install Docker manually before continuing: <a target="_blank" class="underline" href="https://coolify.io/docs/installation#manually">documentation</a>.'); throw new \Exception('Server OS type is not supported for automated installation. Please install Docker manually before continuing: <a target="_blank" class="underline" href="https://coolify.io/docs/installation#manually">documentation</a>.');
} }
if (! SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->exists()) { if (! $server->sslCertificates()->where('is_ca_certificate', true)->exists()) {
$serverCert = SslHelper::generateSslCertificate( $serverCert = SslHelper::generateSslCertificate(
commonName: 'Coolify CA Certificate', commonName: 'Coolify CA Certificate',
serverId: $server->id, serverId: $server->id,

View file

@ -45,7 +45,7 @@ public function handle()
$query->cursor()->each(function ($certificate) use ($regenerated) { $query->cursor()->each(function ($certificate) use ($regenerated) {
try { try {
$caCert = SslCertificate::where('server_id', $certificate->server_id) $caCert = $certificate->server->sslCertificates()
->where('is_ca_certificate', true) ->where('is_ca_certificate', true)
->first(); ->first();

View file

@ -6,7 +6,6 @@
use App\Actions\Database\StopDatabaseProxy; use App\Actions\Database\StopDatabaseProxy;
use App\Helpers\SslHelper; use App\Helpers\SslHelper;
use App\Models\Server; use App\Models\Server;
use App\Models\SslCertificate;
use App\Models\StandaloneDragonfly; use App\Models\StandaloneDragonfly;
use App\Support\ValidationPatterns; use App\Support\ValidationPatterns;
use Carbon\Carbon; use Carbon\Carbon;
@ -249,13 +248,13 @@ public function regenerateSslCertificate()
$server = $this->database->destination->server; $server = $this->database->destination->server;
$caCert = SslCertificate::where('server_id', $server->id) $caCert = $server->sslCertificates()
->where('is_ca_certificate', true) ->where('is_ca_certificate', true)
->first(); ->first();
if (! $caCert) { if (! $caCert) {
$server->generateCaCertificate(); $server->generateCaCertificate();
$caCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $caCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
} }
if (! $caCert) { if (! $caCert) {

View file

@ -6,7 +6,6 @@
use App\Actions\Database\StopDatabaseProxy; use App\Actions\Database\StopDatabaseProxy;
use App\Helpers\SslHelper; use App\Helpers\SslHelper;
use App\Models\Server; use App\Models\Server;
use App\Models\SslCertificate;
use App\Models\StandaloneKeydb; use App\Models\StandaloneKeydb;
use App\Support\ValidationPatterns; use App\Support\ValidationPatterns;
use Carbon\Carbon; use Carbon\Carbon;
@ -255,7 +254,7 @@ public function regenerateSslCertificate()
return; return;
} }
$caCert = SslCertificate::where('server_id', $existingCert->server_id) $caCert = $this->server->sslCertificates()
->where('is_ca_certificate', true) ->where('is_ca_certificate', true)
->first(); ->first();

View file

@ -6,7 +6,6 @@
use App\Actions\Database\StopDatabaseProxy; use App\Actions\Database\StopDatabaseProxy;
use App\Helpers\SslHelper; use App\Helpers\SslHelper;
use App\Models\Server; use App\Models\Server;
use App\Models\SslCertificate;
use App\Models\StandaloneMariadb; use App\Models\StandaloneMariadb;
use App\Support\ValidationPatterns; use App\Support\ValidationPatterns;
use Carbon\Carbon; use Carbon\Carbon;
@ -207,7 +206,7 @@ public function regenerateSslCertificate()
return; return;
} }
$caCert = SslCertificate::where('server_id', $existingCert->server_id)->where('is_ca_certificate', true)->first(); $caCert = $this->server->sslCertificates()->where('is_ca_certificate', true)->first();
SslHelper::generateSslCertificate( SslHelper::generateSslCertificate(
commonName: $existingCert->common_name, commonName: $existingCert->common_name,

View file

@ -6,7 +6,6 @@
use App\Actions\Database\StopDatabaseProxy; use App\Actions\Database\StopDatabaseProxy;
use App\Helpers\SslHelper; use App\Helpers\SslHelper;
use App\Models\Server; use App\Models\Server;
use App\Models\SslCertificate;
use App\Models\StandaloneMongodb; use App\Models\StandaloneMongodb;
use App\Support\ValidationPatterns; use App\Support\ValidationPatterns;
use Carbon\Carbon; use Carbon\Carbon;
@ -215,7 +214,7 @@ public function regenerateSslCertificate()
return; return;
} }
$caCert = SslCertificate::where('server_id', $existingCert->server_id)->where('is_ca_certificate', true)->first(); $caCert = $this->server->sslCertificates()->where('is_ca_certificate', true)->first();
SslHelper::generateSslCertificate( SslHelper::generateSslCertificate(
commonName: $existingCert->common_name, commonName: $existingCert->common_name,

View file

@ -6,7 +6,6 @@
use App\Actions\Database\StopDatabaseProxy; use App\Actions\Database\StopDatabaseProxy;
use App\Helpers\SslHelper; use App\Helpers\SslHelper;
use App\Models\Server; use App\Models\Server;
use App\Models\SslCertificate;
use App\Models\StandaloneMysql; use App\Models\StandaloneMysql;
use App\Support\ValidationPatterns; use App\Support\ValidationPatterns;
use Carbon\Carbon; use Carbon\Carbon;
@ -215,7 +214,7 @@ public function regenerateSslCertificate()
return; return;
} }
$caCert = SslCertificate::where('server_id', $existingCert->server_id)->where('is_ca_certificate', true)->first(); $caCert = $this->server->sslCertificates()->where('is_ca_certificate', true)->first();
SslHelper::generateSslCertificate( SslHelper::generateSslCertificate(
commonName: $existingCert->common_name, commonName: $existingCert->common_name,

View file

@ -6,7 +6,6 @@
use App\Actions\Database\StopDatabaseProxy; use App\Actions\Database\StopDatabaseProxy;
use App\Helpers\SslHelper; use App\Helpers\SslHelper;
use App\Models\Server; use App\Models\Server;
use App\Models\SslCertificate;
use App\Models\StandalonePostgresql; use App\Models\StandalonePostgresql;
use App\Support\ValidationPatterns; use App\Support\ValidationPatterns;
use Carbon\Carbon; use Carbon\Carbon;
@ -169,7 +168,7 @@ public function regenerateSslCertificate()
return; return;
} }
$caCert = SslCertificate::where('server_id', $existingCert->server_id)->where('is_ca_certificate', true)->first(); $caCert = $this->server->sslCertificates()->where('is_ca_certificate', true)->first();
SslHelper::generateSslCertificate( SslHelper::generateSslCertificate(
commonName: $existingCert->common_name, commonName: $existingCert->common_name,

View file

@ -6,7 +6,6 @@
use App\Actions\Database\StopDatabaseProxy; use App\Actions\Database\StopDatabaseProxy;
use App\Helpers\SslHelper; use App\Helpers\SslHelper;
use App\Models\Server; use App\Models\Server;
use App\Models\SslCertificate;
use App\Models\StandaloneRedis; use App\Models\StandaloneRedis;
use App\Support\ValidationPatterns; use App\Support\ValidationPatterns;
use Carbon\Carbon; use Carbon\Carbon;
@ -209,7 +208,7 @@ public function regenerateSslCertificate()
return; return;
} }
$caCert = SslCertificate::where('server_id', $existingCert->server_id)->where('is_ca_certificate', true)->first(); $caCert = $this->server->sslCertificates()->where('is_ca_certificate', true)->first();
SslHelper::generateSslCertificate( SslHelper::generateSslCertificate(
commonName: $existingCert->commonName, commonName: $existingCert->commonName,

View file

@ -39,7 +39,7 @@ public function mount(string $server_uuid)
public function loadCaCertificate() public function loadCaCertificate()
{ {
$this->caCertificate = SslCertificate::where('server_id', $this->server->id)->where('is_ca_certificate', true)->first(); $this->caCertificate = $this->server->sslCertificates()->where('is_ca_certificate', true)->first();
if ($this->caCertificate) { if ($this->caCertificate) {
$this->certificateContent = $this->caCertificate->ssl_certificate; $this->certificateContent = $this->caCertificate->ssl_certificate;

View file

@ -1342,7 +1342,7 @@ public function generateCaCertificate()
isCaCertificate: true, isCaCertificate: true,
validityDays: 10 * 365 validityDays: 10 * 365
); );
$caCertificate = SslCertificate::where('server_id', $this->id)->where('is_ca_certificate', true)->first(); $caCertificate = $this->sslCertificates()->where('is_ca_certificate', true)->first();
ray('CA certificate generated', $caCertificate); ray('CA certificate generated', $caCertificate);
if ($caCertificate) { if ($caCertificate) {
$certificateContent = $caCertificate->ssl_certificate; $certificateContent = $caCertificate->ssl_certificate;

View file

@ -4,7 +4,6 @@
use App\Helpers\SslHelper; use App\Helpers\SslHelper;
use App\Models\Server; use App\Models\Server;
use App\Models\SslCertificate;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
class CaSslCertSeeder extends Seeder class CaSslCertSeeder extends Seeder
@ -13,7 +12,7 @@ public function run()
{ {
Server::chunk(200, function ($servers) { Server::chunk(200, function ($servers) {
foreach ($servers as $server) { foreach ($servers as $server) {
$existingCaCert = SslCertificate::where('server_id', $server->id)->where('is_ca_certificate', true)->first(); $existingCaCert = $server->sslCertificates()->where('is_ca_certificate', true)->first();
if (! $existingCaCert) { if (! $existingCaCert) {
$caCert = SslHelper::generateSslCertificate( $caCert = SslHelper::generateSslCertificate(