cache: add option validations (#468)

Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
This commit is contained in:
Bobby DeSimone 2020-01-31 20:24:52 -08:00 committed by GitHub
parent 9f02c79b90
commit b80790a061
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 180 additions and 5 deletions

27
cache/cache.go vendored
View file

@ -10,11 +10,13 @@ import (
stdlog "log"
"github.com/pomerium/pomerium/config"
"github.com/pomerium/pomerium/internal/cryptutil"
"github.com/pomerium/pomerium/internal/kv"
"github.com/pomerium/pomerium/internal/kv/autocache"
"github.com/pomerium/pomerium/internal/kv/bolt"
"github.com/pomerium/pomerium/internal/kv/redis"
"github.com/pomerium/pomerium/internal/log"
"github.com/pomerium/pomerium/internal/urlutil"
)
// Cache represents the cache service. The cache service is a simple interface
@ -25,7 +27,11 @@ type Cache struct {
// New creates a new cache service.
func New(opts config.Options) (*Cache, error) {
cache, err := NewCacheStore(opts.CacheStore, &opts)
if err := validate(opts); err != nil {
return nil, fmt.Errorf("cache: bad option: %w", err)
}
cache, err := newCacheStore(opts.CacheStore, &opts)
if err != nil {
return nil, err
}
@ -34,20 +40,31 @@ func New(opts config.Options) (*Cache, error) {
}, nil
}
// NewCacheStore creates a new cache store by name and given a set of
// validate checks that proper configuration settings are set to create
// a cache instance
func validate(o config.Options) error {
if _, err := cryptutil.NewAEADCipherFromBase64(o.SharedKey); err != nil {
return fmt.Errorf("invalid 'SHARED_SECRET': %w", err)
}
if err := urlutil.ValidateURL(o.CacheURL); err != nil {
return fmt.Errorf("invalid 'CACHE_SERVICE_URL': %w", err)
}
return nil
}
// newCacheStore creates a new cache store by name and given a set of
// configuration options.
func NewCacheStore(name string, o *config.Options) (s kv.Store, err error) {
func newCacheStore(name string, o *config.Options) (s kv.Store, err error) {
switch name {
case bolt.Name:
s, err = bolt.New(&bolt.Options{Path: o.CacheStorePath})
case redis.Name:
// todo(bdd): make path configurable in config.Options
s, err = redis.New(&redis.Options{
Addr: o.CacheStoreAddr,
Password: o.CacheStorePassword,
})
case autocache.Name:
acLog := log.Logger.With().Str("service", "autocache").Logger()
acLog := log.Logger.With().Str("service", autocache.Name).Logger()
s, err = autocache.New(&autocache.Options{
SharedKey: o.SharedKey,
Log: stdlog.New(acLog, "", 0),