-
Notifications
You must be signed in to change notification settings - Fork 130
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
base: master
Are you sure you want to change the base?
TLS dialer #106
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should not assume files, and have 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
There was a problem hiding this comment.
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.