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 flags for enabling TLS for store #139

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 13 additions & 1 deletion cmd/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ type config struct {
id string
storeBackend string
storeEndpoints string
storeCertFile string
storeKeyFile string
storeCACertFile string
dataDir string
clusterName string
listenAddress string
Expand Down Expand Up @@ -90,6 +93,9 @@ func init() {
cmdKeeper.PersistentFlags().StringVar(&cfg.id, "id", "", "keeper id (must be unique in the cluster and can contain only lower-case letters, numbers and the underscore character). If not provided a random id will be generated.")
cmdKeeper.PersistentFlags().StringVar(&cfg.storeBackend, "store-backend", "", "store backend type (etcd or consul)")
cmdKeeper.PersistentFlags().StringVar(&cfg.storeEndpoints, "store-endpoints", "", "a comma-delimited list of store endpoints (defaults: 127.0.0.1:2379 for etcd, 127.0.0.1:8500 for consul)")
cmdKeeper.PersistentFlags().StringVar(&cfg.storeCertFile, "store-cert", "", "path to the client server TLS cert file")
cmdKeeper.PersistentFlags().StringVar(&cfg.storeKeyFile, "store-key", "", "path to the client server TLS key file")
cmdKeeper.PersistentFlags().StringVar(&cfg.storeCACertFile, "store-cacert", "", "path to the client server TLS trusted CA key file")
cmdKeeper.PersistentFlags().StringVar(&cfg.dataDir, "data-dir", "", "data directory")
cmdKeeper.PersistentFlags().StringVar(&cfg.clusterName, "cluster-name", "", "cluster name")
cmdKeeper.PersistentFlags().StringVar(&cfg.listenAddress, "listen-address", "localhost", "keeper listening address")
Expand Down Expand Up @@ -245,7 +251,13 @@ type PostgresKeeper struct {
func NewPostgresKeeper(id string, cfg *config, stop chan bool, end chan error) (*PostgresKeeper, error) {
storePath := filepath.Join(common.StoreBasePath, cfg.clusterName)

kvstore, err := store.NewStore(store.Backend(cfg.storeBackend), cfg.storeEndpoints)
kvstore, err := store.NewStore(
store.Backend(cfg.storeBackend),
cfg.storeEndpoints,
cfg.storeCertFile,
cfg.storeKeyFile,
cfg.storeCACertFile,
)
if err != nil {
return nil, fmt.Errorf("cannot create store: %v", err)
}
Expand Down
28 changes: 20 additions & 8 deletions cmd/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,26 @@ var cmdProxy = &cobra.Command{
}

type config struct {
storeBackend string
storeEndpoints string
clusterName string
listenAddress string
port string
stopListening bool
debug bool
storeBackend string
storeEndpoints string
storeCertFile string
storeKeyFile string
storeCACertFile string
clusterName string
listenAddress string
port string
stopListening bool
debug bool
}

var cfg config

func init() {
cmdProxy.PersistentFlags().StringVar(&cfg.storeBackend, "store-backend", "", "store backend type (etcd or consul)")
cmdProxy.PersistentFlags().StringVar(&cfg.storeEndpoints, "store-endpoints", "", "a comma-delimited list of store endpoints (defaults: 127.0.0.1:2379 for etcd, 127.0.0.1:8500 for consul)")
cmdProxy.PersistentFlags().StringVar(&cfg.storeCertFile, "store-cert", "", "path to the client server TLS cert file")
cmdProxy.PersistentFlags().StringVar(&cfg.storeKeyFile, "store-key", "", "path to the client server TLS key file")
cmdProxy.PersistentFlags().StringVar(&cfg.storeCACertFile, "store-cacert", "", "path to the client server TLS trusted CA key file")
cmdProxy.PersistentFlags().StringVar(&cfg.clusterName, "cluster-name", "", "cluster name")
cmdProxy.PersistentFlags().StringVar(&cfg.listenAddress, "listen-address", "127.0.0.1", "proxy listening address")
cmdProxy.PersistentFlags().StringVar(&cfg.port, "port", "5432", "proxy listening port")
Expand All @@ -83,7 +89,13 @@ type ClusterChecker struct {
func NewClusterChecker(id string, cfg config) (*ClusterChecker, error) {
storePath := filepath.Join(common.StoreBasePath, cfg.clusterName)

kvstore, err := store.NewStore(store.Backend(cfg.storeBackend), cfg.storeEndpoints)
kvstore, err := store.NewStore(
store.Backend(cfg.storeBackend),
cfg.storeEndpoints,
cfg.storeCertFile,
cfg.storeKeyFile,
cfg.storeCACertFile,
)
if err != nil {
return nil, fmt.Errorf("cannot create store: %v", err)
}
Expand Down
14 changes: 13 additions & 1 deletion cmd/sentinel/sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ const (
type config struct {
storeBackend string
storeEndpoints string
storeCertFile string
storeKeyFile string
storeCACertFile string
clusterName string
listenAddress string
port string
Expand All @@ -74,6 +77,9 @@ var cfg config
func init() {
cmdSentinel.PersistentFlags().StringVar(&cfg.storeBackend, "store-backend", "", "store backend type (etcd or consul)")
cmdSentinel.PersistentFlags().StringVar(&cfg.storeEndpoints, "store-endpoints", "", "a comma-delimited list of store endpoints (defaults: 127.0.0.1:2379 for etcd, 127.0.0.1:8500 for consul)")
cmdSentinel.PersistentFlags().StringVar(&cfg.storeCertFile, "store-cert", "", "path to the client server TLS cert file")
cmdSentinel.PersistentFlags().StringVar(&cfg.storeKeyFile, "store-key", "", "path to the client server TLS key file")
cmdSentinel.PersistentFlags().StringVar(&cfg.storeCACertFile, "store-cacert", "", "path to the client server TLS trusted CA key file")
cmdSentinel.PersistentFlags().StringVar(&cfg.clusterName, "cluster-name", "", "cluster name")
cmdSentinel.PersistentFlags().StringVar(&cfg.listenAddress, "listen-address", "localhost", "sentinel listening address")
cmdSentinel.PersistentFlags().StringVar(&cfg.port, "port", "6431", "sentinel listening port")
Expand Down Expand Up @@ -663,7 +669,13 @@ func NewSentinel(id string, cfg *config, stop chan bool, end chan bool) (*Sentin
}

storePath := filepath.Join(common.StoreBasePath, cfg.clusterName)
kvstore, err := store.NewStore(store.Backend(cfg.storeBackend), cfg.storeEndpoints)
kvstore, err := store.NewStore(
store.Backend(cfg.storeBackend),
cfg.storeEndpoints,
cfg.storeCertFile,
cfg.storeKeyFile,
cfg.storeCACertFile,
)
if err != nil {
return nil, fmt.Errorf("cannot create store: %v", err)
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/stolonctl/config_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ func getConfig(e *store.StoreManager) (*cluster.NilConfig, error) {
func configGet(cmd *cobra.Command, args []string) {
storePath := filepath.Join(common.StoreBasePath, cfg.clusterName)

kvstore, err := store.NewStore(store.Backend(cfg.storeBackend), cfg.storeEndpoints)
kvstore, err := store.NewStore(
store.Backend(cfg.storeBackend),
cfg.storeEndpoints,
cfg.storeCertFile,
cfg.storeKeyFile,
cfg.storeCACertFile,
)
if err != nil {
die("cannot create store: %v", err)
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/stolonctl/config_patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,13 @@ func configPatch(cmd *cobra.Command, args []string) {
}

storePath := filepath.Join(common.StoreBasePath, cfg.clusterName)
kvstore, err := store.NewStore(store.Backend(cfg.storeBackend), cfg.storeEndpoints)
kvstore, err := store.NewStore(
store.Backend(cfg.storeBackend),
cfg.storeEndpoints,
cfg.storeCertFile,
cfg.storeKeyFile,
cfg.storeCACertFile,
)
if err != nil {
die("cannot create store: %v", err)
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/stolonctl/config_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,13 @@ func configReplace(cmd *cobra.Command, args []string) {

storePath := filepath.Join(common.StoreBasePath, cfg.clusterName)

kvstore, err := store.NewStore(store.Backend(cfg.storeBackend), cfg.storeEndpoints)
kvstore, err := store.NewStore(
store.Backend(cfg.storeBackend),
cfg.storeEndpoints,
cfg.storeCertFile,
cfg.storeKeyFile,
cfg.storeCACertFile,
)
if err != nil {
die("cannot create store: %v", err)
}
Expand Down
9 changes: 8 additions & 1 deletion cmd/stolonctl/listclusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ func init() {
}

func getClusters(storeBasePath string) ([]string, error) {
kvstore, err := store.NewStore(store.Backend(cfg.storeBackend), cfg.storeEndpoints)
kvstore, err := store.NewStore(
store.Backend(cfg.storeBackend),
cfg.storeEndpoints,
cfg.storeCertFile,
cfg.storeKeyFile,
cfg.storeCACertFile,
)

if err != nil {
return nil, fmt.Errorf("cannot create store: %v", err)
}
Expand Down
8 changes: 7 additions & 1 deletion cmd/stolonctl/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,13 @@ func status(cmd *cobra.Command, args []string) {
}
storePath := filepath.Join(common.StoreBasePath, cfg.clusterName)

kvstore, err := store.NewStore(store.Backend(cfg.storeBackend), cfg.storeEndpoints)
kvstore, err := store.NewStore(
store.Backend(cfg.storeBackend),
cfg.storeEndpoints,
cfg.storeCertFile,
cfg.storeKeyFile,
cfg.storeCACertFile,
)
if err != nil {
die("cannot create store: %v", err)
}
Expand Down
12 changes: 9 additions & 3 deletions cmd/stolonctl/stolonctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,22 @@ var cmdStolonCtl = &cobra.Command{
}

type config struct {
storeBackend string
storeEndpoints string
clusterName string
storeBackend string
storeEndpoints string
storeCertFile string
storeKeyFile string
storeCACertFile string
clusterName string
}

var cfg config

func init() {
cmdStolonCtl.PersistentFlags().StringVar(&cfg.storeBackend, "store-backend", "", "store backend type (etcd or consul)")
cmdStolonCtl.PersistentFlags().StringVar(&cfg.storeEndpoints, "store-endpoints", "", "a comma-delimited list of store endpoints (defaults: 127.0.0.1:2379 for etcd, 127.0.0.1:8500 for consul)")
cmdStolonCtl.PersistentFlags().StringVar(&cfg.storeCertFile, "store-cert", "", "path to the client server TLS cert file")
cmdStolonCtl.PersistentFlags().StringVar(&cfg.storeKeyFile, "store-key", "", "path to the client server TLS key file")
cmdStolonCtl.PersistentFlags().StringVar(&cfg.storeCACertFile, "store-cacert", "", "path to the client server TLS trusted CA key file")
cmdStolonCtl.PersistentFlags().StringVar(&cfg.clusterName, "cluster-name", "", "cluster name")
}

Expand Down
42 changes: 40 additions & 2 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
package store

import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -70,7 +73,7 @@ type StoreManager struct {
store kvstore.Store
}

func NewStore(backend Backend, addrsStr string) (kvstore.Store, error) {
func NewStore(backend Backend, addrsStr, certFile, keyFile, caCertFile string) (kvstore.Store, error) {

var kvbackend kvstore.Backend
switch backend {
Expand All @@ -92,7 +95,42 @@ func NewStore(backend Backend, addrsStr string) (kvstore.Store, error) {
}
addrs := strings.Split(addrsStr, ",")

store, err := libkv.NewStore(kvbackend, addrs, &kvstore.Config{ConnectionTimeout: 10 * time.Second})
var tlsC *tls.Config = nil
if certFile != "" && keyFile != "" {
cc := &kvstore.ClientTLSConfig{
CertFile: certFile,
KeyFile: keyFile,
CACertFile: caCertFile,
}

var certPool *x509.CertPool = nil
if caCertFile != "" {
if pemBytes, err := ioutil.ReadFile(cc.CACertFile); err == nil {
certPool = x509.NewCertPool()
certPool.AppendCertsFromPEM(pemBytes)
} else {
return nil, err
}
}

if tlsCert, err := tls.LoadX509KeyPair(cc.CertFile, cc.KeyFile); err == nil {
tlsC = &tls.Config{
RootCAs: certPool,
Certificates: []tls.Certificate{tlsCert},
}
} else {
return nil, err
}
}

store, err := libkv.NewStore(
kvbackend,
addrs,
&kvstore.Config{
TLS: tlsC,
ConnectionTimeout: 10 * time.Second,
},
)
if err != nil {
return nil, err
}
Expand Down
8 changes: 7 additions & 1 deletion tests/integration/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ func TestServerParameters(t *testing.T) {

storePath := filepath.Join(common.StoreBasePath, clusterName)

kvstore, err := store.NewStore(tstore.storeBackend, storeEndpoints)
kvstore, err := store.NewStore(
tstore.storeBackend,
storeEndpoints,
"",
"",
"",
)
if err != nil {
t.Fatalf("cannot create store: %v", err)
}
Expand Down
16 changes: 14 additions & 2 deletions tests/integration/ha_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ func TestInitWithMultipleKeepers(t *testing.T) {

storePath := filepath.Join(common.StoreBasePath, clusterName)

kvstore, err := store.NewStore(tstore.storeBackend, storeEndpoints)
kvstore, err := store.NewStore(
tstore.storeBackend,
storeEndpoints,
"",
"",
"",
)
if err != nil {
t.Fatalf("cannot create store: %v", err)
}
Expand Down Expand Up @@ -131,7 +137,13 @@ func setupServers(t *testing.T, clusterName, dir string, numKeepers, numSentinel

storePath := filepath.Join(common.StoreBasePath, clusterName)

kvstore, err := store.NewStore(tstore.storeBackend, storeEndpoints)
kvstore, err := store.NewStore(
tstore.storeBackend,
storeEndpoints,
"",
"",
"",
)
if err != nil {
t.Fatalf("cannot create store: %v", err)
}
Expand Down
24 changes: 21 additions & 3 deletions tests/integration/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,13 @@ func TestInitUsers(t *testing.T) {
clusterName = uuid.NewV4().String()
storePath := filepath.Join(common.StoreBasePath, clusterName)

kvstore, err := store.NewStore(tstore.storeBackend, storeEndpoints)
kvstore, err := store.NewStore(
tstore.storeBackend,
storeEndpoints,
"",
"",
"",
)
if err != nil {
t.Fatalf("cannot create store: %v", err)
}
Expand Down Expand Up @@ -154,7 +160,13 @@ func TestInitUsers(t *testing.T) {
clusterName = uuid.NewV4().String()
storePath = filepath.Join(common.StoreBasePath, clusterName)

kvstore, err = store.NewStore(tstore.storeBackend, storeEndpoints)
kvstore, err = store.NewStore(
tstore.storeBackend,
storeEndpoints,
"",
"",
"",
)
if err != nil {
t.Fatalf("cannot create store: %v", err)
}
Expand Down Expand Up @@ -216,7 +228,13 @@ func TestInitialClusterConfig(t *testing.T) {
storeEndpoints := fmt.Sprintf("%s:%s", tstore.listenAddress, tstore.port)
storePath := filepath.Join(common.StoreBasePath, clusterName)

kvstore, err := store.NewStore(tstore.storeBackend, storeEndpoints)
kvstore, err := store.NewStore(
tstore.storeBackend,
storeEndpoints,
"",
"",
"",
)
if err != nil {
t.Fatalf("cannot create store: %v", err)
}
Expand Down
8 changes: 7 additions & 1 deletion tests/integration/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ func TestProxyListening(t *testing.T) {

storePath := filepath.Join(common.StoreBasePath, clusterName)

kvstore, err := store.NewStore(tstore.storeBackend, storeEndpoints)
kvstore, err := store.NewStore(
tstore.storeBackend,
storeEndpoints,
"",
"",
"",
)
if err != nil {
t.Fatalf("cannot create store: %v", err)
}
Expand Down
Loading