Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

Feat: Delete unused private keys button

peaklabs-devdbc723089b04
diff --git a/app/Jobs/CleanupSshKeysJob.php b/app/Jobs/CleanupSshKeysJob.php
deleted file mode 100644
index 485bf99eba..0000000000
--- a/app/Jobs/CleanupSshKeysJob.php
+++ /dev/null
@@ -1,27 +0,0 @@
-<?php
-
-namespace App\Jobs;
-
-use App\Models\PrivateKey;
-use Illuminate\Bus\Queueable;
-use Illuminate\Contracts\Queue\ShouldQueue;
-use Illuminate\Foundation\Bus\Dispatchable;
-use Illuminate\Queue\InteractsWithQueue;
-use Illuminate\Queue\SerializesModels;
-use Carbon\Carbon;
-
-class CleanupSshKeysJob implements ShouldQueue
-{
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
-
- public function handle()
- {
- $oneWeekAgo = Carbon::now()->subWeek();
-
- PrivateKey::where('created_at', '<', $oneWeekAgo)
- ->get()
- ->each(function ($privateKey) {
- $privateKey->safeDelete();
- });
- }
-}
diff --git a/app/Livewire/Security/PrivateKey/Index.php b/app/Livewire/Security/PrivateKey/Index.php
new file mode 100644
index 0000000000..0b50ba1d80
--- /dev/null
+++ b/app/Livewire/Security/PrivateKey/Index.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace App\Livewire\Security\PrivateKey;
+
+use Livewire\Component;
+use App\Models\PrivateKey;
+
+class Index extends Component
+{
+ public function render()
+ {
+ $privateKeys = PrivateKey::ownedByCurrentTeam(['name', 'uuid', 'is_git_related', 'description'])->get();
+
+ return view('livewire.security.private-key.index', [
+ 'privateKeys' => $privateKeys,
+ ])->layout('components.layout');
+ }
+
+ public function cleanupUnusedKeys()
+ {
+ PrivateKey::cleanupUnusedKeys();
+ $this->dispatch('success', 'Unused keys have been cleaned up.');
+ }
+}
diff --git a/app/Models/PrivateKey.php b/app/Models/PrivateKey.php
index 6985ca536b..e1452fad12 100644
--- a/app/Models/PrivateKey.php
+++ b/app/Models/PrivateKey.php
@@ -218,4 +218,11 @@ class PrivateKey extends BaseModel
return $query->exists();
}
+
+ public static function cleanupUnusedKeys()
+ {
+ self::all()->each(function ($privateKey) {
+ $privateKey->safeDelete();
+ });
+ }
}
diff --git a/resources/views/security/private-key/index.blade.php b/resources/views/livewire/security/private-key/index.blade.php
similarity index 58%
rename from resources/views/security/private-key/index.blade.php
rename to resources/views/livewire/security/private-key/index.blade.php
index cb1ddf2dc6..104bace2b2 100644
--- a/resources/views/security/private-key/index.blade.php
+++ b/resources/views/livewire/security/private-key/index.blade.php
@@ -1,13 +1,20 @@
-<x-layout>
- <x-slot:title>
- Private Keys | Coolify
- </x-slot>
+<div>
<x-security.navbar />
+
<div class="flex gap-2">
<h2 class="pb-4">Private Keys</h2>
<x-modal-input buttonTitle="+ Add" title="New Private Key">
<livewire:security.private-key.create />
</x-modal-input>
+ <x-modal-confirmation
+ title="Confirm unused SSH Key Deletion?"
+ buttonTitle="Delete unused SSH Keys"
+ isErrorButton
+ submitAction="cleanupUnusedKeys"
+ :actions="['All unused SSH keys (marked with unused) are permanently deleted.']"
+ :confirmWithText="false"
+ :confirmWithPassword="false"
+ />
</div>
<div class="grid gap-2 lg:grid-cols-2">
@forelse ($privateKeys as $key)
@@ -19,11 +26,15 @@
</div>
<div class="box-description">
{{ $key->description }}
+ @if (!$key->isInUse())
+ <span class="inline-flex items-center px-2 py-0.5 rounded text-xs font-medium bg-yellow-400 text-black">Unused</span>
+ @endif
</div>
+
</div>
</a>
@empty
<div>No private keys found.</div>
@endforelse
</div>
-</x-layout>
+</div>
\ No newline at end of file
diff --git a/routes/web.php b/routes/web.php
index 8c3fd805d4..bd5edc0751 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -32,6 +32,7 @@ use App\Livewire\Project\Shared\Logs;
use App\Livewire\Project\Shared\ScheduledTask\Show as ScheduledTaskShow;
use App\Livewire\Project\Show as ProjectShow;
use App\Livewire\Security\ApiTokens;
+use App\Livewire\Security\PrivateKey\Index as SecurityPrivateKeyIndex;
use App\Livewire\Security\PrivateKey\Show as SecurityPrivateKeyShow;
use App\Livewire\Server\Destination\Show as DestinationShow;
use App\Livewire\Server\Index as ServerIndex;
@@ -215,9 +216,7 @@ Route::middleware(['auth', 'verified'])->group(function () {
});
// Route::get('/security', fn () => view('security.index'))->name('security.index');
- Route::get('/security/private-key', fn () => view('security.private-key.index', [
- 'privateKeys' => PrivateKey::ownedByCurrentTeam(['name', 'uuid', 'is_git_related', 'description'])->get(),
- ]))->name('security.private-key.index');
+ Route::get('/security/private-key', SecurityPrivateKeyIndex::class)->name('security.private-key.index');
// Route::get('/security/private-key/new', SecurityPrivateKeyCreate::class)->name('security.private-key.create');
Route::get('/security/private-key/{private_key_uuid}', SecurityPrivateKeyShow::class)->name('security.private-key.show');