Skip to content

Commit

Permalink
added WithTLS and WithSecurityBuilder
Browse files Browse the repository at this point in the history
documented precedence order
  • Loading branch information
critterjohnson committed Dec 23, 2019
1 parent f6d8f1e commit 2424156
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func main() {
client, _ := clients.NewClientBuilder().
WithHost("localhost").
WithPort("443").
WithSecurity(security).
WithTLS(security).
Build()

}
Expand Down
13 changes: 10 additions & 3 deletions clients/client_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package clients

import (
"crypto/tls"
"net/http"
)

Expand Down Expand Up @@ -45,8 +46,14 @@ func (b *ClientBuilder) WithPort(port int) *ClientBuilder {
return b
}

// WithSecurity sets the TLS configuration of the client.
func (b *ClientBuilder) WithSecurity(security SecurityConfig) *ClientBuilder {
b.config.Security = security
// WithSecurityBuilder sets the TLS configuration of the client from a SecurityBuilder.
func (b *ClientBuilder) WithSecurityBuilder(builder *SecurityBuilder) *ClientBuilder {
b.config.Security = builder.Config
return b
}

// WithTLS sets the TLS configuration of the client to a tls.Config. Takes precedence over WithSecurityBuilder.
func (b *ClientBuilder) WithTLS(config *tls.Config) *ClientBuilder {
b.config.config = config
return b
}
19 changes: 16 additions & 3 deletions clients/client_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,28 @@ func TestClientBuilder(t *testing.T) {
})
})

Convey(".WithSecurityBuilder is invoked", func() {

security := tests.MustGenerate(reflect.TypeOf(SecurityBuilder{}), t).Interface().(SecurityBuilder)

builder.WithSecurityBuilder(&security)

Convey("it sets the tls", func() {
So(builder.config.Security, ShouldResemble, security.Config)
})
})

Convey(".WithTLS is invoked", func() {

security := tests.MustGenerate(reflect.TypeOf(SecurityConfig{}), t).Interface().(SecurityConfig)
security := tests.MustGenerate(reflect.TypeOf(SecurityBuilder{}), t).Interface().(SecurityBuilder)
config, _ := security.Build()

builder.WithSecurity(security)
builder.WithTLS(config)

Convey("it sets the tls", func() {
So(builder.config.Security, ShouldResemble, security)
So(builder.config.config, ShouldResemble, config)
})

})

Convey(".Build is invoked", func() {
Expand Down
16 changes: 13 additions & 3 deletions clients/client_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,24 @@ type ClientConfig struct {

// Security defines the TLS configuration used by the client.
Security SecurityConfig `json:"security" mapstructure:"security" yaml:"security"`

// config is the tls config passed by ClientBuilder.WithTLS.
config *tls.Config
}

// Build creates an http.Client from the ClientConfig instance.
func (c *ClientConfig) Build() (*http.Client, error) {

configuration, err := c.Security.Build()
if err != nil {
return nil, errors.Wrap(err, "error building tls configuration for client")
var configuration *tls.Config
var err error

if c.config != (&tls.Config{}) {
configuration = c.config
} else {
configuration, err = c.Security.Build()
if err != nil {
return nil, errors.Wrap(err, "error building tls configuration for client")
}
}

client := &http.Client{
Expand Down
12 changes: 6 additions & 6 deletions clients/security_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import "crypto/tls"

// SecurityBuilder provides an builder for client tls.Config instances.
type SecurityBuilder struct {
config SecurityConfig
Config SecurityConfig
}

// NewSecurityBuilder returns a new instance of the SecurityBuilder structure.
Expand All @@ -28,7 +28,7 @@ func NewSecurityBuilder() *SecurityBuilder {

// Build creates a tls.Config from the SecurityBuilder.
func (b *SecurityBuilder) Build() (*tls.Config, error) {
return b.config.Build()
return b.Config.Build()
}

// WithAuthorities sets the certificate authorities trusted by the built tls.Config. The values must be URLs that point
Expand All @@ -38,7 +38,7 @@ func (b *SecurityBuilder) Build() (*tls.Config, error) {
// "base64" scheme is supported for providing the PEM encoded certifiate in the path of the URL directly. This is most
// applicable when the certificate data must be provided via an environement variable.
func (b *SecurityBuilder) WithAuthorities(authorities []string) *SecurityBuilder {
b.config.Authorities = authorities
b.Config.Authorities = authorities
return b
}

Expand All @@ -49,7 +49,7 @@ func (b *SecurityBuilder) WithAuthorities(authorities []string) *SecurityBuilder
// "base64" scheme is supported for providing the PEM encoded certifiate in the path of the URL directly. This is most
// applicable when the certificate data must be provided via an environement variable.
func (b *SecurityBuilder) WithCertificate(certificate string) *SecurityBuilder {
b.config.Certificate = certificate
b.Config.Certificate = certificate
return b
}

Expand All @@ -60,12 +60,12 @@ func (b *SecurityBuilder) WithCertificate(certificate string) *SecurityBuilder {
// "base64" scheme is supported for providing the PEM encoded certifiate in the path of the URL directly. This is most
// applicable when the certificate data must be provided via an environement variable.
func (b *SecurityBuilder) WithKey(key string) *SecurityBuilder {
b.config.Key = key
b.Config.Key = key
return b
}

// WithServer sets the server name used for certificate verification.
func (b *SecurityBuilder) WithServer(server string) *SecurityBuilder {
b.config.Server = server
b.Config.Server = server
return b
}
8 changes: 4 additions & 4 deletions clients/security_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestSecurityBuilder(t *testing.T) {
builder.WithAuthorities(authorities)

Convey("it sets the authorities", func() {
So(builder.config.Authorities, ShouldResemble, authorities)
So(builder.Config.Authorities, ShouldResemble, authorities)
})
})

Expand All @@ -59,7 +59,7 @@ func TestSecurityBuilder(t *testing.T) {
builder.WithCertificate(certificate)

Convey("it sets the certificate", func() {
So(builder.config.Certificate, ShouldEqual, certificate)
So(builder.Config.Certificate, ShouldEqual, certificate)
})
})

Expand All @@ -70,7 +70,7 @@ func TestSecurityBuilder(t *testing.T) {
builder.WithKey(key)

Convey("it sets the key", func() {
So(builder.config.Key, ShouldEqual, key)
So(builder.Config.Key, ShouldEqual, key)
})
})

Expand All @@ -81,7 +81,7 @@ func TestSecurityBuilder(t *testing.T) {
builder.WithServer(server)

Convey("it sets the server", func() {
So(builder.config.Server, ShouldEqual, server)
So(builder.Config.Server, ShouldEqual, server)
})
})
})
Expand Down

0 comments on commit 2424156

Please sign in to comment.