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(server): add private key creation to IP server form

Andras Bacsai9b8aeb475840
diff --git a/app/Livewire/Server/New/ByIp.php b/app/Livewire/Server/New/ByIp.php
index f5ea2ae801..a85306f6bf 100644
--- a/app/Livewire/Server/New/ByIp.php
+++ b/app/Livewire/Server/New/ByIp.php
@@ -3,6 +3,7 @@
namespace App\Livewire\Server\New;
use App\Enums\ProxyTypes;
+use App\Models\PrivateKey;
use App\Models\Server;
use App\Models\Team;
use App\Rules\ValidServerIp;
@@ -84,11 +85,51 @@ class ByIp extends Component
]);
}
+ public function getListeners(): array
+ {
+ return [
+ 'privateKeyCreated' => 'handlePrivateKeyCreated',
+ ];
+ }
+
public function setPrivateKey(string $private_key_id)
{
$this->private_key_id = $private_key_id;
}
+ public function generatePrivateKey(string $type): void
+ {
+ try {
+ $this->authorize('create', PrivateKey::class);
+
+ if (! in_array($type, ['ed25519', 'rsa'], true)) {
+ $this->dispatch('error', 'Invalid private key type.');
+
+ return;
+ }
+
+ $keyData = PrivateKey::generateNewKeyPair($type);
+ $privateKey = PrivateKey::createAndStore([
+ 'name' => $keyData['name'],
+ 'description' => $keyData['description'],
+ 'private_key' => $keyData['private_key'],
+ 'team_id' => currentTeam()->id,
+ ]);
+
+ $this->handlePrivateKeyCreated($privateKey->id);
+ $this->dispatch('success', 'Private key created successfully.');
+ } catch (\Throwable $e) {
+ handleError($e, $this);
+ }
+ }
+
+ public function handlePrivateKeyCreated($keyId): void
+ {
+ $this->private_keys = PrivateKey::ownedAndOnlySShKeys()->where('id', '!=', 0)->get();
+ $this->private_key_id = $keyId;
+ $this->resetErrorBag('private_key_id');
+ }
+
public function instantSave()
{
// $this->dispatch('success', 'Application settings updated!');
diff --git a/resources/views/livewire/server/new/by-ip.blade.php b/resources/views/livewire/server/new/by-ip.blade.php
index e41342ca25..39d9ca0fe1 100644
--- a/resources/views/livewire/server/new/by-ip.blade.php
+++ b/resources/views/livewire/server/new/by-ip.blade.php
@@ -16,16 +16,65 @@
<div class="text-xs dark:text-warning text-coollabs ">Non-root user is experimental: <a
class="font-bold underline" target="_blank"
href="https://coolify.io/docs/knowledge-base/server/non-root-user">docs</a>.</div>
- <x-forms.select label="Private Key" id="private_key_id">
- <option disabled>Select a private key</option>
- @foreach ($private_keys as $key)
- @if ($loop->first)
- <option selected value="{{ $key->id }}">{{ $key->name }}</option>
- @else
- <option value="{{ $key->id }}">{{ $key->name }}</option>
- @endif
- @endforeach
- </x-forms.select>
+ <div class="flex items-end gap-2">
+ <div class="grow">
+ <x-forms.select label="Private Key" id="private_key_id">
+ <option disabled>Select a private key</option>
+ @foreach ($private_keys as $key)
+ <option value="{{ $key->id }}">{{ $key->name }}</option>
+ @endforeach
+ </x-forms.select>
+ </div>
+ @can('create', App\Models\PrivateKey::class)
+ <div x-data="{ dropdownOpen: false }" class="relative w-fit" @click.outside="dropdownOpen = false">
+ <x-forms.button isHighlighted @click="dropdownOpen = !dropdownOpen" type="button">
+ + Add
+ <svg class="w-4 h-4 ml-2" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"
+ stroke-width="1.5" stroke="currentColor">
+ <path stroke-linecap="round" stroke-linejoin="round"
+ d="M8.25 15L12 18.75 15.75 15m-7.5-6L12 5.25 15.75 9" />
+ </svg>
+ </x-forms.button>
+
+ <div x-show="dropdownOpen" @click.away="dropdownOpen=false" x-transition:enter="ease-out duration-200"
+ x-transition:enter-start="-translate-y-2" x-transition:enter-end="translate-y-0"
+ class="absolute right-0 top-0 z-50 mt-10 min-w-max" x-cloak>
+ <div
+ class="p-1 mt-1 bg-white border rounded-sm shadow-sm dark:bg-coolgray-200 dark:border-coolgray-300 border-neutral-300">
+ <div class="flex flex-col gap-1">
+ <a class="dropdown-item" wire:click="generatePrivateKey('ed25519')"
+ @click="dropdownOpen = false">
+ <svg class="size-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
+ d="M12 4.5v15m7.5-7.5h-15" />
+ </svg>
+ Generate ED25519
+ </a>
+ <a class="dropdown-item" wire:click="generatePrivateKey('rsa')" @click="dropdownOpen = false">
+ <svg class="size-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
+ d="M12 4.5v15m7.5-7.5h-15" />
+ </svg>
+ Generate RSA
+ </a>
+ <x-modal-input title="Add Private Key Manually">
+ <x-slot:content>
+ <div class="dropdown-item" @click="dropdownOpen = false">
+ <svg class="size-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
+ <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
+ d="M12 4.5v15m7.5-7.5h-15" />
+ </svg>
+ Add manually
+ </div>
+ </x-slot:content>
+ <livewire:security.private-key.create :modal_mode="true" from="server" />
+ </x-modal-input>
+ </div>
+ </div>
+ </div>
+ </div>
+ @endcan
+ </div>
<div class="">
<x-forms.checkbox instantSave type="checkbox" id="is_build_server"
helper="Build servers are used to build your applications, so you cannot deploy applications to it."
diff --git a/tests/Feature/NewServerPrivateKeySelectionTest.php b/tests/Feature/NewServerPrivateKeySelectionTest.php
new file mode 100644
index 0000000000..b1d77266a1
--- /dev/null
+++ b/tests/Feature/NewServerPrivateKeySelectionTest.php
@@ -0,0 +1,93 @@
+<?php
+
+use App\Livewire\Server\New\ByIp;
+use App\Models\InstanceSettings;
+use App\Models\PrivateKey;
+use App\Models\Team;
+use App\Models\User;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Facades\Storage;
+use Livewire\Livewire;
+use Visus\Cuid2\Cuid2;
+
+uses(RefreshDatabase::class);
+
+beforeEach(function () {
+ config(['app.maintenance.driver' => 'file']);
+ Storage::fake('ssh-keys');
+
+ InstanceSettings::forceCreate(['id' => 0, 'is_api_enabled' => true]);
+
+ $this->team = Team::factory()->create();
+ $this->user = User::factory()->create();
+ $this->team->members()->attach($this->user->id, ['role' => 'owner']);
+
+ session(['currentTeam' => $this->team]);
+
+ $this->actingAs($this->user);
+
+ $this->existingPrivateKey = PrivateKey::withoutEvents(fn () => PrivateKey::forceCreate([
+ 'uuid' => (string) new Cuid2,
+ 'name' => 'Existing SSH Key',
+ 'description' => 'Existing SSH Key',
+ 'private_key' => 'test-private-key',
+ 'team_id' => $this->team->id,
+ ]));
+});
+
+it('generates and preselects a new private key without clearing server form data', function () {
+ $component = Livewire::test(ByIp::class, [
+ 'private_keys' => collect([$this->existingPrivateKey]),
+ 'limit_reached' => false,
+ ])
+ ->set('name', 'Production Server')
+ ->set('description', 'Deploy target')
+ ->set('ip', '192.0.2.50')
+ ->set('user', 'deploy.user')
+ ->set('port', 2222)
+ ->set('is_build_server', true)
+ ->call('generatePrivateKey', 'ed25519')
+ ->assertHasNoErrors()
+ ->assertSet('name', 'Production Server')
+ ->assertSet('description', 'Deploy target')
+ ->assertSet('ip', '192.0.2.50')
+ ->assertSet('user', 'deploy.user')
+ ->assertSet('port', 2222)
+ ->assertSet('is_build_server', true);
+
+ $newPrivateKeyId = $component->get('private_key_id');
+
+ expect($newPrivateKeyId)->not->toBe($this->existingPrivateKey->id)
+ ->and(PrivateKey::find($newPrivateKeyId))->not->toBeNull()
+ ->and($component->get('private_keys')->pluck('id'))->toContain($newPrivateKeyId);
+});
+
+it('preselects a manually added private key without clearing server form data', function () {
+ $manualPrivateKey = PrivateKey::withoutEvents(fn () => PrivateKey::forceCreate([
+ 'uuid' => (string) new Cuid2,
+ 'name' => 'Manual SSH Key',
+ 'description' => 'Manual SSH Key',
+ 'private_key' => 'manual-test-private-key',
+ 'team_id' => $this->team->id,
+ ]));
+
+ Livewire::test(ByIp::class, [
+ 'private_keys' => collect([$this->existingPrivateKey]),
+ 'limit_reached' => false,
+ ])
+ ->set('name', 'Production Server')
+ ->set('description', 'Deploy target')
+ ->set('ip', '192.0.2.51')
+ ->set('user', 'deploy.user')
+ ->set('port', 2222)
+ ->set('is_build_server', true)
+ ->call('handlePrivateKeyCreated', $manualPrivateKey->id)
+ ->assertSet('private_key_id', $manualPrivateKey->id)
+ ->assertSet('name', 'Production Server')
+ ->assertSet('description', 'Deploy target')
+ ->assertSet('ip', '192.0.2.51')
+ ->assertSet('user', 'deploy.user')
+ ->assertSet('port', 2222)
+ ->assertSet('is_build_server', true)
+ ->assertSee('Manual SSH Key');
+});