Skip to content

Commit 4194d9d

Browse files
add more component init edge cases, move to its own method
1 parent 0ec0d41 commit 4194d9d

File tree

2 files changed

+33
-11
lines changed

2 files changed

+33
-11
lines changed

edge-apis/client_base.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,9 @@ func (self *BaseClient[A]) AuthenticateWithPreviousSession(credentials Credentia
145145
// If Components are provided with nil transport/client, they are initialized with warnings logged.
146146
func (self *BaseClient[A]) initializeComponents(config *ApiClientConfig) {
147147
if config.Components != nil {
148-
149-
if config.Components.TlsAwareTransport == nil {
150-
pfxlog.Logger().Warn("components were provided but the transport was nil, it is being initialized")
151-
config.Components.TlsAwareTransport = NewTlsAwareHttpTransport(nil)
152-
}
153-
154-
if config.Components.HttpClient == nil {
155-
pfxlog.Logger().Warn("components were provided but the http client was nil, it is being initialized")
156-
config.Components.HttpClient = NewHttpClient(config.Components.TlsAwareTransport)
157-
}
158-
148+
config.Components.assertComponents(config)
159149
self.Components = *config.Components
150+
160151
if config.Proxy != nil {
161152
pfxlog.Logger().Warn("components were provided along with a proxy function on the ApiClientConfig, it is being ignored, if needed properly set on components")
162153
}

edge-apis/http_components.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/url"
99
"time"
1010

11+
"github.com/michaelquigley/pfxlog"
1112
"github.com/openziti/edge-api/rest_util"
1213
)
1314

@@ -19,6 +20,36 @@ type Components struct {
1920
CaPool *x509.CertPool
2021
}
2122

23+
// assertComponents ensures that the components are initialized properly.
24+
func (c Components) assertComponents(config *ApiClientConfig) {
25+
if config.Components.HttpClient == nil {
26+
pfxlog.Logger().Warn("components were provided but the http client was nil, it is being initialized")
27+
28+
if config.Components.TlsAwareTransport == nil {
29+
config.Components.TlsAwareTransport = NewTlsAwareHttpTransport(nil)
30+
pfxlog.Logger().Warn("components were provided but the client and transport are nil, they are being initialized with a default")
31+
}
32+
33+
config.Components.HttpClient = NewHttpClient(config.Components.TlsAwareTransport)
34+
}
35+
36+
if config.Components.TlsAwareTransport == nil {
37+
if tlsAwareTransport, ok := config.Components.HttpClient.Transport.(TlsAwareTransport); ok {
38+
config.Components.TlsAwareTransport = tlsAwareTransport
39+
pfxlog.Logger().Warn("components were provided but the transport was nil, it is being initialized with the transport from the http client")
40+
} else {
41+
pfxlog.Logger().Warn("components were provided but the transport was nil and the client did not have a suitable transport, it is being initialized with a default")
42+
config.Components.TlsAwareTransport = NewTlsAwareHttpTransport(nil)
43+
config.Components.HttpClient.Transport = config.Components.TlsAwareTransport
44+
}
45+
}
46+
47+
if config.Components.HttpClient.Transport != config.Components.TlsAwareTransport {
48+
pfxlog.Logger().Warn("components were provided but the http client transport was not the same as the transport in components, it is being initialized")
49+
config.Components.HttpClient.Transport = config.Components.TlsAwareTransport
50+
}
51+
}
52+
2253
// ComponentsConfig contains configuration options for creating Components.
2354
type ComponentsConfig struct {
2455
Proxy func(*http.Request) (*url.URL, error)

0 commit comments

Comments
 (0)