-
Notifications
You must be signed in to change notification settings - Fork 123
/
conf.go
74 lines (59 loc) · 2.01 KB
/
conf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2021-2023 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package overlay
import (
"errors"
"time"
"github.com/cerbos/cerbos/internal/config"
"github.com/cerbos/cerbos/internal/storage"
"go.uber.org/multierr"
)
const (
confKey = storage.ConfKey + ".overlay"
defaultFallbackErrorThreshold = 5
defaultFallbackErrorWindow = 5 * time.Minute
)
// Conf is required (if driver is set to 'overlay') configuration for overlay storage driver.
// +desc=This section is required only if storage.driver is overlay.
type Conf struct {
// BaseDriver is the default storage driver
BaseDriver string `yaml:"baseDriver" conf:"required,example=blob"`
// FallbackDriver is the secondary or fallback storage driver
FallbackDriver string `yaml:"fallbackDriver" conf:"required,example=disk"`
// FallbackErrorThreshold is the max number of errors we allow within the fallbackErrorWindow period
FallbackErrorThreshold int `yaml:"fallbackErrorThreshold,omitempty" conf:",example=5"`
// FallbackErrorWindow is the cyclic period within which we aggregate failures
FallbackErrorWindow time.Duration `yaml:"fallbackErrorWindow" conf:",example=5m"`
}
func (conf *Conf) Key() string {
return confKey
}
func (conf *Conf) Validate() error {
var errs []error
if conf.BaseDriver == "" {
errs = append(errs, errors.New("baseDriver is required"))
}
if conf.FallbackDriver == "" {
errs = append(errs, errors.New("fallbackDriver is required"))
}
if conf.BaseDriver != "" && conf.BaseDriver == conf.FallbackDriver {
errs = append(errs, errors.New("baseDriver and fallbackDriver cannot be the same"))
}
if len(errs) > 0 {
return multierr.Combine(errs...)
}
return nil
}
func (conf *Conf) SetDefaults() {
if conf.FallbackErrorThreshold == 0 {
conf.FallbackErrorThreshold = defaultFallbackErrorThreshold
}
if conf.FallbackErrorWindow == 0 {
conf.FallbackErrorWindow = defaultFallbackErrorWindow
}
}
func GetConf() (*Conf, error) {
conf := &Conf{}
err := config.GetSection(conf)
return conf, err
}