Skip to content

Commit

Permalink
fix(dgraph): giving users the option to control tls versions (#6820)
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-bansal committed Nov 17, 2020
1 parent 98f0be3 commit ab14ed8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 26 deletions.
14 changes: 3 additions & 11 deletions dgraph/cmd/alpha/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,17 +187,6 @@ they form a Raft group and provide synchronous replication.
flag.Uint64("normalize_node_limit", 1e4,
"Limit for the maximum number of nodes that can be returned in a query that uses the "+
"normalize directive.")

// TLS configurations
flag.String("tls_dir", "", "Path to directory that has TLS certificates and keys.")
flag.Bool("tls_use_system_ca", true, "Include System CA into CA Certs.")
flag.String("tls_client_auth", "VERIFYIFGIVEN", "Enable TLS client authentication")
flag.Bool("tls_internal_port_enabled", false, "(optional) enable inter node TLS encryption between cluster nodes.")
flag.String("tls_cert", "", "(optional) The Cert file name in tls_dir which is needed to "+
"connect as a client with the other nodes in the cluster.")
flag.String("tls_key", "", "(optional) The private key file name "+
"in tls_dir needed to connect as a client with the other nodes in the cluster.")

//Custom plugins.
flag.String("custom_tokenizers", "",
"Comma separated list of tokenizer plugins")
Expand All @@ -221,6 +210,8 @@ they form a Raft group and provide synchronous replication.
PostingListCache,PstoreBlockCache,PstoreIndexCache,WstoreBlockCache,WstoreIndexCache).
PostingListCache should be 0 and is a no-op.
`)
// TLS configurations
x.RegisterServerTLSFlags(flag)
}

func setupCustomTokenizers() {
Expand Down Expand Up @@ -715,6 +706,7 @@ func run() {
TLSClientConfig: tlsConf,
TLSDir: Alpha.Conf.GetString("tls_dir"),
TLSInterNodeEnabled: Alpha.Conf.GetBool("tls_internal_port_enabled"),
TLSMinVersion: Alpha.Conf.GetString("tls_min_version"),
}
if x.WorkerConfig.EncryptionKey, err = enc.ReadKey(Alpha.Conf); err != nil {
glog.Infof("unable to read key %v", err)
Expand Down
16 changes: 6 additions & 10 deletions dgraph/cmd/zero/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,6 @@ instances to achieve high-availability.
" exporter does not support annotation logs and would discard them.")
flag.Bool("ludicrous_mode", false, "Run zero in ludicrous mode")
flag.String("enterprise_license", "", "Path to the enterprise license file.")
// TLS configurations
flag.String("tls_dir", "", "Path to directory that has TLS certificates and keys.")
flag.Bool("tls_use_system_ca", true, "Include System CA into CA Certs.")
flag.String("tls_client_auth", "VERIFYIFGIVEN", "Enable TLS client authentication")
flag.Bool("tls_internal_port_enabled", false, "(optional) enable inter node TLS encryption between cluster nodes.")
flag.String("tls_cert", "", "(optional) The Cert file name in tls_dir which is needed to "+
"connect as a client with the other nodes in the cluster.")
flag.String("tls_key", "", "(optional) The private key file name "+
"in tls_dir which is needed to connect as a client with the other nodes in the cluster.")
// Cache flags
flag.Int64("cache_mb", 0, "Total size of cache (in MB) to be used in zero.")
flag.String("cache_percentage", "100,0",
Expand All @@ -131,6 +122,8 @@ instances to achieve high-availability.
"log directory. mmap consumes more RAM, but provides better performance.")
flag.Int("badger.compression_level", 3,
"The compression level for Badger. A higher value uses more resources.")
// TLS configurations
x.RegisterServerTLSFlags(flag)
}

func setupListener(addr string, port int, kind string) (listener net.Listener, err error) {
Expand All @@ -154,7 +147,10 @@ func (st *state) serveGRPC(l net.Listener, store *raftwal.DiskStorage) {
grpc.StatsHandler(&ocgrpc.ServerHandler{}),
}

tlsConf, err := x.LoadServerTLSConfigForInternalPort(Zero.Conf.GetBool("tls_internal_port_enabled"), Zero.Conf.GetString("tls_dir"))
tlsConf, err := x.LoadServerTLSConfigForInternalPort(
Zero.Conf.GetBool("tls_internal_port_enabled"),
Zero.Conf.GetString("tls_dir"),
Zero.Conf.GetString("tls_min_version"))
x.Check(err)
if tlsConf != nil {
grpcOpts = append(grpcOpts, grpc.Creds(credentials.NewTLS(tlsConf)))
Expand Down
5 changes: 4 additions & 1 deletion worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ func Init(ps *badger.DB) {
grpc.StatsHandler(&ocgrpc.ServerHandler{}),
}

tlsConf, err := x.LoadServerTLSConfigForInternalPort(x.WorkerConfig.TLSInterNodeEnabled, x.WorkerConfig.TLSDir)
tlsConf, err := x.LoadServerTLSConfigForInternalPort(
x.WorkerConfig.TLSInterNodeEnabled,
x.WorkerConfig.TLSDir,
x.WorkerConfig.TLSMinVersion)
x.Check(err)
if tlsConf != nil {
grpcOpts = append(grpcOpts, grpc.Creds(credentials.NewTLS(tlsConf)))
Expand Down
2 changes: 2 additions & 0 deletions x/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ type WorkerOptions struct {
TLSDir string
// Set to true if inter node tls is enabled for the cluster
TLSInterNodeEnabled bool
// min TLS version supported
TLSMinVersion string
// RaftId represents the id of this alpha instance for participating in the RAFT
// consensus protocol.
RaftId uint64
Expand Down
58 changes: 54 additions & 4 deletions x/tls_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package x
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"path"
"strings"
Expand Down Expand Up @@ -47,6 +48,20 @@ type TLSHelperConfig struct {
RootCACert string
ClientAuth string
UseSystemCACerts bool
MinVersion string
}

// RegisterServerTLSFlags registers the required flags to set up a TLS client.
func RegisterServerTLSFlags(flag *pflag.FlagSet) {
flag.String("tls_dir", "", "Path to directory that has TLS certificates and keys.")
flag.Bool("tls_use_system_ca", true, "Include System CA into CA Certs.")
flag.String("tls_client_auth", "VERIFYIFGIVEN", "Enable TLS client authentication")
flag.Bool("tls_internal_port_enabled", false, "(optional) enable inter node TLS encryption between cluster nodes.")
flag.String("tls_cert", "", "(optional) The Cert file name in tls_dir which is needed to "+
"connect as a client with the other nodes in the cluster.")
flag.String("tls_key", "", "(optional) The private key file name "+
"in tls_dir needed to connect as a client with the other nodes in the cluster.")
flag.String("tls_min_version", "TLS11", "min version of tls supported. Valid values are TLS11, TLS12")
}

// RegisterClientTLSFlags registers the required flags to set up a TLS client.
Expand Down Expand Up @@ -99,7 +114,7 @@ func LoadClientTLSConfigForInternalPort(v *viper.Viper) (*tls.Config, error) {
}

// LoadServerTLSConfigForInternalPort loads the TLS config for the internal ports of the cluster
func LoadServerTLSConfigForInternalPort(tlsEnabled bool, tlsDir string) (*tls.Config, error) {
func LoadServerTLSConfigForInternalPort(tlsEnabled bool, tlsDir, tlsMinVersion string) (*tls.Config, error) {
if !tlsEnabled {
return nil, nil
}
Expand All @@ -112,6 +127,7 @@ func LoadServerTLSConfigForInternalPort(tlsEnabled bool, tlsDir string) (*tls.Co
conf.Cert = path.Join(conf.CertDir, TLSNodeCert)
conf.Key = path.Join(conf.CertDir, TLSNodeKey)
conf.ClientAuth = "REQUIREANDVERIFY"
conf.MinVersion = tlsMinVersion
return GenerateServerTLSConfig(&conf)
}

Expand All @@ -128,6 +144,7 @@ func LoadServerTLSConfig(v *viper.Viper, tlsCertFile string, tlsKeyFile string)
conf.Cert = path.Join(conf.CertDir, tlsCertFile)
conf.Key = path.Join(conf.CertDir, tlsKeyFile)
conf.ClientAuth = v.GetString("tls_client_auth")
conf.MinVersion = v.GetString("tls_min_version")
}
conf.UseSystemCACerts = v.GetBool("tls_use_system_ca")

Expand Down Expand Up @@ -243,14 +260,47 @@ func GenerateServerTLSConfig(config *TLSHelperConfig) (tlsCfg *tls.Config, err e
}
tlsCfg.ClientAuth = auth

tlsCfg.MinVersion = tls.VersionTLS11
tlsCfg.MaxVersion = tls.VersionTLS12

err = setupVersion(tlsCfg, config.MinVersion)
if err != nil {
return nil, err
}
return tlsCfg, nil
}
return nil, nil
}

func setupVersion(cfg *tls.Config, minVersion string) error {
tlsVersion := map[string]uint16{
"TLS11": tls.VersionTLS11,
"TLS12": tls.VersionTLS12,
}

if val, has := tlsVersion[strings.ToUpper(minVersion)]; has {
cfg.MinVersion = val
} else {
return fmt.Errorf("invalid min_version '%s'. Valid values [TLS11, TLS12]", minVersion)
}

cfg.MaxVersion = tls.VersionTLS12
cfg.CipherSuites = []uint16{
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
}

return nil
}

// GenerateClientTLSConfig creates and returns a new client side *tls.Config with the
// configuration provided.
func GenerateClientTLSConfig(config *TLSHelperConfig) (tlsCfg *tls.Config, err error) {
Expand Down

0 comments on commit ab14ed8

Please sign in to comment.