mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
plugins: Fix default trigger mode in the non-discovery path
When individual plugins (except discovery) were configured, the default trigger mode was set as periodic. So if the plugin specified a different mode (eg. manual), the configuration check would incorrectly fail on account of a mode mismatch. This commit fixes the issue by updating the trigger mode check to not specify a default mode in scenarios when only plugins (except discovery) are configured. Fixes: #3797 Signed-off-by: Ashutosh Narkar <anarkar4387@gmail.com>
This commit is contained in:
+7
-37
@@ -278,28 +278,11 @@ func (c *Config) validateAndInjectDefaults(services []string, pluginsList []stri
|
||||
return fmt.Errorf("invalid decision_log config, must have a `service`, `plugin`, or `console` logging enabled")
|
||||
}
|
||||
|
||||
if trigger == nil {
|
||||
t := plugins.DefaultTriggerMode
|
||||
trigger = &t
|
||||
} else {
|
||||
err := validateTriggerMode(*trigger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if c.Reporting.Trigger == nil {
|
||||
c.Reporting.Trigger = trigger
|
||||
} else {
|
||||
err := validateTriggerMode(*c.Reporting.Trigger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if *c.Reporting.Trigger != *trigger {
|
||||
return fmt.Errorf("invalid decision_log config, discovery has trigger mode %s, decision_log has %s", *trigger, *c.Reporting.Trigger)
|
||||
}
|
||||
t, err := plugins.ValidateAndInjectDefaultsForTriggerMode(trigger, c.Reporting.Trigger)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "invalid decision_log config")
|
||||
}
|
||||
c.Reporting.Trigger = t
|
||||
|
||||
min := defaultMinDelaySeconds
|
||||
max := defaultMaxDelaySeconds
|
||||
@@ -349,7 +332,6 @@ func (c *Config) validateAndInjectDefaults(services []string, pluginsList []stri
|
||||
c.MaskDecision = &maskDecision
|
||||
}
|
||||
|
||||
var err error
|
||||
c.maskDecisionRef, err = ref.ParseDataPath(*c.MaskDecision)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "invalid mask_decision in decision_logs")
|
||||
@@ -370,15 +352,6 @@ func (c *Config) validateAndInjectDefaults(services []string, pluginsList []stri
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateTriggerMode(mode plugins.TriggerMode) error {
|
||||
switch mode {
|
||||
case plugins.TriggerPeriodic, plugins.TriggerManual:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("invalid trigger mode %q (want %q or %q)", mode, plugins.TriggerPeriodic, plugins.TriggerManual)
|
||||
}
|
||||
}
|
||||
|
||||
// Plugin implements decision log buffering and uploading.
|
||||
type Plugin struct {
|
||||
manager *plugins.Manager
|
||||
@@ -401,8 +374,9 @@ type reconfigure struct {
|
||||
}
|
||||
|
||||
// ParseConfig validates the config and injects default values.
|
||||
func ParseConfig(config []byte, services []string, plugins []string) (*Config, error) {
|
||||
return NewConfigBuilder().WithBytes(config).WithServices(services).WithPlugins(plugins).WithTriggerMode(nil).Parse()
|
||||
func ParseConfig(config []byte, services []string, pluginList []string) (*Config, error) {
|
||||
t := plugins.DefaultTriggerMode
|
||||
return NewConfigBuilder().WithBytes(config).WithServices(services).WithPlugins(pluginList).WithTriggerMode(&t).Parse()
|
||||
}
|
||||
|
||||
// ConfigBuilder assists in the construction of the plugin configuration.
|
||||
@@ -438,10 +412,6 @@ func (b *ConfigBuilder) WithPlugins(plugins []string) *ConfigBuilder {
|
||||
|
||||
// WithTriggerMode sets the plugin trigger mode.
|
||||
func (b *ConfigBuilder) WithTriggerMode(trigger *plugins.TriggerMode) *ConfigBuilder {
|
||||
if trigger == nil {
|
||||
t := plugins.DefaultTriggerMode
|
||||
trigger = &t
|
||||
}
|
||||
b.trigger = trigger
|
||||
return b
|
||||
}
|
||||
|
||||
@@ -1690,14 +1690,14 @@ func TestParseConfigTriggerMode(t *testing.T) {
|
||||
config: []byte(`{"reporting": {"trigger": "manual"}}`),
|
||||
expected: plugins.TriggerPeriodic,
|
||||
wantErr: true,
|
||||
err: fmt.Errorf("invalid decision_log config, discovery has trigger mode periodic, decision_log has manual"),
|
||||
err: fmt.Errorf("invalid decision_log config: trigger mode mismatch: periodic and manual (hint: check discovery configuration)"),
|
||||
},
|
||||
{
|
||||
note: "bad trigger mode",
|
||||
config: []byte(`{"reporting": {"trigger": "foo"}}`),
|
||||
expected: "foo",
|
||||
wantErr: true,
|
||||
err: fmt.Errorf("invalid trigger mode \"foo\" (want \"periodic\" or \"manual\")"),
|
||||
err: fmt.Errorf("invalid decision_log config: invalid trigger mode \"foo\" (want \"periodic\" or \"manual\")"),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user