-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from vanguardsh/feat-remove-ssh-key-from-server
Feat: SSH Key Removal
- Loading branch information
Showing
17 changed files
with
350 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace App\Actions\RemoteServer; | ||
|
||
use App\Mail\RemoteServers\FailedToRemoveKey; | ||
use App\Mail\RemoteServers\SuccessfullyRemovedKey; | ||
use App\Models\RemoteServer; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\Facades\Mail; | ||
use phpseclib3\Crypt\PublicKeyLoader; | ||
use phpseclib3\Net\SSH2; | ||
use RuntimeException; | ||
|
||
class RemoveSSHKey | ||
{ | ||
public function handle(RemoteServer $remoteServer): void | ||
{ | ||
Log::info('Removing SSH key from server.', ['server_id' => $remoteServer->id]); | ||
|
||
$key = PublicKeyLoader::load(get_ssh_private_key(), config('app.ssh.passphrase')); | ||
|
||
try { | ||
$ssh = new SSH2($remoteServer->ip_address, $remoteServer->port, 5); | ||
|
||
$ssh->login($remoteServer->username, $key); | ||
|
||
$vanguardsPublicKey = get_ssh_public_key(); | ||
|
||
$ssh->exec("sed -i '/{$vanguardsPublicKey}/d' ~/.ssh/authorized_keys"); | ||
|
||
Log::info('Removed SSH key from server.', ['server_id' => $remoteServer->id]); | ||
Log::info('Updated server to indicate SSH key was removed.', ['server_id' => $remoteServer->id]); | ||
Mail::to($remoteServer->user->email)->queue(new SuccessfullyRemovedKey($remoteServer)); | ||
|
||
} catch (RuntimeException $e) { | ||
Log::debug('[SSH Key Removal] Failed to connect to remote server', ['error' => $e->getMessage()]); | ||
Mail::to($remoteServer->user->email)->queue(new FailedToRemoveKey($remoteServer, $e->getMessage())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace App\Jobs\RemoteServers; | ||
|
||
use App\Actions\RemoteServer\RemoveSSHKey; | ||
use App\Models\RemoteServer; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class RemoveSSHKeyJob implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
public function __construct(public RemoteServer $remoteServer) | ||
{ | ||
// | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
Log::info('Removing SSH key from server.', ['server_id' => $this->remoteServer->id]); | ||
|
||
$action = new RemoveSSHKey; | ||
$action->handle($this->remoteServer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Jobs\RemoteServers; | ||
|
||
use App\Models\RemoteServer; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class RemoveServerJob implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
public function __construct(public RemoteServer $remoteServer) | ||
{ | ||
// | ||
} | ||
|
||
public function handle(): void | ||
{ | ||
Log::info('Removing server.', ['server_id' => $this->remoteServer->id]); | ||
$this->remoteServer->forceDelete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Mail\RemoteServers; | ||
|
||
use App\Models\RemoteServer; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Mail\Mailable; | ||
use Illuminate\Mail\Mailables\Content; | ||
use Illuminate\Mail\Mailables\Envelope; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class FailedToRemoveKey extends Mailable implements ShouldQueue | ||
{ | ||
use Queueable, SerializesModels; | ||
|
||
public function __construct(public readonly RemoteServer $remoteServer, public readonly string $message = '') | ||
{ | ||
// | ||
} | ||
|
||
public function envelope(): Envelope | ||
{ | ||
return new Envelope( | ||
subject: __('Failed to Remove SSH Key'), | ||
); | ||
} | ||
|
||
public function content(): Content | ||
{ | ||
return new Content( | ||
markdown: 'mail.remote-servers.failed-to-remove-key', | ||
with: ['remoteServer' => $this->remoteServer, 'message' => $this->message, 'user' => $this->remoteServer->user], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Mail\RemoteServers; | ||
|
||
use App\Models\RemoteServer; | ||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Mail\Mailable; | ||
use Illuminate\Mail\Mailables\Content; | ||
use Illuminate\Mail\Mailables\Envelope; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class SuccessfullyRemovedKey extends Mailable implements ShouldQueue | ||
{ | ||
use Queueable, SerializesModels; | ||
|
||
public function __construct(public readonly RemoteServer $remoteServer) | ||
{ | ||
// | ||
} | ||
|
||
public function envelope(): Envelope | ||
{ | ||
return new Envelope( | ||
subject: __('Notice of SSH Key Removal'), | ||
); | ||
} | ||
|
||
public function content(): Content | ||
{ | ||
return new Content( | ||
markdown: 'mail.remote-servers.successfully-removed-key', | ||
with: ['remoteServer' => $this->remoteServer, 'user' => $this->remoteServer->user], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
database/migrations/2024_06_14_162816_add_marked_for_deletion_at_to_remote_servers_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up(): void | ||
{ | ||
Schema::table('remote_servers', function (Blueprint $table) { | ||
$table->timestamp('marked_for_deletion_at')->nullable(); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
resources/views/mail/remote-servers/failed-to-remove-key.blade.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<x-mail::message> | ||
# {{ $remoteServer->label }} - Failed to Remove Key | ||
|
||
Hey, {{ $user->first_name }}! | ||
|
||
We have failed to remove our SSH key from the server: {{ $remoteServer->label }}. | ||
|
||
You can find the error message below: | ||
|
||
<x-mail::panel> | ||
{{ $message }} | ||
</x-mail::panel> | ||
|
||
Please connect to {{ $remoteServer->label }} through your preferred SSH client and remove the key manually by navigating to the `~/.ssh/authorized_keys` file. | ||
|
||
Thanks,<br> | ||
{{ config('app.name') }} | ||
</x-mail::message> |
12 changes: 12 additions & 0 deletions
12
resources/views/mail/remote-servers/successfully-removed-key.blade.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<x-mail::message> | ||
# {{ $remoteServer->label }} - Successfully Removed Key | ||
|
||
Hey, {{ $user->first_name }}! | ||
|
||
We have successfully removed our SSH key from the server: {{ $remoteServer->label }}. | ||
|
||
If you have any questions, please let us know. | ||
|
||
Thanks,<br> | ||
{{ config('app.name') }} | ||
</x-mail::message> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.