2022-03-18 17:16:42 +01:00
|
|
|
package toggle
|
|
|
|
|
2022-03-28 16:01:27 +02:00
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
)
|
2022-03-18 17:16:42 +01:00
|
|
|
|
2022-03-28 16:01:27 +02:00
|
|
|
const (
|
2022-09-06 17:43:04 +02:00
|
|
|
// autogen
|
|
|
|
AutogenInternalsFlagName = "autogenInternals"
|
|
|
|
AutogenInternalsDescription = "Enables autogen internal policies. When this is 'true' policy rules should not be mutated."
|
|
|
|
autogenInternalsEnvVar = "FLAG_AUTOGEN_INTERNALS"
|
|
|
|
defaultAutogenInternals = true
|
|
|
|
// protect managed resource
|
|
|
|
ProtectManagedResourcesFlagName = "protectManagedResources"
|
|
|
|
ProtectManagedResourcesDescription = "Set the flag to 'true', to enable managed resources protection."
|
|
|
|
protectManagedResourcesEnvVar = "FLAG_PROTECT_MANAGED_RESOURCES"
|
|
|
|
defaultProtectManagedResources = false
|
2022-03-28 16:01:27 +02:00
|
|
|
)
|
|
|
|
|
2022-07-21 14:31:42 +05:30
|
|
|
var (
|
2022-09-06 17:43:04 +02:00
|
|
|
AutogenInternals = newToggle(defaultAutogenInternals, autogenInternalsEnvVar)
|
|
|
|
ProtectManagedResources = newToggle(defaultProtectManagedResources, protectManagedResourcesEnvVar)
|
2022-07-21 14:31:42 +05:30
|
|
|
)
|
2022-03-28 16:01:27 +02:00
|
|
|
|
2022-08-31 08:41:14 +02:00
|
|
|
type Toggle interface {
|
|
|
|
Enabled() bool
|
|
|
|
Parse(string) error
|
2022-03-28 16:01:27 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 08:41:14 +02:00
|
|
|
type toggle struct {
|
|
|
|
value *bool
|
|
|
|
defaultValue bool
|
|
|
|
envVar string
|
2022-03-28 16:01:27 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 08:41:14 +02:00
|
|
|
func newToggle(defaultValue bool, envVar string) *toggle {
|
|
|
|
return &toggle{
|
|
|
|
defaultValue: defaultValue,
|
|
|
|
envVar: envVar,
|
2022-03-28 16:01:27 +02:00
|
|
|
}
|
|
|
|
}
|
2022-07-21 14:31:42 +05:30
|
|
|
|
2022-08-31 08:41:14 +02:00
|
|
|
func (t *toggle) Parse(in string) error {
|
2022-07-21 14:31:42 +05:30
|
|
|
if value, err := getBool(in); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
2022-08-31 08:41:14 +02:00
|
|
|
t.value = value
|
2022-07-21 14:31:42 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 08:41:14 +02:00
|
|
|
func (t *toggle) Enabled() bool {
|
|
|
|
if t.value != nil {
|
|
|
|
return *t.value
|
2022-07-21 14:31:42 +05:30
|
|
|
}
|
2022-08-31 08:41:14 +02:00
|
|
|
if value, err := getBool(os.Getenv(t.envVar)); err == nil && value != nil {
|
2022-07-21 14:31:42 +05:30
|
|
|
return *value
|
|
|
|
}
|
2022-08-31 08:41:14 +02:00
|
|
|
return t.defaultValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBool(in string) (*bool, error) {
|
|
|
|
if in == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
value, err := strconv.ParseBool(in)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &value, nil
|
2022-07-21 14:31:42 +05:30
|
|
|
}
|