Skip to content

Commit

Permalink
ubuntu: verify dist is actually ubuntu
Browse files Browse the repository at this point in the history
Signed-off-by: RTann <[email protected]>
  • Loading branch information
RTann authored and crozzy committed Jun 23, 2023
1 parent 83548d3 commit 2462d9e
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 207 deletions.
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

0 comments on commit 2462d9e

Please sign in to comment.