add credentials to services

This commit is contained in:
2025-04-09 11:09:40 +01:00
parent a2a3e9002d
commit eefe6243bc
7 changed files with 39 additions and 29 deletions

View File

@@ -28,10 +28,11 @@ class CreateService
'status' => ServiceStatus::NOT_INSTALLED, '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;
} }
} }

View File

@@ -4,19 +4,15 @@ namespace App\Drivers;
abstract class DatabaseDriver extends Driver abstract class DatabaseDriver extends Driver
{ {
public string $defaultUser = 'keystone';
public string $defaultDb = 'keystone';
public ?string $containerName; public ?string $containerName;
public ?string $containerId; public ?string $containerId;
public ?string $defaultPassword; public ?array $credentials;
abstract public function __construct( abstract public function __construct(
?string $containerName = null, ?string $containerName = null,
?string $containerId = null, ?string $containerId = null,
?string $defaultPassword = null, ?array $credentials = null,
); );
} }

View File

@@ -16,4 +16,6 @@ abstract class Driver
?string $containerName = null, ?string $containerName = null,
?string $containerId = null, ?string $containerId = null,
); );
abstract public function defaultCredentials(): array;
} }

View File

@@ -5,27 +5,28 @@ namespace App\Drivers\Postgres;
use App\Data\Deployments\Plan; use App\Data\Deployments\Plan;
use App\Data\Deployments\PlannedStep as Step; use App\Data\Deployments\PlannedStep as Step;
use App\Drivers\DatabaseDriver; use App\Drivers\DatabaseDriver;
use Illuminate\Support\Str;
class Postgres17Driver extends DatabaseDriver class Postgres17Driver extends DatabaseDriver
{ {
public Plan $deploymentPlan; public Plan $deploymentPlan;
public string $defaultUser = 'keystone';
public string $defaultDb = 'keystone';
public function __construct( public function __construct(
public ?string $containerName = null, public ?string $containerName = null,
public ?string $containerId = 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: [ $this->deploymentPlan = new Plan(steps: [
new Step( new Step(
name: 'Run the docker image', name: 'Run the docker image',
secrets: [ secrets: [
'defaultpassword' => $this->defaultPassword, 'password' => $password
], ],
script: function () { script: function () use ($user, $password, $db) {
$script = collect(); $script = collect();
if ($this->containerName) { if ($this->containerName) {
$script->push('docker stop '.$this->containerName.' || true'); $script->push('docker stop '.$this->containerName.' || true');
@@ -37,14 +38,14 @@ class Postgres17Driver extends DatabaseDriver
if ($this->containerName) { if ($this->containerName) {
$runCommand .= " --name {$this->containerName}"; $runCommand .= " --name {$this->containerName}";
} }
if ($this->defaultPassword) { if ($password) {
$runCommand .= ' -e POSTGRES_PASSWORD=[!defaultPassword!]'; $runCommand .= ' -e POSTGRES_PASSWORD=[!password!]';
} }
if ($this->defaultUser) { if ($user) {
$runCommand .= " -e POSTGRES_USER={$this->defaultUser}"; $runCommand .= " -e POSTGRES_USER={$user}";
} }
if ($this->defaultDb) { if ($db) {
$runCommand .= " -e POSTGRES_DB={$this->defaultDb}"; $runCommand .= " -e POSTGRES_DB={$db}";
} }
$runCommand .= ' -p 5432:5432 postgres:17'; $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',
];
}
} }

View File

@@ -17,14 +17,13 @@ class DeployService implements ShouldQueue
public function __construct( public function __construct(
public Service $service, public Service $service,
public ?string $defaultPassword = null,
) { ) {
// //
} }
public function handle(): void public function handle(): void
{ {
$driver = $this->service->driver($this->defaultPassword); $driver = $this->service->driver();
$this->service->update([ $this->service->update([
'status' => ServiceStatus::INSTALLING, 'status' => ServiceStatus::INSTALLING,
]); ]);
@@ -37,7 +36,7 @@ class DeployService implements ShouldQueue
'status' => DeploymentStatus::PENDING, 'status' => DeploymentStatus::PENDING,
'script' => $plannedStep->getSafeScript(), 'script' => $plannedStep->getSafeScript(),
'secrets' => [ 'secrets' => [
'defaultPassword' => $this->defaultPassword, 'password' => $this->service->credentials['password'],
], ],
]); ]);
if ($index === 0) { if ($index === 0) {

View File

@@ -15,12 +15,15 @@ class Service extends Model
{ {
protected $guarded = []; protected $guarded = [];
protected $hidden = ['credentials', 'container_name', 'container_id'];
protected function casts(): array protected function casts(): array
{ {
return [ return [
'status' => ServiceStatus::class, 'status' => ServiceStatus::class,
'category' => ServiceCategory::class, 'category' => ServiceCategory::class,
'type' => ServiceType::class, 'type' => ServiceType::class,
'credentials' => 'encrypted:array',
]; ];
} }
@@ -39,14 +42,12 @@ class Service extends Model
return $this->morphMany(Deployment::class, 'target'); return $this->morphMany(Deployment::class, 'target');
} }
public function driver( public function driver(): Driver {
?string $defaultPassword = null,
): Driver {
$class = config("keystone.drivers.{$this->driver_name}"); $class = config("keystone.drivers.{$this->driver_name}");
if (! class_exists($class)) { if (! class_exists($class)) {
throw new \Exception("Driver class {$class} not found"); 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);
} }
} }

View File

@@ -18,6 +18,7 @@ return new class extends Migration
$table->string('type'); // postgres / redis / caddy $table->string('type'); // postgres / redis / caddy
$table->string('version'); // 17 / 7 / 2 $table->string('version'); // 17 / 7 / 2
$table->string('driver_name'); $table->string('driver_name');
$table->text('credentials')->nullable();
$table->string('container_name')->nullable(); $table->string('container_name')->nullable();
$table->string('container_id')->nullable(); $table->string('container_id')->nullable();
$table->timestamps(); $table->timestamps();