From cc66bcfacc51edbd524c5f88a78cac4b75fe3dc5 Mon Sep 17 00:00:00 2001 From: AlvoBen Date: Sun, 14 Jul 2024 13:03:56 +0300 Subject: [PATCH] change vorpal-latest-version flow --- internal/commands/scarealtime/sca-realtime.go | 2 +- internal/commands/vorpal/vorpal_test.go | 6 ++-- internal/services/osinstaller/os-installer.go | 14 ++++---- internal/services/vorpal.go | 36 ++++++++++++++++--- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/internal/commands/scarealtime/sca-realtime.go b/internal/commands/scarealtime/sca-realtime.go index 4451fbd4e..83e32b854 100644 --- a/internal/commands/scarealtime/sca-realtime.go +++ b/internal/commands/scarealtime/sca-realtime.go @@ -75,7 +75,7 @@ func RunScaRealtime(scaRealTimeWrapper wrappers.ScaRealTimeWrapper) func(*cobra. fmt.Println("Running SCA Realtime...") // Handle SCA Resolver. Checks if it already exists and if it is in the latest version - err = osinstaller.InstallOrUpgrade(&scaconfig.Params) + _, err = osinstaller.InstallOrUpgrade(&scaconfig.Params) if err != nil { return err } diff --git a/internal/commands/vorpal/vorpal_test.go b/internal/commands/vorpal/vorpal_test.go index 42a838b3e..dde020015 100644 --- a/internal/commands/vorpal/vorpal_test.go +++ b/internal/commands/vorpal/vorpal_test.go @@ -20,14 +20,14 @@ func TestInstallOrUpgrade_firstInstallation_Success(t *testing.T) { func firstInstallation() error { os.RemoveAll(vorpalconfig.Params.WorkingDir()) - err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params) + _, err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params) return err } func TestInstallOrUpgrade_installationIsUpToDate_Success(t *testing.T) { err := firstInstallation() assert.NilError(t, err, "Error on first installation of vorpal") - err = osinstaller.InstallOrUpgrade(&vorpalconfig.Params) + _, err = osinstaller.InstallOrUpgrade(&vorpalconfig.Params) assert.NilError(t, err, "Error when not need to upgrade") } @@ -35,7 +35,7 @@ func TestInstallOrUpgrade_installationIsNotUpToDate_Success(t *testing.T) { err := firstInstallation() assert.NilError(t, err, "Error on first installation of vorpal") changeHashFile() - err = osinstaller.InstallOrUpgrade(&vorpalconfig.Params) + _, err = osinstaller.InstallOrUpgrade(&vorpalconfig.Params) assert.NilError(t, err, "Error when need to upgrade") fileExists, _ := osinstaller.FileExists(vorpalconfig.Params.ExecutableFilePath()) assert.Assert(t, fileExists, "Executable file not found") diff --git a/internal/services/osinstaller/os-installer.go b/internal/services/osinstaller/os-installer.go index b508125c4..aa8eff63e 100644 --- a/internal/services/osinstaller/os-installer.go +++ b/internal/services/osinstaller/os-installer.go @@ -50,38 +50,38 @@ func downloadFile(downloadURLPath, filePath string) error { // InstallOrUpgrade Checks the version according to the hash file, // downloads the RealTime installation if the version is not up-to-date, // Extracts the RealTime installation according to the operating system type -func InstallOrUpgrade(installationConfiguration *InstallationConfiguration) error { +func InstallOrUpgrade(installationConfiguration *InstallationConfiguration) (bool, error) { logger.PrintIfVerbose("Handling RealTime Installation...") if downloadNotNeeded(installationConfiguration) { logger.PrintIfVerbose("RealTime installation already exists and is up to date. Skipping download.") - return nil + return false, nil } // Create temporary working directory if not exists err := createWorkingDirectory(installationConfiguration) if err != nil { - return err + return false, err } // Download RealTime installation err = downloadFile(installationConfiguration.DownloadURL, filepath.Join(installationConfiguration.WorkingDir(), installationConfiguration.FileName)) if err != nil { - return err + return false, err } // Download hash file err = downloadHashFile(installationConfiguration.HashDownloadURL, installationConfiguration.HashFilePath()) if err != nil { - return err + return false, err } // Unzip or extract downloaded zip depending on which OS is running err = UnzipOrExtractFiles(installationConfiguration) if err != nil { - return err + return false, err } - return nil + return true, nil } // createWorkingDirectory Creates a working directory to handle Realtime functionality diff --git a/internal/services/vorpal.go b/internal/services/vorpal.go index 08a65eeab..3cfe84e79 100644 --- a/internal/services/vorpal.go +++ b/internal/services/vorpal.go @@ -96,14 +96,42 @@ func executeScan(vorpalWrapper grpcs.VorpalWrapper, filePath string) (*grpcs.Sca func manageVorpalInstallation(vorpalParams VorpalScanParams, vorpalWrapper grpcs.VorpalWrapper) error { vorpalInstalled, _ := osinstaller.FileExists(vorpalconfig.Params.ExecutableFilePath()) - if vorpalParams.VorpalUpdateVersion || !vorpalInstalled { + if !vorpalInstalled { + return installOrUpgradeVorpal() + } + + if vorpalParams.VorpalUpdateVersion { + if err := updateVorpal(vorpalParams, vorpalWrapper); err != nil { + return err + } + } + + return nil +} + +func installOrUpgradeVorpal() error { + if _, err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params); err != nil { + return err + } + return nil +} + +func updateVorpal(vorpalParams VorpalScanParams, vorpalWrapper grpcs.VorpalWrapper) error { + if err := checkLicense(vorpalParams.IsDefaultAgent, VorpalWrappersParam{JwtWrapper: nil, FeatureFlagsWrapper: nil, VorpalWrapper: vorpalWrapper}); err != nil { + return err + } + + newInstallation, err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params) + if err != nil { + return err + } + + if newInstallation { if err := vorpalWrapper.HealthCheck(); err == nil { _ = vorpalWrapper.ShutDown() } - if err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params); err != nil { - return err - } } + return nil }