Skip to content

Commit

Permalink
change vorpal-latest-version flow
Browse files Browse the repository at this point in the history
  • Loading branch information
BenAlvo1 committed Jul 14, 2024
1 parent 40cfc67 commit cc66bcf
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
2 changes: 1 addition & 1 deletion internal/commands/scarealtime/sca-realtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions internal/commands/vorpal/vorpal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ 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")
}

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")
Expand Down
14 changes: 7 additions & 7 deletions internal/services/osinstaller/os-installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 32 additions & 4 deletions internal/services/vorpal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit cc66bcf

Please sign in to comment.