pomerium/internal/databroker/config.go
Caleb Doxsey 5d60cff21e
databroker: refactor databroker to sync all changes (#1879)
* refactor backend, implement encrypted store

* refactor in-memory store

* wip

* wip

* wip

* add syncer test

* fix redis expiry

* fix linting issues

* fix test by skipping non-config records

* fix backoff import

* fix init issues

* fix query

* wait for initial sync before starting directory sync

* add type to SyncLatest

* add more log messages, fix deadlock in in-memory store, always return server version from SyncLatest

* update sync types and tests

* add redis tests

* skip macos in github actions

* add comments to proto

* split getBackend into separate methods

* handle errors in initVersion

* return different error for not found vs other errors in get

* use exponential backoff for redis transaction retry

* rename raw to result

* use context instead of close channel

* store type urls as constants in databroker

* use timestampb instead of ptypes

* fix group merging not waiting

* change locked names

* update GetAll to return latest record version

* add method to grpcutil to get the type url for a protobuf type
2021-02-18 15:24:33 -07:00

108 lines
3 KiB
Go

package databroker
import (
"crypto/tls"
"encoding/base64"
"time"
"github.com/pomerium/pomerium/internal/log"
"github.com/pomerium/pomerium/pkg/cryptutil"
)
var (
// DefaultDeletePermanentlyAfter is the default amount of time to wait before deleting
// a record permanently.
DefaultDeletePermanentlyAfter = time.Hour
// DefaultStorageType is the default storage type that Server use
DefaultStorageType = "memory"
// DefaultGetAllPageSize is the default page size for GetAll calls.
DefaultGetAllPageSize = 50
)
type serverConfig struct {
deletePermanentlyAfter time.Duration
secret []byte
storageType string
storageConnectionString string
storageCAFile string
storageCertSkipVerify bool
storageCertificate *tls.Certificate
getAllPageSize int
}
func newServerConfig(options ...ServerOption) *serverConfig {
cfg := new(serverConfig)
WithDeletePermanentlyAfter(DefaultDeletePermanentlyAfter)(cfg)
WithStorageType(DefaultStorageType)(cfg)
WithGetAllPageSize(DefaultGetAllPageSize)(cfg)
for _, option := range options {
option(cfg)
}
return cfg
}
// A ServerOption customizes the server.
type ServerOption func(*serverConfig)
// WithDeletePermanentlyAfter sets the deletePermanentlyAfter duration.
// If a record is deleted via Delete, it will be permanently deleted after
// the given duration.
func WithDeletePermanentlyAfter(dur time.Duration) ServerOption {
return func(cfg *serverConfig) {
cfg.deletePermanentlyAfter = dur
}
}
// WithGetAllPageSize sets the page size for GetAll calls.
func WithGetAllPageSize(pageSize int) ServerOption {
return func(cfg *serverConfig) {
cfg.getAllPageSize = pageSize
}
}
// WithSharedKey sets the secret in the config.
func WithSharedKey(sharedKey string) ServerOption {
return func(cfg *serverConfig) {
key, err := base64.StdEncoding.DecodeString(sharedKey)
if err != nil || len(key) != cryptutil.DefaultKeySize {
log.Error().Err(err).Msgf("shared key is required and must be %d bytes long", cryptutil.DefaultKeySize)
return
}
cfg.secret = key
}
}
// WithStorageType sets the storage type.
func WithStorageType(typ string) ServerOption {
return func(cfg *serverConfig) {
cfg.storageType = typ
}
}
// WithStorageConnectionString sets the DSN for storage.
func WithStorageConnectionString(connStr string) ServerOption {
return func(cfg *serverConfig) {
cfg.storageConnectionString = connStr
}
}
// WithStorageCAFile sets the CA file in the config.
func WithStorageCAFile(filePath string) ServerOption {
return func(cfg *serverConfig) {
cfg.storageCAFile = filePath
}
}
// WithStorageCertSkipVerify sets the storageCertSkipVerify in the config.
func WithStorageCertSkipVerify(storageCertSkipVerify bool) ServerOption {
return func(cfg *serverConfig) {
cfg.storageCertSkipVerify = storageCertSkipVerify
}
}
// WithStorageCertificate sets the storageCertificate in the config.
func WithStorageCertificate(certificate *tls.Certificate) ServerOption {
return func(cfg *serverConfig) {
cfg.storageCertificate = certificate
}
}