Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit be7359e

Browse files
committed
Improve structure
Signed-off-by: Christian Dupuis <[email protected]>
1 parent 8e5737b commit be7359e

File tree

9 files changed

+267
-216
lines changed

9 files changed

+267
-216
lines changed

commands/cmd.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
222222

223223
if len(*cves) > 0 {
224224
for _, c := range *cves {
225-
query.FormatCve(sb, &c)
225+
types.FormatCve(sb, &c)
226226

227227
if !remediate {
228228
continue
@@ -239,7 +239,7 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
239239
}
240240
}
241241

242-
if rem := query.FormatPackageRemediation(p, c); rem != "" {
242+
if rem := types.FormatPackageRemediation(p, c); rem != "" {
243243
remediation = append(remediation, rem)
244244
}
245245
}
@@ -250,14 +250,14 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
250250
defer s.Stop()
251251
baseImages, index, _ := query.Detect(img, true, workspace, apiKey)
252252
s.Stop()
253-
var baseImage *query.Image
253+
var baseImage *types.Image
254254
if layerIndex <= index && baseImages != nil && len(*baseImages) > 0 {
255255
baseImage = &(*baseImages)[0]
256256

257257
fmt.Println("")
258258
fmt.Println("installed in base image")
259259
fmt.Println("")
260-
fmt.Println(query.FormatImage(baseImage))
260+
fmt.Println(types.FormatImage(baseImage))
261261
}
262262

263263
if baseImage != nil {
@@ -268,13 +268,13 @@ func NewRootCmd(name string, isPlugin bool, dockerCli command.Cli) *cobra.Comman
268268
if aBaseImage != nil && len(*aBaseImage) > 0 {
269269
e := []string{fmt.Sprintf("Update base image\n\nAlternative base images not vulnerable to %s", c.SourceId)}
270270
for _, a := range *aBaseImage {
271-
e = append(e, query.FormatImage(&a))
271+
e = append(e, types.FormatImage(&a))
272272
}
273273
remediation = append(remediation, strings.Join(e, "\n\n"))
274274
}
275275
}
276276

277-
query.FormatRemediation(remediation)
277+
types.FormatRemediation(remediation)
278278
}
279279

280280
os.Exit(1)

query/base.go

Lines changed: 18 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
10735
type ImageQueryResult struct {
10836
Query struct {
109-
Data [][]Image `edn:"data"`
37+
Data [][]types.Image `edn:"data"`
11038
} `edn:"query"`
11139
}
11240

11341
type 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
12654
var 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

registry/read.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,27 @@
1717
package registry
1818

1919
import (
20-
v1 "github.com/google/go-containerregistry/pkg/v1"
20+
"github.com/atomist-skills/go-skill"
2121
"github.com/google/go-containerregistry/pkg/v1/layout"
2222
"github.com/pkg/errors"
2323
)
2424

25-
func ReadImage(path string) (v1.Image, error) {
25+
func ReadImage(name string, path string) (*ImageCache, error) {
26+
skill.Log.Infof("Loading image from %s", path)
2627
index, err := layout.ImageIndexFromPath(path)
2728
if err != nil {
2829
return nil, errors.Wrapf(err, "failed to read manifest index at %s", path)
2930
}
3031
mani, err := index.IndexManifest()
3132
hash := mani.Manifests[0].Digest
32-
return index.Image(hash)
33+
img, _ := index.Image(hash)
34+
skill.Log.Infof("Loaded image")
35+
return &ImageCache{
36+
Name: name,
37+
Path: path,
38+
Image: &img,
39+
ImagePath: path,
40+
Ref: nil,
41+
copy: false,
42+
}, nil
3343
}

0 commit comments

Comments
 (0)