-
Notifications
You must be signed in to change notification settings - Fork 85
/
packagescanner.go
172 lines (150 loc) · 4.33 KB
/
packagescanner.go
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
// Package nodejs contains components for interrogating nodejs packages in
// container layers.
package nodejs
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io/fs"
"path/filepath"
"runtime/trace"
"strings"
"github.com/quay/zlog"
"github.com/Masterminds/semver"
"github.com/quay/claircore"
"github.com/quay/claircore/indexer"
)
const repository = "npm"
var (
_ indexer.VersionedScanner = (*Scanner)(nil)
_ indexer.PackageScanner = (*Scanner)(nil)
_ indexer.DefaultRepoScanner = (*Scanner)(nil)
Repository = claircore.Repository{
Name: repository,
URI: "https://www.npmjs.com/",
}
)
// Scanner implements the scanner.PackageScanner interface.
//
// It looks for files that seem like package.json and looks at the
// metadata recorded there.
//
// The zero value is ready to use.
type Scanner struct{}
// Name implements scanner.VersionedScanner.
func (*Scanner) Name() string { return "nodejs" }
// Version implements scanner.VersionedScanner.
func (*Scanner) Version() string { return "2" }
// Kind implements scanner.VersionedScanner.
func (*Scanner) Kind() string { return "package" }
// packageJSON represents the fields of a package.json file
// useful for package scanning.
//
// See https://docs.npmjs.com/files/package.json/ for more details
// about the format of package.json files.
type packageJSON struct {
Name string `json:"name"`
Version string `json:"version"`
}
// Scan attempts to find package.json files and record the package
// information there.
//
// A return of (nil, nil) is expected if there's nothing found.
func (s *Scanner) Scan(ctx context.Context, layer *claircore.Layer) ([]*claircore.Package, error) {
defer trace.StartRegion(ctx, "Scanner.Scan").End()
trace.Log(ctx, "layer", layer.Hash.String())
ctx = zlog.ContextWithValues(ctx,
"component", "nodejs/Scanner.Scan",
"version", s.Version(),
"layer", layer.Hash.String())
zlog.Debug(ctx).Msg("start")
defer zlog.Debug(ctx).Msg("done")
if err := ctx.Err(); err != nil {
return nil, err
}
sys, err := layer.FS()
if err != nil {
return nil, fmt.Errorf("nodejs: unable to open layer: %w", err)
}
pkgs, err := packages(ctx, sys)
if err != nil {
return nil, fmt.Errorf("nodejs: failed to find packages: %w", err)
}
if len(pkgs) == 0 {
return nil, nil
}
ret := make([]*claircore.Package, 0, len(pkgs))
var invalidPkgs []string
for _, p := range pkgs {
f, err := sys.Open(p)
if err != nil {
return nil, fmt.Errorf("nodejs: unable to open file %q: %w", p, err)
}
var pkgJSON packageJSON
err = json.NewDecoder(bufio.NewReader(f)).Decode(&pkgJSON)
if err != nil {
invalidPkgs = append(invalidPkgs, p)
continue
}
pkg := &claircore.Package{
Name: pkgJSON.Name,
Version: pkgJSON.Version,
Kind: claircore.BINARY,
PackageDB: "nodejs:" + p,
Filepath: p,
RepositoryHint: repository,
}
if sv, err := semver.NewVersion(pkgJSON.Version); err == nil {
pkg.NormalizedVersion = claircore.FromSemver(sv)
} else {
zlog.Info(ctx).
Str("package", pkg.Name).
Str("version", pkg.Version).
Msg("invalid semantic version")
}
ret = append(ret, pkg)
}
if len(invalidPkgs) > 0 {
zlog.Debug(ctx).Strs("paths", invalidPkgs).Msg("unable to decode package.json, skipping")
}
return ret, nil
}
func packages(ctx context.Context, sys fs.FS) (out []string, err error) {
return out, fs.WalkDir(sys, ".", func(p string, d fs.DirEntry, err error) error {
ev := zlog.Debug(ctx).
Str("file", p)
var success bool
defer func() {
if !success {
ev.Discard().Send()
}
}()
switch {
case err != nil:
return err
case !d.Type().IsRegular():
// Should we chase symlinks with the correct name?
return nil
case strings.HasPrefix(filepath.Base(p), ".wh."):
return nil
case !strings.Contains(p, "node_modules/"):
// Only bother with package.json files within node_modules/ directories.
// See https://docs.npmjs.com/cli/v7/configuring-npm/folders#node-modules
// for more information.
return nil
case strings.HasSuffix(p, "/package.json"):
ev = ev.Str("kind", "package.json")
default:
return nil
}
ev.Msg("found package")
success = true
out = append(out, p)
return nil
})
}
// DefaultRepository implements [indexer.DefaultRepoScanner].
func (*Scanner) DefaultRepository(_ context.Context) *claircore.Repository {
return &Repository
}