@@ -3,6 +3,7 @@ package remote
33import (
44 "context"
55 "crypto/tls"
6+ "fmt"
67 "net"
78 "net/http"
89 "time"
@@ -11,6 +12,7 @@ import (
1112 "github.com/google/go-containerregistry/pkg/name"
1213 v1 "github.com/google/go-containerregistry/pkg/v1"
1314 "github.com/google/go-containerregistry/pkg/v1/remote"
15+ "github.com/google/go-containerregistry/pkg/v1/remote/transport"
1416 v1types "github.com/google/go-containerregistry/pkg/v1/types"
1517 "github.com/hashicorp/go-multierror"
1618 "github.com/samber/lo"
@@ -19,14 +21,15 @@ import (
1921 "github.com/aquasecurity/trivy/pkg/fanal/image/registry"
2022 "github.com/aquasecurity/trivy/pkg/fanal/types"
2123 "github.com/aquasecurity/trivy/pkg/log"
24+ "github.com/aquasecurity/trivy/pkg/version/app"
2225)
2326
2427type Descriptor = remote.Descriptor
2528
2629// Get is a wrapper of google/go-containerregistry/pkg/v1/remote.Get
2730// so that it can try multiple authentication methods.
2831func Get (ctx context.Context , ref name.Reference , option types.RegistryOptions ) (* Descriptor , error ) {
29- transport , err := httpTransport (option )
32+ tr , err := httpTransport (option )
3033 if err != nil {
3134 return nil , xerrors .Errorf ("failed to create http transport: %w" , err )
3235 }
@@ -35,7 +38,7 @@ func Get(ctx context.Context, ref name.Reference, option types.RegistryOptions)
3538 // Try each authentication method until it succeeds
3639 for _ , authOpt := range authOptions (ctx , ref , option ) {
3740 remoteOpts := []remote.Option {
38- remote .WithTransport (transport ),
41+ remote .WithTransport (tr ),
3942 authOpt ,
4043 }
4144
@@ -71,7 +74,7 @@ func Get(ctx context.Context, ref name.Reference, option types.RegistryOptions)
7174// Image is a wrapper of google/go-containerregistry/pkg/v1/remote.Image
7275// so that it can try multiple authentication methods.
7376func Image (ctx context.Context , ref name.Reference , option types.RegistryOptions ) (v1.Image , error ) {
74- transport , err := httpTransport (option )
77+ tr , err := httpTransport (option )
7578 if err != nil {
7679 return nil , xerrors .Errorf ("failed to create http transport: %w" , err )
7780 }
@@ -80,7 +83,7 @@ func Image(ctx context.Context, ref name.Reference, option types.RegistryOptions
8083 // Try each authentication method until it succeeds
8184 for _ , authOpt := range authOptions (ctx , ref , option ) {
8285 remoteOpts := []remote.Option {
83- remote .WithTransport (transport ),
86+ remote .WithTransport (tr ),
8487 authOpt ,
8588 }
8689 index , err := remote .Image (ref , remoteOpts ... )
@@ -98,7 +101,7 @@ func Image(ctx context.Context, ref name.Reference, option types.RegistryOptions
98101// Referrers is a wrapper of google/go-containerregistry/pkg/v1/remote.Referrers
99102// so that it can try multiple authentication methods.
100103func Referrers (ctx context.Context , d name.Digest , option types.RegistryOptions ) (v1.ImageIndex , error ) {
101- transport , err := httpTransport (option )
104+ tr , err := httpTransport (option )
102105 if err != nil {
103106 return nil , xerrors .Errorf ("failed to create http transport: %w" , err )
104107 }
@@ -107,7 +110,7 @@ func Referrers(ctx context.Context, d name.Digest, option types.RegistryOptions)
107110 // Try each authentication method until it succeeds
108111 for _ , authOpt := range authOptions (ctx , d , option ) {
109112 remoteOpts := []remote.Option {
110- remote .WithTransport (transport ),
113+ remote .WithTransport (tr ),
111114 authOpt ,
112115 }
113116 index , err := remote .Referrers (d , remoteOpts ... )
@@ -122,7 +125,7 @@ func Referrers(ctx context.Context, d name.Digest, option types.RegistryOptions)
122125 return nil , errs
123126}
124127
125- func httpTransport (option types.RegistryOptions ) (* http.Transport , error ) {
128+ func httpTransport (option types.RegistryOptions ) (http.RoundTripper , error ) {
126129 d := & net.Dialer {
127130 Timeout : 10 * time .Minute ,
128131 }
@@ -138,7 +141,8 @@ func httpTransport(option types.RegistryOptions) (*http.Transport, error) {
138141 tr .TLSClientConfig .Certificates = []tls.Certificate {cert }
139142 }
140143
141- return tr , nil
144+ tripper := transport .NewUserAgent (tr , fmt .Sprintf ("trivy/%s" , app .Version ()))
145+ return tripper , nil
142146}
143147
144148func authOptions (ctx context.Context , ref name.Reference , option types.RegistryOptions ) []remote.Option {
0 commit comments