Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tweak NewSdkCollectionFromEnv and fix the http-client example #662

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

@andrewpmartinez andrewpmartinez Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an externally consumed function, this would be a major bump if left as as.

You chould make it take an an optional number of strings, which can be 0 or more.

NewSdkCollectionFromEnv(envVariable string, configTypes ...string)

Which allows 0 or more config types to be specified. You still access configTypes like a slice.

This allow old calls NewSdkCollection("myEnv") and NewSdkCollection("myEnv", "config1", "config2") or using an exploded slice NewSdkCollection("myEnv", myConfigs...) where myConfig := []string{"confi1","config2"}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is a grand idea. done

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, []string{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.

Loading