Skip to content

Commit 944d48e

Browse files
saeedvaziryCopilot
andauthored
Add user selection to ssh key deployment (#926)
* Add user selection to ssh key deployment * Update public/api-docs/openapi/ssh-keys.yaml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix migration --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 72a178f commit 944d48e

File tree

18 files changed

+420
-24
lines changed

18 files changed

+420
-24
lines changed

app/Actions/SshKey/DeleteKeyFromServer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@ class DeleteKeyFromServer
1414
*/
1515
public function delete(Server $server, SshKey $sshKey): void
1616
{
17+
$pivot = $server->sshKeys()->where('ssh_keys.id', $sshKey->id)->first();
18+
$user = $pivot?->pivot->user ?? $server->getSshUser();
19+
1720
$sshKey->servers()->updateExistingPivot($server->id, [
1821
'status' => SshKeyStatus::DELETING,
1922
]);
20-
$server->os()->deleteSSHKey($sshKey->public_key);
23+
$server->os()->deleteSSHKey($sshKey->public_key, $user);
2124
$server->sshKeys()->detach($sshKey);
2225
}
2326
}

app/Actions/SshKey/DeployKeyToServer.php

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,52 @@
66
use App\Exceptions\SSHError;
77
use App\Models\Server;
88
use App\Models\SshKey;
9+
use Illuminate\Support\Facades\Validator;
10+
use Illuminate\Validation\Rule;
911

1012
class DeployKeyToServer
1113
{
1214
/**
15+
* @param array<string, mixed> $input
16+
*
1317
* @throws SSHError
1418
*/
15-
public function deploy(Server $server, SshKey $sshKey): void
19+
public function deploy(Server $server, SshKey $sshKey, array $input = []): void
1620
{
21+
// Set default user for backward compatibility
22+
if (! isset($input['user'])) {
23+
$input['user'] = $server->getSshUser();
24+
}
25+
26+
$this->validate($server, $input);
27+
28+
$user = $input['user'];
29+
1730
$server->sshKeys()->attach($sshKey, [
1831
'status' => SshKeyStatus::ADDING,
32+
'user' => $user,
1933
]);
20-
$server->os()->deploySSHKey($sshKey->public_key);
34+
$server->os()->deploySSHKey($sshKey->public_key, $user);
2135
$sshKey->servers()->updateExistingPivot($server->id, [
2236
'status' => SshKeyStatus::ADDED,
37+
'user' => $user,
2338
]);
2439
}
40+
41+
private function validate(Server $server, array $input): void
42+
{
43+
if (empty($input) || ! isset($input['user'])) {
44+
return;
45+
}
46+
47+
$rules = [
48+
'user' => [
49+
'required',
50+
'string',
51+
Rule::in($server->getSshUsers()),
52+
],
53+
];
54+
55+
Validator::make($input, $rules)->validate();
56+
}
2557
}

app/Http/Controllers/API/ServerSSHKeyController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function create(Request $request, Project $project, Server $server): SshK
6363
$sshKey = app(CreateSshKey::class)->create($user, $request->all());
6464
}
6565

66-
app(DeployKeyToServer::class)->deploy($server, $sshKey);
66+
app(DeployKeyToServer::class)->deploy($server, $sshKey, $request->input());
6767

6868
return new SshKeyResource($sshKey);
6969
}

app/Http/Controllers/ServerSshKeyController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function store(Request $request, Server $server): RedirectResponse
5050
]);
5151
}
5252

53-
app(DeployKeyToServer::class)->deploy($server, $sshKey);
53+
app(DeployKeyToServer::class)->deploy($server, $sshKey, $request->input());
5454

5555
return back()->with('success', 'SSH key deployed.');
5656
}

app/Http/Resources/SshKeyResource.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public function toArray(Request $request): array
1818
'id' => $this->id,
1919
'user' => new UserResource($this->whenLoaded('user')),
2020
'name' => $this->name,
21+
'deployment_user' => $this->whenPivotLoaded('server_ssh_keys', function () {
22+
return $this->pivot->user ?? null;
23+
}),
2124
'created_at' => $this->created_at,
2225
'updated_at' => $this->updated_at,
2326
];

app/Models/Server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public function metrics(): HasMany
286286
public function sshKeys(): BelongsToMany
287287
{
288288
return $this->belongsToMany(SshKey::class, 'server_ssh_keys')
289-
->withPivot('status')
289+
->withPivot('status', 'user')
290290
->withTimestamps();
291291
}
292292

app/Models/SshKey.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function user(): BelongsTo
4747
public function servers(): BelongsToMany
4848
{
4949
return $this->belongsToMany(Server::class, 'server_ssh_keys')
50-
->withPivot('status')
50+
->withPivot('status', 'user')
5151
->withTimestamps();
5252
}
5353

app/SSH/OS/OS.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ public function getPublicKey(string $user): string
114114
/**
115115
* @throws SSHError
116116
*/
117-
public function deploySSHKey(string $key): void
117+
public function deploySSHKey(string $key, string $user): void
118118
{
119-
$this->server->ssh()->exec(
119+
$this->server->ssh($user)->exec(
120120
view('ssh.os.deploy-ssh-key', [
121121
'key' => $key,
122122
]),
@@ -127,12 +127,11 @@ public function deploySSHKey(string $key): void
127127
/**
128128
* @throws SSHError
129129
*/
130-
public function deleteSSHKey(string $key): void
130+
public function deleteSSHKey(string $key, string $user): void
131131
{
132-
$this->server->ssh()->exec(
132+
$this->server->ssh($user)->exec(
133133
view('ssh.os.delete-ssh-key', [
134134
'key' => $key,
135-
'user' => $this->server->getSshUser(),
136135
]),
137136
'delete-ssh-key'
138137
);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('server_ssh_keys', function (Blueprint $table): void {
15+
$table->string('user')->nullable()->default(config('core.ssh_user'))->after('status');
16+
});
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*/
22+
public function down(): void
23+
{
24+
Schema::table('server_ssh_keys', function (Blueprint $table): void {
25+
$table->dropColumn('user');
26+
});
27+
}
28+
};

public/api-docs/openapi/schemas/SshKey.yaml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@ properties:
33
id:
44
type: integer
55
example: 1
6-
project_id:
7-
type: integer
8-
example: 1
96
name:
107
type: string
118
example: 'My SSH Key'
12-
public_key:
13-
type: string
14-
example: 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQC...'
15-
fingerprint:
9+
deployment_user:
1610
type: string
17-
example: 'SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
11+
nullable: true
12+
description: Server user the key is deployed to
13+
example: 'vito'
1814
created_at:
1915
type: string
2016
format: date-time

0 commit comments

Comments
 (0)