pkg/storage/redis: add redis TLS support (#1163)

Fixes #1156
This commit is contained in:
Cuong Manh Le 2020-07-31 19:37:23 +07:00 committed by GitHub
parent aab9ec413e
commit bc61206b78
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 409 additions and 88 deletions

View file

@ -1,6 +1,9 @@
package databroker
import "time"
import (
"crypto/tls"
"time"
)
var (
// DefaultDeletePermanentlyAfter is the default amount of time to wait before deleting
@ -18,6 +21,7 @@ type serverConfig struct {
secret []byte
storageType string
storageConnectionString string
storageTLSConfig *tls.Config
}
func newServerConfig(options ...ServerOption) *serverConfig {
@ -70,3 +74,10 @@ func WithStorageConnectionString(connStr string) ServerOption {
cfg.storageConnectionString = connStr
}
}
// WithStorageTLSConfig sets the tls config for connection to storage.
func WithStorageTLSConfig(tlsConfig *tls.Config) ServerOption {
return func(cfg *serverConfig) {
cfg.storageTLSConfig = tlsConfig
}
}

View file

@ -26,7 +26,7 @@ func TestConfigSource(t *testing.T) {
}
defer li.Close()
dataBrokerServer := newTestServer()
dataBrokerServer := New()
srv := grpc.NewServer()
databroker.RegisterDataBrokerServiceServer(srv, dataBrokerServer)
go func() { _ = srv.Serve(li) }()

View file

@ -1,7 +0,0 @@
// +build !redis
package databroker
func newTestServer() *Server {
return New()
}

View file

@ -1,17 +0,0 @@
// +build redis
package databroker
import (
"os"
"github.com/pomerium/pomerium/pkg/storage/redis"
)
func newTestServer() *Server {
address := "redis://localhost:6379/0"
if redisURL := os.Getenv("REDIS_URL"); redisURL != "" {
address = redisURL
}
return New(WithStorageType(redis.Name), WithStorageConnectionString(address))
}

View file

@ -350,9 +350,14 @@ func (srv *Server) getDB(recordType string) (storage.Backend, error) {
func (srv *Server) newDB(recordType string) (db storage.Backend, err error) {
switch srv.cfg.storageType {
case config.StorageInMemoryName:
db = inmemory.NewDB(recordType, srv.cfg.btreeDegree)
return inmemory.NewDB(recordType, srv.cfg.btreeDegree), nil
case config.StorageRedisName:
db, err = redis.New(srv.cfg.storageConnectionString, recordType, int64(srv.cfg.deletePermanentlyAfter.Seconds()))
db, err = redis.New(
srv.cfg.storageConnectionString,
recordType,
int64(srv.cfg.deletePermanentlyAfter.Seconds()),
redis.WithTLSConfig(srv.cfg.storageTLSConfig),
)
if err != nil {
return nil, fmt.Errorf("failed to create new redis storage: %w", err)
}