From 7de2fb50a8e766eb652c1209b6418759a0834888 Mon Sep 17 00:00:00 2001 From: Lionel Villard Date: Mon, 12 Jul 2021 16:47:12 -0400 Subject: [PATCH 1/2] disable @every --- pkg/apis/sources/v1/ping_validation.go | 17 ++++++- pkg/apis/sources/v1/ping_validation_test.go | 45 +++++++++++++++++++ pkg/apis/sources/v1beta2/ping_validation.go | 17 ++++++- .../sources/v1beta2/ping_validation_test.go | 45 +++++++++++++++++++ 4 files changed, 122 insertions(+), 2 deletions(-) diff --git a/pkg/apis/sources/v1/ping_validation.go b/pkg/apis/sources/v1/ping_validation.go index d8648974700..73273c1de78 100644 --- a/pkg/apis/sources/v1/ping_validation.go +++ b/pkg/apis/sources/v1/ping_validation.go @@ -20,6 +20,7 @@ import ( "context" "encoding/base64" "encoding/json" + "errors" "fmt" "strings" @@ -37,8 +38,10 @@ func (c *PingSource) Validate(ctx context.Context) *apis.FieldError { func (cs *PingSourceSpec) Validate(ctx context.Context) *apis.FieldError { var errs *apis.FieldError - schedule := cs.Schedule + + errs = validateDescriptor(schedule) + if cs.Timezone != "" { schedule = "CRON_TZ=" + cs.Timezone + " " + schedule } @@ -98,3 +101,15 @@ func validateJSON(str string) error { var objmap map[string]interface{} return json.Unmarshal([]byte(str), &objmap) } + +func validateDescriptor(spec string) *apis.FieldError { + if strings.HasPrefix(spec, "TZ=") || strings.HasPrefix(spec, "CRON_TZ=") { + i := strings.Index(spec, " ") + spec = strings.TrimSpace(spec[i:]) + } + + if strings.HasPrefix(spec, "@every") { + return apis.ErrInvalidValue(errors.New("unsupported descriptor @every"), "schedule") + } + return nil +} diff --git a/pkg/apis/sources/v1/ping_validation_test.go b/pkg/apis/sources/v1/ping_validation_test.go index f06e399883e..a4ee9092985 100644 --- a/pkg/apis/sources/v1/ping_validation_test.go +++ b/pkg/apis/sources/v1/ping_validation_test.go @@ -362,6 +362,51 @@ func TestPingSourceValidation(t *testing.T) { }, }, want: nil, + }, { + name: "unsupported @every descriptor", + source: PingSource{ + Spec: PingSourceSpec{ + Schedule: "@every 2h", + SourceSpec: duckv1.SourceSpec{ + Sink: duckv1.Destination{ + Ref: &duckv1.KReference{ + APIVersion: "v1", + Kind: "broker", + Name: "default", + }, + }, + }, + }, + }, + want: func() *apis.FieldError { + var errs *apis.FieldError + fe := apis.ErrInvalidValue("unsupported descriptor @every", "spec.schedule") + errs = errs.Also(fe) + return errs + }(), + }, { + name: "unsupported @every descriptor with TZ", + source: PingSource{ + Spec: PingSourceSpec{ + Schedule: "@every 2h", + Timezone: "Europe/Paris", + SourceSpec: duckv1.SourceSpec{ + Sink: duckv1.Destination{ + Ref: &duckv1.KReference{ + APIVersion: "v1", + Kind: "broker", + Name: "default", + }, + }, + }, + }, + }, + want: func() *apis.FieldError { + var errs *apis.FieldError + fe := apis.ErrInvalidValue("unsupported descriptor @every", "spec.schedule") + errs = errs.Also(fe) + return errs + }(), }, } diff --git a/pkg/apis/sources/v1beta2/ping_validation.go b/pkg/apis/sources/v1beta2/ping_validation.go index 5bbd2d7f0ef..6ea14b4046f 100644 --- a/pkg/apis/sources/v1beta2/ping_validation.go +++ b/pkg/apis/sources/v1beta2/ping_validation.go @@ -20,6 +20,7 @@ import ( "context" "encoding/base64" "encoding/json" + "errors" "fmt" "strings" @@ -37,8 +38,10 @@ func (c *PingSource) Validate(ctx context.Context) *apis.FieldError { func (cs *PingSourceSpec) Validate(ctx context.Context) *apis.FieldError { var errs *apis.FieldError - schedule := cs.Schedule + + errs = validateDescriptor(schedule) + if cs.Timezone != "" { schedule = "CRON_TZ=" + cs.Timezone + " " + schedule } @@ -98,3 +101,15 @@ func validateJSON(str string) error { var objmap map[string]interface{} return json.Unmarshal([]byte(str), &objmap) } + +func validateDescriptor(spec string) *apis.FieldError { + if strings.HasPrefix(spec, "TZ=") || strings.HasPrefix(spec, "CRON_TZ=") { + i := strings.Index(spec, " ") + spec = strings.TrimSpace(spec[i:]) + } + + if strings.HasPrefix(spec, "@every") { + return apis.ErrInvalidValue(errors.New("unsupported descriptor @every"), "schedule") + } + return nil +} diff --git a/pkg/apis/sources/v1beta2/ping_validation_test.go b/pkg/apis/sources/v1beta2/ping_validation_test.go index fbbb0a1b01d..664feb948ed 100644 --- a/pkg/apis/sources/v1beta2/ping_validation_test.go +++ b/pkg/apis/sources/v1beta2/ping_validation_test.go @@ -362,6 +362,51 @@ func TestPingSourceValidation(t *testing.T) { }, }, want: nil, + }, { + name: "unsupported @every descriptor", + source: PingSource{ + Spec: PingSourceSpec{ + Schedule: "@every 2h", + SourceSpec: duckv1.SourceSpec{ + Sink: duckv1.Destination{ + Ref: &duckv1.KReference{ + APIVersion: "v1", + Kind: "broker", + Name: "default", + }, + }, + }, + }, + }, + want: func() *apis.FieldError { + var errs *apis.FieldError + fe := apis.ErrInvalidValue("unsupported descriptor @every", "spec.schedule") + errs = errs.Also(fe) + return errs + }(), + }, { + name: "unsupported @every descriptor with TZ", + source: PingSource{ + Spec: PingSourceSpec{ + Schedule: "@every 2h", + Timezone: "Europe/Paris", + SourceSpec: duckv1.SourceSpec{ + Sink: duckv1.Destination{ + Ref: &duckv1.KReference{ + APIVersion: "v1", + Kind: "broker", + Name: "default", + }, + }, + }, + }, + }, + want: func() *apis.FieldError { + var errs *apis.FieldError + fe := apis.ErrInvalidValue("unsupported descriptor @every", "spec.schedule") + errs = errs.Also(fe) + return errs + }(), }, } From 04512e0bff23a3c2d265e3f6c6416a1fb1075e28 Mon Sep 17 00:00:00 2001 From: Lionel Villard Date: Tue, 13 Jul 2021 08:58:52 -0400 Subject: [PATCH 2/2] simpler validation --- pkg/apis/sources/v1/ping_validation.go | 7 +------ pkg/apis/sources/v1beta2/ping_validation.go | 7 +------ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/pkg/apis/sources/v1/ping_validation.go b/pkg/apis/sources/v1/ping_validation.go index 73273c1de78..c60f49a4f95 100644 --- a/pkg/apis/sources/v1/ping_validation.go +++ b/pkg/apis/sources/v1/ping_validation.go @@ -103,12 +103,7 @@ func validateJSON(str string) error { } func validateDescriptor(spec string) *apis.FieldError { - if strings.HasPrefix(spec, "TZ=") || strings.HasPrefix(spec, "CRON_TZ=") { - i := strings.Index(spec, " ") - spec = strings.TrimSpace(spec[i:]) - } - - if strings.HasPrefix(spec, "@every") { + if strings.Contains(spec, "@every") { return apis.ErrInvalidValue(errors.New("unsupported descriptor @every"), "schedule") } return nil diff --git a/pkg/apis/sources/v1beta2/ping_validation.go b/pkg/apis/sources/v1beta2/ping_validation.go index 6ea14b4046f..c8fa47a600d 100644 --- a/pkg/apis/sources/v1beta2/ping_validation.go +++ b/pkg/apis/sources/v1beta2/ping_validation.go @@ -103,12 +103,7 @@ func validateJSON(str string) error { } func validateDescriptor(spec string) *apis.FieldError { - if strings.HasPrefix(spec, "TZ=") || strings.HasPrefix(spec, "CRON_TZ=") { - i := strings.Index(spec, " ") - spec = strings.TrimSpace(spec[i:]) - } - - if strings.HasPrefix(spec, "@every") { + if strings.Contains(spec, "@every") { return apis.ErrInvalidValue(errors.New("unsupported descriptor @every"), "schedule") } return nil