-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
artifact.go
188 lines (157 loc) · 5.3 KB
/
artifact.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
package types
import (
"time"
"github.com/samber/lo"
)
type OS struct {
Family OSType
Name string
Eosl bool `json:"EOSL,omitempty"`
// This field is used for enhanced security maintenance programs such as Ubuntu ESM, Debian Extended LTS.
Extended bool `json:"extended,omitempty"`
}
func (o *OS) Detected() bool {
return o.Family != ""
}
// Merge merges OS version and enhanced security maintenance programs
func (o *OS) Merge(newOS OS) {
if lo.IsEmpty(newOS) {
return
}
switch {
// OLE also has /etc/redhat-release and it detects OLE as RHEL by mistake.
// In that case, OS must be overwritten with the content of /etc/oracle-release.
// There is the same problem between Debian and Ubuntu.
case o.Family == RedHat, o.Family == Debian:
*o = newOS
default:
if o.Family == "" {
o.Family = newOS.Family
}
if o.Name == "" {
o.Name = newOS.Name
}
// Ubuntu has ESM program: https://ubuntu.com/security/esm
// OS version and esm status are stored in different files.
// We have to merge OS version after parsing these files.
if o.Extended || newOS.Extended {
o.Extended = true
}
}
}
type Repository struct {
Family OSType `json:",omitempty"`
Release string `json:",omitempty"`
}
type Layer struct {
Digest string `json:",omitempty"`
DiffID string `json:",omitempty"`
CreatedBy string `json:",omitempty"`
}
type PackageInfo struct {
FilePath string
Packages Packages
}
type Application struct {
// e.g. bundler and pipenv
Type LangType
// Lock files have the file path here, while each package metadata do not have
FilePath string `json:",omitempty"`
// Packages is a list of lang-specific packages
Packages Packages
}
type File struct {
Type string
Path string
Content []byte
}
// ArtifactInfo is stored in cache
type ArtifactInfo struct {
SchemaVersion int
Architecture string
Created time.Time
DockerVersion string
OS string
// Misconfiguration holds misconfiguration in container image config
Misconfiguration *Misconfiguration `json:",omitempty"`
// Secret holds secrets in container image config such as environment variables
Secret *Secret `json:",omitempty"`
// HistoryPackages are packages extracted from RUN instructions
HistoryPackages Packages `json:",omitempty"`
}
// BlobInfo is stored in cache
type BlobInfo struct {
SchemaVersion int
// Layer information
Digest string `json:",omitempty"`
DiffID string `json:",omitempty"`
CreatedBy string `json:",omitempty"`
OpaqueDirs []string `json:",omitempty"`
WhiteoutFiles []string `json:",omitempty"`
// Analysis result
OS OS `json:",omitempty"`
Repository *Repository `json:",omitempty"`
PackageInfos []PackageInfo `json:",omitempty"`
Applications []Application `json:",omitempty"`
Misconfigurations []Misconfiguration `json:",omitempty"`
Secrets []Secret `json:",omitempty"`
Licenses []LicenseFile `json:",omitempty"`
// Red Hat distributions have build info per layer.
// This information will be embedded into packages when applying layers.
// ref. https://redhat-connect.gitbook.io/partner-guide-for-adopting-red-hat-oval-v2/determining-common-platform-enumeration-cpe
BuildInfo *BuildInfo `json:",omitempty"`
// CustomResources hold analysis results from custom analyzers.
// It is for extensibility and not used in OSS.
CustomResources []CustomResource `json:",omitempty"`
}
// ArtifactDetail represents the analysis result.
type ArtifactDetail struct {
OS OS `json:",omitempty"`
Repository *Repository `json:",omitempty"`
Packages Packages `json:",omitempty"`
Applications []Application `json:",omitempty"`
Misconfigurations []Misconfiguration `json:",omitempty"`
Secrets []Secret `json:",omitempty"`
Licenses []LicenseFile `json:",omitempty"`
// ImageConfig has information from container image config
ImageConfig ImageConfigDetail
// CustomResources hold analysis results from custom analyzers.
// It is for extensibility and not used in OSS.
CustomResources []CustomResource `json:",omitempty"`
}
// ImageConfigDetail has information from container image config
type ImageConfigDetail struct {
// Packages are packages extracted from RUN instructions in history
Packages []Package `json:",omitempty"`
// Misconfiguration holds misconfigurations in container image config
Misconfiguration *Misconfiguration `json:",omitempty"`
// Secret holds secrets in container image config
Secret *Secret `json:",omitempty"`
}
// ToBlobInfo is used to store a merged layer in cache.
func (a *ArtifactDetail) ToBlobInfo() BlobInfo {
return BlobInfo{
SchemaVersion: BlobJSONSchemaVersion,
OS: a.OS,
Repository: a.Repository,
PackageInfos: []PackageInfo{
{
FilePath: "merged", // Set a dummy file path
Packages: a.Packages,
},
},
Applications: a.Applications,
Misconfigurations: a.Misconfigurations,
Secrets: a.Secrets,
Licenses: a.Licenses,
CustomResources: a.CustomResources,
}
}
// CustomResource holds the analysis result from a custom analyzer.
// It is for extensibility and not used in OSS.
type CustomResource struct {
Type string
FilePath string
Layer Layer
Data any
}