Skip to content

Commit

Permalink
adjust trickle down occurences of tlsconfig.Trace
Browse files Browse the repository at this point in the history
Signed-off-by: Antoine Grondin <[email protected]>
  • Loading branch information
aybabtme committed Aug 4, 2020
1 parent 9988fc0 commit fdb921e
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 100 deletions.
11 changes: 1 addition & 10 deletions v2/examples/spiffe-grpc/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,8 @@ func main() {
// Allowed SPIFFE ID
serverID := spiffeid.Must("example.org", "server")

localTrace := tlsconfig.Trace{
GetTLSCertificateStart: func(tlsconfig.GetTLSCertificateStart) {
log.Printf("got start of GetTLSCertificate\n")
},
GetTLSCertificateEnd: func(tlsconfig.GetTLSCertificateEnd) {
log.Printf("got end of GetTLSCertificate\n")
},
}

// Create a `tls.Config` to allow mTLS connections, and verify that presented certificate has SPIFFE ID `spiffe://example.org/server`
tlsConfig := tlsconfig.MTLSClientConfig(source, source, tlsconfig.AuthorizeID(serverID), localTrace)
tlsConfig := tlsconfig.MTLSClientConfig(source, source, tlsconfig.AuthorizeID(serverID))
conn, err := grpc.DialContext(ctx, "localhost:50051", grpc.WithTransportCredentials(credentials.NewTLS(tlsConfig)))
if err != nil {
log.Fatalf("Error creating dial: %v", err)
Expand Down
11 changes: 1 addition & 10 deletions v2/examples/spiffe-grpc/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,8 @@ func main() {
// Allowed SPIFFE ID
clientID := spiffeid.Must("example.org", "client")

localTrace := tlsconfig.Trace{
GetTLSCertificateStart: func(tlsconfig.GetTLSCertificateStart) {
log.Printf("got start of GetTLSCertificate\n")
},
GetTLSCertificateEnd: func(tlsconfig.GetTLSCertificateEnd) {
log.Printf("got end of GetTLSCertificate\n")
},
}

// Create a `tls.Config` to allow mTLS connections, and verify that presented certificate has SPIFFE ID `spiffe://example.org/client`
tlsConfig := tlsconfig.MTLSServerConfig(source, source, tlsconfig.AuthorizeID(clientID), localTrace)
tlsConfig := tlsconfig.MTLSServerConfig(source, source, tlsconfig.AuthorizeID(clientID))
s := grpc.NewServer(grpc.Creds(credentials.NewTLS(tlsConfig)))

lis, err := net.Listen("tcp", "127.0.0.1:50051")
Expand Down
11 changes: 1 addition & 10 deletions v2/examples/spiffe-http/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,8 @@ func main() {
// Allowed SPIFFE ID
serverID := spiffeid.Must("example.org", "server")

localTrace := tlsconfig.Trace{
GetTLSCertificateStart: func(tlsconfig.GetTLSCertificateStart) {
log.Printf("got start of GetTLSCertificate\n")
},
GetTLSCertificateEnd: func(tlsconfig.GetTLSCertificateEnd) {
log.Printf("got end of GetTLSCertificate\n")
},
}

// Create a `tls.Config` to allow mTLS connections, and verify that presented certificate has SPIFFE ID `spiffe://example.org/server`
tlsConfig := tlsconfig.MTLSClientConfig(source, source, tlsconfig.AuthorizeID(serverID), localTrace)
tlsConfig := tlsconfig.MTLSClientConfig(source, source, tlsconfig.AuthorizeID(serverID))
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsConfig,
Expand Down
11 changes: 1 addition & 10 deletions v2/examples/spiffe-http/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,8 @@ func main() {
// Allowed SPIFFE ID
clientID := spiffeid.Must("example.org", "client")

localTrace := tlsconfig.Trace{
GetTLSCertificateStart: func(tlsconfig.GetTLSCertificateStart) {
log.Printf("got start of GetTLSCertificate\n")
},
GetTLSCertificateEnd: func(tlsconfig.GetTLSCertificateEnd) {
log.Printf("got end of GetTLSCertificate\n")
},
}

// Create a `tls.Config` to allow mTLS connections, and verify that presented certificate has SPIFFE ID `spiffe://example.org/client`
tlsConfig := tlsconfig.MTLSServerConfig(source, source, tlsconfig.AuthorizeID(clientID), localTrace)
tlsConfig := tlsconfig.MTLSServerConfig(source, source, tlsconfig.AuthorizeID(clientID))
server := &http.Server{
Addr: ":8443",
TLSConfig: tlsConfig,
Expand Down
11 changes: 1 addition & 10 deletions v2/examples/spiffe-jwt-using-proxy/proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,12 @@ func main() {

http.HandleFunc("/", handler(proxy))

localTrace := tlsconfig.Trace{
GetTLSCertificateStart: func(tlsconfig.GetTLSCertificateStart) {
log.Printf("got start of GetTLSCertificate\n")
},
GetTLSCertificateEnd: func(tlsconfig.GetTLSCertificateEnd) {
log.Printf("got end of GetTLSCertificate\n")
},
}

// Create an HTTP server using a TLS configuration that doesn't require
// client certificates, because the proxy is not in charge of authenticating
// the clients.
server := &http.Server{
Addr: ":8443",
TLSConfig: tlsconfig.TLSServerConfig(x509Source, localTrace),
TLSConfig: tlsconfig.TLSServerConfig(x509Source),
}
log.Fatal(server.ListenAndServeTLS("", ""))
}
Expand Down
11 changes: 1 addition & 10 deletions v2/examples/spiffe-jwt-using-proxy/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,9 @@ func main() {
}
http.Handle("/", auth.authenticateClient(http.HandlerFunc(index)))

localTrace := tlsconfig.Trace{
GetTLSCertificateStart: func(tlsconfig.GetTLSCertificateStart) {
log.Printf("got start of GetTLSCertificate\n")
},
GetTLSCertificateEnd: func(tlsconfig.GetTLSCertificateEnd) {
log.Printf("got end of GetTLSCertificate\n")
},
}

server := &http.Server{
Addr: ":8080",
TLSConfig: tlsconfig.TLSServerConfig(x509Source, localTrace),
TLSConfig: tlsconfig.TLSServerConfig(x509Source),
}
log.Fatal(server.ListenAndServeTLS("", ""))
}
11 changes: 1 addition & 10 deletions v2/examples/spiffe-jwt/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,8 @@ func main() {
}
defer x509Source.Close()

localTrace := tlsconfig.Trace{
GetTLSCertificateStart: func(tlsconfig.GetTLSCertificateStart) {
log.Printf("got start of GetTLSCertificate\n")
},
GetTLSCertificateEnd: func(tlsconfig.GetTLSCertificateEnd) {
log.Printf("got end of GetTLSCertificate\n")
},
}

// Create a `tls.Config` with configuration to allow TLS communication with client
tlsConfig := tlsconfig.TLSServerConfig(x509Source, localTrace)
tlsConfig := tlsconfig.TLSServerConfig(x509Source)
server := &http.Server{
Addr: ":8443",
TLSConfig: tlsConfig,
Expand Down
12 changes: 1 addition & 11 deletions v2/federation/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package federation_test

import (
"context"
"log"
"net/http"

"github.com/spiffe/go-spiffe/v2/bundle/spiffebundle"
Expand Down Expand Up @@ -143,19 +142,10 @@ func ExampleHandler_sPIFFEAuth() {
}
defer bundleSource.Close()

localTrace := tlsconfig.Trace{
GetTLSCertificateStart: func(tlsconfig.GetTLSCertificateStart) {
log.Printf("got start of GetTLSCertificate\n")
},
GetTLSCertificateEnd: func(tlsconfig.GetTLSCertificateEnd) {
log.Printf("got end of GetTLSCertificate\n")
},
}

server := http.Server{
Addr: ":8443",
Handler: federation.Handler(trustDomain, bundleSource, logger.Null),
TLSConfig: tlsconfig.TLSServerConfig(x509Source, localTrace),
TLSConfig: tlsconfig.TLSServerConfig(x509Source),
}
if err := server.ListenAndServeTLS("", ""); err != nil {
// TODO: handle error
Expand Down
11 changes: 1 addition & 10 deletions v2/internal/test/fakebundleendpoint/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,9 @@ func WithTestBundles(bundles ...*spiffebundle.Bundle) ServerOption {
}

func WithSPIFFEAuth(bundle *spiffebundle.Bundle, svid *x509svid.SVID) ServerOption {
localTrace := tlsconfig.Trace{
GetTLSCertificateStart: func(tlsconfig.GetTLSCertificateStart) {
fmt.Printf("got start of GetTLSCertificate\n")
},
GetTLSCertificateEnd: func(tlsconfig.GetTLSCertificateEnd) {
fmt.Printf("got end of GetTLSCertificate\n")
},
}

return serverOption(func(s *Server) {
s.rootCAs = x509util.NewCertPool(bundle.X509Authorities())
s.tlscfg = tlsconfig.TLSServerConfig(svid, localTrace)
s.tlscfg = tlsconfig.TLSServerConfig(svid)
})
}

Expand Down
4 changes: 2 additions & 2 deletions v2/spiffetls/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func DialWithMode(ctx context.Context, network, addr string, mode DialMode, opti
case tlsClientMode:
tlsconfig.HookTLSClientConfig(tlsConfig, m.bundle, m.authorizer)
case mtlsClientMode:
tlsconfig.HookMTLSClientConfig(tlsConfig, m.svid, m.bundle, m.authorizer, opt.tlsConfigTrace)
tlsconfig.HookMTLSClientConfig(tlsConfig, m.svid, m.bundle, m.authorizer, opt.mtlsClientConfigOpts...)
case mtlsWebClientMode:
tlsconfig.HookMTLSWebClientConfig(tlsConfig, m.svid, m.roots, opt.tlsConfigTrace)
tlsconfig.HookMTLSWebClientConfig(tlsConfig, m.svid, m.roots, opt.mtlsWebClientConfigOpts...)
default:
return nil, spiffetlsErr.New("unknown client mode: %v", m.mode)
}
Expand Down
4 changes: 2 additions & 2 deletions v2/spiffetls/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ func NewListenerWithMode(ctx context.Context, inner net.Listener, mode ListenMod

switch m.mode {
case tlsServerMode:
tlsconfig.HookTLSServerConfig(tlsConfig, m.svid, opt.tlsConfigTrace)
tlsconfig.HookTLSServerConfig(tlsConfig, m.svid, opt.tlsServerConfigOpts...)
case mtlsServerMode:
tlsconfig.HookMTLSServerConfig(tlsConfig, m.svid, m.bundle, m.authorizer, opt.tlsConfigTrace)
tlsconfig.HookMTLSServerConfig(tlsConfig, m.svid, m.bundle, m.authorizer, opt.mtlsServerConfigOpts...)
case mtlsWebServerMode:
tlsconfig.HookMTLSWebServerConfig(tlsConfig, m.cert, m.bundle, m.authorizer)
default:
Expand Down
42 changes: 37 additions & 5 deletions v2/spiffetls/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ func (fn dialOption) apply(c *dialConfig) {
}

type dialConfig struct {
baseTLSConf *tls.Config
dialer *net.Dialer
tlsConfigTrace tlsconfig.Trace
baseTLSConf *tls.Config
dialer *net.Dialer
mtlsClientConfigOpts []tlsconfig.MTLSClientConfigOption
mtlsWebClientConfigOpts []tlsconfig.MTLSWebClientConfigOption
}

type listenOption func(*listenConfig)

type listenConfig struct {
baseTLSConf *tls.Config
tlsConfigTrace tlsconfig.Trace
baseTLSConf *tls.Config
tlsServerConfigOpts []tlsconfig.TLSServerConfigOption
mtlsServerConfigOpts []tlsconfig.MTLSServerConfigOption
}

func (fn listenOption) apply(c *listenConfig) {
Expand All @@ -47,6 +49,21 @@ func WithDialTLSConfigBase(base *tls.Config) DialOption {
})
}

// WithDialMTLSClientConfigOption provides options to use when doing Client mTLS.
func WithDialMTLSClientConfigOption(opts ...tlsconfig.MTLSClientConfigOption) DialOption {
return dialOption(func(c *dialConfig) {
c.mtlsClientConfigOpts = opts
})
}

// WithDialMTLSWebClientConfigOption provides options to use when doing Client mTLS
// as a web client.
func WithDialMTLSWebClientConfigOption(opts ...tlsconfig.MTLSWebClientConfigOption) DialOption {
return dialOption(func(c *dialConfig) {
c.mtlsWebClientConfigOpts = opts
})
}

// WithDialer provides a net dialer to use. If unset, the standard net dialer
// will be used.
func WithDialer(dialer *net.Dialer) DialOption {
Expand All @@ -68,3 +85,18 @@ func WithListenTLSConfigBase(base *tls.Config) ListenOption {
c.baseTLSConf = base
})
}

// WithDialTLSServerConfigOption provides options to use when doing Server mTLS.
func WithDialTLSServerConfigOption(opts ...tlsconfig.TLSServerConfigOption) ListenOption {
return listenOption(func(c *listenConfig) {
c.tlsServerConfigOpts = opts
})
}

// WithDialMTLSServerConfigOption provides options to use when doing Server mTLS
// as a web client.
func WithDialMTLSServerConfigOption(opts ...tlsconfig.MTLSServerConfigOption) ListenOption {
return listenOption(func(c *listenConfig) {
c.mtlsServerConfigOpts = opts
})
}

0 comments on commit fdb921e

Please sign in to comment.