-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathdocker.go
262 lines (228 loc) · 7.08 KB
/
docker.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package applier
import (
"fmt"
"strings"
"time"
"github.com/knqyf263/nested"
"github.com/samber/lo"
"github.com/aquasecurity/trivy/pkg/fanal/types"
)
type Config struct {
ContainerConfig containerConfig `json:"container_config"`
History []History
}
type containerConfig struct {
Env []string
}
type History struct {
Created time.Time
CreatedBy string `json:"created_by"`
}
func containsPackage(e types.Package, s []types.Package) bool {
for _, a := range s {
if a.Name == e.Name && a.Version == e.Version && a.Release == e.Release {
return true
}
}
return false
}
func lookupOriginLayerForPkg(pkg types.Package, layers []types.BlobInfo) (string, string, *types.BuildInfo) {
for i, layer := range layers {
for _, info := range layer.PackageInfos {
if containsPackage(pkg, info.Packages) {
return layer.Digest, layer.DiffID, lookupBuildInfo(i, layers)
}
}
}
return "", "", nil
}
// lookupBuildInfo looks up Red Hat content sets from all layers
func lookupBuildInfo(index int, layers []types.BlobInfo) *types.BuildInfo {
if layers[index].BuildInfo != nil {
return layers[index].BuildInfo
}
// Base layer (layers[0]) is missing content sets
// - it needs to be shared from layers[1]
if index == 0 {
if len(layers) > 1 {
return layers[1].BuildInfo
}
return nil
}
// Customer's layers build on top of Red Hat image are also missing content sets
// - it needs to be shared from the last Red Hat's layers which contains content sets
for i := index - 1; i >= 1; i-- {
if layers[i].BuildInfo != nil {
return layers[i].BuildInfo
}
}
return nil
}
func lookupOriginLayerForLib(filePath string, lib types.Package, layers []types.BlobInfo) (string, string) {
for _, layer := range layers {
for _, layerApp := range layer.Applications {
if filePath != layerApp.FilePath {
continue
}
if containsPackage(lib, layerApp.Libraries) {
return layer.Digest, layer.DiffID
}
}
}
return "", ""
}
// ApplyLayers returns the merged layer
// nolint: gocyclo
func ApplyLayers(layers []types.BlobInfo) types.ArtifactDetail {
sep := "/"
nestedMap := nested.Nested{}
var mergedLayer types.ArtifactDetail
for _, layer := range layers {
for _, opqDir := range layer.OpaqueDirs {
opqDir = strings.TrimSuffix(opqDir, sep) // this is necessary so that an empty element is not contribute into the array of the DeleteByString function
_ = nestedMap.DeleteByString(opqDir, sep) // nolint
}
for _, whFile := range layer.WhiteoutFiles {
_ = nestedMap.DeleteByString(whFile, sep) // nolint
}
if layer.OS != nil {
mergedLayer.OS = layer.OS
}
if layer.Repository != nil {
mergedLayer.Repository = layer.Repository
}
// Apply OS packages
for _, pkgInfo := range layer.PackageInfos {
key := fmt.Sprintf("%s/type:ospkg", pkgInfo.FilePath)
nestedMap.SetByString(key, sep, pkgInfo)
}
// Apply language-specific packages
for _, app := range layer.Applications {
key := fmt.Sprintf("%s/type:%s", app.FilePath, app.Type)
nestedMap.SetByString(key, sep, app)
}
// Apply misconfigurations
for _, config := range layer.Misconfigurations {
config.Layer = types.Layer{
Digest: layer.Digest,
DiffID: layer.DiffID,
}
key := fmt.Sprintf("%s/type:config", config.FilePath)
nestedMap.SetByString(key, sep, config)
}
// Apply secrets
for _, secret := range layer.Secrets {
secret.Layer = types.Layer{
Digest: layer.Digest,
DiffID: layer.DiffID,
}
key := fmt.Sprintf("%s/type:secret", secret.FilePath)
nestedMap.SetByString(key, sep, secret)
}
// Apply license files
for _, license := range layer.Licenses {
license.Layer = types.Layer{
Digest: layer.Digest,
DiffID: layer.DiffID,
}
key := fmt.Sprintf("%s/type:license,%s", license.FilePath, license.Type)
nestedMap.SetByString(key, sep, license)
}
// Apply custom resources
for _, customResource := range layer.CustomResources {
key := fmt.Sprintf("%s/custom:%s", customResource.FilePath, customResource.Type)
customResource.Layer = types.Layer{
Digest: layer.Digest,
DiffID: layer.DiffID,
}
nestedMap.SetByString(key, sep, customResource)
}
}
// nolint
_ = nestedMap.Walk(func(keys []string, value interface{}) error {
switch v := value.(type) {
case types.PackageInfo:
mergedLayer.Packages = append(mergedLayer.Packages, v.Packages...)
case types.Application:
mergedLayer.Applications = append(mergedLayer.Applications, v)
case types.Misconfiguration:
mergedLayer.Misconfigurations = append(mergedLayer.Misconfigurations, v)
case types.Secret:
mergedLayer.Secrets = append(mergedLayer.Secrets, v)
case types.LicenseFile:
mergedLayer.Licenses = append(mergedLayer.Licenses, v)
case types.CustomResource:
mergedLayer.CustomResources = append(mergedLayer.CustomResources, v)
}
return nil
})
// Extract dpkg licenses
// The license information is not stored in the dpkg database and in a separate file,
// so we have to merge the license information into the package.
dpkgLicenses := map[string][]string{}
mergedLayer.Licenses = lo.Reject(mergedLayer.Licenses, func(license types.LicenseFile, _ int) bool {
if license.Type != types.LicenseTypeDpkg {
return false
}
// e.g.
// "adduser" => {"GPL-2"}
// "openssl" => {"MIT", "BSD"}
dpkgLicenses[license.PkgName] = lo.Map(license.Findings, func(finding types.LicenseFinding, _ int) string {
return finding.Name
})
// Remove this license in the merged result as it is merged into the package information.
return true
})
if len(mergedLayer.Licenses) == 0 {
mergedLayer.Licenses = nil
}
for i, pkg := range mergedLayer.Packages {
originLayerDigest, originLayerDiffID, buildInfo := lookupOriginLayerForPkg(pkg, layers)
mergedLayer.Packages[i].Layer = types.Layer{
Digest: originLayerDigest,
DiffID: originLayerDiffID,
}
mergedLayer.Packages[i].BuildInfo = buildInfo
// Only debian packages
if licenses, ok := dpkgLicenses[pkg.Name]; ok {
mergedLayer.Packages[i].Licenses = licenses
}
}
for _, app := range mergedLayer.Applications {
for i, lib := range app.Libraries {
originLayerDigest, originLayerDiffID := lookupOriginLayerForLib(app.FilePath, lib, layers)
app.Libraries[i].Layer = types.Layer{
Digest: originLayerDigest,
DiffID: originLayerDiffID,
}
}
}
// Aggregate python/ruby/node.js packages and JAR files
aggregate(&mergedLayer)
return mergedLayer
}
// aggregate merges all packages installed by pip/gem/npm/jar into each application
func aggregate(detail *types.ArtifactDetail) {
var apps []types.Application
aggregatedApps := map[string]*types.Application{
types.PythonPkg: {Type: types.PythonPkg},
types.GemSpec: {Type: types.GemSpec},
types.NodePkg: {Type: types.NodePkg},
types.Jar: {Type: types.Jar},
}
for _, app := range detail.Applications {
a, ok := aggregatedApps[app.Type]
if !ok {
apps = append(apps, app)
continue
}
a.Libraries = append(a.Libraries, app.Libraries...)
}
for _, app := range aggregatedApps {
if len(app.Libraries) > 0 {
apps = append(apps, *app)
}
}
// Overwrite Applications
detail.Applications = apps
}