Skip to content

Commit 4e2cb3a

Browse files
committed
Add API for controlling proxy use when connecting to controller
1 parent c235d4f commit 4e2cb3a

File tree

5 files changed

+74
-19
lines changed

5 files changed

+74
-19
lines changed

edge-apis/clients.go

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,12 @@ func (self *BaseClient[A]) Authenticate(credentials Credentials, configTypesOver
123123
}
124124

125125
// initializeComponents assembles the lower level components necessary for the go-swagger/openapi facilities.
126-
func (self *BaseClient[A]) initializeComponents(apiUrls []*url.URL, caPool *x509.CertPool) {
127-
components := NewComponents()
128-
components.HttpTransport.TLSClientConfig.RootCAs = caPool
129-
components.CaPool = caPool
126+
func (self *BaseClient[A]) initializeComponents(config *ApiClientConfig) {
127+
components := NewComponentsWithConfig(&ComponentsConfig{
128+
Proxy: config.Proxy,
129+
})
130+
components.HttpTransport.TLSClientConfig.RootCAs = config.CaPool
131+
components.CaPool = config.CaPool
130132

131133
self.Components = *components
132134
}
@@ -205,6 +207,13 @@ type ManagementApiClient struct {
205207
BaseClient[ZitiEdgeManagement]
206208
}
207209

210+
type ApiClientConfig struct {
211+
ApiUrls []*url.URL
212+
CaPool *x509.CertPool
213+
TotpCallback func(chan string)
214+
Proxy func(r *http.Request) (*url.URL, error)
215+
}
216+
208217
// NewManagementApiClient will assemble an ManagementApiClient. The apiUrl should be the full URL
209218
// to the Edge Management API (e.g. `https://example.com/edge/management/v1`).
210219
//
@@ -217,16 +226,25 @@ type ManagementApiClient struct {
217226
// to obtain and verify the target controllers CAs. Tools should allow users to verify and accept new controllers
218227
// that have not been verified from an outside secret (such as an enrollment token).
219228
func NewManagementApiClient(apiUrls []*url.URL, caPool *x509.CertPool, totpCallback func(chan string)) *ManagementApiClient {
229+
return NewManagementApiClientWithConfig(&ApiClientConfig{
230+
ApiUrls: apiUrls,
231+
CaPool: caPool,
232+
TotpCallback: totpCallback,
233+
Proxy: http.ProxyFromEnvironment,
234+
})
235+
}
236+
237+
func NewManagementApiClientWithConfig(config *ApiClientConfig) *ManagementApiClient {
220238
ret := &ManagementApiClient{}
221239
ret.Schemes = rest_management_api_client.DefaultSchemes
222240
ret.ApiBinding = "edge-management"
223241
ret.ApiVersion = "v1"
224-
ret.ApiUrls = apiUrls
225-
ret.initializeComponents(apiUrls, caPool)
242+
ret.ApiUrls = config.ApiUrls
243+
ret.initializeComponents(config)
226244

227245
transportPool := NewClientTransportPoolRandom()
228246

229-
for _, apiUrl := range apiUrls {
247+
for _, apiUrl := range config.ApiUrls {
230248
newRuntime := NewRuntime(apiUrl, ret.Schemes, ret.Components.HttpClient)
231249
newRuntime.DefaultAuthentication = ret
232250
transportPool.Add(apiUrl, newRuntime)
@@ -235,7 +253,7 @@ func NewManagementApiClient(apiUrls []*url.URL, caPool *x509.CertPool, totpCallb
235253
newApi := rest_management_api_client.New(transportPool, nil)
236254
api := ZitiEdgeManagement{
237255
ZitiEdgeManagement: newApi,
238-
TotpCallback: totpCallback,
256+
TotpCallback: config.TotpCallback,
239257
ClientTransportPool: transportPool,
240258
}
241259

@@ -261,17 +279,26 @@ type ClientApiClient struct {
261279
// to obtain and verify the target controllers CAs. Tools should allow users to verify and accept new controllers
262280
// that have not been verified from an outside secret (such as an enrollment token).
263281
func NewClientApiClient(apiUrls []*url.URL, caPool *x509.CertPool, totpCallback func(chan string)) *ClientApiClient {
282+
return NewClientApiClientWithConfig(&ApiClientConfig{
283+
ApiUrls: apiUrls,
284+
CaPool: caPool,
285+
TotpCallback: totpCallback,
286+
Proxy: http.ProxyFromEnvironment,
287+
})
288+
}
289+
290+
func NewClientApiClientWithConfig(config *ApiClientConfig) *ClientApiClient {
264291
ret := &ClientApiClient{}
265292
ret.ApiBinding = "edge-client"
266293
ret.ApiVersion = "v1"
267294
ret.Schemes = rest_client_api_client.DefaultSchemes
268-
ret.ApiUrls = apiUrls
295+
ret.ApiUrls = config.ApiUrls
269296

270-
ret.initializeComponents(apiUrls, caPool)
297+
ret.initializeComponents(config)
271298

272299
transportPool := NewClientTransportPoolRandom()
273300

274-
for _, apiUrl := range apiUrls {
301+
for _, apiUrl := range config.ApiUrls {
275302
newRuntime := NewRuntime(apiUrl, ret.Schemes, ret.Components.HttpClient)
276303
newRuntime.DefaultAuthentication = ret
277304
transportPool.Add(apiUrl, newRuntime)
@@ -280,7 +307,7 @@ func NewClientApiClient(apiUrls []*url.URL, caPool *x509.CertPool, totpCallback
280307
newApi := rest_client_api_client.New(transportPool, nil)
281308
api := ZitiEdgeClient{
282309
ZitiEdgeClient: newApi,
283-
TotpCallback: totpCallback,
310+
TotpCallback: config.TotpCallback,
284311
ClientTransportPool: transportPool,
285312
}
286313
ret.API = &api

edge-apis/component.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/openziti/edge-api/rest_util"
66
"net/http"
77
"net/http/cookiejar"
8+
"net/url"
89
"time"
910
)
1011

@@ -17,12 +18,22 @@ type Components struct {
1718
CaPool *x509.CertPool
1819
}
1920

21+
type ComponentsConfig struct {
22+
Proxy func(*http.Request) (*url.URL, error)
23+
}
24+
2025
// NewComponents assembles a new set of components with reasonable production defaults.
2126
func NewComponents() *Components {
27+
return NewComponentsWithConfig(&ComponentsConfig{
28+
Proxy: http.ProxyFromEnvironment,
29+
})
30+
}
31+
32+
// NewComponentsWithConfig assembles a new set of components with reasonable production defaults.
33+
func NewComponentsWithConfig(cfg *ComponentsConfig) *Components {
2234
tlsClientConfig, _ := rest_util.NewTlsConfig()
2335

2436
httpTransport := &http.Transport{
25-
Proxy: http.ProxyFromEnvironment,
2637
TLSClientConfig: tlsClientConfig,
2738
ForceAttemptHTTP2: true,
2839
MaxIdleConns: 10,
@@ -31,6 +42,10 @@ func NewComponents() *Components {
3142
ExpectContinueTimeout: 1 * time.Second,
3243
}
3344

45+
if cfg != nil && cfg.Proxy != nil {
46+
httpTransport.Proxy = cfg.Proxy
47+
}
48+
3449
jar, _ := cookiejar.New(nil)
3550

3651
httpClient := &http.Client{

version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.23
1+
0.24

ziti/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"github.com/openziti/identity"
2424
apis "github.com/openziti/sdk-golang/edge-apis"
2525
"github.com/pkg/errors"
26+
"net/http"
27+
"net/url"
2628
"os"
2729
)
2830

@@ -48,6 +50,10 @@ type Config struct {
4850
//EnableHa will signal to the SDK to query and use OIDC authentication which is required for HA controller setups.
4951
//This is a temporary feature flag that will be removed and "default to true" at a later date.
5052
EnableHa bool `json:"enableHa"`
53+
54+
//Allows providing a function which controls how/where requests are proxied. See [http.Transport.Proxy] for
55+
//more information
56+
CtrlProxy func(*http.Request) (*url.URL, error)
5157
}
5258

5359
// NewConfig will create a new Config object from a provided Ziti Edge Client API URL and identity configuration.

ziti/contexts.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,10 @@ func NewContextWithOpts(cfg *Config, options *Options) (Context, error) {
115115
apiUrls = append(apiUrls, apiUrl)
116116
}
117117

118-
newContext.CtrlClt = &CtrlClient{
119-
ClientApiClient: edge_apis.NewClientApiClient(apiUrls, cfg.Credentials.GetCaPool(), func(codeCh chan string) {
118+
apiClientConfig := &edge_apis.ApiClientConfig{
119+
ApiUrls: apiUrls,
120+
CaPool: cfg.Credentials.GetCaPool(),
121+
TotpCallback: func(codeCh chan string) {
120122
provider := rest_model.MfaProvidersZiti
121123

122124
authQuery := &rest_model.AuthQueryDetail{
@@ -140,9 +142,14 @@ func NewContextWithOpts(cfg *Config, options *Options) (Context, error) {
140142
return nil
141143
})
142144
}
143-
}),
144-
Credentials: cfg.Credentials,
145-
ConfigTypes: cfg.ConfigTypes,
145+
},
146+
Proxy: cfg.CtrlProxy,
147+
}
148+
149+
newContext.CtrlClt = &CtrlClient{
150+
ClientApiClient: edge_apis.NewClientApiClientWithConfig(apiClientConfig),
151+
Credentials: cfg.Credentials,
152+
ConfigTypes: cfg.ConfigTypes,
146153
}
147154

148155
newContext.CtrlClt.ClientApiClient.SetAllowOidcDynamicallyEnabled(cfg.EnableHa)

0 commit comments

Comments
 (0)