@@ -18,6 +18,7 @@ import (
18
18
"github.com/PaesslerAG/jsonpath"
19
19
"github.com/go-logr/logr"
20
20
"github.com/go-logr/zapr"
21
+ "github.com/hashicorp/go-cleanhttp"
21
22
"go.uber.org/zap"
22
23
23
24
"github.com/nutanix-cloud-native/prism-go-client"
@@ -62,7 +63,8 @@ type Client struct {
62
63
// error message, incase httpClient is in error state
63
64
ErrorMsg string
64
65
65
- logger * logr.Logger
66
+ logger * logr.Logger
67
+ certpool * x509.CertPool
66
68
}
67
69
68
70
type ClientOption func (* Client ) error
@@ -80,15 +82,23 @@ func WithCredentials(credentials *prismgoclient.Credentials) ClientOption {
80
82
return func (c * Client ) error {
81
83
c .credentials = credentials
82
84
if c .credentials .Insecure {
83
- c .httpClient .Transport .(* http.Transport ).TLSClientConfig .InsecureSkipVerify = true
85
+ transport , ok := c .httpClient .Transport .(* http.Transport )
86
+ if ! ok {
87
+ return fmt .Errorf ("transport is not of type http.Transport: %T" , c .httpClient .Transport )
88
+ }
89
+ transport .TLSClientConfig .InsecureSkipVerify = true
84
90
}
85
91
if c .credentials .ProxyURL != "" {
86
92
c .logger .V (1 ).Info ("Using proxy:" , "proxy" , c .credentials .ProxyURL )
87
93
proxy , err := url .Parse (c .credentials .ProxyURL )
88
94
if err != nil {
89
95
return fmt .Errorf ("error parsing proxy url: %s" , err )
90
96
}
91
- c .httpClient .Transport .(* http.Transport ).Proxy = http .ProxyURL (proxy )
97
+ transport , ok := c .httpClient .Transport .(* http.Transport )
98
+ if ! ok {
99
+ return fmt .Errorf ("transport is not of type http.Transport: %T" , c .httpClient .Transport )
100
+ }
101
+ transport .Proxy = http .ProxyURL (proxy )
92
102
}
93
103
return nil
94
104
}
@@ -143,13 +153,10 @@ func WithAbsolutePath(absolutePath string) ClientOption {
143
153
// WithCertificate adds the certificate to the certificate pool in tls config
144
154
func WithCertificate (cert * x509.Certificate ) ClientOption {
145
155
return func (c * Client ) error {
146
- certPool , err := x509 .SystemCertPool ()
147
- if err != nil {
148
- return fmt .Errorf ("failed to get system cert pool: %s" , err )
156
+ if cert == nil {
157
+ return fmt .Errorf ("certificate is nil" )
149
158
}
150
-
151
- certPool .AddCert (cert )
152
- c .httpClient .Transport .(* http.Transport ).TLSClientConfig .RootCAs = certPool
159
+ c .certpool .AddCert (cert )
153
160
return nil
154
161
}
155
162
}
@@ -167,10 +174,18 @@ func WithRoundTripper(transport http.RoundTripper) ClientOption {
167
174
// NewClient returns a wrapper around http/https (as per isHTTP flag) httpClient with additions of proxy & session_auth if given
168
175
func NewClient (opts ... ClientOption ) (* Client , error ) {
169
176
c := & Client {
170
- httpClient : http .DefaultClient ,
177
+ httpClient : cleanhttp .DefaultClient () ,
171
178
}
179
+
180
+ certPool , err := x509 .SystemCertPool ()
181
+ if err != nil {
182
+ return nil , fmt .Errorf ("failed to get system cert pool: %s" , err )
183
+ }
184
+ c .certpool = certPool
185
+
172
186
c .httpClient .Transport = http .DefaultTransport
173
187
c .httpClient .Transport .(* http.Transport ).TLSClientConfig = & tls.Config {}
188
+ c .httpClient .Transport .(* http.Transport ).TLSClientConfig .RootCAs = c .certpool
174
189
175
190
// If the user does not specify a logger, then we'll use zap for a default one
176
191
// If the user specified a logger, then we'll use that logger
0 commit comments