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

Merged
merged 5 commits into from
Nov 11, 2024
Merged
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
4 changes: 2 additions & 2 deletions examples/tls/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func run() error {
return err
}

certs := []tls.Certificate{cert}
server, err := wire.NewServer(handler, wire.Certificates(certs), wire.Logger(logger), wire.MessageBufferSize(100))
config := &tls.Config{Certificates: []tls.Certificate{cert}}
server, err := wire.NewServer(handler, wire.TLSConfig(config), wire.Logger(logger), wire.MessageBufferSize(100))
if err != nil {
return err
}
Expand Down
10 changes: 2 additions & 8 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 srv.TLSConfig == nil || len(srv.TLSConfig.Certificates) == 0 {
srv.logger.Debug("no TLS certificates available continuing with a insecure connection")
return srv.sslUnsupported(conn, reader, version)
}
Expand All @@ -156,15 +156,9 @@ 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,
}

// 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, srv.TLSConfig)
reader = buffer.NewReader(srv.logger, conn, srv.BufferedMsgSize)

version, err = srv.readVersion(reader)
Expand Down
25 changes: 3 additions & 22 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package wire
import (
"context"
"crypto/tls"
"crypto/x509"
"log/slog"
"regexp"
"strconv"
Expand Down Expand Up @@ -146,29 +145,11 @@ func MessageBufferSize(size int) OptionFn {
}
}

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

// ClientCAs sets the given Client CAs to be used, by the server, to verify a
// secure connection between the front-end (client) and back-end (server).
func ClientCAs(cas *x509.CertPool) OptionFn {
return func(srv *Server) error {
srv.ClientCAs = cas
return nil
}
}

// ClientAuth sets the given Client Auth to be used, by the server, to verify a
// secure connection between the front-end (client) and back-end (server).
func ClientAuth(authType tls.ClientAuthType) OptionFn {
return func(srv *Server) error {
srv.ClientAuth = authType
srv.TLSConfig = config
return nil
}
}
Expand Down
5 changes: 1 addition & 4 deletions wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package wire
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"log/slog"
Expand Down Expand Up @@ -60,9 +59,7 @@ type Server struct {
Auth AuthStrategy
BufferedMsgSize int
Parameters Parameters
Certificates []tls.Certificate
ClientCAs *x509.CertPool
ClientAuth tls.ClientAuthType
TLSConfig *tls.Config
parse ParseFn
Session SessionHandler
Statements StatementCache
Expand Down
Loading