Skip to content

Commit c8a8325

Browse files
authored
fix(api): resolution edition wasn't validating all the fields (including step state) causing internal issues (#250)
Signed-off-by: Romain Beuque <556072+rbeuque74@users.noreply.github.com>
1 parent 4d9c84b commit c8a8325

File tree

3 files changed

+92
-47
lines changed

3 files changed

+92
-47
lines changed

api/handler/resolution.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,30 @@ func UpdateResolution(c *gin.Context, in *updateResolutionIn) error {
255255

256256
if in.Steps != nil {
257257
r.Steps = in.Steps
258+
259+
tt, err := tasktemplate.LoadFromID(dbp, t.TemplateID)
260+
if err != nil {
261+
_ = dbp.Rollback()
262+
return err
263+
}
264+
265+
// valid and normalize steps
266+
for name, st := range r.Steps {
267+
if err := st.ValidAndNormalize(name, tt.BaseConfigurations, r.Steps); err != nil {
268+
return errors.NewNotValid(err, fmt.Sprintf("invalid step %s", name))
269+
}
270+
271+
valid, err := st.CheckIfValidState()
272+
if err != nil {
273+
_ = dbp.Rollback()
274+
return err
275+
}
276+
277+
if !valid {
278+
_ = dbp.Rollback()
279+
return errors.NewBadRequest(nil, fmt.Sprintf("step %q: invalid state provided: %q is not allowed", name, st.State))
280+
}
281+
}
258282
}
259283

260284
if in.ResolverInputs != nil {
@@ -743,6 +767,17 @@ func UpdateResolutionStep(c *gin.Context, in *updateResolutionStepIn) error {
743767
return err
744768
}
745769

770+
valid, err := r.Steps[in.StepName].CheckIfValidState()
771+
if err != nil {
772+
_ = dbp.Rollback()
773+
return err
774+
}
775+
776+
if !valid {
777+
_ = dbp.Rollback()
778+
return errors.NewBadRequest(nil, fmt.Sprintf("invalid state provided: %q is not allowed", in.State))
779+
}
780+
746781
logrus.WithFields(logrus.Fields{"resolution_id": r.PublicID}).Debugf("Handler UpdateResolutionStep: manual update of resolution %s step %s", r.PublicID, in.StepName)
747782

748783
if err := r.Update(dbp); err != nil {

engine/step/step.go

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,7 @@ func (st *Step) ValidAndNormalize(name string, baseConfigs map[string]json.RawMe
561561
}
562562
preHook, err := st.GetPreHook()
563563
if err != nil {
564-
return errors.NewNotValid(err, "Invalid prehook action")
564+
return errors.NewNotValid(err, "Invalid prehook action")
565565
}
566566
if preHook != nil {
567567
ph, err := validExecutor(baseConfigs, *preHook, nil)
@@ -573,52 +573,6 @@ func (st *Step) ValidAndNormalize(name string, baseConfigs map[string]json.RawMe
573573
}
574574
}
575575

576-
// check that we don't set restricted field from the template
577-
if st.State != "" {
578-
return errors.NewNotValid(nil, "step state must not be set")
579-
}
580-
581-
if st.ChildrenSteps != nil {
582-
return errors.NewNotValid(nil, "step children_steps must not be set")
583-
}
584-
585-
if st.ChildrenStepMap != nil {
586-
return errors.NewNotValid(nil, "step children_steps_map must not be set")
587-
}
588-
589-
if st.Output != nil {
590-
return errors.NewNotValid(nil, "step output must not be set")
591-
}
592-
593-
if st.Metadata != nil {
594-
return errors.NewNotValid(nil, "step metadatas must not be set")
595-
}
596-
597-
if st.Tags != nil {
598-
return errors.NewNotValid(nil, "step tags must not be set")
599-
}
600-
601-
if st.Children != nil {
602-
return errors.NewNotValid(nil, "step children must not be set")
603-
}
604-
605-
if st.Error != "" {
606-
return errors.NewNotValid(nil, "step error must not be set")
607-
}
608-
609-
if st.TryCount != 0 {
610-
return errors.NewNotValid(nil, "step try_count must not be set")
611-
}
612-
613-
t := time.Time{}
614-
if st.LastRun != t {
615-
return errors.NewNotValid(nil, "step last_time must not be set")
616-
}
617-
618-
if st.Item != nil {
619-
return errors.NewNotValid(nil, "step item must not be set")
620-
}
621-
622576
if st.ForEachStrategy != "" && st.ForEach == "" {
623577
return errors.NewNotValid(nil, "step foreach_strategy can't be set without foreach")
624578
}
@@ -711,6 +665,58 @@ func (st *Step) ValidAndNormalize(name string, baseConfigs map[string]json.RawMe
711665
return nil
712666
}
713667

668+
// ValidAndNormalizeNewStep will validate that a given step doesn't have extragenous fields defined
669+
// when coming from a task_template.
670+
func (st *Step) ValidAndNormalizeNewStep() error {
671+
// check that we don't set restricted field from the template
672+
if st.State != "" {
673+
return errors.NewNotValid(nil, "step state must not be set")
674+
}
675+
676+
if st.ChildrenSteps != nil {
677+
return errors.NewNotValid(nil, "step children_steps must not be set")
678+
}
679+
680+
if st.ChildrenStepMap != nil {
681+
return errors.NewNotValid(nil, "step children_steps_map must not be set")
682+
}
683+
684+
if st.Output != nil {
685+
return errors.NewNotValid(nil, "step output must not be set")
686+
}
687+
688+
if st.Metadata != nil {
689+
return errors.NewNotValid(nil, "step metadatas must not be set")
690+
}
691+
692+
if st.Tags != nil {
693+
return errors.NewNotValid(nil, "step tags must not be set")
694+
}
695+
696+
if st.Children != nil {
697+
return errors.NewNotValid(nil, "step children must not be set")
698+
}
699+
700+
if st.Error != "" {
701+
return errors.NewNotValid(nil, "step error must not be set")
702+
}
703+
704+
if st.TryCount != 0 {
705+
return errors.NewNotValid(nil, "step try_count must not be set")
706+
}
707+
708+
t := time.Time{}
709+
if st.LastRun != t {
710+
return errors.NewNotValid(nil, "step last_time must not be set")
711+
}
712+
713+
if st.Item != nil {
714+
return errors.NewNotValid(nil, "step item must not be set")
715+
}
716+
717+
return nil
718+
}
719+
714720
// IsRunnable asserts that Step is in a runnable state
715721
func (st *Step) IsRunnable() bool {
716722
return utils.ListContainsString(runnableStates, st.State)

models/tasktemplate/template.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ func (tt *TaskTemplate) Valid() (err error) {
345345
if err := st.ValidAndNormalize(name, tt.BaseConfigurations, tt.Steps); err != nil {
346346
return errors.NewNotValid(err, fmt.Sprintf("Invalid step %s", name))
347347
}
348+
349+
if err := st.ValidAndNormalizeNewStep(); err != nil {
350+
return errors.NewNotValid(err, fmt.Sprintf("Invalid step %s", name))
351+
}
348352
}
349353

350354
// MarshalIndent as it's easier to read line by line

0 commit comments

Comments
 (0)