@@ -18,22 +18,25 @@ const (
1818 TotpRequiredHeader = "totp-required"
1919)
2020
21- // AuthEnabledApi is used as a sentinel interface to detect APIs that support authentication and to work around a golang
22- // limitation dealing with accessing field of generically typed fields .
21+ // AuthEnabledApi is a sentinel interface that detects APIs supporting authentication.
22+ // It provides methods for authenticating, managing sessions, and discovering controllers for high-availability .
2323type AuthEnabledApi interface {
24- //Authenticate will attempt to issue an authentication request using the provided credentials and http client.
25- //These functions act as abstraction around the underlying go-swagger generated client and will use the default
26- //http client if not provided.
24+ // Authenticate authenticates using the provided credentials and returns an ApiSession for subsequent authenticated requests.
2725 Authenticate (credentials Credentials , configTypes []string , httpClient * http.Client ) (ApiSession , error )
26+ // SetUseOidc forces OIDC mode (true) or legacy mode (false).
2827 SetUseOidc (bool )
28+ // ListControllers returns the list of available controllers for HA failover.
2929 ListControllers () (* rest_model.ControllersList , error )
30+ // GetClientTransportPool returns the transport pool managing multiple controller endpoints.
3031 GetClientTransportPool () ClientTransportPool
32+ // SetClientTransportPool sets the transport pool.
3133 SetClientTransportPool (ClientTransportPool )
34+ // RefreshApiSession refreshes an existing session.
3235 RefreshApiSession (apiSession ApiSession , httpClient * http.Client ) (ApiSession , error )
3336}
3437
35- // BaseClient implements the Client interface specifically for the types specified in the ApiType constraint. It
36- // provides shared functionality that all ApiType types require .
38+ // BaseClient provides shared authentication and session management for OpenZiti API clients.
39+ // It handles credential-based authentication, TLS configuration, session storage, and controller failover .
3740type BaseClient [A ApiType ] struct {
3841 API * A
3942 AuthEnabledApi AuthEnabledApi
@@ -84,18 +87,19 @@ func (self *BaseClient[A]) SetAllowOidcDynamicallyEnabled(allow bool) {
8487 apiType .SetAllowOidcDynamicallyEnabled (allow )
8588}
8689
87- // Authenticate will attempt to use the provided credentials to authenticate via the underlying ApiType. On success
88- // the API Session details will be returned and the current client will make authenticated requests on future
89- // calls. On an error the API Session in use will be cleared and subsequent requests will become/continue to be
90- // made in an unauthenticated fashion.
90+ // Authenticate authenticates using provided credentials, updating the TLS configuration based on the credential's CA pool.
91+ // On success, stores the session and processes controller endpoints for HA failover.
92+ // On failure, clears the session and credentials.
9193func (self * BaseClient [A ]) Authenticate (credentials Credentials , configTypesOverride []string ) (ApiSession , error ) {
9294 self .Credentials = nil
9395 self .ApiSession .Store (nil )
9496
97+ tlsClientConfig := self .TlsAwareTransport .GetTlsClientConfig ()
98+
9599 if credCaPool := credentials .GetCaPool (); credCaPool != nil {
96- self . HttpTransport . TLSClientConfig .RootCAs = credCaPool
100+ tlsClientConfig .RootCAs = credCaPool
97101 } else {
98- self . HttpTransport . TLSClientConfig .RootCAs = self .CaPool
102+ tlsClientConfig .RootCAs = self .CaPool
99103 }
100104
101105 apiSession , err := self .AuthEnabledApi .Authenticate (credentials , configTypesOverride , self .HttpClient )
@@ -116,10 +120,11 @@ func (self *BaseClient[A]) AuthenticateWithPreviousSession(credentials Credentia
116120 self .Credentials = nil
117121 self .ApiSession .Store (nil )
118122
123+ tlsClientConfig := self .TlsAwareTransport .GetTlsClientConfig ()
119124 if credCaPool := credentials .GetCaPool (); credCaPool != nil {
120- self . HttpTransport . TLSClientConfig .RootCAs = credCaPool
125+ tlsClientConfig .RootCAs = credCaPool
121126 } else {
122- self . HttpTransport . TLSClientConfig .RootCAs = self .CaPool
127+ tlsClientConfig .RootCAs = self .CaPool
123128 }
124129
125130 refreshedSession , refreshErr := self .AuthEnabledApi .RefreshApiSession (prevApiSession , self .HttpClient )
@@ -136,24 +141,46 @@ func (self *BaseClient[A]) AuthenticateWithPreviousSession(credentials Credentia
136141 return refreshedSession , nil
137142}
138143
139- // initializeComponents assembles the lower level components necessary for the go-swagger/openapi facilities.
144+ // initializeComponents assembles HTTP client infrastructure, either using provided Components or creating new ones.
145+ // If Components are provided with nil transport/client, they are initialized with warnings logged.
140146func (self * BaseClient [A ]) initializeComponents (config * ApiClientConfig ) {
147+ 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+
159+ self .Components = * config .Components
160+ if config .Proxy != nil {
161+ pfxlog .Logger ().Warn ("components were provided along with a proxy function on the ApiClientConfig, it is being ignored, if needed properly set on components" )
162+ }
163+ return
164+ }
165+
141166 components := NewComponentsWithConfig (& ComponentsConfig {
142167 Proxy : config .Proxy ,
143168 })
144- components .HttpTransport .TLSClientConfig .RootCAs = config .CaPool
169+
170+ tlsClientConfig := components .TlsAwareTransport .GetTlsClientConfig ()
171+ tlsClientConfig .RootCAs = config .CaPool
145172 components .CaPool = config .CaPool
146173
147174 self .Components = * components
148175}
149176
150- // NewRuntime creates an OpenAPI runtime configured for the specified API endpoint.
177+ // NewRuntime creates an OpenAPI runtime for communicating with a controller endpoint. Used for HA failover to add multiple controller endpoints .
151178func NewRuntime (apiUrl * url.URL , schemes []string , httpClient * http.Client ) * openapiclient.Runtime {
152179 return openapiclient .NewWithClient (apiUrl .Host , apiUrl .Path , schemes , httpClient )
153180}
154181
155- // AuthenticateRequest implements the openapi runtime.ClientAuthInfoWriter interface from the OpenAPI libraries. It is used
156- // to authenticate outgoing requests .
182+ // AuthenticateRequest authenticates outgoing API requests using the current session or credentials.
183+ // It implements the openapi runtime.ClientAuthInfoWriter interface .
157184func (self * BaseClient [A ]) AuthenticateRequest (request runtime.ClientRequest , registry strfmt.Registry ) error {
158185 if self .AuthInfoWriter != nil {
159186 return self .AuthInfoWriter .AuthenticateRequest (request , registry )
@@ -184,8 +211,7 @@ func (self *BaseClient[A]) AuthenticateRequest(request runtime.ClientRequest, re
184211 return nil
185212}
186213
187- // ProcessControllers queries the authenticated controller for its list of peer controllers
188- // and registers them for high-availability failover.
214+ // ProcessControllers discovers peer controllers and registers them for HA failover. Called after successful authentication.
189215func (self * BaseClient [A ]) ProcessControllers (authEnabledApi AuthEnabledApi ) {
190216 list , err := authEnabledApi .ListControllers ()
191217
0 commit comments