raw_exec: be defensive when disabled

Ensure that no raw_exec task can run on a client where it's disabled,
even if a flaw lead to client being assigned a raw_exec task
unexpectedly.
This commit is contained in:
Mahmood Ali 2019-08-29 09:09:40 -04:00
parent f5792635ca
commit 28e473aaff
2 changed files with 26 additions and 0 deletions

View file

@ -47,6 +47,8 @@ var (
Config: map[string]interface{}{}, Config: map[string]interface{}{},
Factory: func(l hclog.Logger) interface{} { return NewRawExecDriver(l) }, Factory: func(l hclog.Logger) interface{} { return NewRawExecDriver(l) },
} }
errDisabledDriver = fmt.Errorf("raw_exec is disabled")
) )
// PluginLoader maps pre-0.9 client driver options to post-0.9 plugin options. // PluginLoader maps pre-0.9 client driver options to post-0.9 plugin options.
@ -307,6 +309,10 @@ func (d *Driver) RecoverTask(handle *drivers.TaskHandle) error {
} }
func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) { func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drivers.DriverNetwork, error) {
if !d.config.Enabled {
return nil, nil, errDisabledDriver
}
if _, ok := d.tasks.Get(cfg.ID); ok { if _, ok := d.tasks.Get(cfg.ID); ok {
return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID) return nil, nil, fmt.Errorf("task with ID %q already started", cfg.ID)
} }

View file

@ -471,3 +471,23 @@ config {
require.EqualValues(t, expected, tc) require.EqualValues(t, expected, tc)
} }
func TestRawExecDriver_Disabled(t *testing.T) {
t.Parallel()
require := require.New(t)
d := NewRawExecDriver(testlog.HCLogger(t))
d.(*Driver).config.Enabled = false
harness := dtestutil.NewDriverHarness(t, d)
defer harness.Kill()
task := &drivers.TaskConfig{
ID: uuid.Generate(),
Name: "test",
}
handle, _, err := harness.StartTask(task)
require.Error(err)
require.Contains(err.Error(), errDisabledDriver.Error())
require.Nil(handle)
}