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

Implement mTLS for servers #315

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
8 changes: 5 additions & 3 deletions src/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,11 @@ type tlsCommon struct {
* for protocol = "tls"
*/
type Tls struct {
AcmeHosts []string `toml:"acme_hosts" json:"acme_hosts"`
CertPath string `toml:"cert_path" json:"cert_path"`
KeyPath string `toml:"key_path" json:"key_path"`
AcmeHosts []string `toml:"acme_hosts" json:"acme_hosts"`
CertPath string `toml:"cert_path" json:"cert_path"`
KeyPath string `toml:"key_path" json:"key_path"`
ClientAuth string `toml:"client_auth" json:"client_auth"`
ClientCA string `toml:"client_ca" json:"client_ca"`
tlsCommon
}

Expand Down
2 changes: 1 addition & 1 deletion src/healthcheck/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func probe(t core.Target, cfg config.HealthcheckConfig, result chan<- CheckResul
timeout, _ := time.ParseDuration(cfg.Timeout)

checkResult := CheckResult{
Status: Unhealthy,
Status: Unhealthy,
Target: t,
}

Expand Down
29 changes: 29 additions & 0 deletions src/utils/tls/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,31 @@ var versions map[string]uint16 = map[string]uint16{
"tls1.2": tls.VersionTLS12,
}

/**
* TLS ClientAuthType mappings
*/
var client_auths map[string]tls.ClientAuthType = map[string]tls.ClientAuthType{
"no_client_cert": tls.NoClientCert,
"request": tls.RequestClientCert,
"require_any": tls.RequireAnyClientCert,
"verify_if_given": tls.VerifyClientCertIfGiven,
"require_and_verify": tls.RequireAndVerifyClientCert,
}

/**
* Maps tls version from string to golang constant
*/
func MapVersion(version string) uint16 {
return versions[version]
}

/**
* Maps tls client auth from string to tls.ClientAuthType
*/
func MapClientAuth(auth string) tls.ClientAuthType {
return client_auths[auth]
}

/**
* Maps tls ciphers from array of strings to array of golang constants
*/
Expand Down Expand Up @@ -89,6 +107,7 @@ func MakeTlsConfig(tlsC *config.Tls, getCertificate func(*tls.ClientHelloInfo) (
tlsConfig.MinVersion = MapVersion(tlsC.MinVersion)
tlsConfig.MaxVersion = MapVersion(tlsC.MaxVersion)
tlsConfig.SessionTicketsDisabled = !tlsC.SessionTickets
tlsConfig.ClientAuth = MapClientAuth(tlsC.ClientAuth)

if getCertificate != nil {
tlsConfig.GetCertificate = getCertificate
Expand All @@ -101,6 +120,16 @@ func MakeTlsConfig(tlsC *config.Tls, getCertificate func(*tls.ClientHelloInfo) (
return nil, err
}

if tlsConfig.ClientAuth != tls.NoClientCert {
caCertPool := x509.NewCertPool()
certFile, err := ioutil.ReadFile(tlsC.ClientCA)
if err != nil {
return nil, err
}
caCertPool.AppendCertsFromPEM(certFile)
tlsConfig.ClientCAs = caCertPool
}

tlsConfig.Certificates = []tls.Certificate{crt}

return tlsConfig, nil
Expand Down