-
Notifications
You must be signed in to change notification settings - Fork 574
/
identify_release.go
192 lines (167 loc) · 5.25 KB
/
identify_release.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
190
191
192
package linux
import (
"fmt"
"io"
"regexp"
"strings"
"github.com/acobaugh/osrelease"
"github.com/google/go-cmp/cmp"
"github.com/anchore/syft/internal"
"github.com/anchore/syft/internal/log"
"github.com/anchore/syft/syft/file"
)
// returns a distro or nil
type parseFunc func(string) (*Release, error)
type parseEntry struct {
path string
fn parseFunc
}
var identityFiles = []parseEntry{
{
// most distros provide a link at this location
path: "/etc/os-release",
fn: parseOsRelease,
},
{
// standard location for rhel & debian distros
path: "/usr/lib/os-release",
fn: parseOsRelease,
},
{
// check for centos:6
path: "/etc/system-release-cpe",
fn: parseSystemReleaseCPE,
},
{
// last ditch effort for determining older centos version distro information
path: "/etc/redhat-release",
fn: parseRedhatRelease,
},
// /////////////////////////////////////////////////////////////////////////////////////////////////////
// IMPORTANT! checking busybox must be last since other distros contain the busybox binary
{
// check for busybox
path: "/bin/busybox",
fn: parseBusyBox,
},
// /////////////////////////////////////////////////////////////////////////////////////////////////////
}
// IdentifyRelease parses distro-specific files to discover and raise linux distribution release details.
func IdentifyRelease(resolver file.Resolver) *Release {
logger := log.Nested("operation", "identify-release")
for _, entry := range identityFiles {
locations, err := resolver.FilesByPath(entry.path)
if err != nil {
logger.WithFields("error", err, "path", entry.path).Trace("unable to get path")
continue
}
for _, location := range locations {
contentReader, err := resolver.FileContentsByLocation(location)
if err != nil {
logger.WithFields("error", err, "path", location.RealPath).Trace("unable to get contents")
continue
}
content, err := io.ReadAll(contentReader)
internal.CloseAndLogError(contentReader, location.AccessPath)
if err != nil {
logger.WithFields("error", err, "path", location.RealPath).Trace("unable to read contents")
continue
}
release, err := entry.fn(string(content))
if err != nil {
logger.WithFields("error", err, "path", location.RealPath).Trace("unable to parse contents")
continue
}
if release != nil {
return release
}
}
}
return nil
}
func parseOsRelease(contents string) (*Release, error) {
values, err := osrelease.ReadString(contents)
if err != nil {
return nil, fmt.Errorf("unable to read os-release file: %w", err)
}
var idLike []string
for _, s := range strings.Split(values["ID_LIKE"], " ") {
s = strings.TrimSpace(s)
if s == "" {
continue
}
idLike = append(idLike, s)
}
r := Release{
PrettyName: values["PRETTY_NAME"],
Name: values["NAME"],
ID: values["ID"],
IDLike: idLike,
Version: values["VERSION"],
VersionID: values["VERSION_ID"],
VersionCodename: values["VERSION_CODENAME"],
BuildID: values["BUILD_ID"],
ImageID: values["IMAGE_ID"],
ImageVersion: values["IMAGE_VERSION"],
Variant: values["VARIANT"],
VariantID: values["VARIANT_ID"],
HomeURL: values["HOME_URL"],
SupportURL: values["SUPPORT_URL"],
BugReportURL: values["BUG_REPORT_URL"],
PrivacyPolicyURL: values["PRIVACY_POLICY_URL"],
CPEName: values["CPE_NAME"],
SupportEnd: values["SUPPORT_END"],
}
// don't allow for empty contents to result in a Release object being created
if cmp.Equal(r, Release{}) {
return nil, nil
}
return &r, nil
}
var busyboxVersionMatcher = regexp.MustCompile(`BusyBox v[\d.]+`)
func parseBusyBox(contents string) (*Release, error) {
matches := busyboxVersionMatcher.FindAllString(contents, -1)
for _, match := range matches {
parts := strings.Split(match, " ")
version := strings.ReplaceAll(parts[1], "v", "")
return simpleRelease(match, "busybox", version, ""), nil
}
return nil, nil
}
// example CPE: cpe:/o:centos:linux:6:GA
var systemReleaseCpeMatcher = regexp.MustCompile(`cpe:\/o:(.*?):.*?:(.*?):.*?$`)
// parseSystemReleaseCPE parses the older centos (6) file to determine distro metadata
func parseSystemReleaseCPE(contents string) (*Release, error) {
matches := systemReleaseCpeMatcher.FindAllStringSubmatch(contents, -1)
for _, match := range matches {
if len(match) < 3 {
continue
}
return simpleRelease(match[1], strings.ToLower(match[1]), match[2], match[0]), nil
}
return nil, nil
}
// example: "CentOS release 6.10 (Final)"
var redhatReleaseMatcher = regexp.MustCompile(`(.*?)\srelease\s(\d\.\d+)`)
// parseRedhatRelease is a fallback parsing method for determining distro information in older redhat versions
func parseRedhatRelease(contents string) (*Release, error) {
matches := redhatReleaseMatcher.FindAllStringSubmatch(contents, -1)
for _, match := range matches {
if len(match) < 3 {
continue
}
return simpleRelease(match[1], strings.ToLower(match[1]), match[2], ""), nil
}
return nil, nil
}
func simpleRelease(prettyName, name, version, cpe string) *Release {
return &Release{
PrettyName: prettyName,
Name: name,
ID: name,
IDLike: []string{name},
Version: version,
VersionID: version,
CPEName: cpe,
}
}