Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ubuntu: verify dist is actually ubuntu #970

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions ubuntu/distributionscanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

const (
scannerName = "ubuntu"
scannerVersion = "2"
scannerVersion = "3"
scannerKind = "distribution"

osReleasePath = `etc/os-release`
Expand Down Expand Up @@ -74,23 +74,26 @@ func (ds *DistributionScanner) Scan(ctx context.Context, l *claircore.Layer) ([]
func findDist(sys fs.FS) (*claircore.Distribution, error) {
var err error
var b []byte
var verKey, nameKey string
var idKey, verKey, nameKey string

b, err = fs.ReadFile(sys, lsbReleasePath)
if errors.Is(err, nil) {
idKey = `DISTRIB_ID`
verKey = `DISTRIB_RELEASE`
nameKey = `DISTRIB_CODENAME`
goto Found
}
b, err = fs.ReadFile(sys, osReleasePath)
if errors.Is(err, nil) {
idKey = `ID`
verKey = `VERSION_ID`
nameKey = `VERSION_CODENAME`
goto Found
}
return nil, nil

Found:
var hasID bool
var ver, name string
buf := bytes.NewBuffer(b)
for l, err := buf.ReadString('\n'); len(l) != 0; l, err = buf.ReadString('\n') {
Expand All @@ -106,12 +109,22 @@ Found:
}
v = strings.Trim(v, "\"\r\n")
switch k {
case idKey:
if !strings.EqualFold(v, "ubuntu") {
// This is not Ubuntu, so skip it.
return nil, nil
}
hasID = true
case nameKey:
name = v
case verKey:
ver = v
}
}
if !hasID {
// If ID or DISTRIB_ID is missing, just say this is not Ubuntu.
return nil, nil
}
if name != "" && ver != "" {
return mkDist(ver, name), nil
}
Expand Down
Loading