forked from quay/claircore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packagescanner.go
189 lines (164 loc) · 4.69 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Package ruby contains components for interrogating ruby packages in
// container layers.
package ruby
import (
"bufio"
"context"
"fmt"
"io/fs"
"path/filepath"
"regexp"
"runtime/trace"
"strings"
"github.com/quay/zlog"
"github.com/quay/claircore"
"github.com/quay/claircore/indexer"
)
var (
gemspecPath = regexp.MustCompile(`.*/specifications/.+\.gemspec`)
// Example gemspec:
//
// Gem::Specification.new do |s|
// s.name = 'example'
// s.version = '0.1.0'
// s.licenses = ['MIT']
// s.summary = "This is an example!"
// s.description = "Much longer explanation of the example!"
// s.authors = ["Ruby Coder"]
// s.email = 'rubycoder@example.com'
// s.files = ["lib/example.rb"]
// s.homepage = 'https://rubygems.org/gems/example'
// s.metadata = { "source_code_uri" => "https://github.com/example/example" }
// end
nameLine = regexp.MustCompile(`^\S+\.\s*name\s*=\s*(?P<name>\S+)$`)
versionLine = regexp.MustCompile(`^\S+\.\s*version\s*=\s*(?P<version>\S+)$`)
)
const (
nameIdx = 1
versionIdx = 1
repository = "rubygems"
)
var (
_ indexer.VersionedScanner = (*Scanner)(nil)
_ indexer.PackageScanner = (*Scanner)(nil)
_ indexer.DefaultRepoScanner = (*Scanner)(nil)
Repository = claircore.Repository{
Name: repository,
URI: "https://rubygems.org/gems/",
}
)
// Scanner implements the scanner.PackageScanner interface.
//
// It looks for files that seem like gems, and looks at the
// metadata recorded there. This type attempts to follow the specs documented
// here: https://guides.rubygems.org/specification-reference/.
//
// The zero value is ready to use.
type Scanner struct{}
// Name implements scanner.VersionedScanner.
func (*Scanner) Name() string { return "ruby" }
// Version implements scanner.VersionedScanner.
func (*Scanner) Version() string { return "1" }
// Kind implements scanner.VersionedScanner.
func (*Scanner) Kind() string { return "package" }
// Scan attempts to find gems and record the package information there.
//
// A return of (nil, nil) is expected if there's nothing found.
func (ps *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", "ruby/Scanner.Scan",
"version", ps.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("ruby: unable to open layer: %w", err)
}
gs, err := gems(ctx, sys)
if err != nil {
return nil, fmt.Errorf("ruby: failed to find packages: %w", err)
}
var ret []*claircore.Package
for _, g := range gs {
f, err := sys.Open(g)
if err != nil {
return nil, fmt.Errorf("ruby: unable to open file: %w", err)
}
var name, version string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if matches := nameLine.FindStringSubmatch(line); matches != nil {
name = trim(matches[nameIdx])
}
if matches := versionLine.FindStringSubmatch(line); matches != nil {
version = trim(matches[versionIdx])
}
}
if err := scanner.Err(); err != nil {
zlog.Warn(ctx).
Err(err).
Str("path", g).
Msg("unable to read metadata, skipping")
continue
}
if name == "" || version == "" {
zlog.Warn(ctx).
Str("path", g).
Msg("couldn't parse name or version, skipping")
continue
}
ret = append(ret, &claircore.Package{
Name: name,
Version: version,
Kind: claircore.BINARY,
PackageDB: g,
RepositoryHint: repository,
})
}
return ret, nil
}
// DefaultRepository implements [indexer.DefaultRepoScanner].
func (Scanner) DefaultRepository(ctx context.Context) *claircore.Repository {
return &Repository
}
func trim(s string) string {
s = strings.TrimSpace(s)
s = strings.TrimSuffix(s, `.freeze`)
return strings.Trim(s, `'"`)
}
func gems(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 gemspecPath.MatchString(p):
ev = ev.Str("kind", "gem")
default:
return nil
}
ev.Msg("found package")
success = true
out = append(out, p)
return nil
})
}