Skip to content

Commit d6576aa

Browse files
ybelMekktommytroen
andcommitted
fix: improve security for tls config grpc
* handle errors in dp-track client Co-authored-by: Tommy Trøen <[email protected]>
1 parent db4e236 commit d6576aa

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

cmd/api/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type config struct {
3030
DependencytrackUrl string `envconfig:"DEPENDENCYTRACK_URL" required:"true"`
3131
DependencytrackApiKey string `envconfig:"DEPENDENCYTRACK_API_KEY" required:"true"`
3232
DatabaseUrl string `envconfig:"DATABASE_URL" required:"true"`
33-
UpdateInterval time.Duration `envconfig:"UPDATE_INTERVAL" default:"1h"`
33+
UpdateInterval time.Duration `envconfig:"UPDATE_INTERVAL" default:"1m"`
3434
}
3535

3636
// handle env vars better

internal/dependencytrack/dependencytrack.go

+22-7
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,26 @@ func setupConfig(url, apiKey string) *client.Configuration {
4848
}
4949

5050
func NewClient(url, apiKey string) (Client, error) {
51+
if url == "" {
52+
return nil, fmt.Errorf("NewClient: URL cannot be empty")
53+
}
54+
if apiKey == "" {
55+
return nil, fmt.Errorf("NewClient: API key cannot be empty")
56+
}
57+
5158
return &dependencyTrackClient{client.NewAPIClient(setupConfig(url, apiKey))}, nil
5259
}
5360

5461
func (c *dependencyTrackClient) GetFindings(ctx context.Context, uuid string, suppressed bool) ([]client.Finding, error) {
5562
p, _, err := c.client.FindingAPI.GetFindingsByProject(ctx, uuid).
5663
Suppressed(suppressed).
5764
Execute()
58-
return p, err
65+
66+
if err != nil {
67+
return nil, fmt.Errorf("failed to get findings for project %s: %w", uuid, err)
68+
}
69+
70+
return p, nil
5971
}
6072

6173
func (c *dependencyTrackClient) paginateProjects(ctx context.Context, limit, offset int32, callFunc func(ctx context.Context, offset int32) ([]client.Project, error)) ([]client.Project, error) {
@@ -96,17 +108,17 @@ func (c *dependencyTrackClient) GetProject(ctx context.Context, name, version st
96108
Version(version).
97109
Execute()
98110

99-
if resp != nil && resp.StatusCode == 404 {
100-
b, err := io.ReadAll(resp.Body)
101-
if err != nil {
102-
return nil, err
111+
if err != nil && resp != nil && resp.StatusCode == 404 {
112+
body, readErr := io.ReadAll(resp.Body)
113+
if readErr != nil {
114+
return nil, fmt.Errorf("failed to read response body: %w", readErr)
103115
}
104116

105-
if strings.Contains(string(b), "The project could not be found") {
117+
if strings.Contains(string(body), "The project could not be found") {
106118
return nil, nil
107119
}
108120

109-
return nil, fmt.Errorf("getting project: 404 not found %s", string(b))
121+
return nil, fmt.Errorf("project not found: %s", string(body))
110122
}
111123

112124
return p, err
@@ -115,6 +127,9 @@ func (c *dependencyTrackClient) GetProject(ctx context.Context, name, version st
115127
func (c *dependencyTrackClient) GetProjects(ctx context.Context, limit, offset int32) ([]client.Project, error) {
116128
return c.paginateProjects(ctx, limit, offset, func(ctx context.Context, offset int32) ([]client.Project, error) {
117129
pageNumber := (offset / limit) + 1
130+
if offset%limit != 0 {
131+
pageNumber++
132+
}
118133
p, _, err := c.client.ProjectAPI.GetProjects(ctx).
119134
PageSize(limit).
120135
PageNumber(pageNumber).

0 commit comments

Comments
 (0)