From eefe6243bc5fbce20207667b8e05a09f94fb758e Mon Sep 17 00:00:00 2001 From: "Harry (hjbdev)" Date: Wed, 9 Apr 2025 11:09:40 +0100 Subject: [PATCH] add credentials to services --- app/Actions/Services/CreateService.php | 7 ++-- app/Drivers/DatabaseDriver.php | 8 ++--- app/Drivers/Driver.php | 2 ++ app/Drivers/Postgres/Postgres17Driver.php | 36 ++++++++++++------- app/Jobs/Services/DeployService.php | 5 ++- app/Models/Service.php | 9 ++--- ...025_03_27_121050_create_services_table.php | 1 + 7 files changed, 39 insertions(+), 29 deletions(-) diff --git a/app/Actions/Services/CreateService.php b/app/Actions/Services/CreateService.php index 19e24c7..1b51c5c 100644 --- a/app/Actions/Services/CreateService.php +++ b/app/Actions/Services/CreateService.php @@ -28,10 +28,11 @@ class CreateService 'status' => ServiceStatus::NOT_INSTALLED, ]); - $defaultPassword = Str::random(16); + $service->credentials = $service->driver()->defaultCredentials(); + $service->save(); - dispatch(new DeployService($service, $defaultPassword)); + dispatch(new DeployService($service)); - return ['defaultPassword' => $defaultPassword, 'service' => $service]; + return $service; } } diff --git a/app/Drivers/DatabaseDriver.php b/app/Drivers/DatabaseDriver.php index d4d0f35..b1597b5 100644 --- a/app/Drivers/DatabaseDriver.php +++ b/app/Drivers/DatabaseDriver.php @@ -4,19 +4,15 @@ namespace App\Drivers; abstract class DatabaseDriver extends Driver { - public string $defaultUser = 'keystone'; - - public string $defaultDb = 'keystone'; - public ?string $containerName; public ?string $containerId; - public ?string $defaultPassword; + public ?array $credentials; abstract public function __construct( ?string $containerName = null, ?string $containerId = null, - ?string $defaultPassword = null, + ?array $credentials = null, ); } diff --git a/app/Drivers/Driver.php b/app/Drivers/Driver.php index d1bdda9..51085c6 100644 --- a/app/Drivers/Driver.php +++ b/app/Drivers/Driver.php @@ -16,4 +16,6 @@ abstract class Driver ?string $containerName = null, ?string $containerId = null, ); + + abstract public function defaultCredentials(): array; } diff --git a/app/Drivers/Postgres/Postgres17Driver.php b/app/Drivers/Postgres/Postgres17Driver.php index 66a9824..236d12f 100644 --- a/app/Drivers/Postgres/Postgres17Driver.php +++ b/app/Drivers/Postgres/Postgres17Driver.php @@ -5,27 +5,28 @@ namespace App\Drivers\Postgres; use App\Data\Deployments\Plan; use App\Data\Deployments\PlannedStep as Step; use App\Drivers\DatabaseDriver; +use Illuminate\Support\Str; class Postgres17Driver extends DatabaseDriver { public Plan $deploymentPlan; - public string $defaultUser = 'keystone'; - - public string $defaultDb = 'keystone'; - public function __construct( public ?string $containerName = null, public ?string $containerId = null, - public ?string $defaultPassword = null, + public ?array $credentials = null, ) { + $user = $credentials['user']; + $password = $credentials['password']; + $db = $credentials['db']; + $this->deploymentPlan = new Plan(steps: [ new Step( name: 'Run the docker image', secrets: [ - 'defaultpassword' => $this->defaultPassword, + 'password' => $password ], - script: function () { + script: function () use ($user, $password, $db) { $script = collect(); if ($this->containerName) { $script->push('docker stop '.$this->containerName.' || true'); @@ -37,14 +38,14 @@ class Postgres17Driver extends DatabaseDriver if ($this->containerName) { $runCommand .= " --name {$this->containerName}"; } - if ($this->defaultPassword) { - $runCommand .= ' -e POSTGRES_PASSWORD=[!defaultPassword!]'; + if ($password) { + $runCommand .= ' -e POSTGRES_PASSWORD=[!password!]'; } - if ($this->defaultUser) { - $runCommand .= " -e POSTGRES_USER={$this->defaultUser}"; + if ($user) { + $runCommand .= " -e POSTGRES_USER={$user}"; } - if ($this->defaultDb) { - $runCommand .= " -e POSTGRES_DB={$this->defaultDb}"; + if ($db) { + $runCommand .= " -e POSTGRES_DB={$db}"; } $runCommand .= ' -p 5432:5432 postgres:17'; @@ -58,4 +59,13 @@ class Postgres17Driver extends DatabaseDriver ), ]); } + + public function defaultCredentials(): array + { + return [ + 'password' => Str::random(16), + 'user' => 'keystone', + 'db' => 'keystone', + ]; + } } diff --git a/app/Jobs/Services/DeployService.php b/app/Jobs/Services/DeployService.php index 22d2fa4..2fe3cf7 100644 --- a/app/Jobs/Services/DeployService.php +++ b/app/Jobs/Services/DeployService.php @@ -17,14 +17,13 @@ class DeployService implements ShouldQueue public function __construct( public Service $service, - public ?string $defaultPassword = null, ) { // } public function handle(): void { - $driver = $this->service->driver($this->defaultPassword); + $driver = $this->service->driver(); $this->service->update([ 'status' => ServiceStatus::INSTALLING, ]); @@ -37,7 +36,7 @@ class DeployService implements ShouldQueue 'status' => DeploymentStatus::PENDING, 'script' => $plannedStep->getSafeScript(), 'secrets' => [ - 'defaultPassword' => $this->defaultPassword, + 'password' => $this->service->credentials['password'], ], ]); if ($index === 0) { diff --git a/app/Models/Service.php b/app/Models/Service.php index 8409662..8371e30 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -15,12 +15,15 @@ class Service extends Model { protected $guarded = []; + protected $hidden = ['credentials', 'container_name', 'container_id']; + protected function casts(): array { return [ 'status' => ServiceStatus::class, 'category' => ServiceCategory::class, 'type' => ServiceType::class, + 'credentials' => 'encrypted:array', ]; } @@ -39,14 +42,12 @@ class Service extends Model return $this->morphMany(Deployment::class, 'target'); } - public function driver( - ?string $defaultPassword = null, - ): Driver { + public function driver(): Driver { $class = config("keystone.drivers.{$this->driver_name}"); if (! class_exists($class)) { throw new \Exception("Driver class {$class} not found"); } - return new $class($this->container_name, $this->container_id, defaultPassword: $defaultPassword); + return new $class($this->container_name, $this->container_id, credentials: $this->credentials); } } diff --git a/database/migrations/2025_03_27_121050_create_services_table.php b/database/migrations/2025_03_27_121050_create_services_table.php index 250e41a..fad4080 100644 --- a/database/migrations/2025_03_27_121050_create_services_table.php +++ b/database/migrations/2025_03_27_121050_create_services_table.php @@ -18,6 +18,7 @@ return new class extends Migration $table->string('type'); // postgres / redis / caddy $table->string('version'); // 17 / 7 / 2 $table->string('driver_name'); + $table->text('credentials')->nullable(); $table->string('container_name')->nullable(); $table->string('container_id')->nullable(); $table->timestamps();