Skip to content

Commit 43bb70f

Browse files
committed
update
1 parent d225215 commit 43bb70f

File tree

1 file changed

+18
-44
lines changed

1 file changed

+18
-44
lines changed

src/ProxmoxNodeVm.php

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2886,57 +2886,30 @@ public function detachDisk(string $node, int $vmid, string $disk, bool $type = f
28862886
* @return array
28872887
* @throws Exception
28882888
*/
2889-
public function generateSSHKey(
2890-
string $keyName,
2891-
string $keyPath = null,
2892-
string $type = 'ed25519',
2893-
int $bits = 4096
2894-
): array
2889+
public function generateSSHKey($path)
28952890
{
2896-
// Set default path if not provided
2897-
if ($keyPath === null) {
2898-
$keyPath = storage_path('app/ssh');
2899-
}
2891+
$name = preg_replace('/[^a-z0-9]+/i', '_', strtolower(request()->name));
2892+
$keyPath = $path;
2893+
$keyName = $name;
2894+
$fullPath = "{$keyPath}/{$keyName}";
29002895

2901-
// Create directory if it doesn't exist
2902-
if (!is_dir($keyPath)) {
2896+
// Ensure directory exists
2897+
if (!file_exists($keyPath)) {
29032898
mkdir($keyPath, 0700, true);
29042899
}
29052900

2906-
$privateKeyPath = "{$keyPath}/{$keyName}";
2907-
$publicKeyPath = "{$keyPath}/{$keyName}.pub";
2908-
2909-
// Generate key pair
2910-
$config = [
2911-
'private_key_type' => $type === 'rsa' ? OPENSSL_KEYTYPE_RSA : OPENSSL_KEYTYPE_EC,
2912-
'private_key_bits' => $bits,
2913-
];
2914-
2915-
if ($type === 'ed25519') {
2916-
$config['curve_name'] = 'ed25519';
2917-
}
2918-
2919-
$key = openssl_pkey_new($config);
2920-
if (!$key) {
2921-
throw new Exception('Failed to generate SSH key pair: ' . openssl_error_string());
2922-
}
2923-
2924-
// Export private key
2925-
openssl_pkey_export($key, $privateKey);
2926-
file_put_contents($privateKeyPath, $privateKey);
2927-
chmod($privateKeyPath, 0600);
2901+
// Execute ssh-keygen
2902+
exec("ssh-keygen -t rsa -b 2048 -f {$fullPath} -N 'pass'");
29282903

2929-
// Export public key
2930-
$keyDetails = openssl_pkey_get_details($key);
2931-
$publicKey = "{$type} {$keyDetails['key']} {$keyName}";
2932-
file_put_contents($publicKeyPath, $publicKey);
2933-
chmod($publicKeyPath, 0644);
29342904

2935-
return [
2936-
'private_key_path' => $privateKeyPath,
2937-
'public_key_path' => $publicKeyPath,
2938-
'public_key' => $publicKey
2905+
$response = [
2906+
'private_key_path' => $fullPath,
2907+
'private_key' => file_get_contents($fullPath),
2908+
'public_key_path' => $fullPath . '.pub',
2909+
'public_key' => file_get_contents($fullPath . '.pub')
29392910
];
2911+
2912+
return ResponseHelper::generate(true,'SSH key generated successfully', $response);
29402913
}
29412914

29422915
/**
@@ -2957,12 +2930,13 @@ public function attachSSHKey(string $node, int $vmid, string $publicKey): array
29572930
throw new Exception("VM {$vmid} not found on node {$node}");
29582931
}
29592932

2933+
29602934
// Clean up the public key
29612935
$publicKey = trim($publicKey);
29622936

29632937
// Set SSH key in cloud-init config
29642938
return $this->setVMConfig($node, $vmid, [
2965-
'sshkeys' => urlencode($publicKey)
2939+
'sshkeys' => $publicKey
29662940
]);
29672941
}
29682942

0 commit comments

Comments
 (0)