|
| 1 | +package chainguard |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "github.com/quay/zlog" |
| 9 | + "io/fs" |
| 10 | + "runtime/trace" |
| 11 | + |
| 12 | + "github.com/quay/claircore" |
| 13 | + "github.com/quay/claircore/indexer" |
| 14 | + "github.com/quay/claircore/osrelease" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + scannerName = "chainguard" |
| 19 | + scannerVersion = "1" |
| 20 | + scannerKind = "distribution" |
| 21 | + |
| 22 | + chainguard = `chainguard` |
| 23 | + wolfi = `wolfi` |
| 24 | +) |
| 25 | + |
| 26 | +var ( |
| 27 | + _ indexer.DistributionScanner = (*DistributionScanner)(nil) |
| 28 | + _ indexer.VersionedScanner = (*DistributionScanner)(nil) |
| 29 | +) |
| 30 | + |
| 31 | +// DistributionScanner attempts to discover if a layer |
| 32 | +// displays characteristics of a chainguard or wolfi distribution. |
| 33 | +type DistributionScanner struct{} |
| 34 | + |
| 35 | +// Name implements scanner.VersionedScanner. |
| 36 | +func (*DistributionScanner) Name() string { return scannerName } |
| 37 | + |
| 38 | +// Version implements scanner.VersionedScanner. |
| 39 | +func (*DistributionScanner) Version() string { return scannerVersion } |
| 40 | + |
| 41 | +// Kind implements scanner.VersionedScanner. |
| 42 | +func (*DistributionScanner) Kind() string { return scannerKind } |
| 43 | + |
| 44 | +// Scan will inspect the layer for an os-release |
| 45 | +// and determine if it represents a chainguard or wolfi release. |
| 46 | +// |
| 47 | +// If the file is not found, or the file does not represent a chainguard nor wolfi release, |
| 48 | +// (nil, nil) is returned. |
| 49 | +func (s *DistributionScanner) Scan(ctx context.Context, l *claircore.Layer) ([]*claircore.Distribution, error) { |
| 50 | + defer trace.StartRegion(ctx, "Scanner.Scan").End() |
| 51 | + ctx = zlog.ContextWithValues(ctx, |
| 52 | + "component", "chainguard/DistributionScanner.Scan", |
| 53 | + "version", s.Version(), |
| 54 | + "layer", l.Hash.String()) |
| 55 | + zlog.Debug(ctx).Msg("start") |
| 56 | + defer zlog.Debug(ctx).Msg("done") |
| 57 | + |
| 58 | + sys, err := l.FS() |
| 59 | + if err != nil { |
| 60 | + return nil, fmt.Errorf("chainguard: unable to open layer: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + b, err := fs.ReadFile(sys, osrelease.Path) |
| 64 | + switch { |
| 65 | + case errors.Is(err, nil): |
| 66 | + m, err := osrelease.Parse(ctx, bytes.NewReader(b)) |
| 67 | + if err != nil { |
| 68 | + return nil, err |
| 69 | + } |
| 70 | + |
| 71 | + switch id := m[`ID`]; id { |
| 72 | + case chainguard, wolfi: |
| 73 | + return []*claircore.Distribution{ |
| 74 | + { |
| 75 | + Name: m[`NAME`], |
| 76 | + DID: id, |
| 77 | + // Neither chainguard nor wolfi images are considered to be "versioned". |
| 78 | + // Explicitly set the version to the empty string for clarity. |
| 79 | + Version: "", |
| 80 | + PrettyName: m[`PRETTY_NAME`], |
| 81 | + }, |
| 82 | + }, nil |
| 83 | + default: |
| 84 | + // This is neither chainguard nor wolfi. |
| 85 | + return nil, nil |
| 86 | + } |
| 87 | + case errors.Is(err, fs.ErrNotExist): |
| 88 | + // os-release file must exist in chainguard and wolfi images. |
| 89 | + return nil, nil |
| 90 | + default: |
| 91 | + return nil, err |
| 92 | + } |
| 93 | +} |
0 commit comments