Skip to content

Commit 56826a5

Browse files
authored
fix(blueprint): keep the scheduled plan unmasked after PATCH /blueprints (apache#9247)
PatchBlueprint sanitized the blueprint it had just handed to the cron job, and the gitextractor sanitizer rewrote the options map in place. The next scheduled pipeline then cloned with a masked URL until the server restarted. Normal-mode blueprints with credentials in beforePlan or afterPlan were hit too, since those tasks are shared with the generated plan. Sanitizing now works on copies: SanitizePluginOption masks a clone of the options, SanitizeTask returns a new task, and SanitizeBlueprint and SanitizePipeline build a new plan. PatchBlueprint sanitizes a copy of the saved blueprint for its response, so the plan it returns is still masked. Closes apache#9243 Signed-off-by: Yashvardhan Thanvi <[email protected]>
1 parent 6c6e20d commit 56826a5

3 files changed

Lines changed: 103 additions & 18 deletions

File tree

‎backend/server/services/blueprint.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,12 @@ func PatchBlueprint(id uint64, body map[string]interface{}) (*models.Blueprint,
230230
if err != nil {
231231
return nil, err
232232
}
233-
if err := SanitizeBlueprint(blueprint); err != nil {
233+
// the cron job registered by saveBlueprint holds this blueprint, so sanitize a copy for the response
234+
sanitized := *blueprint
235+
if err := SanitizeBlueprint(&sanitized); err != nil {
234236
return nil, errors.Convert(err)
235237
}
236-
return blueprint, nil
238+
return &sanitized, nil
237239
}
238240

239241
// DeleteBlueprint FIXME ...

‎backend/server/services/blueprint_test.go‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,72 @@ import (
2121
"testing"
2222

2323
coreModels "github.com/apache/devlake/core/models"
24+
"github.com/apache/devlake/impls/logruslog"
25+
mockdal "github.com/apache/devlake/mocks/core/dal"
26+
"github.com/robfig/cron/v3"
2427
"github.com/stretchr/testify/assert"
28+
"github.com/stretchr/testify/mock"
29+
"github.com/stretchr/testify/require"
2530
)
2631

32+
const (
33+
testGitUrl = "https://git:[email protected]/group/repo.git"
34+
testMaskedGitUrl = "https://git:************@example.com/group/repo.git"
35+
)
36+
37+
func TestSanitizeBlueprintLeavesTheSourcePlanUnchanged(t *testing.T) {
38+
bp := &coreModels.Blueprint{
39+
Plan: coreModels.PipelinePlan{{
40+
{Plugin: "gitextractor", Options: map[string]interface{}{"url": testGitUrl, "name": "group/repo"}},
41+
}},
42+
}
43+
44+
sanitized := *bp
45+
require.NoError(t, SanitizeBlueprint(&sanitized))
46+
47+
assert.Equal(t, map[string]interface{}{"url": testMaskedGitUrl, "name": "group/repo"}, sanitized.Plan[0][0].Options)
48+
assert.Equal(t, map[string]interface{}{"url": testGitUrl, "name": "group/repo"}, bp.Plan[0][0].Options)
49+
}
50+
51+
func TestPatchBlueprintKeepsTheScheduledPlanUnsanitized(t *testing.T) {
52+
withProjectTestValidator(t)
53+
testDB := mockdal.NewDal(t)
54+
withProjectTestDatabase(t, testDB)
55+
previousLogger, previousCron, previousCronIds := logger, cronManager, bpCronIdMap
56+
logger, cronManager, bpCronIdMap = logruslog.Global, cron.New(), map[uint64]cron.EntryID{}
57+
t.Cleanup(func() {
58+
logger, cronManager, bpCronIdMap = previousLogger, previousCron, previousCronIds
59+
})
60+
61+
testDB.On("First", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
62+
bp := args.Get(0).(*coreModels.Blueprint)
63+
bp.ID = 9
64+
bp.Name = "advanced"
65+
bp.Mode = coreModels.BLUEPRINT_MODE_ADVANCED
66+
bp.Enable = true
67+
bp.CronConfig = "0 0 * * *"
68+
bp.Plan = coreModels.PipelinePlan{{
69+
{Plugin: "gitextractor", Options: map[string]interface{}{"url": testGitUrl}},
70+
}}
71+
}).Return(nil).Once()
72+
testDB.On("Pluck", "name", mock.Anything, mock.Anything).Return(nil).Once()
73+
testDB.On("All", mock.Anything, mock.Anything).Return(nil).Once()
74+
testDB.On("Update", mock.Anything).Return(nil).Once()
75+
testDB.On("Delete", mock.Anything, mock.Anything).Return(nil).Times(3)
76+
77+
patched, err := PatchBlueprint(9, map[string]interface{}{"cronConfig": "*/5 * * * *"})
78+
require.NoError(t, err)
79+
assert.Equal(t, testMaskedGitUrl, patched.Plan[0][0].Options["url"])
80+
81+
// the cron job must still run the stored plan, not the one sanitized for the response
82+
entries := cronManager.Entries()
83+
require.Len(t, entries, 1)
84+
job, ok := entries[0].Job.(*BlueprintJob)
85+
require.True(t, ok)
86+
assert.Equal(t, "*/5 * * * *", job.Blueprint.CronConfig)
87+
assert.Equal(t, testGitUrl, job.Blueprint.Plan[0][0].Options["url"])
88+
}
89+
2790
func TestParallelizePipelineTasks(t *testing.T) {
2891
plan1 := coreModels.PipelinePlan{
2992
{

‎backend/server/services/pipeline.go‎

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ package services
2020
import (
2121
"context"
2222
"fmt"
23+
"maps"
2324
"net/url"
2425
"os"
2526
"path/filepath"
27+
"slices"
2628
"strings"
2729
"sync"
2830
"time"
@@ -44,6 +46,10 @@ import (
4446

4547
var defaultNotificationService *DefaultPipelineNotificationService
4648
var globalPipelineLog = logruslog.Global.Nested("pipeline service")
49+
50+
// pluginOptionSanitizers mask secrets in plugin options. Each one receives a
51+
// shallow copy of the options, so it may replace top-level values but must not
52+
// modify nested maps or slices in place.
4753
var pluginOptionSanitizers = map[string]func(map[string]interface{}){
4854
"gitextractor": func(options map[string]interface{}) {
4955
if v, ok := options["url"]; ok {
@@ -144,45 +150,59 @@ func CreatePipeline(newPipeline *models.NewPipeline, shouldSanitize bool) (*mode
144150
return pipeline, nil
145151
}
146152

153+
// SanitizeBlueprint replaces blueprint.Plan with a sanitized copy. The original
154+
// plan is left untouched because it may be shared, e.g. with a scheduled job.
147155
func SanitizeBlueprint(blueprint *models.Blueprint) error {
148-
for planStageIdx, pipelineStage := range blueprint.Plan {
149-
for planTaskIdx := range pipelineStage {
150-
pipelineTask, err := SanitizeTask(blueprint.Plan[planStageIdx][planTaskIdx])
151-
if err != nil {
152-
return err
153-
}
154-
blueprint.Plan[planStageIdx][planTaskIdx] = pipelineTask
155-
}
156+
plan, err := sanitizePlan(blueprint.Plan)
157+
if err != nil {
158+
return err
156159
}
160+
blueprint.Plan = plan
157161
return nil
158162
}
159163

164+
// SanitizePipeline replaces pipeline.Plan with a sanitized copy.
160165
func SanitizePipeline(pipeline *models.Pipeline) error {
161-
for planStageIdx, pipelineStage := range pipeline.Plan {
162-
for planTaskIdx := range pipelineStage {
163-
task := pipeline.Plan[planStageIdx][planTaskIdx]
166+
plan, err := sanitizePlan(pipeline.Plan)
167+
if err != nil {
168+
return err
169+
}
170+
pipeline.Plan = plan
171+
return nil
172+
}
173+
174+
func sanitizePlan(plan models.PipelinePlan) (models.PipelinePlan, error) {
175+
sanitized := slices.Clone(plan)
176+
for planStageIdx, pipelineStage := range plan {
177+
sanitized[planStageIdx] = slices.Clone(pipelineStage)
178+
for planTaskIdx, task := range pipelineStage {
164179
pipelineTask, err := SanitizeTask(task)
165180
if err != nil {
166-
return err
181+
return nil, err
167182
}
168-
pipeline.Plan[planStageIdx][planTaskIdx] = pipelineTask
183+
sanitized[planStageIdx][planTaskIdx] = pipelineTask
169184
}
170185
}
171-
return nil
186+
return sanitized, nil
172187
}
173188

189+
// SanitizeTask returns a copy of the task with sanitized options.
174190
func SanitizeTask(pipelineTask *models.PipelineTask) (*models.PipelineTask, error) {
175191
pluginName := pipelineTask.Plugin
176192
options, err := SanitizePluginOption(pluginName, pipelineTask.Options)
177193
if err != nil {
178194
return pipelineTask, err
179195
}
180-
pipelineTask.Options = options
181-
return pipelineTask, nil
196+
sanitized := *pipelineTask
197+
sanitized.Options = options
198+
return &sanitized, nil
182199
}
183200

201+
// SanitizePluginOption returns the sanitized options. The sanitizer works on a
202+
// shallow copy, so the given map is never modified.
184203
func SanitizePluginOption(pluginName string, option map[string]interface{}) (map[string]interface{}, error) {
185204
if sanitizer, ok := pluginOptionSanitizers[pluginName]; ok {
205+
option = maps.Clone(option)
186206
sanitizer(option)
187207
}
188208
return option, nil

0 commit comments

Comments
 (0)