Skip to content

Commit

Permalink
CLI | Change Vorpal vorpal-latest-version flag flow (AST-48376) (#797)
Browse files Browse the repository at this point in the history
* change vorpal-latest-version flow

* change vorpal-latest-version flow

* resolve conversations

* add bool type to osinstaller to improve readability

* added unit tests

* added unit tests

* change the port configuration

* fix unit tests

---------

Co-authored-by: AlvoBen <[email protected]>
Co-authored-by: tamarleviCm <[email protected]>
  • Loading branch information
3 people authored Jul 16, 2024
1 parent 32ca45d commit 71e3ec4
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 28 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
16 changes: 9 additions & 7 deletions internal/services/osinstaller/os-installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -50,38 +52,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) (NewSuccessfulInstallation, 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
35 changes: 18 additions & 17 deletions internal/services/vorpal.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,12 @@ type VorpalWrappersParam struct {
}

func CreateVorpalScanRequest(vorpalParams VorpalScanParams, wrapperParams VorpalWrappersParam) (*grpcs.ScanResult, error) {
var err error
wrapperParams.VorpalWrapper, err = configureVorpalWrapper(wrapperParams.VorpalWrapper)
vorpalWrapper := wrapperParams.VorpalWrapper
err := manageVorpalInstallation(vorpalParams, wrapperParams)
if err != nil {
return nil, err
}

err = manageVorpalInstallation(vorpalParams, wrapperParams.VorpalWrapper)
if err != nil {
return nil, err
}

err = ensureVorpalServiceRunning(wrapperParams, vorpalWrapper.GetPort(), vorpalParams)
err = ensureVorpalServiceRunning(wrapperParams, vorpalParams)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -93,16 +86,21 @@ 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, vorpalWrappers VorpalWrappersParam) error {
vorpalInstalled, _ := osinstaller.FileExists(vorpalconfig.Params.ExecutableFilePath())

if vorpalParams.VorpalUpdateVersion || !vorpalInstalled {
if err := vorpalWrapper.HealthCheck(); err == nil {
_ = vorpalWrapper.ShutDown()
if !vorpalInstalled || vorpalParams.VorpalUpdateVersion {
if err := checkLicense(vorpalParams.IsDefaultAgent, vorpalWrappers); err != nil {
_ = vorpalWrappers.VorpalWrapper.ShutDown()
return err
}
if err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params); err != nil {
newInstallation, err := osinstaller.InstallOrUpgrade(&vorpalconfig.Params)
if err != nil {
return err
}
if newInstallation {
_ = vorpalWrappers.VorpalWrapper.ShutDown()
}
}
return nil
}
Expand Down Expand Up @@ -142,14 +140,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
}

Expand Down
58 changes: 58 additions & 0 deletions internal/services/vorpal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -66,3 +67,60 @@ 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, 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, 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())
_ = wrapperParams.VorpalWrapper.ShutDown()
}

0 comments on commit 71e3ec4

Please sign in to comment.