Skip to content

Commit

Permalink
Merge pull request #662 from openziti/fix-http-client-example
Browse files Browse the repository at this point in the history
tweak NewSdkCollectionFromEnv and fix the http-client example
  • Loading branch information
dovholuknf authored Jan 23, 2025
2 parents f60fb40 + b560078 commit a195721
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
5 changes: 4 additions & 1 deletion example/http-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import (
)

func newZitiClient() *http.Client {
ziti.DefaultCollection.ForAll(func(ctx ziti.Context) {
ctx.Authenticate()
})
zitiTransport := http.DefaultTransport.(*http.Transport).Clone() // copy default transport
zitiTransport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
dialer := ziti.NewDialerWithFallback(ctx, nil)
dialer := ziti.DefaultCollection.NewDialer()
return dialer.Dial(network, addr)
}
zitiTransport.TLSClientConfig.InsecureSkipVerify = true
Expand Down
5 changes: 4 additions & 1 deletion ziti/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewSdkCollection() *CtxCollection {

// NewSdkCollectionFromEnv will create an empty CtxCollection and then attempt to populate it from configuration files
// provided in a semicolon separate list of file paths retrieved from an environment variable.
func NewSdkCollectionFromEnv(envVariable string) *CtxCollection {
func NewSdkCollectionFromEnv(envVariable string, configTypes ...string) *CtxCollection {
collection := NewSdkCollection()

envValue := os.Getenv(envVariable)
Expand All @@ -74,13 +74,16 @@ func NewSdkCollectionFromEnv(envVariable string) *CtxCollection {
if identityFile == "" {
continue
}

cfg, err := NewConfigFromFile(identityFile)

if err != nil {
pfxlog.Logger().Errorf("failed to load config from file '%s'", identityFile)
continue
}

cfg.ConfigTypes = append(cfg.ConfigTypes, configTypes...)

//collection.NewContext stores the new ctx in its internal collection
_, err = collection.NewContext(cfg)

Expand Down
3 changes: 1 addition & 2 deletions ziti/default_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ var DefaultCollection *CtxCollection
const IdentitiesEnv = "ZITI_IDENTITIES"

func init() {
DefaultCollection = NewSdkCollectionFromEnv(IdentitiesEnv)
DefaultCollection.ConfigTypes = []string{InterceptV1, ClientConfigV1}
DefaultCollection = NewSdkCollectionFromEnv(IdentitiesEnv, InterceptV1, ClientConfigV1)
}

// Deprecated: ForAllContexts iterates over all Context instances in the DefaultCollection and call the provided function `f`.
Expand Down
2 changes: 1 addition & 1 deletion ziti/sdkinfo/build_info.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit a195721

Please sign in to comment.