-
Notifications
You must be signed in to change notification settings - Fork 14
/
base.go
411 lines (340 loc) · 14 KB
/
base.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* Copyright 2018-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tomcat
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/paketo-buildpacks/libpak/sbom"
"github.com/buildpacks/libcnb"
"github.com/heroku/color"
"github.com/paketo-buildpacks/libpak"
"github.com/paketo-buildpacks/libpak/bard"
"github.com/paketo-buildpacks/libpak/crush"
"github.com/paketo-buildpacks/libpak/sherpa"
)
type Base struct {
AccessLoggingDependency libpak.BuildpackDependency
ApplicationPath string
BuildpackPath string
ConfigurationResolver libpak.ConfigurationResolver
ContextPath string
DependencyCache libpak.DependencyCache
ExternalConfigurationDependency *libpak.BuildpackDependency
LayerContributor libpak.LayerContributor
LifecycleDependency libpak.BuildpackDependency
LoggingDependency libpak.BuildpackDependency
Logger bard.Logger
WarFilesExist bool
}
func NewBase(
applicationPath string,
buildpackPath string,
configurationResolver libpak.ConfigurationResolver,
contextPath string,
accessLoggingDependency libpak.BuildpackDependency,
externalConfigurationDependency *libpak.BuildpackDependency,
lifecycleDependency libpak.BuildpackDependency,
loggingDependency libpak.BuildpackDependency,
cache libpak.DependencyCache,
warFilesExist bool,
) (Base, []libcnb.BOMEntry) {
dependencies := []libpak.BuildpackDependency{accessLoggingDependency, lifecycleDependency, loggingDependency}
if externalConfigurationDependency != nil {
dependencies = append(dependencies, *externalConfigurationDependency)
}
b := Base{
AccessLoggingDependency: accessLoggingDependency,
ApplicationPath: applicationPath,
BuildpackPath: buildpackPath,
ConfigurationResolver: configurationResolver,
ContextPath: contextPath,
DependencyCache: cache,
ExternalConfigurationDependency: externalConfigurationDependency,
LayerContributor: libpak.NewLayerContributor("Apache Tomcat Support", map[string]interface{}{
"context-path": contextPath,
"dependencies": dependencies,
}, libcnb.LayerTypes{
Launch: true,
}),
LifecycleDependency: lifecycleDependency,
LoggingDependency: loggingDependency,
WarFilesExist: warFilesExist,
}
var bomEntries []libcnb.BOMEntry
var entry libcnb.BOMEntry
entry = accessLoggingDependency.AsBOMEntry()
entry.Metadata["layer"] = b.Name()
entry.Launch = true
bomEntries = append(bomEntries, entry)
entry = lifecycleDependency.AsBOMEntry()
entry.Metadata["layer"] = b.Name()
entry.Launch = true
bomEntries = append(bomEntries, entry)
entry = loggingDependency.AsBOMEntry()
entry.Metadata["layer"] = b.Name()
entry.Launch = true
bomEntries = append(bomEntries, entry)
if externalConfigurationDependency != nil {
entry = externalConfigurationDependency.AsBOMEntry()
entry.Metadata["layer"] = b.Name()
entry.Launch = true
bomEntries = append(bomEntries, entry)
}
return b, bomEntries
}
func (b Base) Contribute(layer libcnb.Layer) (libcnb.Layer, error) {
b.LayerContributor.Logger = b.Logger
var syftArtifacts []sbom.SyftArtifact
return b.LayerContributor.Contribute(layer, func() (libcnb.Layer, error) {
if err := b.ContributeConfiguration(layer); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to contribute configuration\n%w", err)
}
if err := b.ContributeAccessLogging(layer); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to contribute access logging\n%w", err)
}
if syftArtifact, err := b.AccessLoggingDependency.AsSyftArtifact(); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to get Syft Artifact for dependency: %s, \n%w", b.AccessLoggingDependency.Name, err)
} else {
syftArtifacts = append(syftArtifacts, syftArtifact)
}
if err := b.ContributeLifecycle(layer); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to contribute lifecycle\n%w", err)
}
if syftArtifact, err := b.LifecycleDependency.AsSyftArtifact(); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to get Syft Artifact for dependency: %s, \n%w", b.LifecycleDependency.Name, err)
} else {
syftArtifacts = append(syftArtifacts, syftArtifact)
}
if err := b.ContributeLogging(layer); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to contribute logging\n%w", err)
}
if syftArtifact, err := b.LoggingDependency.AsSyftArtifact(); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to get Syft Artifact for dependency: %s, \n%w", b.LoggingDependency.Name, err)
} else {
syftArtifacts = append(syftArtifacts, syftArtifact)
}
if b.ExternalConfigurationDependency != nil {
if err := b.ContributeExternalConfiguration(layer); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to contribute external configuration\n%w", err)
}
if syftArtifact, err := b.ExternalConfigurationDependency.AsSyftArtifact(); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to get Syft Artifact for dependency: %s, \n%w", b.ExternalConfigurationDependency.Name, err)
} else {
syftArtifacts = append(syftArtifacts, syftArtifact)
}
}
file := filepath.Join(layer.Path, "temp")
if err := os.MkdirAll(file, 0700); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to create directory %s\n%w", file, err)
}
file = filepath.Join(layer.Path, "webapps")
if b.WarFilesExist {
if err := os.Symlink(b.ApplicationPath, file); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to create symlink from %s to %s\n%w", b.ApplicationPath, file, err)
}
if err := b.explodeWarFiles(); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to explode war files in %s\n%w", b.ApplicationPath, err)
}
} else {
if err := os.MkdirAll(file, 0755); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to create directory %s\n%w", file, err)
}
file = filepath.Join(layer.Path, "webapps", b.ContextPath)
b.Logger.Headerf("Mounting application at %s", b.ContextPath)
if err := os.Symlink(b.ApplicationPath, file); err != nil {
return libcnb.Layer{}, fmt.Errorf("unable to create symlink from %s to %s\n%w", b.ApplicationPath, file, err)
}
}
environmentPropertySourceDisabled := b.ConfigurationResolver.ResolveBool("BP_TOMCAT_ENV_PROPERTY_SOURCE_DISABLED")
if !environmentPropertySourceDisabled {
layer.LaunchEnvironment.Default("CATALINA_OPTS", "-Dorg.apache.tomcat.util.digester.PROPERTY_SOURCE=org.apache.tomcat.util.digester.EnvironmentPropertySource")
}
layer.LaunchEnvironment.Default("CATALINA_BASE", layer.Path)
layer.LaunchEnvironment.Default("CATALINA_TMPDIR", "/tmp")
if err := b.writeDependencySBOM(layer, syftArtifacts); err != nil {
return libcnb.Layer{}, err
}
return layer, nil
})
}
func (b Base) ContributeAccessLogging(layer libcnb.Layer) error {
b.Logger.Header(color.BlueString("%s %s", b.AccessLoggingDependency.Name, b.AccessLoggingDependency.Version))
artifact, err := b.DependencyCache.Artifact(b.AccessLoggingDependency)
if err != nil {
return fmt.Errorf("unable to get dependency %s\n%w", b.AccessLoggingDependency.ID, err)
}
defer artifact.Close()
b.Logger.Bodyf("Copying to %s/lib", layer.Path)
file := filepath.Join(layer.Path, "lib", filepath.Base(b.AccessLoggingDependency.URI))
if err := sherpa.CopyFile(artifact, file); err != nil {
return fmt.Errorf("unable to copy %s to %s\n%w", filepath.Base(b.AccessLoggingDependency.URI), file, err)
}
return nil
}
func (b Base) ContributeConfiguration(layer libcnb.Layer) error {
file := filepath.Join(layer.Path, "conf")
if err := os.MkdirAll(file, 0755); err != nil {
return fmt.Errorf("unable to create directory %s\n%w", file, err)
}
b.Logger.Bodyf("Copying context.xml to %s/conf", layer.Path)
file = filepath.Join(b.BuildpackPath, "resources", "context.xml")
in, err := os.Open(file)
if err != nil {
return fmt.Errorf("unable to open %s\n%w", file, err)
}
defer in.Close()
file = filepath.Join(layer.Path, "conf", "context.xml")
if err := sherpa.CopyFile(in, file); err != nil {
return fmt.Errorf("unable to copy %s to %s\n%w", in.Name(), file, err)
}
b.Logger.Bodyf("Copying logging.properties to %s/conf", layer.Path)
file = filepath.Join(b.BuildpackPath, "resources", "logging.properties")
in, err = os.Open(file)
if err != nil {
return fmt.Errorf("unable to open %s\n%w", file, err)
}
defer in.Close()
file = filepath.Join(layer.Path, "conf", "logging.properties")
if err := sherpa.CopyFile(in, file); err != nil {
return fmt.Errorf("unable to copy %s to %s\n%w", in.Name(), file, err)
}
b.Logger.Bodyf("Copying server.xml to %s/conf", layer.Path)
file = filepath.Join(b.BuildpackPath, "resources", "server.xml")
in, err = os.Open(file)
if err != nil {
return fmt.Errorf("unable to open %s\n%w", file, err)
}
defer in.Close()
file = filepath.Join(layer.Path, "conf", "server.xml")
if err := sherpa.CopyFile(in, file); err != nil {
return fmt.Errorf("unable to copy %s to %s\n%w", in.Name(), file, err)
}
b.Logger.Bodyf("Copying web.xml to %s/conf", layer.Path)
file = filepath.Join(b.BuildpackPath, "resources", "web.xml")
in, err = os.Open(file)
if err != nil {
return fmt.Errorf("unable to open %s\n%w", file, err)
}
defer in.Close()
file = filepath.Join(layer.Path, "conf", "web.xml")
if err := sherpa.CopyFile(in, file); err != nil {
return fmt.Errorf("unable to copy %s to %s\n%w", in.Name(), file, err)
}
return nil
}
func (b Base) ContributeExternalConfiguration(layer libcnb.Layer) error {
b.Logger.Header(color.BlueString("%s %s", b.ExternalConfigurationDependency.Name, b.ExternalConfigurationDependency.Version))
artifact, err := b.DependencyCache.Artifact(*b.ExternalConfigurationDependency)
if err != nil {
return fmt.Errorf("unable to get dependency %s\n%w", b.ExternalConfigurationDependency.ID, err)
}
defer artifact.Close()
b.Logger.Bodyf("Expanding to %s", layer.Path)
c := 0
if s, ok := b.ConfigurationResolver.Resolve("BP_TOMCAT_EXT_CONF_STRIP"); ok {
if c, err = strconv.Atoi(s); err != nil {
return fmt.Errorf("unable to parse %s to integer\n%w", s, err)
}
}
if err := crush.ExtractTarGz(artifact, layer.Path, c); err != nil {
return fmt.Errorf("unable to expand external configuration\n%w", err)
}
return nil
}
func (b Base) ContributeLifecycle(layer libcnb.Layer) error {
b.Logger.Header(color.BlueString("%s %s", b.LifecycleDependency.Name, b.LifecycleDependency.Version))
artifact, err := b.DependencyCache.Artifact(b.LifecycleDependency)
if err != nil {
return fmt.Errorf("unable to get dependency %s\n%w", b.LifecycleDependency.ID, err)
}
defer artifact.Close()
b.Logger.Bodyf("Copying to %s/lib", layer.Path)
file := filepath.Join(layer.Path, "lib", filepath.Base(b.LifecycleDependency.URI))
if err := sherpa.CopyFile(artifact, file); err != nil {
return fmt.Errorf("unable to copy %s to %s\n%w", filepath.Base(b.LifecycleDependency.URI), file, err)
}
return nil
}
func (b Base) ContributeLogging(layer libcnb.Layer) error {
b.Logger.Header(color.BlueString("%s %s", b.LoggingDependency.Name, b.LoggingDependency.Version))
artifact, err := b.DependencyCache.Artifact(b.LoggingDependency)
if err != nil {
return fmt.Errorf("unable to get dependency %s\n%w", b.LoggingDependency.ID, err)
}
defer artifact.Close()
b.Logger.Bodyf("Copying to %s/bin", layer.Path)
file := filepath.Join(layer.Path, "bin", filepath.Base(b.LoggingDependency.URI))
if err := sherpa.CopyFile(artifact, file); err != nil {
return fmt.Errorf("unable to copy %s to %s\n%w", filepath.Base(b.LoggingDependency.URI), file, err)
}
b.Logger.Bodyf("Writing %s/bin/setenv.sh", layer.Path)
var s string
additionalJars, ok := os.LookupEnv("BPI_TOMCAT_ADDITIONAL_JARS")
if ok {
b.Logger.Bodyf("found BPI_TOMCAT_ADDITIONAL_JARS %q", additionalJars)
s = fmt.Sprintf(`CLASSPATH="%s:%s"`, file, additionalJars)
} else {
s = fmt.Sprintf(`CLASSPATH="%s"`, file)
}
file = filepath.Join(layer.Path, "bin", "setenv.sh")
if err = os.WriteFile(file, []byte(s), 0755); err != nil {
return fmt.Errorf("unable to write file %s\n%w", file, err)
}
return nil
}
func (b Base) writeDependencySBOM(layer libcnb.Layer, syftArtifacts []sbom.SyftArtifact) error {
sbomPath := layer.SBOMPath(libcnb.SyftJSON)
dep := sbom.NewSyftDependency(layer.Path, syftArtifacts)
b.Logger.Debugf("Writing Syft SBOM at %s: %+v", sbomPath, dep)
if err := dep.WriteTo(sbomPath); err != nil {
return fmt.Errorf("unable to write SBOM\n%w", err)
}
return nil
}
func (b Base) explodeWarFiles() error {
warFiles, err := filepath.Glob(filepath.Join(b.ApplicationPath, "*.war"))
if err != nil {
return err
}
for _, warFilePath := range warFiles {
b.Logger.Debugf("Extracting: %s\n", warFilePath)
if _, err := os.Stat(warFilePath); err == nil {
in, err := os.Open(warFilePath)
if err != nil {
return fmt.Errorf("An error occurred while extracting %s: %s\n", warFilePath, err)
}
defer in.Close()
targetDir := strings.TrimSuffix(warFilePath, filepath.Ext(warFilePath))
if err := os.MkdirAll(targetDir, 0755); err != nil {
return fmt.Errorf("An error occurred while extracting %s: %s\n", warFilePath, err)
}
if err := crush.Extract(in, targetDir, 0); err != nil {
return fmt.Errorf("An error occurred while extracting %s: %s\n", warFilePath, err)
}
err = os.Remove(warFilePath)
if err != nil {
return fmt.Errorf("An error occurred while removing the .war file: %s\n", err)
}
}
}
return nil
}
func (Base) Name() string {
return "catalina-base"
}