-
Notifications
You must be signed in to change notification settings - Fork 86
/
distributionscanner.go
119 lines (105 loc) · 2.75 KB
/
distributionscanner.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
package ubuntu
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/fs"
"runtime/trace"
"strings"
"github.com/quay/zlog"
"github.com/quay/claircore"
"github.com/quay/claircore/indexer"
"github.com/quay/claircore/pkg/tarfs"
)
const (
scannerName = "ubuntu"
scannerVersion = "2"
scannerKind = "distribution"
osReleasePath = `etc/os-release`
lsbReleasePath = `etc/lsb-release`
)
var (
_ indexer.DistributionScanner = (*DistributionScanner)(nil)
_ indexer.VersionedScanner = (*DistributionScanner)(nil)
)
// DistributionScanner implements [indexer.DistributionScanner] looking for Ubuntu distributions.
type DistributionScanner struct{}
// Name implements [scanner.VersionedScanner].
func (*DistributionScanner) Name() string { return scannerName }
// Version implements [scanner.VersionedScanner].
func (*DistributionScanner) Version() string { return scannerVersion }
// Kind implements [scanner.VersionedScanner].
func (*DistributionScanner) Kind() string { return scannerKind }
// Scan implements [indexer.DistributionScanner].
func (ds *DistributionScanner) Scan(ctx context.Context, l *claircore.Layer) ([]*claircore.Distribution, error) {
defer trace.StartRegion(ctx, "Scanner.Scan").End()
ctx = zlog.ContextWithValues(ctx,
"component", "ubuntu/DistributionScanner.Scan",
"version", ds.Version(),
"layer", l.Hash.String())
zlog.Debug(ctx).Msg("start")
defer zlog.Debug(ctx).Msg("done")
rd, err := l.Reader()
if err != nil {
return nil, fmt.Errorf("ubuntu: unable to open layer: %w", err)
}
defer rd.Close()
sys, err := tarfs.New(rd)
if err != nil {
return nil, fmt.Errorf("ubuntu: unable to open layer: %w", err)
}
d, err := findDist(sys)
if err != nil {
return nil, fmt.Errorf("ubuntu: %w", err)
}
if d == nil {
return nil, nil
}
return []*claircore.Distribution{d}, nil
}
func findDist(sys fs.FS) (*claircore.Distribution, error) {
var err error
var b []byte
var verKey, nameKey string
b, err = fs.ReadFile(sys, lsbReleasePath)
if errors.Is(err, nil) {
verKey = `DISTRIB_RELEASE`
nameKey = `DISTRIB_CODENAME`
goto Found
}
b, err = fs.ReadFile(sys, osReleasePath)
if errors.Is(err, nil) {
verKey = `VERSION_ID`
nameKey = `VERSION_CODENAME`
goto Found
}
return nil, nil
Found:
var ver, name string
buf := bytes.NewBuffer(b)
for l, err := buf.ReadString('\n'); len(l) != 0; l, err = buf.ReadString('\n') {
switch {
case errors.Is(err, nil):
case errors.Is(err, io.EOF):
default:
return nil, fmt.Errorf("unexpected error looking for %q: %w", verKey, err)
}
k, v, ok := strings.Cut(l, "=")
if !ok {
continue
}
v = strings.Trim(v, "\"\r\n")
switch k {
case nameKey:
name = v
case verKey:
ver = v
}
}
if name != "" && ver != "" {
return mkDist(ver, name), nil
}
return nil, nil
}