@@ -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+
127140func (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 }
0 commit comments