-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathpredicates-http.go
More file actions
201 lines (166 loc) · 5.94 KB
/
predicates-http.go
File metadata and controls
201 lines (166 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package wrappers
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"strings"
"github.com/checkmarx/ast-cli/internal/logger"
"github.com/checkmarx/ast-cli/internal/params"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
const (
failedToParsePredicates = "Failed to parse predicates response."
invalidScanType = "Invalid scan type %s"
)
type ResultsPredicatesHTTPWrapper struct {
path string
}
func NewResultsPredicatesHTTPWrapper() ResultsPredicatesWrapper {
return &ResultsPredicatesHTTPWrapper{}
}
func (r *ResultsPredicatesHTTPWrapper) GetAllPredicatesForSimilarityID(similarityID, projectID, scannerType string) (
*PredicatesCollectionResponseModel, *WebError, error,
) {
clientTimeout := viper.GetUint(params.ClientTimeoutKey)
var triageAPIPath string
if strings.EqualFold(strings.TrimSpace(scannerType), params.KicsType) || strings.EqualFold(strings.TrimSpace(scannerType), params.IacType) {
triageAPIPath = viper.GetString(params.KicsResultsPredicatesPathKey)
} else if strings.EqualFold(strings.TrimSpace(scannerType), params.SastType) {
triageAPIPath = viper.GetString(params.SastResultsPredicatesPathKey)
} else if strings.EqualFold(strings.TrimSpace(scannerType), params.ScaType) {
return &PredicatesCollectionResponseModel{}, nil, nil
} else {
return nil, nil, errors.Errorf(invalidScanType, scannerType)
}
logger.PrintIfVerbose(fmt.Sprintf("Fetching the predicate history for SimilarityID : %s", similarityID))
r.SetPath(triageAPIPath)
var request = "/" + similarityID + "?project-ids=" + projectID
logger.PrintIfVerbose(fmt.Sprintf("Sending GET request to %s", r.path+request))
resp, err := SendHTTPRequest(http.MethodGet, r.path+request, http.NoBody, true, clientTimeout)
if err != nil {
return nil, nil, err
}
defer func() {
if err == nil {
_ = resp.Body.Close()
}
}()
return handleResponseWithBody(resp, err)
}
func (r *ResultsPredicatesHTTPWrapper) SetPath(newPath string) {
r.path = newPath
}
func (r ResultsPredicatesHTTPWrapper) PredicateSeverityAndState(predicate *PredicateRequest, scanType string) (
*WebError, error,
) {
clientTimeout := viper.GetUint(params.ClientTimeoutKey)
b := [...]PredicateRequest{*predicate}
jsonBytes, err := json.Marshal(b)
if err != nil {
return nil, err
}
var triageAPIPath string
if strings.EqualFold(strings.TrimSpace(scanType), params.SastType) {
triageAPIPath = viper.GetString(params.SastResultsPredicatesPathKey)
} else if strings.EqualFold(strings.TrimSpace(scanType), params.KicsType) || strings.EqualFold(strings.TrimSpace(scanType), params.IacType) {
triageAPIPath = viper.GetString(params.KicsResultsPredicatesPathKey)
} else {
return nil, errors.Errorf(invalidScanType, scanType)
}
logger.PrintIfVerbose(fmt.Sprintf("Sending POST request to %s", triageAPIPath))
logger.PrintIfVerbose(fmt.Sprintf("Request Payload: %s", string(jsonBytes)))
r.SetPath(triageAPIPath)
resp, err := SendHTTPRequest(http.MethodPost, r.path, bytes.NewBuffer(jsonBytes), true, clientTimeout)
if err != nil {
return nil, err
}
logger.PrintIfVerbose(fmt.Sprintf("Response : %s ", resp.Status))
defer func() {
_ = resp.Body.Close()
}()
switch resp.StatusCode {
case http.StatusBadRequest, http.StatusInternalServerError:
return nil, errors.Errorf("Predicate bad request.")
case http.StatusOK, http.StatusCreated:
fmt.Println("Predicate updated successfully.")
return nil, nil
case http.StatusNotModified:
return nil, errors.Errorf("No changes to update.")
case http.StatusForbidden:
return nil, errors.Errorf("No permission to update predicate.")
case http.StatusNotFound:
return nil, errors.Errorf("Predicate not found.")
default:
return nil, errors.Errorf("response status code %d", resp.StatusCode)
}
}
func handleResponseWithBody(resp *http.Response, err error) (*PredicatesCollectionResponseModel, *WebError, error) {
if err != nil {
return nil, nil, err
}
logger.PrintIfVerbose(fmt.Sprintf("Response : %s", resp.Status))
decoder := json.NewDecoder(resp.Body)
defer func() {
_ = resp.Body.Close()
}()
switch resp.StatusCode {
case http.StatusBadRequest, http.StatusInternalServerError:
errorModel := WebError{}
err = decoder.Decode(&errorModel)
if err != nil {
return responsePredicateParsingFailed(err)
}
return nil, &errorModel, nil
case http.StatusOK:
model := PredicatesCollectionResponseModel{}
err = decoder.Decode(&model)
if err != nil {
return responsePredicateParsingFailed(err)
}
return &model, nil, nil
case http.StatusForbidden:
return nil, nil, errors.Errorf("No permission to show predicate.")
case http.StatusNotFound:
return nil, nil, errors.Errorf("Predicate not found.")
default:
return nil, nil, errors.Errorf("response status code %d", resp.StatusCode)
}
}
func responsePredicateParsingFailed(err error) (*PredicatesCollectionResponseModel, *WebError, error) {
return nil, nil, errors.Wrapf(err, failedToParsePredicates)
}
type CustomStatesHTTPWrapper struct {
path string
}
func NewCustomStatesHTTPWrapper() CustomStatesWrapper {
return &CustomStatesHTTPWrapper{
path: viper.GetString(params.CustomStatesAPIPathKey),
}
}
func (c *CustomStatesHTTPWrapper) GetAllCustomStates(includeDeleted bool) ([]CustomState, error) {
clientTimeout := viper.GetUint(params.ClientTimeoutKey)
if c.path == "" {
return nil, errors.New("CustomStatesAPIPathKey is not set")
}
requestURL := c.path
if includeDeleted {
requestURL += "?include-deleted=true"
}
logger.PrintIfVerbose(fmt.Sprintf("Fetching custom states from: %s", requestURL))
resp, err := SendHTTPRequest(http.MethodGet, requestURL, http.NoBody, true, clientTimeout)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, errors.Errorf("Failed to fetch custom states. HTTP status: %d", resp.StatusCode)
}
var states []CustomState
err = json.NewDecoder(resp.Body).Decode(&states)
if err != nil {
return nil, errors.Wrap(err, "Failed to parse custom states response")
}
return states, nil
}