From cc66bcfacc51edbd524c5f88a78cac4b75fe3dc5 Mon Sep 17 00:00:00 2001 From: AlvoBen Date: Sun, 14 Jul 2024 13:03:56 +0300 Subject: [PATCH 1/8] 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 } From 6ee08caa3390d3cce2ab25031da0b61d47f071d7 Mon Sep 17 00:00:00 2001 From: AlvoBen Date: Sun, 14 Jul 2024 14:20:39 +0300 Subject: [PATCH 2/8] change vorpal-latest-version flow --- internal/services/vorpal.go | 46 ++++++++++--------------------------- 1 file changed, 12 insertions(+), 34 deletions(-) diff --git a/internal/services/vorpal.go b/internal/services/vorpal.go index 3cfe84e79..fca24e019 100644 --- a/internal/services/vorpal.go +++ b/internal/services/vorpal.go @@ -44,7 +44,7 @@ func CreateVorpalScanRequest(vorpalParams VorpalScanParams, wrapperParams Vorpal return nil, err } - err = manageVorpalInstallation(vorpalParams, wrapperParams.VorpalWrapper) + err = manageVorpalInstallation(vorpalParams, wrapperParams.VorpalWrapper, wrapperParams) if err != nil { return nil, err } @@ -93,45 +93,23 @@ func executeScan(vorpalWrapper grpcs.VorpalWrapper, filePath string) (*grpcs.Sca return vorpalWrapper.Scan(fileName, sourceCode) } -func manageVorpalInstallation(vorpalParams VorpalScanParams, vorpalWrapper grpcs.VorpalWrapper) error { +func manageVorpalInstallation(vorpalParams VorpalScanParams, vorpalWrapper grpcs.VorpalWrapper, vorpalWrappers VorpalWrappersParam) error { vorpalInstalled, _ := osinstaller.FileExists(vorpalconfig.Params.ExecutableFilePath()) - if !vorpalInstalled { - return installOrUpgradeVorpal() - } - - if vorpalParams.VorpalUpdateVersion { - if err := updateVorpal(vorpalParams, vorpalWrapper); err != nil { + if !vorpalInstalled || vorpalParams.VorpalUpdateVersion { + if err := checkLicense(vorpalParams.IsDefaultAgent, vorpalWrappers); 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() + newInstallation, err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params) + if err != nil { + return err + } + if newInstallation && vorpalParams.VorpalUpdateVersion { + if err := vorpalWrapper.HealthCheck(); err == nil { + _ = vorpalWrapper.ShutDown() + } } } - return nil } From 860b53c78537b27fd46d82c6ededf820ebc647f5 Mon Sep 17 00:00:00 2001 From: AlvoBen Date: Sun, 14 Jul 2024 14:59:32 +0300 Subject: [PATCH 3/8] resolve conversations --- internal/services/vorpal.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/internal/services/vorpal.go b/internal/services/vorpal.go index fca24e019..ce754d438 100644 --- a/internal/services/vorpal.go +++ b/internal/services/vorpal.go @@ -98,16 +98,15 @@ func manageVorpalInstallation(vorpalParams VorpalScanParams, vorpalWrapper grpcs if !vorpalInstalled || vorpalParams.VorpalUpdateVersion { if err := checkLicense(vorpalParams.IsDefaultAgent, vorpalWrappers); err != nil { + _ = vorpalWrapper.ShutDown() return err } newInstallation, err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params) if err != nil { return err } - if newInstallation && vorpalParams.VorpalUpdateVersion { - if err := vorpalWrapper.HealthCheck(); err == nil { - _ = vorpalWrapper.ShutDown() - } + if newInstallation { + _ = vorpalWrapper.ShutDown() } } return nil From 74f7e5af44b207caaf5398d4f593c15c3da717e9 Mon Sep 17 00:00:00 2001 From: AlvoBen Date: Sun, 14 Jul 2024 15:16:17 +0300 Subject: [PATCH 4/8] add bool type to osinstaller to improve readability --- internal/services/osinstaller/os-installer.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/services/osinstaller/os-installer.go b/internal/services/osinstaller/os-installer.go index aa8eff63e..82994a457 100644 --- a/internal/services/osinstaller/os-installer.go +++ b/internal/services/osinstaller/os-installer.go @@ -15,6 +15,8 @@ import ( "github.com/pkg/errors" ) +type NewSuccessfulInstallation bool + // downloadFile Downloads a file from url path func downloadFile(downloadURLPath, filePath string) error { _, fileName := filepath.Split(filePath) @@ -50,7 +52,7 @@ 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) (bool, error) { +func InstallOrUpgrade(installationConfiguration *InstallationConfiguration) (NewSuccessfulInstallation, error) { logger.PrintIfVerbose("Handling RealTime Installation...") if downloadNotNeeded(installationConfiguration) { logger.PrintIfVerbose("RealTime installation already exists and is up to date. Skipping download.") From 2409eed8950a71eda53e086c1e90a564e9ea0dc9 Mon Sep 17 00:00:00 2001 From: AlvoBen Date: Sun, 14 Jul 2024 16:26:34 +0300 Subject: [PATCH 5/8] added unit tests --- internal/services/vorpal.go | 8 ++--- internal/services/vorpal_test.go | 57 ++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/internal/services/vorpal.go b/internal/services/vorpal.go index ce754d438..06be835e4 100644 --- a/internal/services/vorpal.go +++ b/internal/services/vorpal.go @@ -44,7 +44,7 @@ func CreateVorpalScanRequest(vorpalParams VorpalScanParams, wrapperParams Vorpal return nil, err } - err = manageVorpalInstallation(vorpalParams, wrapperParams.VorpalWrapper, wrapperParams) + err = manageVorpalInstallation(vorpalParams, wrapperParams) if err != nil { return nil, err } @@ -93,12 +93,12 @@ func executeScan(vorpalWrapper grpcs.VorpalWrapper, filePath string) (*grpcs.Sca return vorpalWrapper.Scan(fileName, sourceCode) } -func manageVorpalInstallation(vorpalParams VorpalScanParams, vorpalWrapper grpcs.VorpalWrapper, vorpalWrappers VorpalWrappersParam) error { +func manageVorpalInstallation(vorpalParams VorpalScanParams, vorpalWrappers VorpalWrappersParam) error { vorpalInstalled, _ := osinstaller.FileExists(vorpalconfig.Params.ExecutableFilePath()) if !vorpalInstalled || vorpalParams.VorpalUpdateVersion { if err := checkLicense(vorpalParams.IsDefaultAgent, vorpalWrappers); err != nil { - _ = vorpalWrapper.ShutDown() + _ = vorpalWrappers.VorpalWrapper.ShutDown() return err } newInstallation, err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params) @@ -106,7 +106,7 @@ func manageVorpalInstallation(vorpalParams VorpalScanParams, vorpalWrapper grpcs return err } if newInstallation { - _ = vorpalWrapper.ShutDown() + _ = vorpalWrappers.VorpalWrapper.ShutDown() } } return nil diff --git a/internal/services/vorpal_test.go b/internal/services/vorpal_test.go index 4d20f7d03..7e8c622ca 100644 --- a/internal/services/vorpal_test.go +++ b/internal/services/vorpal_test.go @@ -5,6 +5,7 @@ import ( "testing" errorconstants "github.com/checkmarx/ast-cli/internal/constants/errors" + "github.com/checkmarx/ast-cli/internal/wrappers/grpcs" "github.com/checkmarx/ast-cli/internal/wrappers/mock" "github.com/stretchr/testify/assert" ) @@ -66,3 +67,59 @@ func TestCreateVorpalScanRequest_SpecialAgentAndNoLicense_Fail(t *testing.T) { _, err := CreateVorpalScanRequest(vorpalParams, wrapperParams) assert.ErrorContains(t, err, errorconstants.NoVorpalLicense) } + +func TestCreateVorpalScanRequest_EngineRunningAndSpecialAgentAndNoLicense_Fail(t *testing.T) { + port, err := getAvailablePort() + if err != nil { + t.Fatalf("Failed to get available port: %v", err) + } + + vorpalParams := VorpalScanParams{ + FilePath: "data/python-vul-file.py", + VorpalUpdateVersion: true, + IsDefaultAgent: false, + } + + wrapperParams := VorpalWrappersParam{ + JwtWrapper: &mock.JWTMockWrapper{}, + FeatureFlagsWrapper: &mock.FeatureFlagsMockWrapper{}, + VorpalWrapper: grpcs.NewVorpalGrpcWrapper(port), + } + + err = ensureVorpalServiceRunning(wrapperParams, port, vorpalParams) + assert.Nil(t, err) + assert.Nil(t, wrapperParams.VorpalWrapper.HealthCheck()) + + wrapperParams.JwtWrapper = &mock.JWTMockWrapper{AIEnabled: mock.AIProtectionDisabled} + + err = manageVorpalInstallation(vorpalParams, wrapperParams) + assert.ErrorContains(t, err, errorconstants.NoVorpalLicense) + assert.NotNil(t, wrapperParams.VorpalWrapper.HealthCheck()) +} + +func TestCreateVorpalScanRequest_EngineRunningAndDefaultAgentAndNoLicense_Success(t *testing.T) { + port, err := getAvailablePort() + if err != nil { + t.Fatalf("Failed to get available port: %v", err) + } + + vorpalParams := VorpalScanParams{ + FilePath: "data/python-vul-file.py", + VorpalUpdateVersion: true, + IsDefaultAgent: true, + } + + wrapperParams := VorpalWrappersParam{ + JwtWrapper: &mock.JWTMockWrapper{AIEnabled: mock.AIProtectionDisabled}, + FeatureFlagsWrapper: &mock.FeatureFlagsMockWrapper{}, + VorpalWrapper: grpcs.NewVorpalGrpcWrapper(port), + } + + err = ensureVorpalServiceRunning(wrapperParams, port, vorpalParams) + assert.Nil(t, err) + assert.Nil(t, wrapperParams.VorpalWrapper.HealthCheck()) + + err = manageVorpalInstallation(vorpalParams, wrapperParams) + assert.Nil(t, err) + assert.Nil(t, wrapperParams.VorpalWrapper.HealthCheck()) +} From df83b5fe0cbfb9ebfd4101a480067cef0a48254f Mon Sep 17 00:00:00 2001 From: AlvoBen Date: Sun, 14 Jul 2024 16:27:52 +0300 Subject: [PATCH 6/8] added unit tests --- internal/services/vorpal_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/services/vorpal_test.go b/internal/services/vorpal_test.go index 7e8c622ca..ea6deef6a 100644 --- a/internal/services/vorpal_test.go +++ b/internal/services/vorpal_test.go @@ -122,4 +122,5 @@ func TestCreateVorpalScanRequest_EngineRunningAndDefaultAgentAndNoLicense_Succes err = manageVorpalInstallation(vorpalParams, wrapperParams) assert.Nil(t, err) assert.Nil(t, wrapperParams.VorpalWrapper.HealthCheck()) + _ = wrapperParams.VorpalWrapper.ShutDown() } From 13a79d2b0e5ace993157fa6e56fd9e556efeb879 Mon Sep 17 00:00:00 2001 From: tamarleviCm Date: Mon, 15 Jul 2024 15:08:49 +0300 Subject: [PATCH 7/8] change the port configuration --- internal/services/vorpal.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/internal/services/vorpal.go b/internal/services/vorpal.go index 06be835e4..8a5ac2277 100644 --- a/internal/services/vorpal.go +++ b/internal/services/vorpal.go @@ -38,8 +38,6 @@ type VorpalWrappersParam struct { func CreateVorpalScanRequest(vorpalParams VorpalScanParams, wrapperParams VorpalWrappersParam) (*grpcs.ScanResult, error) { var err error - wrapperParams.VorpalWrapper, err = configureVorpalWrapper(wrapperParams.VorpalWrapper) - vorpalWrapper := wrapperParams.VorpalWrapper if err != nil { return nil, err } @@ -49,7 +47,7 @@ func CreateVorpalScanRequest(vorpalParams VorpalScanParams, wrapperParams Vorpal return nil, err } - err = ensureVorpalServiceRunning(wrapperParams, vorpalWrapper.GetPort(), vorpalParams) + err = ensureVorpalServiceRunning(wrapperParams, vorpalParams) if err != nil { return nil, err } @@ -147,14 +145,17 @@ func setConfigPropertyQuiet(propName string, propValue int) { } } -func ensureVorpalServiceRunning(wrappersParam VorpalWrappersParam, port int, vorpalParams VorpalScanParams) error { +func ensureVorpalServiceRunning(wrappersParam VorpalWrappersParam, vorpalParams VorpalScanParams) error { if err := wrappersParam.VorpalWrapper.HealthCheck(); err != nil { err = checkLicense(vorpalParams.IsDefaultAgent, wrappersParam) if err != nil { return err } - - if err := RunVorpalEngine(port); err != nil { + wrappersParam.VorpalWrapper, err = configureVorpalWrapper(wrappersParam.VorpalWrapper) + if err != nil { + return err + } + if err := RunVorpalEngine(wrappersParam.VorpalWrapper.GetPort()); err != nil { return err } From bfeb8042e00418646ec9d6c6e5ebfde88a7dfd7e Mon Sep 17 00:00:00 2001 From: AlvoBen Date: Mon, 15 Jul 2024 18:46:57 +0300 Subject: [PATCH 8/8] fix unit tests --- internal/services/vorpal.go | 7 +------ internal/services/vorpal_test.go | 4 ++-- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/internal/services/vorpal.go b/internal/services/vorpal.go index 8a5ac2277..cc84e9b65 100644 --- a/internal/services/vorpal.go +++ b/internal/services/vorpal.go @@ -37,12 +37,7 @@ type VorpalWrappersParam struct { } func CreateVorpalScanRequest(vorpalParams VorpalScanParams, wrapperParams VorpalWrappersParam) (*grpcs.ScanResult, error) { - var err error - if err != nil { - return nil, err - } - - err = manageVorpalInstallation(vorpalParams, wrapperParams) + err := manageVorpalInstallation(vorpalParams, wrapperParams) if err != nil { return nil, err } diff --git a/internal/services/vorpal_test.go b/internal/services/vorpal_test.go index ea6deef6a..6300e7e3e 100644 --- a/internal/services/vorpal_test.go +++ b/internal/services/vorpal_test.go @@ -86,7 +86,7 @@ func TestCreateVorpalScanRequest_EngineRunningAndSpecialAgentAndNoLicense_Fail(t VorpalWrapper: grpcs.NewVorpalGrpcWrapper(port), } - err = ensureVorpalServiceRunning(wrapperParams, port, vorpalParams) + err = ensureVorpalServiceRunning(wrapperParams, vorpalParams) assert.Nil(t, err) assert.Nil(t, wrapperParams.VorpalWrapper.HealthCheck()) @@ -115,7 +115,7 @@ func TestCreateVorpalScanRequest_EngineRunningAndDefaultAgentAndNoLicense_Succes VorpalWrapper: grpcs.NewVorpalGrpcWrapper(port), } - err = ensureVorpalServiceRunning(wrapperParams, port, vorpalParams) + err = ensureVorpalServiceRunning(wrapperParams, vorpalParams) assert.Nil(t, err) assert.Nil(t, wrapperParams.VorpalWrapper.HealthCheck())