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,
]);
$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
{
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,
);
}

View File

@@ -16,4 +16,6 @@ abstract class Driver
?string $containerName = 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\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',
];
}
}

View File

@@ -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) {

View File

@@ -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);
}
}