From 22a1d3882eb59f43544d66732490ad0708adc5e9 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Wed, 3 Apr 2024 13:45:49 +0200 Subject: [PATCH] feat: able to make rsa/ed ssh keys --- app/Livewire/Security/PrivateKey/Create.php | 13 ++++++++++- bootstrap/helpers/shared.php | 22 ++++++++++++++----- .../security/private-key/create.blade.php | 10 +++++++-- .../server/show-private-key.blade.php | 10 ++++----- 4 files changed, 41 insertions(+), 14 deletions(-) diff --git a/app/Livewire/Security/PrivateKey/Create.php b/app/Livewire/Security/PrivateKey/Create.php index cd1c06568..30449b220 100644 --- a/app/Livewire/Security/PrivateKey/Create.php +++ b/app/Livewire/Security/PrivateKey/Create.php @@ -26,7 +26,7 @@ class Create extends Component 'value' => 'private Key', ]; - public function generateNewKey() + public function generateNewRSAKey() { try { $this->rateLimit(10); @@ -37,6 +37,17 @@ public function generateNewKey() return handleError($e, $this); } } + public function generateNewEDKey() + { + try { + $this->rateLimit(10); + $this->name = generate_random_name(); + $this->description = 'Created by Coolify'; + ['private' => $this->value, 'public' => $this->publicKey] = generateSSHKey('ed25519'); + } catch(\Throwable $e) { + return handleError($e, $this); + } + } public function updated($updateProperty) { if ($updateProperty === 'value') { diff --git a/bootstrap/helpers/shared.php b/bootstrap/helpers/shared.php index baee4ce15..458dbae85 100644 --- a/bootstrap/helpers/shared.php +++ b/bootstrap/helpers/shared.php @@ -39,6 +39,7 @@ use Lcobucci\JWT\Signer\Key\InMemory; use Lcobucci\JWT\Signer\Hmac\Sha256; use Lcobucci\JWT\Token\Builder; +use phpseclib3\Crypt\EC; use Poliander\Cron\CronExpression; use Visus\Cuid2\Cuid2; use phpseclib3\Crypt\RSA; @@ -165,13 +166,22 @@ function generate_random_name(?string $cuid = null): string } return Str::kebab("{$generator->getName()}-$cuid"); } -function generateSSHKey() +function generateSSHKey(string $type = 'rsa') { - $key = RSA::createKey(); - return [ - 'private' => $key->toString('PKCS1'), - 'public' => $key->getPublicKey()->toString('OpenSSH', ['comment' => 'coolify-generated-ssh-key']) - ]; + if ($type === 'rsa') { + $key = RSA::createKey(); + return [ + 'private' => $key->toString('PKCS1'), + 'public' => $key->getPublicKey()->toString('OpenSSH', ['comment' => 'coolify-generated-ssh-key']) + ]; + } else if ($type === 'ed25519') { + $key = EC::createKey('Ed25519'); + return [ + 'private' => $key->toString('OpenSSH'), + 'public' => $key->getPublicKey()->toString('OpenSSH', ['comment' => 'coolify-generated-ssh-key']) + ]; + } + throw new Exception('Invalid key type'); } function formatPrivateKey(string $privateKey) { diff --git a/resources/views/livewire/security/private-key/create.blade.php b/resources/views/livewire/security/private-key/create.blade.php index d44e2c470..1bace9f3a 100644 --- a/resources/views/livewire/security/private-key/create.blade.php +++ b/resources/views/livewire/security/private-key/create.blade.php @@ -1,6 +1,12 @@
- {{--
Private Keys are used to connect to your servers without passwords.
--}} - Generate new SSH key for me +
+
Private Keys are used to connect to your servers without passwords.
+
You should not use passphrase protected keys.
+
+
+ Generate new RSA SSH Key + Generate new ED25519 SSH Key +
diff --git a/resources/views/livewire/server/show-private-key.blade.php b/resources/views/livewire/server/show-private-key.blade.php index ac52d601f..62b1c4614 100644 --- a/resources/views/livewire/server/show-private-key.blade.php +++ b/resources/views/livewire/server/show-private-key.blade.php @@ -26,12 +26,12 @@

Choose another Key

@forelse ($privateKeys as $private_key) -
-
-
{{ $private_key->name }}
-
{{ $private_key->description }}
+
+
+
{{ $private_key->name }}
+
{{ $private_key->description }}
+
-
@empty
No private keys found.
@endforelse