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

TLS dialer #106

Open
wants to merge 1 commit into
base: master
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
24 changes: 18 additions & 6 deletions dnshostprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ type DNSHostProvider struct {
lookupHost func(string) ([]string, error) // Override of net.LookupHost, for testing.
}

func addrsByHostname(server string) ([]string, error) {
Copy link

Choose a reason for hiding this comment

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

nit: since this is used in the function below, this function would be sorted below it.

res := []string{}
host, port, err := net.SplitHostPort(server)
if err != nil {
return nil, err
}
addrs, err := net.LookupHost(host)
if err != nil {
return nil, err
}
for _, addr := range addrs {
res = append(res, net.JoinHostPort(addr, port))
}
return res, nil
}

// Init is called first, with the servers specified in the connection
// string. It uses DNS to look up addresses for each server, then
// shuffles them all together.
Expand All @@ -32,16 +48,12 @@ func (hp *DNSHostProvider) Init(servers []string) error {

found := []string{}
for _, server := range servers {
host, port, err := net.SplitHostPort(server)
if err != nil {
return err
}
addrs, err := lookupHost(host)
addrs, err := addrsByHostname(server)
if err != nil {
return err
}
for _, addr := range addrs {
found = append(found, net.JoinHostPort(addr, port))
found = append(found, addr)
}
}

Expand Down
60 changes: 60 additions & 0 deletions tls_dialer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package zk

import (
"crypto/tls"
"crypto/x509"
"errors"
"net"
"os"
"time"
)

func CreateTLSConfig(rootCAFile, certFile, keyFile string) (*tls.Config, error) {
Copy link

Choose a reason for hiding this comment

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

This should not assume files, and have io.Reader as input for all three inputs. This allows a better library experience having tests and callers pass in content as they see fit.

also the tls package allows this to work then with https://pkg.go.dev/crypto/tls#X509KeyPair

rootCABytes, err := os.ReadFile(rootCAFile)
if err != nil {
return nil, err
}

rootCA := x509.NewCertPool()
ok := rootCA.AppendCertsFromPEM(rootCABytes)
if !ok {
return nil, err
}

cert, err := tls.LoadX509KeyPair(certFile, keyFile)
if err != nil {
return nil, err
}

return &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: rootCA,
}, nil
}

func GetTLSDialer(servers []string, dialer *net.Dialer, tlsConfig *tls.Config) (Dialer, error) {
Copy link

Choose a reason for hiding this comment

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

is this function then used as the option to use as an option? The notion would normally be that the surrounding library would pass in this dialer themselves. If we wanted to support TLS Dial in the zk lib. I would want to have this outside the main zk package and in a supporting package

if len(servers) == 0 {
return nil, errors.New("zk: server list must not be empty")
}
srvs := FormatServers(servers)

addrToHostname := map[string]string{}

for _, server := range srvs {
ips, err := addrsByHostname(server)
if err != nil {
return nil, err
}
for _, ip := range ips {
addrToHostname[ip] = server
}
}

return func(network, address string, _ time.Duration) (net.Conn, error) {
server, ok := addrToHostname[address]
if !ok {
server = address
}
return tls.DialWithDialer(dialer, network, server, tlsConfig)
}, nil
}