Skip to content

Commit 4396185

Browse files
authored
Merge pull request #12 from nais/fix_no_sbom
update(dependencytrack): better concurrency for fetching apps & no sbom fix
2 parents 9574a1d + c4b5820 commit 4396185

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/jackc/pgx/v5 v5.5.3
1717
github.com/joho/godotenv v1.5.1
1818
github.com/lithammer/fuzzysearch v1.1.8
19-
github.com/nais/dependencytrack v0.0.0-20240208124913-513b3ad1d961
19+
github.com/nais/dependencytrack v0.0.0-20240301095153-b46382e6a8be
2020
github.com/nais/liberator v0.0.0-20240223134957-13b72a76ba9d
2121
github.com/patrickmn/go-cache v2.1.0+incompatible
2222
github.com/pressly/goose/v3 v3.18.0
@@ -123,7 +123,7 @@ require (
123123
github.com/lestrrat-go/httpcc v1.0.1 // indirect
124124
github.com/lestrrat-go/httprc v1.0.4 // indirect
125125
github.com/lestrrat-go/iter v1.0.2 // indirect
126-
github.com/lestrrat-go/jwx/v2 v2.0.19 // indirect
126+
github.com/lestrrat-go/jwx/v2 v2.0.20 // indirect
127127
github.com/lestrrat-go/option v1.0.1 // indirect
128128
github.com/magiconair/properties v1.8.7 // indirect
129129
github.com/mailru/easyjson v0.7.7 // indirect

go.sum

+4-4
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ github.com/lestrrat-go/httprc v1.0.4 h1:bAZymwoZQb+Oq8MEbyipag7iSq6YIga8Wj6GOiJG
361361
github.com/lestrrat-go/httprc v1.0.4/go.mod h1:mwwz3JMTPBjHUkkDv/IGJ39aALInZLrhBp0X7KGUZlo=
362362
github.com/lestrrat-go/iter v1.0.2 h1:gMXo1q4c2pHmC3dn8LzRhJfP1ceCbgSiT9lUydIzltI=
363363
github.com/lestrrat-go/iter v1.0.2/go.mod h1:Momfcq3AnRlRjI5b5O8/G5/BvpzrhoFTZcn06fEOPt4=
364-
github.com/lestrrat-go/jwx/v2 v2.0.19 h1:ekv1qEZE6BVct89QA+pRF6+4pCpfVrOnEJnTnT4RXoY=
365-
github.com/lestrrat-go/jwx/v2 v2.0.19/go.mod h1:l3im3coce1lL2cDeAjqmaR+Awx+X8Ih+2k8BuHNJ4CU=
364+
github.com/lestrrat-go/jwx/v2 v2.0.20 h1:sAgXuWS/t8ykxS9Bi2Qtn5Qhpakw1wrcjxChudjolCc=
365+
github.com/lestrrat-go/jwx/v2 v2.0.20/go.mod h1:UlCSmKqw+agm5BsOBfEAbTvKsEApaGNqHAEUTv5PJC4=
366366
github.com/lestrrat-go/option v1.0.1 h1:oAzP2fvZGQKWkvHa1/SAcFolBEca1oN+mQ7eooNBEYU=
367367
github.com/lestrrat-go/option v1.0.1/go.mod h1:5ZHFbivi4xwXxhxY9XHDe2FHo6/Z7WWmtT7T5nBBp3I=
368368
github.com/libsql/sqlite-antlr4-parser v0.0.0-20230802215326-5cb5bb604475 h1:6PfEMwfInASh9hkN83aR0j4W/eKaAZt/AURtXAXlas0=
@@ -401,8 +401,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq
401401
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
402402
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
403403
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
404-
github.com/nais/dependencytrack v0.0.0-20240208124913-513b3ad1d961 h1:FtfEwEQq2hWEZFmg3F8Ix9vq6x8o0PF4bEvCn7qwCxw=
405-
github.com/nais/dependencytrack v0.0.0-20240208124913-513b3ad1d961/go.mod h1:zC59yj0La1TS291o/9XSVZ0XAY9UlJS6pUFD3ouHmjU=
404+
github.com/nais/dependencytrack v0.0.0-20240301095153-b46382e6a8be h1:Ri7fb/vmxJySaIxDO1dL9PQIIA4rN1Mtrt2vfD3+Nyo=
405+
github.com/nais/dependencytrack v0.0.0-20240301095153-b46382e6a8be/go.mod h1:kgE6W/n/1C1k+09xVbf7VD8J5J7v0ueHSY9E9vRCM1Y=
406406
github.com/nais/liberator v0.0.0-20240223134957-13b72a76ba9d h1:NA//xj14122osF306Q19oARHyg9JW7g4qJReFO1O3Ds=
407407
github.com/nais/liberator v0.0.0-20240223134957-13b72a76ba9d/go.mod h1:cWThp1WBBbkRFhMI2DQMvBTTEN+6GPzmmh+Xjv8vffE=
408408
github.com/onsi/ginkgo/v2 v2.14.0 h1:vSmGj2Z5YPb9JwCWT6z6ihcUvDhuXLc3sJiqd3jMKAY=

internal/thirdparty/dependencytrack/dependencytrack.go

+18-14
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
"net/http"
77
"net/url"
88
"strings"
9-
"sync"
109
"time"
1110

11+
"github.com/sourcegraph/conc/pool"
12+
1213
"github.com/google/uuid"
1314
"github.com/nais/api/internal/graph/model"
1415
"github.com/nais/api/internal/graph/scalar"
@@ -137,28 +138,24 @@ func (c *Client) VulnerabilitySummary(ctx context.Context, app *AppInstance) (*m
137138
}
138139

139140
func (c *Client) GetVulnerabilities(ctx context.Context, apps []*AppInstance) ([]*model.Vulnerability, error) {
140-
var wg sync.WaitGroup
141141
now := time.Now()
142-
143142
nodes := make([]*model.Vulnerability, 0)
144-
for _, a := range apps {
145-
wg.Add(1)
146-
go func(app *AppInstance) {
147-
defer wg.Done()
148-
v, err := c.findingsForApp(ctx, app)
143+
p := pool.New().WithMaxGoroutines(10)
144+
for _, app := range apps {
145+
p.Go(func() {
146+
appVulnNode, err := c.findingsForApp(ctx, app)
149147
if err != nil {
150148
c.log.Errorf("retrieveFindings for app %q: %v", app.ID(), err)
151149
return
152150
}
153-
if v == nil {
151+
if appVulnNode == nil {
154152
c.log.Debugf("no findings found in DependencyTrack for app %q", app.ID())
155153
return
156154
}
157-
nodes = append(nodes, v)
158-
}(a)
155+
nodes = append(nodes, appVulnNode)
156+
})
159157
}
160-
wg.Wait()
161-
158+
p.Wait()
162159
c.log.Debugf("DependencyTrack fetch: %v\n", time.Since(now))
163160
return nodes, nil
164161
}
@@ -186,7 +183,7 @@ func (c *Client) findingsForApp(ctx context.Context, app *AppInstance) (*model.V
186183
findingsLink := fmt.Sprintf("%s/projects/%s/findings", u, p.Uuid)
187184

188185
v.FindingsLink = findingsLink
189-
v.HasBom = p.LastBomImportFormat != ""
186+
v.HasBom = hasBom(p)
190187

191188
if !v.HasBom {
192189
c.log.Debugf("no bom found in DependencyTrack for project %s", p.Name)
@@ -206,6 +203,13 @@ func (c *Client) findingsForApp(ctx context.Context, app *AppInstance) (*model.V
206203
return v, nil
207204
}
208205

206+
// Due to the nature of the DependencyTrack API, the 'LastBomImportFormat' is not reliable to determine if a project has a BOM.
207+
// The 'LastBomImportFormat' can be empty even if the project has a BOM.
208+
// As a fallback, we can check if projects has registered any components, then we assume that if a project has components, it has a BOM.
209+
func hasBom(p *dependencytrack.Project) bool {
210+
return p.LastBomImportFormat != "" || p.Metrics != nil && p.Metrics.Components > 0
211+
}
212+
209213
func (c *Client) retrieveFindings(ctx context.Context, uuid string) ([]*dependencytrack.Finding, error) {
210214
findings, err := c.client.GetFindings(ctx, uuid)
211215
if err != nil {

0 commit comments

Comments
 (0)