From cfffff96bf73442839c3faff05a3d86afd50277e Mon Sep 17 00:00:00 2001 From: pbao9 Date: Tue, 12 May 2026 15:37:19 +0700 Subject: [PATCH] refactor: dynamicize jumphost credentials from database and restrict CORS allowed origins --- app/Http/Services/vCenter.php | 31 ++++++++++++++++++++++++++----- config/cors.php | 7 ++++++- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/app/Http/Services/vCenter.php b/app/Http/Services/vCenter.php index ff039cf..2f3001b 100644 --- a/app/Http/Services/vCenter.php +++ b/app/Http/Services/vCenter.php @@ -10,6 +10,7 @@ use Exception; use Illuminate\Support\Facades\Http; use Illuminate\Http\Client\ConnectionException; use App\Http\Facade\SignRSA; +use App\Models\InfraManagement; use Illuminate\Support\Str; use phpseclib3\Net\SSH2; @@ -397,17 +398,37 @@ class vCenter } private function LoginAuth() { - try { - $connection = new SSH2('10.10.10.173', 22); + $infra = InfraManagement::where('type', 'jumphost') + ->where('status', true) + ->first(); - if ($connection->login('administrator', 'Thegioiso@123**$$8899TgTk2')) { - $connection->setTimeout(0); // No timeout + if (!$infra) { + Log::error('SSH Login Failed: No active jumphost configuration found in InfraManagement.'); + return false; + } + + $sshHost = $infra->host; + $sshPort = 22; + if (isset($infra->meta_data['port'])) { + $sshPort = $infra->meta_data['port']; + } + + $sshUser = $infra->username; + $sshPass = SignRSA::DecryptFunc($infra->password); + + $connection = new SSH2($sshHost, $sshPort); + + if ($connection->login($sshUser, $sshPass)) { + $connection->setTimeout(0); return $connection; } + + Log::error('SSH Login Failed: Invalid credentials for jumphost.'); return false; } catch (Exception $error) { - return $error->getCode(); + Log::error('SSH Login Failed: ' . $error->getMessage()); + return false; } } diff --git a/config/cors.php b/config/cors.php index 7145735..57f0207 100644 --- a/config/cors.php +++ b/config/cors.php @@ -18,7 +18,12 @@ return [ 'allowed_methods' => ['*'], - 'allowed_origins' => ['*'], + 'allowed_origins' => [ + 'http://localhost:3000', + 'http://localhost:5173', + 'http://localhost:5174', + 'https://portal.tgs.com.vn', + ], 'allowed_origins_patterns' => [],