Skip to content

Commit af1d257

Browse files
authored
feat(sbom): set User-Agent header on requests to Rekor (#7396)
Signed-off-by: Bob Callaway <bcallaway@google.com>
1 parent 1a6295c commit af1d257

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ require (
4747
github.com/docker/go-connections v0.5.0
4848
github.com/fatih/color v1.17.0
4949
github.com/go-git/go-git/v5 v5.12.0
50-
github.com/go-openapi/runtime v0.28.0
51-
github.com/go-openapi/strfmt v0.23.0
50+
github.com/go-openapi/runtime v0.28.0 // indirect
51+
github.com/go-openapi/strfmt v0.23.0 // indirect
5252
github.com/go-redis/redis/v8 v8.11.5
5353
github.com/golang-jwt/jwt/v5 v5.2.1
5454
github.com/google/go-containerregistry v0.20.2

pkg/rekor/client.go

+5-10
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package rekor
22

33
import (
44
"context"
5-
"net/url"
5+
"fmt"
66
"slices"
77

8-
httptransport "github.com/go-openapi/runtime/client"
9-
"github.com/go-openapi/strfmt"
8+
pkgclient "github.com/sigstore/rekor/pkg/client"
109
"github.com/sigstore/rekor/pkg/generated/client"
1110
eclient "github.com/sigstore/rekor/pkg/generated/client/entries"
1211
"github.com/sigstore/rekor/pkg/generated/client/index"
1312
"github.com/sigstore/rekor/pkg/generated/models"
1413
"golang.org/x/xerrors"
1514

1615
"github.com/aquasecurity/trivy/pkg/log"
16+
"github.com/aquasecurity/trivy/pkg/version/app"
1717
)
1818

1919
const (
@@ -64,15 +64,10 @@ type Client struct {
6464
}
6565

6666
func NewClient(rekorURL string) (*Client, error) {
67-
u, err := url.Parse(rekorURL)
67+
c, err := pkgclient.GetRekorClient(rekorURL, pkgclient.WithUserAgent(fmt.Sprintf("trivy/%s", app.Version())))
6868
if err != nil {
69-
return nil, xerrors.Errorf("failed to parse url: %w", err)
69+
return nil, xerrors.Errorf("failed to create rekor client: %w", err)
7070
}
71-
72-
c := client.New(
73-
httptransport.New(u.Host, client.DefaultBasePath, []string{u.Scheme}),
74-
strfmt.Default,
75-
)
7671
return &Client{Rekor: c}, nil
7772
}
7873

pkg/rekor/client_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"net/http"
66
"net/http/httptest"
7+
"strings"
78
"testing"
89

910
"github.com/stretchr/testify/assert"
@@ -56,6 +57,9 @@ func TestClient_Search(t *testing.T) {
5657
for _, tt := range tests {
5758
t.Run(tt.name, func(t *testing.T) {
5859
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
60+
if !strings.HasPrefix(r.UserAgent(), "trivy/") {
61+
t.Fatalf("User-Agent header was not specified")
62+
}
5963
http.ServeFile(w, r, tt.mockResponseFile)
6064
return
6165
}))
@@ -148,6 +152,9 @@ func TestClient_GetEntries(t *testing.T) {
148152
for _, tt := range tests {
149153
t.Run(tt.name, func(t *testing.T) {
150154
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
155+
if !strings.HasPrefix(r.UserAgent(), "trivy/") {
156+
t.Fatalf("User-Agent header was not specified")
157+
}
151158
http.ServeFile(w, r, tt.mockResponseFile)
152159
return
153160
}))

0 commit comments

Comments
 (0)