@@ -3,6 +3,7 @@ package remote
3
3
import (
4
4
"context"
5
5
"crypto/tls"
6
+ "fmt"
6
7
"net"
7
8
"net/http"
8
9
"time"
@@ -11,6 +12,7 @@ import (
11
12
"github.com/google/go-containerregistry/pkg/name"
12
13
v1 "github.com/google/go-containerregistry/pkg/v1"
13
14
"github.com/google/go-containerregistry/pkg/v1/remote"
15
+ "github.com/google/go-containerregistry/pkg/v1/remote/transport"
14
16
v1types "github.com/google/go-containerregistry/pkg/v1/types"
15
17
"github.com/hashicorp/go-multierror"
16
18
"github.com/samber/lo"
@@ -19,14 +21,15 @@ import (
19
21
"github.com/aquasecurity/trivy/pkg/fanal/image/registry"
20
22
"github.com/aquasecurity/trivy/pkg/fanal/types"
21
23
"github.com/aquasecurity/trivy/pkg/log"
24
+ "github.com/aquasecurity/trivy/pkg/version/app"
22
25
)
23
26
24
27
type Descriptor = remote.Descriptor
25
28
26
29
// Get is a wrapper of google/go-containerregistry/pkg/v1/remote.Get
27
30
// so that it can try multiple authentication methods.
28
31
func Get (ctx context.Context , ref name.Reference , option types.RegistryOptions ) (* Descriptor , error ) {
29
- transport , err := httpTransport (option )
32
+ tr , err := httpTransport (option )
30
33
if err != nil {
31
34
return nil , xerrors .Errorf ("failed to create http transport: %w" , err )
32
35
}
@@ -35,7 +38,7 @@ func Get(ctx context.Context, ref name.Reference, option types.RegistryOptions)
35
38
// Try each authentication method until it succeeds
36
39
for _ , authOpt := range authOptions (ctx , ref , option ) {
37
40
remoteOpts := []remote.Option {
38
- remote .WithTransport (transport ),
41
+ remote .WithTransport (tr ),
39
42
authOpt ,
40
43
}
41
44
@@ -71,7 +74,7 @@ func Get(ctx context.Context, ref name.Reference, option types.RegistryOptions)
71
74
// Image is a wrapper of google/go-containerregistry/pkg/v1/remote.Image
72
75
// so that it can try multiple authentication methods.
73
76
func Image (ctx context.Context , ref name.Reference , option types.RegistryOptions ) (v1.Image , error ) {
74
- transport , err := httpTransport (option )
77
+ tr , err := httpTransport (option )
75
78
if err != nil {
76
79
return nil , xerrors .Errorf ("failed to create http transport: %w" , err )
77
80
}
@@ -80,7 +83,7 @@ func Image(ctx context.Context, ref name.Reference, option types.RegistryOptions
80
83
// Try each authentication method until it succeeds
81
84
for _ , authOpt := range authOptions (ctx , ref , option ) {
82
85
remoteOpts := []remote.Option {
83
- remote .WithTransport (transport ),
86
+ remote .WithTransport (tr ),
84
87
authOpt ,
85
88
}
86
89
index , err := remote .Image (ref , remoteOpts ... )
@@ -98,7 +101,7 @@ func Image(ctx context.Context, ref name.Reference, option types.RegistryOptions
98
101
// Referrers is a wrapper of google/go-containerregistry/pkg/v1/remote.Referrers
99
102
// so that it can try multiple authentication methods.
100
103
func Referrers (ctx context.Context , d name.Digest , option types.RegistryOptions ) (v1.ImageIndex , error ) {
101
- transport , err := httpTransport (option )
104
+ tr , err := httpTransport (option )
102
105
if err != nil {
103
106
return nil , xerrors .Errorf ("failed to create http transport: %w" , err )
104
107
}
@@ -107,7 +110,7 @@ func Referrers(ctx context.Context, d name.Digest, option types.RegistryOptions)
107
110
// Try each authentication method until it succeeds
108
111
for _ , authOpt := range authOptions (ctx , d , option ) {
109
112
remoteOpts := []remote.Option {
110
- remote .WithTransport (transport ),
113
+ remote .WithTransport (tr ),
111
114
authOpt ,
112
115
}
113
116
index , err := remote .Referrers (d , remoteOpts ... )
@@ -122,7 +125,7 @@ func Referrers(ctx context.Context, d name.Digest, option types.RegistryOptions)
122
125
return nil , errs
123
126
}
124
127
125
- func httpTransport (option types.RegistryOptions ) (* http.Transport , error ) {
128
+ func httpTransport (option types.RegistryOptions ) (http.RoundTripper , error ) {
126
129
d := & net.Dialer {
127
130
Timeout : 10 * time .Minute ,
128
131
}
@@ -138,7 +141,8 @@ func httpTransport(option types.RegistryOptions) (*http.Transport, error) {
138
141
tr .TLSClientConfig .Certificates = []tls.Certificate {cert }
139
142
}
140
143
141
- return tr , nil
144
+ tripper := transport .NewUserAgent (tr , fmt .Sprintf ("trivy/%s" , app .Version ()))
145
+ return tripper , nil
142
146
}
143
147
144
148
func authOptions (ctx context.Context , ref name.Reference , option types.RegistryOptions ) []remote.Option {
0 commit comments