diff --git a/connectors/semaphore/semaphore.go b/connectors/semaphore/semaphore.go index 0c88b6d..ec6c1ef 100644 --- a/connectors/semaphore/semaphore.go +++ b/connectors/semaphore/semaphore.go @@ -49,6 +49,9 @@ type Template struct { // Vars are the extra variables the template takes, each with a // description. Every one is required, so the judged action is complete. Vars map[string]string `json:"vars"` + // HostVar names an extra variable that also carries the host, for a + // template whose playbook targets it rather than honouring a task's limit. + HostVar string `json:"host_var"` } var validAction = regexp.MustCompile(`^[a-z][a-z0-9_]{0,62}$`) @@ -91,11 +94,40 @@ func newSemaphore(cfg connector.Config) (*semaphore, error) { if _, err := reversibility(t.Effect.Reversibility, t.Effect.ReadOnly); err != nil { return nil, fmt.Errorf("template %q: %w", t.Action, err) } + if _, ok := t.Vars[t.HostVar]; ok { + return nil, fmt.Errorf("template %q: host_var %q is also one of its vars", t.Action, t.HostVar) + } s.templates[t.Action] = t } + ctx, cancel := context.WithTimeout(context.Background(), s.client.Timeout) + defer cancel() + if err := s.confined(ctx); err != nil { + return nil, err + } return s, nil } +// confined refuses a template that would run against more than the host it +// is given. Semaphore drops a task's limit unless the template allows +// overriding it, so the playbook would run against the whole inventory. +func (s *semaphore) confined(ctx context.Context) error { + for _, name := range slices.Sorted(maps.Keys(s.templates)) { + t := s.templates[name] + var tpl struct { + TaskParams struct { + AllowOverrideLimit bool `json:"allow_override_limit"` + } `json:"task_params"` + } + if err := s.do(ctx, http.MethodGet, fmt.Sprintf("/api/project/%d/templates/%d", s.project, t.TemplateID), nil, &tpl); err != nil { + return fmt.Errorf("template %q: %w", t.Action, err) + } + if !tpl.TaskParams.AllowOverrideLimit && t.HostVar == "" { + return fmt.Errorf("template %q: Semaphore template %d ignores a task's limit, so it would run against every host; allow overriding the limit or set host_var", t.Action, t.TemplateID) + } + } + return nil +} + func reversibility(name string, readOnly bool) (connectorv1.Reversibility, error) { switch name { case "undone_automatically": @@ -158,13 +190,27 @@ func (s *semaphore) parse(action string, raw jsontext.Value) (Template, actionIn return Template{}, actionInput{}, fmt.Errorf("input: %w", err) } host := in["host"] - if host == "" { - return Template{}, actionInput{}, errors.New("input needs a host") + if !validHost.MatchString(host) || host == "all" { + return Template{}, actionInput{}, fmt.Errorf("host %q must name one inventory host", host) } delete(in, "host") + for name := range in { + if _, ok := t.Vars[name]; !ok { + return Template{}, actionInput{}, fmt.Errorf("%q isn't a variable of %s", name, action) + } + } + for name := range t.Vars { + if in[name] == "" { + return Template{}, actionInput{}, fmt.Errorf("input needs %s", name) + } + } return t, actionInput{Host: host, Vars: in}, nil } +// validHost is one inventory name: no patterns, lists or exclusions that +// would widen a limit. +var validHost = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,252}$`) + func (s *semaphore) Resolve(_ context.Context, action string, raw jsontext.Value) (*connectorv1.ResolveResponse, error) { t, in, err := s.parse(action, raw) if err != nil { @@ -228,11 +274,21 @@ func (s *semaphore) findTask(ctx context.Context, templateID int, message string } func (s *semaphore) launch(ctx context.Context, t Template, in actionInput, message string) (task, error) { - env, err := json.Marshal(in.Vars, json.FormatNilMapAsNull(false)) + vars := maps.Clone(in.Vars) + if vars == nil { + vars = map[string]string{} + } + if t.HostVar != "" { + vars[t.HostVar] = in.Host + } + env, err := json.Marshal(vars, json.Deterministic(true)) if err != nil { return task{}, err } - body := map[string]any{"template_id": t.TemplateID, "environment": string(env), "limit": in.Host, "message": message} + body := map[string]any{ + "template_id": t.TemplateID, "environment": string(env), "message": message, + "params": map[string]any{"limit": []string{in.Host}}, + } var tk task if err := s.do(ctx, http.MethodPost, fmt.Sprintf("/api/project/%d/tasks", s.project), body, &tk); err != nil { return task{}, fmt.Errorf("launch template %d: %w", t.TemplateID, err) diff --git a/connectors/semaphore/semaphore_test.go b/connectors/semaphore/semaphore_test.go index 749936c..65dc806 100644 --- a/connectors/semaphore/semaphore_test.go +++ b/connectors/semaphore/semaphore_test.go @@ -22,6 +22,18 @@ type fakeSemaphore struct { tasks []task launches int finalStatus string + // limitFixed is a template that ignores a task's limit. + limitFixed bool + lastLaunch launchBody +} + +type launchBody struct { + TemplateID int `json:"template_id"` + Message string `json:"message"` + Environment string `json:"environment"` + Params struct { + Limit []string `json:"limit"` + } `json:"params"` } func (f *fakeSemaphore) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -32,14 +44,14 @@ func (f *fakeSemaphore) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } switch { + case r.Method == http.MethodGet && r.URL.Path == "/api/project/3/templates/7": + _ = json.MarshalWrite(w, map[string]any{"id": 7, "task_params": map[string]any{"allow_override_limit": !f.limitFixed}}) case r.Method == http.MethodGet && r.URL.Path == "/api/project/3/tasks/last": _ = json.MarshalWrite(w, f.tasks) case r.Method == http.MethodPost && r.URL.Path == "/api/project/3/tasks": - var body struct { - TemplateID int `json:"template_id"` - Message string `json:"message"` - } + var body launchBody _ = json.UnmarshalRead(r.Body, &body) + f.lastLaunch = body f.launches++ t := task{ID: len(f.tasks) + 1, TemplateID: body.TemplateID, Status: "waiting", Message: body.Message} f.tasks = append(f.tasks, t) @@ -59,19 +71,27 @@ func (f *fakeSemaphore) ServeHTTP(w http.ResponseWriter, r *http.Request) { func newTestConnector(t *testing.T, url string) *semaphore { t.Helper() + s, err := startConnector(url, "") + if err != nil { + t.Fatal(err) + } + return s +} + +func startConnector(url, hostVar string) (*semaphore, error) { s, err := newSemaphore(connector.Config{ Settings: map[string]string{ "base_url": url, "project_id": "3", - "templates": `[{"action":"clean_tmp","template_id":7,"effect":{"reversibility":"irreversible","summary":"delete old files"},"target":{"kind":"host","environment":"non-production"},"vars":{"older_than_hours":"Age of files to delete"}}]`, + "templates": `[{"action":"clean_tmp","template_id":7,"host_var":"` + hostVar + `","effect":{"reversibility":"irreversible","summary":"delete old files"},"target":{"kind":"host","environment":"non-production"},"vars":{"older_than_hours":"Age of files to delete"}}]`, }, Secrets: map[string]string{"api.token": "plainly-fake-token"}, //nolint:gosec // a fake token the fake server checks }) if err != nil { - t.Fatal(err) + return nil, err } s.pollEvery, s.deadline = time.Millisecond, 50*time.Millisecond - return s + return s, nil } // TestLaunchHappensOncePerKey guards #2001's idempotency: a launch carries @@ -116,3 +136,55 @@ func TestLaunchHappensOncePerKey(t *testing.T) { }) } } + +// TestALaunchRunsOnlyOnItsHost guards the connector's confinement: Semaphore +// drops a task's limit unless the template allows overriding it, so a template +// that would run against the whole inventory is refused at start, and an input +// can neither widen the host nor add variables the operator didn't declare. +func TestALaunchRunsOnlyOnItsHost(t *testing.T) { + tests := []struct { + name string + limitFixed bool + hostVar string + input string + wantStart bool + wantLaunch bool + wantEnv string + }{ + {name: "a template honouring the limit gets it", input: `{"host":"web-1","older_than_hours":"24"}`, wantStart: true, wantLaunch: true, wantEnv: `{"older_than_hours":"24"}`}, + {name: "a template ignoring the limit is refused", limitFixed: true, wantStart: false}, + {name: "a template ignoring the limit gets the host as its variable", limitFixed: true, hostVar: "clean_host", input: `{"host":"web-1","older_than_hours":"24"}`, wantStart: true, wantLaunch: true, wantEnv: `{"clean_host":"web-1","older_than_hours":"24"}`}, + {name: "an undeclared variable isn't passed", input: `{"host":"web-1","older_than_hours":"24","ansible_become_user":"root"}`, wantStart: true}, + {name: "a missing variable isn't launched", input: `{"host":"web-1"}`, wantStart: true}, + {name: "all isn't a host", input: `{"host":"all","older_than_hours":"24"}`, wantStart: true}, + {name: "a pattern isn't a host", input: `{"host":"web-*","older_than_hours":"24"}`, wantStart: true}, + {name: "a list isn't a host", input: `{"host":"web-1,web-2","older_than_hours":"24"}`, wantStart: true}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + fake := &fakeSemaphore{finalStatus: "success", limitFixed: tt.limitFixed} + srv := httptest.NewServer(fake) + defer srv.Close() + s, err := startConnector(srv.URL, tt.hostVar) + if (err == nil) != tt.wantStart { + t.Fatalf("start err = %v, want started %v", err, tt.wantStart) + } + if err != nil { + return + } + _, _, err = s.Invoke(t.Context(), "clean_tmp", jsontext.Value(tt.input), "run-1:investigate:4") + if launched := fake.launches > 0; launched != tt.wantLaunch || (err == nil) != tt.wantLaunch { + t.Fatalf("launched = %v, err = %v; want launched %v", launched, err, tt.wantLaunch) + } + if !tt.wantLaunch { + return + } + if got := fake.lastLaunch.Params.Limit; len(got) != 1 || got[0] != "web-1" { + t.Fatalf("limit = %v, want [web-1]", got) + } + if fake.lastLaunch.Environment != tt.wantEnv { + t.Fatalf("environment = %s, want %s", fake.lastLaunch.Environment, tt.wantEnv) + } + }) + } +}