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

Add a TLSConfig option #67

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 11 additions & 6 deletions handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (srv *Server) potentialConnUpgrade(conn net.Conn, reader *buffer.Reader, ve

srv.logger.Debug("attempting to upgrade the client to a TLS connection")

if len(srv.Certificates) == 0 {
if len(srv.Certificates) == 0 && srv.TLSConfig == nil {
srv.logger.Debug("no TLS certificates available continuing with a insecure connection")
return srv.sslUnsupported(conn, reader, version)
}
Expand All @@ -156,15 +156,20 @@ func (srv *Server) potentialConnUpgrade(conn net.Conn, reader *buffer.Reader, ve
return conn, reader, version, err
}

tlsConfig := tls.Config{
Certificates: srv.Certificates,
ClientAuth: srv.ClientAuth,
ClientCAs: srv.ClientCAs,
var tlsConfig *tls.Config
if srv.TLSConfig != nil {
tlsConfig = srv.TLSConfig
} else {
tlsConfig = &tls.Config{
Certificates: srv.Certificates,
ClientAuth: srv.ClientAuth,
ClientCAs: srv.ClientCAs,
}
Comment on lines +159 to +167
Copy link
Owner

Choose a reason for hiding this comment

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

I like the idea of simply passing a tls.Config. I am ok to introduce a breaking change and simply pass the tls.Config type directly.

}

// NOTE: initialize the TLS connection and construct a new buffered
// reader for the constructed TLS connection.
conn = tls.Server(conn, &tlsConfig)
conn = tls.Server(conn, tlsConfig)
reader = buffer.NewReader(srv.logger, conn, srv.BufferedMsgSize)

version, err = srv.readVersion(reader)
Expand Down
9 changes: 9 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ func MessageBufferSize(size int) OptionFn {
}
}

// TLSConfig sets the given TLS config to be used to initialize a
// secure connection between the front-end (client) and back-end (server).
func TLSConfig(config *tls.Config) OptionFn {
return func(srv *Server) error {
srv.TLSConfig = config
return nil
}
}

// Certificates sets the given TLS certificates to be used to initialize a
// secure connection between the front-end (client) and back-end (server).
func Certificates(certs []tls.Certificate) OptionFn {
Expand Down
1 change: 1 addition & 0 deletions wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Server struct {
Auth AuthStrategy
BufferedMsgSize int
Parameters Parameters
TLSConfig *tls.Config
Certificates []tls.Certificate
ClientCAs *x509.CertPool
ClientAuth tls.ClientAuthType
Expand Down