#591 - server-ssh-keys

This commit is contained in:
Saeed Vaziry
2025-05-29 21:20:33 +02:00
parent 8b6f65db97
commit 0fce4dba9c
22 changed files with 438 additions and 195 deletions

View File

@ -6,20 +6,14 @@
use App\Exceptions\SSHError;
use App\Models\Server;
use App\Models\SshKey;
use App\Models\User;
use Illuminate\Validation\Rule;
class DeployKeyToServer
{
/**
* @param array<string, mixed> $input
*
* @throws SSHError
*/
public function deploy(Server $server, array $input): void
public function deploy(Server $server, SshKey $sshKey): void
{
/** @var SshKey $sshKey */
$sshKey = SshKey::query()->findOrFail($input['key_id']);
$server->sshKeys()->attach($sshKey, [
'status' => SshKeyStatus::ADDING,
]);
@ -28,18 +22,4 @@ public function deploy(Server $server, array $input): void
'status' => SshKeyStatus::ADDED,
]);
}
/**
* @return array<string, array<string>>
*/
public static function rules(User $user, Server $server): array
{
return [
'key_id' => [
'required',
Rule::exists('ssh_keys', 'id')->where('user_id', $user->id),
Rule::unique('server_ssh_keys', 'ssh_key_id')->where('server_id', $server->id),
],
];
}
}

View File

@ -5,6 +5,7 @@
use App\Actions\SshKey\CreateSshKey;
use App\Actions\SshKey\DeleteKeyFromServer;
use App\Actions\SshKey\DeployKeyToServer;
use App\Exceptions\SSHError;
use App\Http\Controllers\Controller;
use App\Http\Resources\SshKeyResource;
use App\Models\Project;
@ -41,6 +42,9 @@ public function index(Project $project, Server $server): ResourceCollection
return SshKeyResource::collection($server->sshKeys()->simplePaginate(25));
}
/**
* @throws SSHError
*/
#[Post('/', name: 'api.projects.servers.ssh-keys.create', middleware: 'ability:write')]
#[Endpoint(title: 'create', description: 'Deploy ssh key to server.')]
#[BodyParam(name: 'key_id', description: 'The ID of the key.')]
@ -58,9 +62,7 @@ public function create(Request $request, Project $project, Server $server): SshK
$sshKey = null;
if ($request->has('key_id')) {
$this->validate($request, DeployKeyToServer::rules($user, $server));
/** @var ?SshKey $sshKey */
/** @var SshKey $sshKey */
$sshKey = $user->sshKeys()->findOrFail($request->key_id);
}
@ -69,7 +71,7 @@ public function create(Request $request, Project $project, Server $server): SshK
$sshKey = app(CreateSshKey::class)->create($user, $request->all());
}
app(DeployKeyToServer::class)->deploy($server, ['key_id' => $sshKey->id]);
app(DeployKeyToServer::class)->deploy($server, $sshKey);
return new SshKeyResource($sshKey);
}

View File

@ -0,0 +1,63 @@
<?php
namespace App\Http\Controllers;
use App\Actions\SshKey\DeleteKeyFromServer;
use App\Actions\SshKey\DeployKeyToServer;
use App\Exceptions\SSHError;
use App\Http\Resources\SshKeyResource;
use App\Models\Server;
use App\Models\SshKey;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
use Spatie\RouteAttributes\Attributes\Delete;
use Spatie\RouteAttributes\Attributes\Get;
use Spatie\RouteAttributes\Attributes\Middleware;
use Spatie\RouteAttributes\Attributes\Post;
use Spatie\RouteAttributes\Attributes\Prefix;
#[Prefix('servers/{server}/ssh-keys')]
#[Middleware(['auth', 'has-project'])]
class ServerSshKeyController extends Controller
{
#[Get('/', name: 'server-ssh-keys')]
public function index(Server $server): Response
{
$this->authorize('viewAnyServer', [SshKey::class, $server]);
return Inertia::render('server-ssh-keys/index', [
'sshKeys' => SshKeyResource::collection($server->sshKeys()->with('user')->simplePaginate(config('web.pagination_size'))),
]);
}
/**
* @throws SSHError
*/
#[Post('/', name: 'server-ssh-keys.store')]
public function store(Request $request, Server $server): RedirectResponse
{
$this->authorize('createServer', [SshKey::class, $server]);
/** @var SshKey $sshKey */
$sshKey = user()->sshKeys()->findOrFail($request->input('key'));
app(DeployKeyToServer::class)->deploy($server, $sshKey);
return back()->with('success', 'SSH key deployed.');
}
/**
* @throws SSHError
*/
#[Delete('/{sshKey}', name: 'server-ssh-keys.destroy')]
public function destroy(Server $server, SshKey $sshKey): RedirectResponse
{
$this->authorize('deleteServer', [SshKey::class, $server]);
app(DeleteKeyFromServer::class)->delete($server, $sshKey);
return back()->with('success', 'SSH key deleted.');
}
}

View File

@ -7,6 +7,7 @@
use App\Models\SshKey;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Inertia\Inertia;
use Inertia\Response;
use Spatie\RouteAttributes\Attributes\Delete;
@ -17,7 +18,7 @@
#[Prefix('settings/ssh-keys')]
#[Middleware(['auth'])]
class SSHKeyController extends Controller
class SshKeyController extends Controller
{
#[Get('/', name: 'ssh-keys')]
public function index(): Response
@ -29,6 +30,14 @@ public function index(): Response
]);
}
#[Get('/json', name: 'ssh-keys.json')]
public function json(): ResourceCollection
{
$this->authorize('viewAny', SshKey::class);
return SshKeyResource::collection(user()->sshKeys()->get());
}
#[Post('/', name: 'ssh-keys.store')]
public function store(Request $request): RedirectResponse
{

View File

@ -16,7 +16,7 @@ public function toArray(Request $request): array
{
return [
'id' => $this->id,
'user' => $this->user_id ? new UserResource($this->user) : null,
'user' => new UserResource($this->whenLoaded('user')),
'name' => $this->name,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,

View File

@ -2,6 +2,7 @@
namespace App\Models;
use Database\Factories\SshKeyFactory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
@ -16,7 +17,7 @@
*/
class SshKey extends AbstractModel
{
/** @use HasFactory<\Database\Factories\SshKeyFactory> */
/** @use HasFactory<SshKeyFactory> */
use HasFactory;
use SoftDeletes;