Refactor SSH Keys
This commit is contained in:
@@ -3,22 +3,14 @@
|
||||
namespace App\Livewire\Security\PrivateKey;
|
||||
|
||||
use App\Models\PrivateKey;
|
||||
use DanHarrin\LivewireRateLimiting\WithRateLimiting;
|
||||
use Livewire\Component;
|
||||
use phpseclib3\Crypt\PublicKeyLoader;
|
||||
|
||||
class Create extends Component
|
||||
{
|
||||
use WithRateLimiting;
|
||||
|
||||
public string $name;
|
||||
|
||||
public string $value;
|
||||
|
||||
public string $name = '';
|
||||
public string $value = '';
|
||||
public ?string $from = null;
|
||||
|
||||
public ?string $description = null;
|
||||
|
||||
public ?string $publicKey = null;
|
||||
|
||||
protected $rules = [
|
||||
@@ -26,84 +18,69 @@ class Create extends Component
|
||||
'value' => 'required|string',
|
||||
];
|
||||
|
||||
protected $validationAttributes = [
|
||||
'name' => 'name',
|
||||
'value' => 'private Key',
|
||||
];
|
||||
|
||||
public function generateNewRSAKey()
|
||||
{
|
||||
try {
|
||||
$this->rateLimit(10);
|
||||
$this->name = generate_random_name();
|
||||
$this->description = 'Created by Coolify';
|
||||
['private' => $this->value, 'public' => $this->publicKey] = generateSSHKey();
|
||||
} catch (\Throwable $e) {
|
||||
return handleError($e, $this);
|
||||
}
|
||||
$this->generateNewKey('rsa');
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
$this->generateNewKey('ed25519');
|
||||
}
|
||||
|
||||
public function updated($updateProperty)
|
||||
private function generateNewKey($type)
|
||||
{
|
||||
if ($updateProperty === 'value') {
|
||||
try {
|
||||
$key = PublicKeyLoader::load($this->$updateProperty);
|
||||
$this->publicKey = $key->getPublicKey()->toString('OpenSSH', ['comment' => '']);
|
||||
} catch (\Throwable $e) {
|
||||
$this->publicKey = '';
|
||||
$this->addError('value', 'Invalid private key');
|
||||
}
|
||||
$keyData = PrivateKey::generateNewKeyPair($type);
|
||||
$this->setKeyData($keyData);
|
||||
}
|
||||
|
||||
public function updated($property)
|
||||
{
|
||||
if ($property === 'value') {
|
||||
$this->validatePrivateKey();
|
||||
}
|
||||
$this->validateOnly($updateProperty);
|
||||
}
|
||||
|
||||
public function createPrivateKey()
|
||||
{
|
||||
$this->validate([
|
||||
'name' => 'required|string',
|
||||
'value' => [
|
||||
'required',
|
||||
'string',
|
||||
function ($attribute, $value, $fail) {
|
||||
try {
|
||||
PublicKeyLoader::load($value);
|
||||
} catch (\Throwable $e) {
|
||||
$fail('The private key is invalid.');
|
||||
}
|
||||
},
|
||||
],
|
||||
]);
|
||||
$this->validate();
|
||||
|
||||
try {
|
||||
$this->value = trim($this->value);
|
||||
if (! str_ends_with($this->value, "\n")) {
|
||||
$this->value .= "\n";
|
||||
}
|
||||
$private_key = PrivateKey::create([
|
||||
$privateKey = PrivateKey::createAndStore([
|
||||
'name' => $this->name,
|
||||
'description' => $this->description,
|
||||
'private_key' => $this->value,
|
||||
'private_key' => trim($this->value) . "\n",
|
||||
'team_id' => currentTeam()->id,
|
||||
]);
|
||||
if ($this->from === 'server') {
|
||||
return redirect()->route('dashboard');
|
||||
}
|
||||
|
||||
return redirect()->route('security.private-key.show', ['private_key_uuid' => $private_key->uuid]);
|
||||
return $this->redirectAfterCreation($privateKey);
|
||||
} catch (\Throwable $e) {
|
||||
return handleError($e, $this);
|
||||
}
|
||||
}
|
||||
|
||||
private function setKeyData(array $keyData)
|
||||
{
|
||||
$this->name = $keyData['name'];
|
||||
$this->description = $keyData['description'];
|
||||
$this->value = $keyData['private_key'];
|
||||
$this->publicKey = $keyData['public_key'];
|
||||
}
|
||||
|
||||
private function validatePrivateKey()
|
||||
{
|
||||
$validationResult = PrivateKey::validateAndExtractPublicKey($this->value);
|
||||
$this->publicKey = $validationResult['publicKey'];
|
||||
|
||||
if (!$validationResult['isValid']) {
|
||||
$this->addError('value', 'Invalid private key');
|
||||
}
|
||||
}
|
||||
|
||||
private function redirectAfterCreation(PrivateKey $privateKey)
|
||||
{
|
||||
return $this->from === 'server'
|
||||
? redirect()->route('dashboard')
|
||||
: redirect()->route('security.private-key.show', ['private_key_uuid' => $privateKey->uuid]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user