Skip to content

Commit a24ae72

Browse files
feat(config): tls config: skip verify for provided client (#219)
1 parent 9f1b9ab commit a24ae72

2 files changed

Lines changed: 47 additions & 29 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/)
99
- Managed Database OpenSearch support
1010
- Support for defining NIC model upon creating or modifying a server. Also exported constants for each support NIC model.
1111

12+
### Changed
13+
- client: overwrite the HTTP Client Transport accordingly when `UPCLOUD_DEBUG_SKIP_CERTIFICATE_VERIFY` is set to `1`
14+
1215
## [6.1.1]
1316

1417
### Added

upcloud/client/client.go

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -140,34 +140,63 @@ func (c *Client) getBaseURL() string {
140140
return fmt.Sprintf("%s/%s", c.config.baseURL, APIVersion)
141141
}
142142

143-
type configFn func(o *config)
143+
type ConfigFn func(o *config)
144144

145-
func WithBaseURL(baseURL string) configFn {
145+
// WithBaseURL modifies the client baseURL
146+
func WithBaseURL(baseURL string) ConfigFn {
146147
return func(c *config) {
147148
c.baseURL = baseURL
148149
}
149150
}
150151

151-
func WithHTTPClient(httpClient *http.Client) configFn {
152+
// WithInsecureSkipVerify modifies the client's httpClient to skip verifying
153+
// the server's certificate chain and host name. This should be used only for testing.
154+
func WithInsecureSkipVerify() ConfigFn {
155+
return func(c *config) {
156+
if c.httpClient != nil { // #nosec G402 // allow setting InsecureSkipVerify to true as explicitly requested
157+
if t, ok := c.httpClient.Transport.(*http.Transport); ok {
158+
cfg := &tls.Config{InsecureSkipVerify: true}
159+
if t.TLSClientConfig == nil {
160+
t.TLSClientConfig = cfg
161+
162+
return
163+
}
164+
165+
t.TLSClientConfig.InsecureSkipVerify = cfg.InsecureSkipVerify
166+
}
167+
}
168+
}
169+
}
170+
171+
// WithHTTPClient replaces the client's default httpClient with the specified one
172+
func WithHTTPClient(httpClient *http.Client) ConfigFn {
152173
return func(c *config) {
153174
c.httpClient = httpClient
154175
}
155176
}
156177

157-
func WithTimeout(timeout time.Duration) configFn {
178+
// WithTimeout modifies the client's httpClient timeout
179+
func WithTimeout(timeout time.Duration) ConfigFn {
158180
return func(c *config) {
159181
c.httpClient.Timeout = timeout
160182
}
161183
}
162184

163-
// New creates and returns a new client configured with the specified user and password
164-
func New(username, password string, c ...configFn) *Client {
185+
// New creates and returns a new client configured with the specified user and password and optional
186+
// config functions.
187+
func New(username, password string, c ...ConfigFn) *Client {
165188
config := config{
166189
username: username,
167190
password: password,
168191
baseURL: clientBaseURL(os.Getenv(EnvDebugAPIBaseURL)),
169-
httpClient: httpClient(),
192+
httpClient: cleanhttp.DefaultClient(),
193+
}
194+
195+
// If set, replace http client transport with one skipping tls verification
196+
if os.Getenv(EnvDebugSkipCertificateVerify) == "1" {
197+
c = append(c, WithInsecureSkipVerify())
170198
}
199+
171200
for _, fn := range c {
172201
fn(&config)
173202
}
@@ -177,34 +206,20 @@ func New(username, password string, c ...configFn) *Client {
177206
}
178207
}
179208

180-
func httpClient() *http.Client {
181-
var client *http.Client
182-
if os.Getenv(EnvDebugSkipCertificateVerify) == "1" {
183-
client = &http.Client{
184-
Transport: &http.Transport{
185-
TLSClientConfig: &tls.Config{
186-
InsecureSkipVerify: true, //nolint
187-
},
188-
},
189-
}
190-
} else {
191-
client = cleanhttp.DefaultClient()
192-
}
193-
return client
194-
}
195-
196209
func userAgent() string {
197210
return fmt.Sprintf("upcloud-go-api/%s", Version)
198211
}
199212

200213
func clientBaseURL(URL string) string {
201-
if URL != "" {
202-
if u, err := url.Parse(URL); err != nil || u.Scheme == "" || u.Host == "" {
203-
return APIBaseURL
204-
}
205-
return URL
214+
if URL == "" {
215+
return APIBaseURL
206216
}
207-
return APIBaseURL
217+
218+
if u, err := url.Parse(URL); err != nil || u.Scheme == "" || u.Host == "" {
219+
return APIBaseURL
220+
}
221+
222+
return URL
208223
}
209224

210225
// Parses the response and returns either the response body or an error

0 commit comments

Comments
 (0)