@@ -23,96 +23,24 @@ import (
2323 "io"
2424 "net/http"
2525 "sort"
26- "time"
2726
27+ "github.com/docker/index-cli-plugin/types"
2828 v1 "github.com/google/go-containerregistry/pkg/v1"
2929 "github.com/opencontainers/go-digest"
3030 "github.com/opencontainers/image-spec/identity"
3131 "github.com/pkg/errors"
3232 "olympos.io/encoding/edn"
3333)
3434
35- type IndexImage struct {
36- Digest string `json:"digest"`
37- CreatedAt time.Time `json:"createdAt"`
38- Platform struct {
39- Os string `json:"os"`
40- Arch string `json:"arch"`
41- Variant string `json:"variant"`
42- } `json:"platform"`
43- Layers []struct {
44- Digest string `json:"digest"`
45- Size int `json:"size"`
46- LastModified time.Time `json:"lastModified"`
47- } `json:"layers"`
48- DigestChainId string `json:"digestChainId"`
49- DiffIdChainId string `json:"diffIdChainId"`
50- }
51-
52- type IndexManifestList struct {
53- Name string `json:"name"`
54- Tags []string `json:"tags"`
55- Digest string `json:"digest"`
56- Images []IndexImage `json:"images"`
57- }
58-
59- type ManifestList struct {
60- Digest string `edn:"docker.manifest-list/digest"`
61- Tags []struct {
62- Name string `edn:"docker.tag/name"`
63- } `edn:"docker.manifest-list/tag"`
64- }
65-
66- type Report struct {
67- Total int64 `edn:"vulnerability.report/total"`
68- Critical int64 `edn:"vulnerability.report/critical"`
69- High int64 `edn:"vulnerability.report/high"`
70- Medium int64 `edn:"vulnerability.report/medium"`
71- Low int64 `edn:"vulnerability.report/low"`
72- Unspecified int64 `edn:"vulnerability.report/unspecified"`
73- }
74-
75- type Repository struct {
76- Badge string `edn:"docker.repository/badge"`
77- Host string `edn:"docker.repository/host"`
78- Name string `edn:"docker.repository/name"`
79- SupportedTags []string `edn:"docker.repository/supported-tags"`
80- }
81-
82- type Image struct {
83- TeamId string `edn:"atomist/team-id"`
84- Digest string `edn:"docker.image/digest"`
85- CreatedAt time.Time `edn:"docker.image/created-at"`
86- Tags []string `edn:"docker.image/tags"`
87- Tag []struct {
88- Name string `edn:"docker.tag/name"`
89- } `edn:"docker.image/tag"`
90- ManifestList []ManifestList `edn:"docker.image/manifest-list"`
91- Repository Repository `edn:"docker.image/repository"`
92- File struct {
93- Path string `edn:"git.file/path"`
94- } `edn:"docker.image/file"`
95- Commit struct {
96- Sha string `edn:"git.commit/sha"`
97- Repo struct {
98- Name string `edn:"git.repo/name"`
99- Org struct {
100- Name string `edn:"git.org/name"`
101- } `edn:"git.repo/org"`
102- } `edn:"git.commit/repo"`
103- } `edn:"docker.image/commit"`
104- Report []Report `edn:"vulnerability.report/report"`
105- }
106-
10735type ImageQueryResult struct {
10836 Query struct {
109- Data [][]Image `edn:"data"`
37+ Data [][]types. Image `edn:"data"`
11038 } `edn:"query"`
11139}
11240
11341type RepositoryQueryResult struct {
11442 Query struct {
115- Data [][]Repository `edn:"data"`
43+ Data [][]types. Repository `edn:"data"`
11644 } `edn:"query"`
11745}
11846
@@ -125,7 +53,7 @@ var baseImageCveQuery string
12553//go:embed repository_query.edn
12654var repositoryQuery string
12755
128- func Detect (img * v1.Image , excludeSelf bool , workspace string , apiKey string ) (* []Image , int , error ) {
56+ func Detect (img * v1.Image , excludeSelf bool , workspace string , apiKey string ) (* []types. Image , int , error ) {
12957 digests := make ([]digest.Digest , 0 )
13058 layers , _ := (* img ).Layers ()
13159 for _ , layer := range layers {
@@ -138,7 +66,7 @@ func Detect(img *v1.Image, excludeSelf bool, workspace string, apiKey string) (*
13866 }
13967
14068 chainIds := make ([]digest.Digest , 0 )
141- var images * []Image
69+ var images * []types. Image
14270 var index int
14371 for i := range digests {
14472 chainIds = append (chainIds , digests [i ])
@@ -158,7 +86,7 @@ func Detect(img *v1.Image, excludeSelf bool, workspace string, apiKey string) (*
15886 return images , index , nil
15987}
16088
161- func ForBaseImageInIndex (digest digest.Digest , workspace string , apiKey string ) (* []Image , error ) {
89+ func ForBaseImageInIndex (digest digest.Digest , workspace string , apiKey string ) (* []types. Image , error ) {
16290 url := fmt .Sprintf ("https://api.dso.docker.com/docker-images/chain-ids/%s.json" , digest .String ())
16391
16492 resp , err := http .Get (url )
@@ -167,7 +95,7 @@ func ForBaseImageInIndex(digest digest.Digest, workspace string, apiKey string)
16795 }
16896
16997 if resp .StatusCode == 200 {
170- var manifestList []IndexManifestList
98+ var manifestList []types. IndexManifestList
17199 body , err := io .ReadAll (resp .Body )
172100 if err != nil {
173101 return nil , errors .Wrapf (err , "failed to read response body" )
@@ -176,7 +104,7 @@ func ForBaseImageInIndex(digest digest.Digest, workspace string, apiKey string)
176104 if err != nil {
177105 return nil , errors .Wrapf (err , "failed to unmarshal response body" )
178106 }
179- var ii IndexImage
107+ var ii types. IndexImage
180108 for _ , i := range manifestList [0 ].Images {
181109 if i .DigestChainId == digest .String () || i .DiffIdChainId == digest .String () {
182110 ii = i
@@ -187,22 +115,22 @@ func ForBaseImageInIndex(digest digest.Digest, workspace string, apiKey string)
187115 if err != nil {
188116 return nil , errors .Wrapf (err , "failed to query for respository" )
189117 }
190- image := Image {
118+ image := types. Image {
191119 Digest : ii .Digest ,
192120 CreatedAt : ii .CreatedAt ,
193121 Tags : manifestList [0 ].Tags ,
194122 Repository : * repository ,
195- Report : []Report {{
123+ Report : []types. Report {{
196124 Total : - 1 ,
197125 }},
198126 }
199- return & []Image {image }, nil
127+ return & []types. Image {image }, nil
200128 }
201129
202130 return nil , nil
203131}
204132
205- func ForBaseImageWithoutCve (cve string , name string , img * v1.Image , workspace string , apiKey string ) (* []Image , error ) {
133+ func ForBaseImageWithoutCve (cve string , name string , img * v1.Image , workspace string , apiKey string ) (* []types. Image , error ) {
206134 cf , _ := (* img ).ConfigFile ()
207135 resp , err := query (fmt .Sprintf (baseImageCveQuery , cve , name , cf .OS , cf .Architecture , cf .Variant ), "base_image_cve_query" , workspace , apiKey )
208136
@@ -212,7 +140,7 @@ func ForBaseImageWithoutCve(cve string, name string, img *v1.Image, workspace st
212140 return nil , errors .Wrapf (err , "failed to unmarshal response" )
213141 }
214142 if len (result .Query .Data ) > 0 {
215- images := make ([]Image , 0 )
143+ images := make ([]types. Image , 0 )
216144
217145 for _ , img := range result .Query .Data {
218146 tba := true
@@ -228,8 +156,8 @@ func ForBaseImageWithoutCve(cve string, name string, img *v1.Image, workspace st
228156 }
229157 }
230158 sort .Slice (images , func (i , j int ) bool {
231- itag := Tags (& images [i ])[0 ]
232- jtag := Tags (& images [j ])[0 ]
159+ itag := types . Tags (& images [i ])[0 ]
160+ jtag := types . Tags (& images [j ])[0 ]
233161 both := []string {itag , jtag }
234162 sort .Strings (both )
235163 return both [0 ] == itag
@@ -241,7 +169,7 @@ func ForBaseImageWithoutCve(cve string, name string, img *v1.Image, workspace st
241169}
242170
243171// ForBaseImageInDb returns images with matching digest in :docker.image/blob-digest or :docker.image/diff-chain-id
244- func ForBaseImageInDb (digest digest.Digest , workspace string , apiKey string ) (* []Image , error ) {
172+ func ForBaseImageInDb (digest digest.Digest , workspace string , apiKey string ) (* []types. Image , error ) {
245173 resp , err := query (fmt .Sprintf (baseImageQuery , digest ), "base_image_query" , workspace , apiKey )
246174
247175 var result ImageQueryResult
@@ -250,7 +178,7 @@ func ForBaseImageInDb(digest digest.Digest, workspace string, apiKey string) (*[
250178 return nil , errors .Wrapf (err , "failed to unmarshal response" )
251179 }
252180 if len (result .Query .Data ) > 0 {
253- images := make ([]Image , 0 )
181+ images := make ([]types. Image , 0 )
254182
255183 for _ , img := range result .Query .Data {
256184 tba := true
@@ -271,7 +199,7 @@ func ForBaseImageInDb(digest digest.Digest, workspace string, apiKey string) (*[
271199 }
272200}
273201
274- func ForRepositoryInDb (repo string , workspace string , apiKey string ) (* Repository , error ) {
202+ func ForRepositoryInDb (repo string , workspace string , apiKey string ) (* types. Repository , error ) {
275203 resp , err := query (fmt .Sprintf (repositoryQuery , repo ), "repository_query" , workspace , apiKey )
276204
277205 var result RepositoryQueryResult
0 commit comments