44 "bytes"
55 "encoding/json"
66 "fmt"
7+ "io"
78 "net/http"
89 "strings"
910
@@ -26,18 +27,21 @@ func NewResultsPredicatesHTTPWrapper() ResultsPredicatesWrapper {
2627 return & ResultsPredicatesHTTPWrapper {}
2728}
2829
29- func (r * ResultsPredicatesHTTPWrapper ) ScaPredicateResult (vulnerabilityDetails []string , projectId string ) (* ScaPredicateResult , error ) {
30+ func (r * ResultsPredicatesHTTPWrapper ) ScaPredicateResult (vulnerabilityDetails []string , projectID string ) (* ScaPredicateResult , error ) {
3031 clientTimeout := viper .GetUint (params .ClientTimeoutKey )
3132 r .SetPath (viper .GetString (params .ScaResultsPredicatesPathEnv ))
3233 var request = "/entity-profile/search"
3334 logger .PrintIfVerbose (fmt .Sprintf ("Sending POST request to %s" , r .path + request ))
3435
3536 scaPredicateRequest := make (map [string ]interface {})
3637 for _ , vulnerability := range vulnerabilityDetails {
37- vulnerabilityKeyVal := strings .SplitN (vulnerability , "=" , 2 )
38+ vulnerabilityKeyVal := strings .Split (vulnerability , "=" )
39+ if len (vulnerabilityKeyVal ) != params .KeyValuePairSize {
40+ return nil , errors .Errorf ("Invalid vulnerability details format: %s" , vulnerability )
41+ }
3842 scaPredicateRequest [vulnerabilityKeyVal [0 ]] = vulnerabilityKeyVal [1 ]
3943 }
40- scaPredicateRequest ["projectId" ] = projectId
44+ scaPredicateRequest ["projectId" ] = projectID
4145 scaPredicateRequest ["actionType" ] = params .ChangeState
4246 jsonBody , err := json .Marshal (scaPredicateRequest )
4347 if err != nil {
@@ -55,7 +59,6 @@ func (r *ResultsPredicatesHTTPWrapper) ScaPredicateResult(vulnerabilityDetails [
5559 if resp .StatusCode != http .StatusOK {
5660 return nil , errors .Errorf ("Failed to get SCA predicate result." )
5761 }
58- fmt .Println ("HM Response: " , resp .Body )
5962 decoder := json .NewDecoder (resp .Body )
6063 var scaPredicates ScaPredicateResult
6164 err = decoder .Decode (& scaPredicates )
@@ -147,27 +150,16 @@ func (r ResultsPredicatesHTTPWrapper) PredicateSeverityAndState(predicate interf
147150 * WebError , error ,
148151) {
149152 clientTimeout := viper .GetUint (params .ClientTimeoutKey )
150- var predicateModel interface {}
151- if ! strings .EqualFold (strings .TrimSpace (scanType ), params .ScaType ) {
152- predicateModel = []interface {}{predicate }
153- } else {
154- predicateModel = predicate
155- }
153+
154+ predicateModel := preparePredicateModel (predicate , scanType )
156155 jsonBytes , err := json .Marshal (predicateModel )
157156 if err != nil {
158157 return nil , err
159158 }
160- var triageAPIPath string
161- if strings .EqualFold (strings .TrimSpace (scanType ), params .SastType ) {
162- triageAPIPath = viper .GetString (params .SastResultsPredicatesPathKey )
163- } else if strings .EqualFold (strings .TrimSpace (scanType ), params .KicsType ) || strings .EqualFold (strings .TrimSpace (scanType ), params .IacType ) {
164- triageAPIPath = viper .GetString (params .KicsResultsPredicatesPathKey )
165- } else if strings .EqualFold (strings .TrimSpace (scanType ), params .ScsType ) {
166- triageAPIPath = viper .GetString (params .ScsResultsWritePredicatesPathKey )
167- } else if strings .EqualFold (strings .TrimSpace (scanType ), params .ScaType ) {
168- triageAPIPath = viper .GetString (params .ScaResultsPredicatesPathEnv )
169- } else {
170- return nil , errors .Errorf (invalidScanType , scanType )
159+
160+ triageAPIPath , err := getTriageAPIPath (scanType )
161+ if err != nil {
162+ return nil , err
171163 }
172164
173165 logger .PrintIfVerbose (fmt .Sprintf ("Sending POST request to %s" , triageAPIPath ))
@@ -186,38 +178,71 @@ func (r ResultsPredicatesHTTPWrapper) PredicateSeverityAndState(predicate interf
186178 _ = resp .Body .Close ()
187179 }()
188180
189- // in case of ne/pne when mandatory comment arent provided, cli is not transforming error message
181+ if err := checkMandatoryCommentError (resp .Body , scanType ); err != nil {
182+ return nil , err
183+ }
184+
185+ return nil , handlePredicateStatusCode (resp .StatusCode )
186+ }
187+
188+ func preparePredicateModel (predicate interface {}, scanType string ) interface {} {
189+ if ! strings .EqualFold (strings .TrimSpace (scanType ), params .ScaType ) {
190+ return []interface {}{predicate }
191+ }
192+ return predicate
193+ }
194+
195+ func getTriageAPIPath (scanType string ) (string , error ) {
196+ ScanType := strings .ToLower (strings .TrimSpace (scanType ))
197+
198+ switch ScanType {
199+ case strings .ToLower (params .SastType ):
200+ return viper .GetString (params .SastResultsPredicatesPathKey ), nil
201+ case strings .ToLower (params .KicsType ), strings .ToLower (params .IacType ):
202+ return viper .GetString (params .KicsResultsPredicatesPathKey ), nil
203+ case strings .ToLower (params .ScsType ):
204+ return viper .GetString (params .ScsResultsWritePredicatesPathKey ), nil
205+ case strings .ToLower (params .ScaType ):
206+ return viper .GetString (params .ScaResultsPredicatesPathEnv ), nil
207+ default :
208+ return "" , errors .Errorf (invalidScanType , scanType )
209+ }
210+ }
211+
212+ func checkMandatoryCommentError (body io.ReadCloser , scanType string ) error {
190213 responseMap := make (map [string ]interface {})
191- if err := json .NewDecoder (resp . Body ).Decode (& responseMap ); err != nil {
192- if scanType != params .ScaType { // for sca, we are not getting any response in the response body, so we are not logging the error
214+ if err := json .NewDecoder (body ).Decode (& responseMap ); err != nil {
215+ if scanType != params .ScaType {
193216 logger .PrintIfVerbose (fmt .Sprintf ("failed to read the response, %v" , err .Error ()))
194217 }
195- } else {
196- if val , ok := responseMap ["code" ].(float64 ); ok {
197- if val == 4002 && responseMap ["message" ] != nil {
198- if errMsg , ok := responseMap ["message" ].(string ); ok {
199- if errMsg == "A comment is required to make changes to the result state" {
200- return nil , errors .Errorf (errMsg )
201- }
202- }
218+ return nil
219+ }
220+
221+ if val , ok := responseMap ["code" ].(float64 ); ok && val == 4002 {
222+ if errMsg , ok := responseMap ["message" ].(string ); ok {
223+ if errMsg == "A comment is required to make changes to the result state" {
224+ return errors .Errorf (errMsg )
203225 }
204226 }
205227 }
228+ return nil
229+ }
206230
207- switch resp .StatusCode {
208- case http .StatusBadRequest , http .StatusInternalServerError :
209- return nil , errors .Errorf ("Predicate bad request." )
231+ func handlePredicateStatusCode (statusCode int ) error {
232+ switch statusCode {
210233 case http .StatusOK , http .StatusCreated :
211234 fmt .Println ("Predicate updated successfully." )
212- return nil , nil
235+ return nil
213236 case http .StatusNotModified :
214- return nil , errors .Errorf ("No changes to update." )
237+ return errors .Errorf ("No changes to update." )
215238 case http .StatusForbidden :
216- return nil , errors .Errorf ("No permission to update predicate." )
239+ return errors .Errorf ("No permission to update predicate." )
217240 case http .StatusNotFound :
218- return nil , errors .Errorf ("Predicate not found." )
241+ return errors .Errorf ("Predicate not found." )
242+ case http .StatusBadRequest , http .StatusInternalServerError :
243+ return errors .Errorf ("Predicate bad request." )
219244 default :
220- return nil , errors .Errorf ("response status code %d" , resp . StatusCode )
245+ return errors .Errorf ("response status code %d" , statusCode )
221246 }
222247}
223248
0 commit comments