2022-12-20 15:11:51 +00:00
|
|
|
// Package fflag provides a simple feature flag system.
|
|
|
|
//
|
|
|
|
// Feature names are lowercase and can only contain letters, numbers, undercores
|
|
|
|
// and dots.
|
|
|
|
//
|
|
|
|
// good: "foo", "foo_bar", "foo.bar"
|
|
|
|
// bad: "Foo", "foo-bar"
|
|
|
|
//
|
|
|
|
// A feature flag can be enabled by the user with an environment variable
|
|
|
|
// or by adding it to {ConfigDir}/feature.yaml
|
|
|
|
//
|
|
|
|
// I.e. CROWDSEC_FEATURE_FOO_BAR=true
|
|
|
|
// or in feature.yaml:
|
|
|
|
// ---
|
|
|
|
// - foo_bar
|
|
|
|
//
|
|
|
|
// If the variable is set to false, the feature can still be enabled
|
|
|
|
// in feature.yaml. Features cannot be disabled in the file.
|
|
|
|
//
|
|
|
|
// A feature flag can be deprecated or retired. A deprecated feature flag is
|
2023-12-18 08:35:57 +00:00
|
|
|
// still accepted but a warning is logged (only if a deprecation message is provided).
|
|
|
|
// A retired feature flag is ignored and an error is logged.
|
2022-12-20 15:11:51 +00:00
|
|
|
//
|
2023-12-18 08:35:57 +00:00
|
|
|
// The message is inteded to inform the user of the behavior
|
2022-12-20 15:11:51 +00:00
|
|
|
// that has been decided when the flag is/was finally retired.
|
2022-12-26 13:23:41 +00:00
|
|
|
|
2022-12-20 15:11:51 +00:00
|
|
|
package fflag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/goccy/go-yaml"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
var (
|
|
|
|
ErrFeatureNameEmpty = errors.New("name is empty")
|
|
|
|
ErrFeatureNameCase = errors.New("name is not lowercase")
|
|
|
|
ErrFeatureNameInvalid = errors.New("invalid name (allowed a-z, 0-9, _, .)")
|
|
|
|
ErrFeatureUnknown = errors.New("unknown feature")
|
|
|
|
ErrFeatureDeprecated = errors.New("the flag is deprecated")
|
|
|
|
ErrFeatureRetired = errors.New("the flag is retired")
|
|
|
|
)
|
|
|
|
|
2022-12-20 15:11:51 +00:00
|
|
|
const (
|
2022-12-26 13:23:41 +00:00
|
|
|
ActiveState = iota // the feature can be enabled, and its description is logged (Info)
|
|
|
|
DeprecatedState // the feature can be enabled, and a deprecation message is logged (Warning)
|
|
|
|
RetiredState // the feature is ignored and a deprecation message is logged (Error)
|
2022-12-20 15:11:51 +00:00
|
|
|
)
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
type Feature struct {
|
|
|
|
Name string
|
|
|
|
State int // active, deprecated, retired
|
|
|
|
|
|
|
|
// Description should be a short sentence, explaining the feature.
|
|
|
|
Description string
|
|
|
|
|
|
|
|
// DeprecationMessage is used to inform the user of the behavior that has
|
|
|
|
// been decided when the flag is/was finally retired.
|
|
|
|
DeprecationMsg string
|
2022-12-20 15:11:51 +00:00
|
|
|
|
|
|
|
enabled bool
|
|
|
|
}
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
func (f *Feature) IsEnabled() bool {
|
2022-12-20 15:11:51 +00:00
|
|
|
return f.enabled
|
|
|
|
}
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
// Set enables or disables a feature flag
|
|
|
|
// It should not be called directly by the user, but by SetFromEnv or SetFromYaml
|
|
|
|
func (f *Feature) Set(value bool) error {
|
2022-12-20 15:11:51 +00:00
|
|
|
// retired feature flags are ignored
|
2022-12-26 13:23:41 +00:00
|
|
|
if f.State == RetiredState {
|
|
|
|
return ErrFeatureRetired
|
2022-12-20 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f.enabled = value
|
|
|
|
|
|
|
|
// deprecated feature flags are still accepted, but a warning is triggered.
|
|
|
|
// We return an error but set the feature anyway.
|
2022-12-26 13:23:41 +00:00
|
|
|
if f.State == DeprecatedState {
|
|
|
|
return ErrFeatureDeprecated
|
2022-12-20 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
// A register allows to enable features from the environment or a file
|
|
|
|
type FeatureRegister struct {
|
|
|
|
EnvPrefix string
|
|
|
|
features map[string]*Feature
|
2022-12-20 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var featureNameRexp = regexp.MustCompile(`^[a-z0-9_\.]+$`)
|
|
|
|
|
|
|
|
func validateFeatureName(featureName string) error {
|
|
|
|
if featureName == "" {
|
|
|
|
return ErrFeatureNameEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
if featureName != strings.ToLower(featureName) {
|
|
|
|
return ErrFeatureNameCase
|
|
|
|
}
|
|
|
|
|
|
|
|
if !featureNameRexp.MatchString(featureName) {
|
|
|
|
return ErrFeatureNameInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
func (fr *FeatureRegister) RegisterFeature(feat *Feature) error {
|
|
|
|
if err := validateFeatureName(feat.Name); err != nil {
|
|
|
|
return fmt.Errorf("feature flag '%s': %w", feat.Name, err)
|
|
|
|
}
|
2022-12-20 15:11:51 +00:00
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
if fr.features == nil {
|
|
|
|
fr.features = make(map[string]*Feature)
|
2022-12-20 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
fr.features[feat.Name] = feat
|
|
|
|
|
|
|
|
return nil
|
2022-12-20 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
func (fr *FeatureRegister) GetFeature(featureName string) (*Feature, error) {
|
|
|
|
feat, ok := fr.features[featureName]
|
2022-12-20 15:11:51 +00:00
|
|
|
if !ok {
|
|
|
|
return feat, ErrFeatureUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
return feat, nil
|
|
|
|
}
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
func (fr *FeatureRegister) SetFromEnv(logger *logrus.Logger) error {
|
2022-12-20 15:11:51 +00:00
|
|
|
for _, e := range os.Environ() {
|
|
|
|
// ignore non-feature variables
|
2022-12-26 13:23:41 +00:00
|
|
|
if !strings.HasPrefix(e, fr.EnvPrefix) {
|
2022-12-20 15:11:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract feature name and value
|
|
|
|
pair := strings.SplitN(e, "=", 2)
|
|
|
|
varName := pair[0]
|
2022-12-26 13:23:41 +00:00
|
|
|
featureName := strings.ToLower(varName[len(fr.EnvPrefix):])
|
2022-12-20 15:11:51 +00:00
|
|
|
value := pair[1]
|
|
|
|
|
|
|
|
var enable bool
|
|
|
|
|
|
|
|
switch value {
|
|
|
|
case "true":
|
|
|
|
enable = true
|
|
|
|
case "false":
|
|
|
|
enable = false
|
|
|
|
default:
|
|
|
|
logger.Errorf("Ignored envvar %s=%s: invalid value (must be 'true' or 'false')", varName, value)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
feat, err := fr.GetFeature(featureName)
|
2022-12-20 15:11:51 +00:00
|
|
|
if err != nil {
|
2022-12-26 13:23:41 +00:00
|
|
|
logger.Errorf("Ignored envvar '%s': %s.", varName, err)
|
2022-12-20 15:11:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = feat.Set(enable)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, ErrFeatureRetired):
|
2022-12-26 13:23:41 +00:00
|
|
|
logger.Errorf("Ignored envvar '%s': %s. %s", varName, err, feat.DeprecationMsg)
|
2022-12-20 15:11:51 +00:00
|
|
|
continue
|
|
|
|
case errors.Is(err, ErrFeatureDeprecated):
|
2023-12-18 08:35:57 +00:00
|
|
|
if feat.DeprecationMsg != "" {
|
|
|
|
logger.Warningf("Envvar '%s': %s. %s", varName, err, feat.DeprecationMsg)
|
|
|
|
}
|
2022-12-20 15:11:51 +00:00
|
|
|
case err != nil:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-13 12:42:42 +00:00
|
|
|
logger.Debugf("Feature flag: %s=%t (from envvar). %s", featureName, enable, feat.Description)
|
2022-12-20 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
func (fr *FeatureRegister) SetFromYaml(r io.Reader, logger *logrus.Logger) error {
|
2022-12-20 15:11:51 +00:00
|
|
|
var cfg []string
|
|
|
|
|
|
|
|
bys, err := io.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse config file
|
|
|
|
if err := yaml.Unmarshal(bys, &cfg); err != nil {
|
|
|
|
if !errors.Is(err, io.EOF) {
|
|
|
|
return fmt.Errorf("failed to parse feature flags: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug("No feature flags in config file")
|
|
|
|
}
|
|
|
|
|
|
|
|
// set features
|
|
|
|
for _, k := range cfg {
|
2022-12-26 13:23:41 +00:00
|
|
|
feat, err := fr.GetFeature(k)
|
2022-12-20 15:11:51 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("Ignored feature flag '%s': %s", k, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
err = feat.Set(true)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, ErrFeatureRetired):
|
2022-12-26 13:23:41 +00:00
|
|
|
logger.Errorf("Ignored feature flag '%s': %s. %s", k, err, feat.DeprecationMsg)
|
2022-12-20 15:11:51 +00:00
|
|
|
continue
|
|
|
|
case errors.Is(err, ErrFeatureDeprecated):
|
2022-12-26 13:23:41 +00:00
|
|
|
logger.Warningf("Feature '%s': %s. %s", k, err, feat.DeprecationMsg)
|
2022-12-20 15:11:51 +00:00
|
|
|
case err != nil:
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-13 12:42:42 +00:00
|
|
|
logger.Debugf("Feature flag: %s=true (from config file). %s", k, feat.Description)
|
2022-12-20 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
func (fr *FeatureRegister) SetFromYamlFile(path string, logger *logrus.Logger) error {
|
2022-12-20 15:11:51 +00:00
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2023-01-13 12:42:42 +00:00
|
|
|
logger.Tracef("Feature flags config file '%s' does not exist", path)
|
2022-12-20 15:11:51 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("failed to open feature flags file: %w", err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
logger.Debugf("Reading feature flags from %s", path)
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
return fr.SetFromYaml(f, logger)
|
2022-12-20 15:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetEnabledFeatures returns the list of features that have been enabled by the user
|
2022-12-26 13:23:41 +00:00
|
|
|
func (fr *FeatureRegister) GetEnabledFeatures() []string {
|
2022-12-20 15:11:51 +00:00
|
|
|
ret := make([]string, 0)
|
|
|
|
|
2022-12-26 13:23:41 +00:00
|
|
|
for k, feat := range fr.features {
|
2022-12-20 15:11:51 +00:00
|
|
|
if feat.IsEnabled() {
|
|
|
|
ret = append(ret, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(ret)
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
2023-01-20 08:32:10 +00:00
|
|
|
|
|
|
|
// GetAllFeatures returns a slice of all the known features, ordered by name
|
|
|
|
func (fr *FeatureRegister) GetAllFeatures() []Feature {
|
|
|
|
ret := make([]Feature, len(fr.features))
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for _, feat := range fr.features {
|
|
|
|
ret[i] = *feat
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(ret, func(i, j int) bool {
|
|
|
|
return ret[i].Name < ret[j].Name
|
|
|
|
})
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|