From 974af959f18ce5acb14c061dfaa16669c28e5033 Mon Sep 17 00:00:00 2001 From: Saeed Vaziry Date: Mon, 7 Oct 2024 16:22:28 +0200 Subject: [PATCH] generate ssh keys command --- app/Console/Commands/GenerateKeysCommand.php | 33 ++++++++++++++++++++ app/Support/helpers.php | 5 +++ 2 files changed, 38 insertions(+) create mode 100644 app/Console/Commands/GenerateKeysCommand.php diff --git a/app/Console/Commands/GenerateKeysCommand.php b/app/Console/Commands/GenerateKeysCommand.php new file mode 100644 index 0000000..a4d30ab --- /dev/null +++ b/app/Console/Commands/GenerateKeysCommand.php @@ -0,0 +1,33 @@ +option('force')) { + $this->error('Keys already exist. Use --force to overwrite.'); + return; + } + + exec('openssl genpkey -algorithm RSA -out ' . $privateKeyPath); + exec('chmod 600 ' . $privateKeyPath); + exec('ssh-keygen -y -f ' . $privateKeyPath . ' > ' . $publicKeyPath); + exec('chown -R ' . get_current_user() . ':' . get_current_user() . ' ' . $privateKeyPath); + exec('chown -R ' . get_current_user() . ':' . get_current_user() . ' ' . $publicKeyPath); + + $this->info('Keys generated successfully.'); + } +} diff --git a/app/Support/helpers.php b/app/Support/helpers.php index 7b1a05c..ab5a41b 100755 --- a/app/Support/helpers.php +++ b/app/Support/helpers.php @@ -5,6 +5,7 @@ use Filament\Notifications\Actions\Action; use Filament\Notifications\Notification; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Route; function generate_public_key($privateKeyPath, $publicKeyPath): void @@ -56,6 +57,10 @@ function convert_time_format($string): string function get_public_key_content(): string { + if (! file_exists(storage_path(config('core.ssh_public_key_name')))) { + Artisan::call('ssh-key:generate --force'); + } + return str(file_get_contents(storage_path(config('core.ssh_public_key_name')))) ->replace("\n", '') ->toString();