refactor(configuration): centralize configuration management in ConfigurationRepository

This commit is contained in:
Andras Bacsai
2025-03-24 21:01:27 +01:00
parent d01889a0c2
commit e891318396
7 changed files with 110 additions and 61 deletions

View File

@@ -7,6 +7,7 @@ use App\Models\PrivateKey;
use App\Models\Project; use App\Models\Project;
use App\Models\Server; use App\Models\Server;
use App\Models\Team; use App\Models\Team;
use App\Services\ConfigurationRepository;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Livewire\Component; use Livewire\Component;
use Visus\Cuid2\Cuid2; use Visus\Cuid2\Cuid2;
@@ -266,7 +267,7 @@ uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA==
public function validateServer() public function validateServer()
{ {
try { try {
config()->set('constants.ssh.mux_enabled', false); $this->disableSshMux();
// EC2 does not have `uptime` command, lol // EC2 does not have `uptime` command, lol
instant_remote_process(['ls /'], $this->createdServer, true); instant_remote_process(['ls /'], $this->createdServer, true);
@@ -376,6 +377,12 @@ uZx9iFkCELtxrh31QJ68AAAAEXNhaWxANzZmZjY2ZDJlMmRkAQIDBA==
['private' => $this->privateKey, 'public' => $this->publicKey] = generateSSHKey(); ['private' => $this->privateKey, 'public' => $this->publicKey] = generateSSHKey();
} }
private function disableSshMux(): void
{
$configRepository = app(ConfigurationRepository::class);
$configRepository->disableSshMux();
}
public function render() public function render()
{ {
return view('livewire.boarding.index')->layout('layouts.boarding'); return view('livewire.boarding.index')->layout('layouts.boarding');

View File

@@ -10,6 +10,7 @@ use App\Events\ServerReachabilityChanged;
use App\Jobs\CheckAndStartSentinelJob; use App\Jobs\CheckAndStartSentinelJob;
use App\Notifications\Server\Reachable; use App\Notifications\Server\Reachable;
use App\Notifications\Server\Unreachable; use App\Notifications\Server\Unreachable;
use App\Services\ConfigurationRepository;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -543,7 +544,7 @@ $schema://$host {
$this->settings->save(); $this->settings->save();
$sshKeyFileLocation = "id.root@{$this->uuid}"; $sshKeyFileLocation = "id.root@{$this->uuid}";
Storage::disk('ssh-keys')->delete($sshKeyFileLocation); Storage::disk('ssh-keys')->delete($sshKeyFileLocation);
Storage::disk('ssh-mux')->delete($this->muxFilename()); $this->disableSshMux();
} }
public function sentinelHeartbeat(bool $isReset = false) public function sentinelHeartbeat(bool $isReset = false)
@@ -1103,7 +1104,7 @@ $schema://$host {
public function validateConnection(bool $justCheckingNewKey = false) public function validateConnection(bool $justCheckingNewKey = false)
{ {
config()->set('constants.ssh.mux_enabled', false); $this->disableSshMux();
if ($this->skipServer()) { if ($this->skipServer()) {
return ['uptime' => false, 'error' => 'Server skipped.']; return ['uptime' => false, 'error' => 'Server skipped.'];
@@ -1330,4 +1331,10 @@ $schema://$host {
$this->databases()->count() == 0 && $this->databases()->count() == 0 &&
$this->services()->count() == 0; $this->services()->count() == 0;
} }
private function disableSshMux(): void
{
$configRepository = app(ConfigurationRepository::class);
$configRepository->disableSshMux();
}
} }

View File

@@ -2,6 +2,7 @@
namespace App\Notifications\Channels; namespace App\Notifications\Channels;
use App\Services\ConfigurationRepository;
use Exception; use Exception;
use Illuminate\Mail\Message; use Illuminate\Mail\Message;
use Illuminate\Notifications\Notification; use Illuminate\Notifications\Notification;
@@ -9,6 +10,13 @@ use Illuminate\Support\Facades\Mail;
class EmailChannel class EmailChannel
{ {
private ConfigurationRepository $configRepository;
public function __construct(ConfigurationRepository $configRepository)
{
$this->configRepository = $configRepository;
}
public function send(SendsEmail $notifiable, Notification $notification): void public function send(SendsEmail $notifiable, Notification $notification): void
{ {
try { try {
@@ -57,34 +65,6 @@ class EmailChannel
return; return;
} }
config()->set('mail.from.address', $emailSettings->smtp_from_address ?? 'test@example.com'); $this->configRepository->updateMailConfig($emailSettings);
config()->set('mail.from.name', $emailSettings->smtp_from_name ?? 'Test');
if ($emailSettings->resend_enabled) {
config()->set('mail.default', 'resend');
config()->set('resend.api_key', $emailSettings->resend_api_key);
}
if ($emailSettings->smtp_enabled) {
$encryption = match (strtolower($emailSettings->smtp_encryption)) {
'starttls' => null,
'tls' => 'tls',
'none' => null,
default => null,
};
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
'transport' => 'smtp',
'host' => $emailSettings->smtp_host,
'port' => $emailSettings->smtp_port,
'encryption' => $encryption,
'username' => $emailSettings->smtp_username,
'password' => $emailSettings->smtp_password,
'timeout' => $emailSettings->smtp_timeout,
'local_domain' => null,
'auto_tls' => $emailSettings->smtp_encryption === 'none' ? '0' : '', // If encryption is "none", it will not try to upgrade to TLS via StartTLS to make sure it is unencrypted.
]);
}
} }
} }

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Providers;
use App\Services\ConfigurationRepository;
use Illuminate\Support\ServiceProvider;
class ConfigurationServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(ConfigurationRepository::class, function ($app) {
return new ConfigurationRepository($app['config']);
});
}
public function boot(): void
{
//
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Services;
use Illuminate\Config\Repository;
class ConfigurationRepository
{
private Repository $config;
public function __construct(Repository $config)
{
$this->config = $config;
}
public function updateMailConfig($settings): void
{
if ($settings->resend_enabled) {
$this->config->set('mail.default', 'resend');
$this->config->set('mail.from.address', $settings->smtp_from_address ?? 'test@example.com');
$this->config->set('mail.from.name', $settings->smtp_from_name ?? 'Test');
$this->config->set('resend.api_key', $settings->resend_api_key);
return;
}
if ($settings->smtp_enabled) {
$encryption = match (strtolower($settings->smtp_encryption)) {
'starttls' => null,
'tls' => 'tls',
'none' => null,
default => null,
};
$this->config->set('mail.default', 'smtp');
$this->config->set('mail.from.address', $settings->smtp_from_address ?? 'test@example.com');
$this->config->set('mail.from.name', $settings->smtp_from_name ?? 'Test');
$this->config->set('mail.mailers.smtp', [
'transport' => 'smtp',
'host' => $settings->smtp_host,
'port' => $settings->smtp_port,
'encryption' => $encryption,
'username' => $settings->smtp_username,
'password' => $settings->smtp_password,
'timeout' => $settings->smtp_timeout,
'local_domain' => null,
'auto_tls' => $settings->smtp_encryption === 'none' ? '0' : '',
]);
}
}
public function disableSshMux(): void
{
$this->config->set('constants.ssh.mux_enabled', false);
}
}

View File

@@ -1,6 +1,5 @@
<?php <?php
use App\Models\InstanceSettings;
use App\Models\Team; use App\Models\Team;
use App\Notifications\Internal\GeneralNotification; use App\Notifications\Internal\GeneralNotification;
use Illuminate\Mail\Message; use Illuminate\Mail\Message;
@@ -54,7 +53,7 @@ function send_user_an_email(MailMessage $mail, string $email, ?string $cc = null
} }
} }
function set_transanctional_email_settings(?InstanceSettings $settings = null): ?string // returns null|resend|smtp and defaults to array based on mail.php config function set_transanctional_email_settings($settings = null)
{ {
if (! $settings) { if (! $settings) {
$settings = instanceSettings(); $settings = instanceSettings();
@@ -63,38 +62,16 @@ function set_transanctional_email_settings(?InstanceSettings $settings = null):
return null; return null;
} }
if (data_get($settings, 'resend_enabled')) { $configRepository = app('App\Services\ConfigurationRepository'::class);
config()->set('mail.default', 'resend'); $configRepository->updateMailConfig($settings);
config()->set('mail.from.address', data_get($settings, 'smtp_from_address'));
config()->set('mail.from.name', data_get($settings, 'smtp_from_name'));
config()->set('resend.api_key', data_get($settings, 'resend_api_key'));
if (data_get($settings, 'resend_enabled')) {
return 'resend'; return 'resend';
} }
$encryption = match (strtolower(data_get($settings, 'smtp_encryption'))) {
'starttls' => null,
'tls' => 'tls',
'none' => null,
default => null,
};
if (data_get($settings, 'smtp_enabled')) { if (data_get($settings, 'smtp_enabled')) {
config()->set('mail.from.address', data_get($settings, 'smtp_from_address'));
config()->set('mail.from.name', data_get($settings, 'smtp_from_name'));
config()->set('mail.default', 'smtp');
config()->set('mail.mailers.smtp', [
'transport' => 'smtp',
'host' => data_get($settings, 'smtp_host'),
'port' => data_get($settings, 'smtp_port'),
'encryption' => $encryption,
'username' => data_get($settings, 'smtp_username'),
'password' => data_get($settings, 'smtp_password'),
'timeout' => data_get($settings, 'smtp_timeout'),
'local_domain' => null,
'auto_tls' => data_get($settings, 'smtp_encryption') === 'none' ? '0' : '', // If encryption is "none", it will not try to upgrade to TLS via StartTLS to make sure it is unencrypted.
]);
return 'smtp'; return 'smtp';
} }
return null;
} }

View File

@@ -199,6 +199,7 @@ return [
App\Providers\EventServiceProvider::class, App\Providers\EventServiceProvider::class,
App\Providers\HorizonServiceProvider::class, App\Providers\HorizonServiceProvider::class,
App\Providers\RouteServiceProvider::class, App\Providers\RouteServiceProvider::class,
App\Providers\ConfigurationServiceProvider::class,
], ],
/* /*