fix(daemon): open model egress only for workflows with a model step
This commit is contained in:
3 files changed
+73
-1
No files matched your search
@@ -149,8 +149,12 @@ type ModelSession struct {
|
||||
}
|
||||
|
||||
// openModelEgress opens task's model session, or refuses: provider keys
|
||||
// never reach a container, so without the proxy no model call can run.
|
||||
// never reach a container, so without the proxy no model call can run. A
|
||||
// workflow with no model step needs no session and no provider credential.
|
||||
func (d *Daemon) openModelEgress(ctx context.Context, task *workflow.Task, credential string) (ModelSession, func(), error) {
|
||||
if !workflow.UsesModel(task.WorkflowDefinitionYAML) {
|
||||
return ModelSession{}, func() {}, nil
|
||||
}
|
||||
cfg, err := d.taskConfig(ctx, task)
|
||||
if err != nil {
|
||||
return ModelSession{}, nil, err
|
||||
|
||||
@@ -294,3 +294,44 @@ func checkStepAliases(workflowID string, steps []StepRecord, aliases map[string]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// modelFreeStepTypes are the step types that never call a model. Any other
|
||||
// type counts as model use, so an unknown or new step keeps the credential
|
||||
// boundary.
|
||||
var modelFreeStepTypes = map[string]bool{
|
||||
FinishStepName: true, HandoffStepName: true, ApproveStepName: true,
|
||||
CloseIssueStepName: true, CommentStepName: true, CommandRunStepName: true,
|
||||
WorkflowCallStepName: true, RepoPrepareStepName: true, RepoCommitStepName: true,
|
||||
RepoOpenPRStepName: true, GateRepositoryName: true, GateDiffSizeStepName: true, DiffRulesStepName: true,
|
||||
}
|
||||
|
||||
// UsesModel reports whether a pinned definition has any step that may call a
|
||||
// model. A missing or undecodable definition counts as using one.
|
||||
func UsesModel(src string) bool {
|
||||
var definition YAMLDefinition
|
||||
if err := yaml.Unmarshal([]byte(src), &definition); err != nil || len(definition.Steps) == 0 {
|
||||
return true
|
||||
}
|
||||
return stepsUseModel(definition.Steps)
|
||||
}
|
||||
|
||||
func stepsUseModel(steps []StepRecord) bool {
|
||||
for _, step := range steps {
|
||||
for _, branch := range step.Parallel {
|
||||
if stepsUseModel(branch) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
if step.Switch != nil {
|
||||
for _, branch := range step.Switch.Cases {
|
||||
if stepsUseModel(branch) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
if step.Parallel == nil && step.Switch == nil && !modelFreeStepTypes[step.Type] {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package workflow
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestUsesModel(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
src string
|
||||
want bool
|
||||
}{
|
||||
{"finish only", "id: w\nsteps:\n - type: workflow.finish\n", false},
|
||||
{"repo and gates", "id: w\nsteps:\n - type: repo.prepare\n - type: gate.repository\n - type: repo.commit\n", false},
|
||||
{"agent step", "id: w\nsteps:\n - type: agent.run\n", true},
|
||||
{"agent inside parallel", "id: w\nsteps:\n - parallel:\n a:\n - type: workflow.finish\n b:\n - type: agent.run\n", true},
|
||||
{"agent inside switch", "id: w\nsteps:\n - switch:\n on: inputs.x\n cases:\n default:\n - type: agent.run\n", true},
|
||||
{"unknown step", "id: w\nsteps:\n - type: something.new\n", true},
|
||||
{"no definition", "", true},
|
||||
{"undecodable", "steps: [", true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := UsesModel(tc.src); got != tc.want {
|
||||
t.Fatalf("UsesModel = %v, want %v", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user