fix(daemon): bound a package workflow's triggers by its accepted authority

This commit is contained in:
sam committed 2026-10-11 04:08:02 +11:00
1 parent 23a96bf015
commit 2ee0b1df45
3 files changed
+106

No files matched your search

+3
View File
@@ -2064,6 +2064,9 @@ func (d *Daemon) pinWorkflowDefinition(ctx context.Context, task *workflow.Task)
if !enablement.Enabled(task.Org, task.Workflow) {
return pinFailure{taskstate.ParkNeedsHuman, fmt.Errorf("workflow %s is disabled", task.Workflow)}
}
if err := d.checkPackageTrigger(ctx, task); err != nil {
return pinFailure{taskstate.ParkNeedsHuman, err}
}
return nil
}
+59
View File
@@ -0,0 +1,59 @@
package daemon
import (
"context"
"fmt"
"slices"
"strings"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
// startTriggers names how a task was started, in the vocabulary a package's
// accepted authority lists: "source:<source>" for where the task came from,
// "call" for a workflow.call, "binding:<id>" for a binding dispatch and
// "label:<name>" for each issue label.
func startTriggers(task *workflow.Task) []string {
source := task.Source
if source == "" {
source = workflow.SourceForge
}
triggers := []string{"source:" + source}
if task.CallParentTaskID != 0 {
triggers = append(triggers, "call")
}
if task.BindingID != "" {
triggers = append(triggers, "binding:"+task.BindingID)
}
for label := range strings.SplitSeq(task.Labels, ",") {
if label = strings.TrimSpace(label); label != "" {
triggers = append(triggers, "label:"+label)
}
}
return triggers
}
// checkPackageTrigger refuses to run a package's workflow unless one of the
// ways the task started is a trigger accepted for that package. A missing,
// unaccepted or unreadable package accepts none. Operator workflows are not
// bounded here.
func (d *Daemon) checkPackageTrigger(ctx context.Context, task *workflow.Task) error {
name := workflow.PackageOf(task.WorkflowDefinitionYAML)
if name == "" {
return nil
}
if d.PackageAuthorities == nil {
return fmt.Errorf("package %s: no accepted authority is available", name)
}
authority, err := d.PackageAuthorities.AcceptedAuthority(ctx, name)
if err != nil {
return fmt.Errorf("package %s: accepted authority unavailable: %w", name, err)
}
started := startTriggers(task)
for _, accepted := range authority.Triggers {
if slices.Contains(started, accepted) {
return nil
}
}
return fmt.Errorf("package %s: none of the triggers that started this task (%s) is accepted", name, strings.Join(started, ", "))
}
+44
View File
@@ -0,0 +1,44 @@
package daemon
import (
"context"
"testing"
"github.com/samcharles93/archie-core/internal/domain/storepkg"
"github.com/samcharles93/archie-core/internal/domain/workflow"
)
// TestCheckPackageTrigger pins that a package workflow starts only by a
// trigger accepted for its package, failing closed when none is accepted or
// the package is unknown, while operator workflows are unbounded.
func TestCheckPackageTrigger(t *testing.T) {
d := &Daemon{PackageAuthorities: acceptedTools{
"triage": storepkg.Authority{Triggers: []string{"label:triage", "binding:b1", "call"}},
"chatty": storepkg.Authority{Triggers: []string{"source:chat"}},
"unsigned": storepkg.Authority{},
}}
tests := []struct {
name string
task workflow.Task
wantErr bool
}{
{"operator workflow", workflow.Task{WorkflowDefinitionYAML: "id: a\n"}, false},
{"accepted label", workflow.Task{WorkflowDefinitionYAML: "package: triage\nid: a\n", Labels: "bug, triage"}, false},
{"accepted binding", workflow.Task{WorkflowDefinitionYAML: "package: triage\nid: a\n", BindingID: "b1"}, false},
{"accepted call", workflow.Task{WorkflowDefinitionYAML: "package: triage\nid: a\n", CallParentTaskID: 7}, false},
{"accepted source", workflow.Task{WorkflowDefinitionYAML: "package: chatty\nid: a\n", Source: "chat"}, false},
{"unaccepted label", workflow.Task{WorkflowDefinitionYAML: "package: triage\nid: a\n", Labels: "bug"}, true},
{"unaccepted binding", workflow.Task{WorkflowDefinitionYAML: "package: triage\nid: a\n", BindingID: "b2"}, true},
{"forge source not accepted", workflow.Task{WorkflowDefinitionYAML: "package: chatty\nid: a\n"}, true},
{"nothing accepted", workflow.Task{WorkflowDefinitionYAML: "package: unsigned\nid: a\n", Labels: "triage"}, true},
{"package that is gone", workflow.Task{WorkflowDefinitionYAML: "package: gone\nid: a\n", Labels: "triage"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := d.checkPackageTrigger(context.Background(), &tt.task)
if (err != nil) != tt.wantErr {
t.Fatalf("err = %v, wantErr %v", err, tt.wantErr)
}
})
}
}