Skip to content

Commit

Permalink
fix: Disallow object names with more than 63 chars (#5324)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Behar <simbeh7@gmail.com>
  • Loading branch information
simster7 committed Mar 8, 2021
1 parent e16bd95 commit 87b62c0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions workflow/validate/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ const (
anyItemMagicValue = "item.*"
anyWorkflowOutputParameterMagicValue = "workflow.outputs.parameters.*"
anyWorkflowOutputArtifactMagicValue = "workflow.outputs.artifacts.*"
maxCharsInObjectName = 63
)

var placeholderGenerator = common.NewPlaceholderGenerator()
Expand Down Expand Up @@ -109,6 +110,10 @@ func ValidateWorkflow(wftmplGetter templateresolution.WorkflowTemplateNamespaced
var wfTmplRef *wfv1.TemplateRef
var err error

if len(wf.Name) > maxCharsInObjectName {
return nil, fmt.Errorf("workflow name %q must not be more than 63 characters long (currently %d)", wf.Name, len(wf.Name))
}

entrypoint := wf.Spec.Entrypoint

hasWorkflowTemplateRef := wf.Spec.WorkflowTemplateRef != nil
Expand Down Expand Up @@ -233,6 +238,10 @@ func ValidateWorkflowTemplateRefFields(wfSpec wfv1.WorkflowSpec) error {

// ValidateWorkflowTemplate accepts a workflow template and performs validation against it.
func ValidateWorkflowTemplate(wftmplGetter templateresolution.WorkflowTemplateNamespacedGetter, cwftmplGetter templateresolution.ClusterWorkflowTemplateGetter, wftmpl *wfv1.WorkflowTemplate) (*wfv1.Conditions, error) {
if len(wftmpl.Name) > maxCharsInObjectName {
return nil, fmt.Errorf("workflow template name %q must not be more than 63 characters long (currently %d)", wftmpl.Name, len(wftmpl.Name))
}

wf := &wfv1.Workflow{
ObjectMeta: v1.ObjectMeta{
Labels: wftmpl.ObjectMeta.Labels,
Expand All @@ -245,6 +254,10 @@ func ValidateWorkflowTemplate(wftmplGetter templateresolution.WorkflowTemplateNa

// ValidateClusterWorkflowTemplate accepts a cluster workflow template and performs validation against it.
func ValidateClusterWorkflowTemplate(wftmplGetter templateresolution.WorkflowTemplateNamespacedGetter, cwftmplGetter templateresolution.ClusterWorkflowTemplateGetter, cwftmpl *wfv1.ClusterWorkflowTemplate) (*wfv1.Conditions, error) {
if len(cwftmpl.Name) > maxCharsInObjectName {
return nil, fmt.Errorf("cluster workflow template name %q must not be more than 63 characters long (currently %d)", cwftmpl.Name, len(cwftmpl.Name))
}

wf := &wfv1.Workflow{
ObjectMeta: v1.ObjectMeta{
Labels: cwftmpl.ObjectMeta.Labels,
Expand All @@ -257,6 +270,10 @@ func ValidateClusterWorkflowTemplate(wftmplGetter templateresolution.WorkflowTem

// ValidateCronWorkflow validates a CronWorkflow
func ValidateCronWorkflow(wftmplGetter templateresolution.WorkflowTemplateNamespacedGetter, cwftmplGetter templateresolution.ClusterWorkflowTemplateGetter, cronWf *wfv1.CronWorkflow) error {
if len(cronWf.Name) > maxCharsInObjectName {
return fmt.Errorf("cron workflow name %q must not be more than 63 characters long (currently %d)", cronWf.Name, len(cronWf.Name))
}

if _, err := cron.ParseStandard(cronWf.Spec.Schedule); err != nil {
return errors.Errorf(errors.CodeBadRequest, "cron schedule is malformed: %s", err)
}
Expand Down
19 changes: 19 additions & 0 deletions workflow/validate/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package validate

import (
"context"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -2682,3 +2683,21 @@ func TestValidActiveDeadlineSecondsArgoVariable(t *testing.T) {
err := validateWorkflowTemplate(validActiveDeadlineSecondsArgoVariable)
assert.NoError(t, err)
}

func TestMaxLengthName(t *testing.T) {
wf := &wfv1.Workflow{ObjectMeta: metav1.ObjectMeta{Name: strings.Repeat("a", 70)}}
_, err := ValidateWorkflow(wftmplGetter, cwftmplGetter, wf, ValidateOpts{})
assert.EqualError(t, err, "workflow name \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" must not be more than 63 characters long (currently 70)")

wftmpl := &wfv1.WorkflowTemplate{ObjectMeta: metav1.ObjectMeta{Name: strings.Repeat("a", 70)}}
_, err = ValidateWorkflowTemplate(wftmplGetter, cwftmplGetter, wftmpl)
assert.EqualError(t, err, "workflow template name \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" must not be more than 63 characters long (currently 70)")

cwftmpl := &wfv1.ClusterWorkflowTemplate{ObjectMeta: metav1.ObjectMeta{Name: strings.Repeat("a", 70)}}
_, err = ValidateClusterWorkflowTemplate(wftmplGetter, cwftmplGetter, cwftmpl)
assert.EqualError(t, err, "cluster workflow template name \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" must not be more than 63 characters long (currently 70)")

cwf := &wfv1.CronWorkflow{ObjectMeta: metav1.ObjectMeta{Name: strings.Repeat("a", 70)}}
err = ValidateCronWorkflow(wftmplGetter, cwftmplGetter, cwf)
assert.EqualError(t, err, "cron workflow name \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" must not be more than 63 characters long (currently 70)")
}

0 comments on commit 87b62c0

Please sign in to comment.