From 3d48364862bda12a647d4c4b30f2ce32f69b9191 Mon Sep 17 00:00:00 2001 From: Adrien <99400874+hubadr@users.noreply.github.com> Date: Wed, 18 May 2022 17:10:00 +0200 Subject: [PATCH] Fix project config reset when preset is set (#3782) --- cmd/checkmarxExecuteScan.go | 1 - pkg/checkmarx/checkmarx.go | 45 +++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/cmd/checkmarxExecuteScan.go b/cmd/checkmarxExecuteScan.go index 8dd5e9c95..e84aa1c79 100644 --- a/cmd/checkmarxExecuteScan.go +++ b/cmd/checkmarxExecuteScan.go @@ -653,7 +653,6 @@ func setPresetForProject(sys checkmarx.System, projectID, presetIDValue int, pro if err != nil { return errors.Wrapf(err, "updating configuration of project %v failed", projectName) } - log.Entry().Debugf("Configuration of project %v updated", projectName) return nil } diff --git a/pkg/checkmarx/checkmarx.go b/pkg/checkmarx/checkmarx.go index 0d0a6cf99..e29c50cf4 100644 --- a/pkg/checkmarx/checkmarx.go +++ b/pkg/checkmarx/checkmarx.go @@ -111,6 +111,31 @@ type Project struct { Link Link `json:"link"` } +// ScanSettings - scan settings at project level +type ScanSettings struct { + Project ProjectLink `json:"project"` + Preset PresetLink `json:"preset"` + EngineConfiguration EngineConfigurationLink `json:"engineConfiguration" ` +} + +// ProjectLink - project link found in ScanSettings response +type ProjectLink struct { + ProjectID int `json:"id"` + Link Link `json:"link"` +} + +// PresetLink - preset link found in ScanSettings response +type PresetLink struct { + PresetID int `json:"id"` + Link Link `json:"link"` +} + +// EngineConfigurationLink - engine configuration link found in ScanSettings response +type EngineConfigurationLink struct { + EngineConfigurationID int `json:"id"` + Link Link `json:"link"` +} + // Team - Team Structure type Team struct { ID json.RawMessage `json:"id"` @@ -476,6 +501,23 @@ func (sys *SystemInstance) GetPresets() []Preset { // UpdateProjectConfiguration updates the configuration of the project addressed by projectID func (sys *SystemInstance) UpdateProjectConfiguration(projectID int, presetID int, engineConfigurationID string) error { engineConfigID, _ := strconv.Atoi(engineConfigurationID) + + var projectScanSettings ScanSettings + header := http.Header{} + header.Set("Content-Type", "application/json") + data, err := sendRequest(sys, http.MethodGet, fmt.Sprintf("/sast/scanSettings/%v", projectID), nil, header) + if err != nil { + // if an error happens, try to update the config anyway + sys.logger.Warnf("Failed to fetch scan settings of project %v: %s", projectID, err) + } else { + // Check if the current project config needs to be updated + json.Unmarshal(data, &projectScanSettings) + if projectScanSettings.Preset.PresetID == presetID && projectScanSettings.EngineConfiguration.EngineConfigurationID == engineConfigID { + sys.logger.Debugf("Project configuration does not need to be updated") + return nil + } + } + jsonData := map[string]interface{}{ "projectId": projectID, "presetId": presetID, @@ -487,12 +529,11 @@ func (sys *SystemInstance) UpdateProjectConfiguration(projectID int, presetID in return errors.Wrapf(err, "error marshalling project data") } - header := http.Header{} - header.Set("Content-Type", "application/json") _, err = sendRequest(sys, http.MethodPost, "/sast/scanSettings", bytes.NewBuffer(jsonValue), header) if err != nil { return errors.Wrapf(err, "request to checkmarx system failed") } + sys.logger.Debugf("Project configuration updated") return nil }