Skip to content

Commit 020aeec

Browse files
committed
Restrict/warn overridding of pre/post definitions in tests and requires a manual flag
1 parent 3c4f98a commit 020aeec

File tree

5 files changed

+392
-0
lines changed

5 files changed

+392
-0
lines changed

pkg/api/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,10 @@ type MultiStageTestConfiguration struct {
12241224
// NodeArchitectureOverrides is a map that allows overriding the node architecture for specific steps
12251225
// that exist in the Pre, Test and Post steps. The key is the name of the step and the value is the architecture.
12261226
NodeArchitectureOverrides NodeArchitectureOverrides `json:"node_architecture_overrides,omitempty"`
1227+
// AllowPrePostStepOverrides must be set to true when a test configuration overrides the pre or post steps
1228+
// from a workflow. This is a safety mechanism to prevent accidental overriding of critical setup and
1229+
// teardown steps that could cause resource leaks.
1230+
AllowPrePostStepOverrides *bool `json:"allow_pre_post_step_overrides,omitempty"`
12271231
}
12281232

12291233
type NodeArchitectureOverrides map[string]NodeArchitecture

pkg/api/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/registry/resolver.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package registry
22

33
import (
44
"fmt"
5+
"strings"
56

67
utilerrors "k8s.io/apimachinery/pkg/util/errors"
78
"k8s.io/apimachinery/pkg/util/sets"
@@ -68,6 +69,12 @@ func NewResolver(stepsByName ReferenceByName, chainsByName ChainByName, workflow
6869

6970
func (r *registry) Resolve(name string, config api.MultiStageTestConfiguration) (api.MultiStageTestConfigurationLiteral, error) {
7071
var overridden [][]api.TestStep
72+
73+
// Validate workflow overrides before merging
74+
if err := r.validateWorkflowOverrides(config); err != nil {
75+
return api.MultiStageTestConfigurationLiteral{}, err
76+
}
77+
7178
if config.Workflow != nil {
7279
var errs []error
7380
overridden, errs = r.mergeWorkflow(&config)
@@ -78,6 +85,35 @@ func (r *registry) Resolve(name string, config api.MultiStageTestConfiguration)
7885
return r.resolveTest(config, stackForTest(name, config.Environment, config.Dependencies, config.DNSConfig, config.NodeArchitecture), overridden)
7986
}
8087

88+
// validateWorkflowOverrides validates that tests properly acknowledge when they override workflow pre/post steps
89+
func (r *registry) validateWorkflowOverrides(config api.MultiStageTestConfiguration) error {
90+
if config.Workflow == nil {
91+
return nil
92+
}
93+
94+
workflow, ok := r.workflowsByName[*config.Workflow]
95+
if !ok {
96+
// This will be caught by mergeWorkflow, no need to duplicate the error
97+
return nil
98+
}
99+
100+
hasPreOverride := config.Pre != nil && len(workflow.Pre) > 0
101+
hasPostOverride := config.Post != nil && len(workflow.Post) > 0
102+
103+
if (hasPreOverride || hasPostOverride) && (config.AllowPrePostStepOverrides == nil || !*config.AllowPrePostStepOverrides) {
104+
var overriddenSections []string
105+
if hasPreOverride {
106+
overriddenSections = append(overriddenSections, "pre")
107+
}
108+
if hasPostOverride {
109+
overriddenSections = append(overriddenSections, "post")
110+
}
111+
return fmt.Errorf("test configuration overrides %s steps from workflow %q but does not have 'allow_pre_post_step_overrides: true' set. This flag is required to prevent accidental overriding of critical setup and teardown steps that could cause resource leaks", strings.Join(overriddenSections, " and "), *config.Workflow)
112+
}
113+
114+
return nil
115+
}
116+
81117
func (r *registry) mergeWorkflow(config *api.MultiStageTestConfiguration) ([][]api.TestStep, []error) {
82118
var overridden [][]api.TestStep
83119
workflow, ok := r.workflowsByName[*config.Workflow]
@@ -103,6 +139,7 @@ func (r *registry) mergeWorkflow(config *api.MultiStageTestConfiguration) ([][]a
103139
} else {
104140
overridden = append(overridden, workflow.Post)
105141
}
142+
106143
config.Environment = mergeEnvironments(workflow.Environment, config.Environment)
107144
config.Dependencies = mergeDependencies(workflow.Dependencies, config.Dependencies)
108145
config.DependencyOverrides = mergeDependencyOverrides(workflow.DependencyOverrides, config.DependencyOverrides)

0 commit comments

Comments
 (0)