Skip to content

Commit 27a3e55

Browse files
DmitriyLewenknqyf263
authored andcommitted
fix(java): download java-db once (#5442)
1 parent d223732 commit 27a3e55

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

pkg/fanal/analyzer/language/java/jar/jar.go

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"github.com/aquasecurity/trivy/pkg/fanal/analyzer/language"
1616
"github.com/aquasecurity/trivy/pkg/fanal/types"
1717
"github.com/aquasecurity/trivy/pkg/javadb"
18-
"github.com/aquasecurity/trivy/pkg/log"
1918
"github.com/aquasecurity/trivy/pkg/parallel"
2019
)
2120

@@ -34,8 +33,7 @@ var requiredExtensions = []string{
3433

3534
// javaLibraryAnalyzer analyzes jar/war/ear/par files
3635
type javaLibraryAnalyzer struct {
37-
client *javadb.DB
38-
slow bool
36+
slow bool
3937
}
4038

4139
func newJavaLibraryAnalyzer(options analyzer.AnalyzerOptions) (analyzer.PostAnalyzer, error) {
@@ -46,23 +44,20 @@ func newJavaLibraryAnalyzer(options analyzer.AnalyzerOptions) (analyzer.PostAnal
4644

4745
func (a *javaLibraryAnalyzer) PostAnalyze(ctx context.Context, input analyzer.PostAnalysisInput) (*analyzer.AnalysisResult, error) {
4846
// TODO: think about the sonatype API and "--offline"
49-
var err error
50-
log.Logger.Info("JAR files found")
51-
a.client, err = javadb.NewClient()
47+
client, err := javadb.NewClient()
5248
if err != nil {
5349
return nil, xerrors.Errorf("Unable to initialize the Java DB: %s", err)
5450
}
55-
defer func() { _ = a.client.Close() }()
56-
log.Logger.Info("Analyzing JAR files takes a while...")
51+
defer func() { _ = client.Close() }()
5752

5853
// Skip analyzing JAR files as the nil client means the Java DB was not downloaded successfully.
59-
if a.client == nil {
54+
if client == nil {
6055
return nil, nil
6156
}
6257

6358
// It will be called on each JAR file
6459
onFile := func(path string, info fs.FileInfo, r dio.ReadSeekerAt) (*types.Application, error) {
65-
p := jar.NewParser(a.client, jar.WithSize(info.Size()), jar.WithFilePath(path))
60+
p := jar.NewParser(client, jar.WithSize(info.Size()), jar.WithFilePath(path))
6661
return language.ParsePackage(types.Jar, path, r, p, input.Options.FileChecksum)
6762
}
6863

pkg/javadb/client.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"path/filepath"
99
"sort"
10+
"sync"
1011
"time"
1112

1213
"golang.org/x/xerrors"
@@ -31,6 +32,7 @@ type Updater struct {
3132
skip bool
3233
quiet bool
3334
registryOption ftypes.RegistryOptions
35+
once sync.Once // we need to update java-db once per run
3436
}
3537

3638
func (u *Updater) Update() error {
@@ -93,10 +95,12 @@ func Update() error {
9395
if updater == nil {
9496
return xerrors.New("Java DB client not initialized")
9597
}
96-
if err := updater.Update(); err != nil {
97-
return xerrors.Errorf("Java DB update error: %w", err)
98-
}
99-
return nil
98+
99+
var err error
100+
updater.once.Do(func() {
101+
err = updater.Update()
102+
})
103+
return err
100104
}
101105

102106
type DB struct {

0 commit comments

Comments
 (0)