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:
@@ -116,44 +116,19 @@ func (c *Config) validateAndInjectDefaults(services []string, pluginsList []stri
|
||||
return fmt.Errorf("invalid status config, must have a `service`, `plugin`, or `console` logging specified")
|
||||
}
|
||||
|
||||
if trigger == nil {
|
||||
t := plugins.DefaultTriggerMode
|
||||
trigger = &t
|
||||
} else {
|
||||
err := validateTriggerMode(*trigger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if c.Trigger == nil {
|
||||
c.Trigger = trigger
|
||||
} else {
|
||||
err := validateTriggerMode(*c.Trigger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if *c.Trigger != *trigger {
|
||||
return fmt.Errorf("invalid status config, discovery has trigger mode %s, status has %s", *trigger, *c.Trigger)
|
||||
}
|
||||
t, err := plugins.ValidateAndInjectDefaultsForTriggerMode(trigger, c.Trigger)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "invalid status config")
|
||||
}
|
||||
c.Trigger = t
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// 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, pluginsList []string) (*Config, error) {
|
||||
t := plugins.DefaultTriggerMode
|
||||
return NewConfigBuilder().WithBytes(config).WithServices(services).WithPlugins(pluginsList).WithTriggerMode(&t).Parse()
|
||||
}
|
||||
|
||||
// ConfigBuilder assists in the construction of the plugin configuration.
|
||||
@@ -189,10 +164,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
|
||||
}
|
||||
|
||||
@@ -593,14 +593,14 @@ func TestParseConfigTriggerMode(t *testing.T) {
|
||||
config: []byte(`{"trigger": "manual"}`),
|
||||
expected: plugins.TriggerPeriodic,
|
||||
wantErr: true,
|
||||
err: fmt.Errorf("invalid status config, discovery has trigger mode periodic, status has manual"),
|
||||
err: fmt.Errorf("invalid status config: trigger mode mismatch: periodic and manual (hint: check discovery configuration)"),
|
||||
},
|
||||
{
|
||||
note: "bad trigger mode",
|
||||
config: []byte(`{"trigger": "foo"}`),
|
||||
expected: "foo",
|
||||
wantErr: true,
|
||||
err: fmt.Errorf("invalid trigger mode \"foo\" (want \"periodic\" or \"manual\")"),
|
||||
err: fmt.Errorf("invalid status config: invalid trigger mode \"foo\" (want \"periodic\" or \"manual\")"),
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user