Skip to content

Commit 0269dc8

Browse files
authored
Changes vulns retrieve endpoint to vulnDirect API (#11)
* Changes vulns retrieve endpoint to vulnDirect API * Updates digest of test image * Check image scan-result status before making a call to vulnDirect API
1 parent a81d346 commit 0269dc8

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

pkg/secure/client.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,30 +124,55 @@ func (s *client) checkErrorInSecureAPI(response *http.Response, body []byte) err
124124
return errors.New(secureError.Message)
125125
}
126126

127+
type imageScanResultResponse struct {
128+
Results []*imageScanResult `json:"results"`
129+
}
130+
131+
type imageScanResult struct {
132+
AnalysisStatus string `json:"analysisStatus"`
133+
AnalyzedAt int `json:"analyzedAt"`
134+
CreatedAt int `json:"createdAt"`
135+
ImageDigest string `json:"imageDigest"`
136+
ImageId string `json:"imageId"`
137+
FullTag string `json:"fullTag"`
138+
}
139+
127140
func (s *client) GetVulnerabilities(shaDigest string) (VulnerabilityReport, error) {
141+
var checkScanResultResponse imageScanResultResponse
128142
var result VulnerabilityReport
129143

130144
response, body, err := s.doRequest(
131145
http.MethodGet,
132-
fmt.Sprintf("/api/scanning/v1/anchore/images/%s/vuln/all", shaDigest),
146+
fmt.Sprintf("/api/scanning/v1/results?filter=%s&limit=%d", shaDigest, 1),
133147
nil)
134148
if err != nil {
135149
return result, err
136150
}
137151

138152
if err = s.checkErrorInSecureAPI(response, body); err != nil {
139-
if response.StatusCode == http.StatusNotFound {
140-
if err.Error() == "image not found in DB" {
141-
return result, ErrImageNotFound
142-
}
143-
144-
if strings.HasPrefix(err.Error(), "image is not analyzed - analysis_status:") {
145-
return result, ErrVulnerabiltyReportNotReady
146-
}
147-
}
153+
return result, err
154+
}
155+
if err = json.Unmarshal(body, &checkScanResultResponse); err != nil {
156+
return result, err
157+
}
158+
159+
if len(checkScanResultResponse.Results) == 0 {
160+
return result, ErrImageNotFound
161+
} else if img := checkScanResultResponse.Results[0]; img.AnalysisStatus != "analyzed" {
162+
return result, ErrVulnerabiltyReportNotReady
163+
}
164+
165+
response, body, err = s.doRequest(
166+
http.MethodGet,
167+
fmt.Sprintf("/api/scanning/v1/images/%s/vulnDirect/all?includeVulnExceptions=%t", shaDigest, false),
168+
nil)
169+
if err != nil {
148170
return result, err
149171
}
150172

173+
if err = s.checkErrorInSecureAPI(response, body); err != nil {
174+
return result, err
175+
}
151176
if err = json.Unmarshal(body, &result); err != nil {
152177
return result, err
153178
}

pkg/secure/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var _ = Describe("Sysdig Secure Client", func() {
4444

4545
Context("when retrieving vulnerabilities for an image", func() {
4646
It("gets the report for a SHA", func() {
47-
response, _ := client.GetVulnerabilities("sha256:fda6b046981f5dab88aad84c6cebed4e47a0d6ad1c8ff7f58b5f0e6a95a5b2c1")
47+
response, _ := client.GetVulnerabilities("sha256:1e331e745ddf2b295d93f04c1477489fce34bf9ac26f4ab964f14e9dbe4e2dc4")
4848

4949
Expect(response).NotTo(Equal(secure.VulnerabilityReport{}))
5050
Expect(len(response.Vulnerabilities)).To(BeNumerically(">", 0))
@@ -108,7 +108,7 @@ var _ = Describe("Sysdig Secure Client", func() {
108108

109109
Context("when getting an image information", func() {
110110
It("returns the image information", func() {
111-
image, _ := client.GetImage("sha256:7cd23a94051e17b191b5cc5b4682ed9f3ece26b8283dc39b8a5b894462cec696")
111+
image, _ := client.GetImage("sha256:1e331e745ddf2b295d93f04c1477489fce34bf9ac26f4ab964f14e9dbe4e2dc4")
112112

113113
Expect(image).NotTo(Equal(secure.ScanResponse{}))
114114
})

pkg/secure/model.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ type ErrorResponse struct {
5151

5252
type VulnerabilityReport struct {
5353
ImageDigest string `json:"imageDigest"`
54-
VulnerabilityType string `json:"vulnerability_type"`
55-
Vulnerabilities []*Vulnerability `json:"vulnerabilities"`
54+
VulnerabilityType string `json:"vtype"`
55+
Vulnerabilities []*Vulnerability `json:"vulns"`
5656
}
5757

5858
type Vulnerability struct {
@@ -72,12 +72,12 @@ type Vulnerability struct {
7272
}
7373

7474
type NVDData struct {
75-
ID string `json:"id"`
76-
CSSV2 *CSS `json:"css_v2"`
77-
CSSV3 *CSS `json:"css_v3"`
75+
ID string `json:"id"`
76+
CVSSV2 *CVSS `json:"cvss_v2"`
77+
CVSSV3 *CVSS `json:"cvss_v3"`
7878
}
7979

80-
type CSS struct {
80+
type CVSS struct {
8181
BaseScore float32 `json:"base_score"`
8282
ExploitabilityScore float32 `json:"exploitability_score"`
8383
ImpactScore float32 `json:"impact_score"`

0 commit comments

Comments
 (0)