Skip to content

Commit

Permalink
feat: WithPort, WithInsecureSkipVerifyTLS
Browse files Browse the repository at this point in the history
  • Loading branch information
eliobischof committed Nov 11, 2024
1 parent b392fea commit eec761e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func newConnection(
tokenSource oauth2.TokenSource,
opts ...grpc.DialOption,
) (*grpc.ClientConn, error) {
transportCreds, err := transportCredentials(zitadel.Domain(), zitadel.IsTLS())
transportCreds, err := transportCredentials(zitadel.Domain(), zitadel.IsTLS(), zitadel.IsInsecureSkipVerifyTLS())

Check warning on line 99 in pkg/client/client.go

View check run for this annotation

Codecov / codecov/patch

pkg/client/client.go#L99

Added line #L99 was not covered by tests
if err != nil {
return nil, err
}
Expand Down
15 changes: 12 additions & 3 deletions pkg/client/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"crypto/tls"
"crypto/x509"

"golang.org/x/oauth2"
Expand Down Expand Up @@ -55,16 +56,24 @@ func requestMetadataFromToken(token *oauth2.Token) map[string]string {
}
}

func transportCredentials(domain string, tls bool) (credentials.TransportCredentials, error) {
if !tls {
func transportCredentials(domain string, withTLS bool, insecureSkipVerifyTLS bool) (credentials.TransportCredentials, error) {
if !withTLS {

Check warning on line 60 in pkg/client/credentials.go

View check run for this annotation

Codecov / codecov/patch

pkg/client/credentials.go#L59-L60

Added lines #L59 - L60 were not covered by tests
return insecure.NewCredentials(), nil
}
tlsConfig := &tls.Config{
ServerName: domain,
InsecureSkipVerify: insecureSkipVerifyTLS,
}
if insecureSkipVerifyTLS {
return credentials.NewTLS(tlsConfig), nil
}

Check warning on line 69 in pkg/client/credentials.go

View check run for this annotation

Codecov / codecov/patch

pkg/client/credentials.go#L63-L69

Added lines #L63 - L69 were not covered by tests
ca, err := x509.SystemCertPool()
if err != nil {
return nil, err
}
if ca == nil {
ca = x509.NewCertPool()
}
return credentials.NewClientTLSFromCert(ca, domain), nil
tlsConfig.RootCAs = ca
return credentials.NewTLS(tlsConfig), nil

Check warning on line 78 in pkg/client/credentials.go

View check run for this annotation

Codecov / codecov/patch

pkg/client/credentials.go#L77-L78

Added lines #L77 - L78 were not covered by tests
}
27 changes: 21 additions & 6 deletions pkg/zitadel/zitadel.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import (
// This includes authentication, authorization as well as explicit API interaction
// and is dependent of the provided information and initialization of such.
type Zitadel struct {
domain string
port string
tls bool
domain string
port string
tls bool
insecureSkipVerifyTLS bool
}

func New(domain string, options ...Option) *Zitadel {
zitadel := &Zitadel{
domain: domain,
port: "443",
tls: true,
domain: domain,
port: "443",
tls: true,
insecureSkipVerifyTLS: false,

Check warning on line 23 in pkg/zitadel/zitadel.go

View check run for this annotation

Codecov / codecov/patch

pkg/zitadel/zitadel.go#L20-L23

Added lines #L20 - L23 were not covered by tests
}
for _, option := range options {
option(zitadel)
Expand All @@ -30,13 +32,22 @@ func New(domain string, options ...Option) *Zitadel {
type Option func(*Zitadel)

// WithInsecure allows to connect to a ZITADEL instance running without TLS
// Do not use in production
func WithInsecure(port string) Option {
return func(z *Zitadel) {
z.port = port
z.tls = false
}
}

// WithInsecureSkipVerifyTLS allows to connect to a ZITADEL instance running with TLS but has an untrusted certificate
// Do not use in production
func WithInsecureSkipVerifyTLS() Option {
return func(z *Zitadel) {
z.insecureSkipVerifyTLS = true
}

Check warning on line 48 in pkg/zitadel/zitadel.go

View check run for this annotation

Codecov / codecov/patch

pkg/zitadel/zitadel.go#L45-L48

Added lines #L45 - L48 were not covered by tests
}

// WithPort allows to connect to a ZITADEL instance running on a different port
func WithPort(port uint16) Option {
return func(z *Zitadel) {
Expand All @@ -61,6 +72,10 @@ func (z *Zitadel) IsTLS() bool {
return z.tls
}

func (z *Zitadel) IsInsecureSkipVerifyTLS() bool {
return z.insecureSkipVerifyTLS

Check warning on line 76 in pkg/zitadel/zitadel.go

View check run for this annotation

Codecov / codecov/patch

pkg/zitadel/zitadel.go#L75-L76

Added lines #L75 - L76 were not covered by tests
}

func (z *Zitadel) Domain() string {
return z.domain
}
Expand Down

0 comments on commit eec761e

Please sign in to comment.