-
Notifications
You must be signed in to change notification settings - Fork 413
/
event_listener_validation.go
154 lines (133 loc) · 4.62 KB
/
event_listener_validation.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package v1alpha1
import (
"context"
"fmt"
"net/http"
pipelinev1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1alpha1"
"k8s.io/apimachinery/pkg/util/validation"
"knative.dev/pkg/apis"
)
// Validate EventListener.
func (e *EventListener) Validate(ctx context.Context) *apis.FieldError {
return e.Spec.validate(ctx, e)
}
func (s *EventListenerSpec) validate(ctx context.Context, el *EventListener) *apis.FieldError {
if len(s.Triggers) == 0 {
return apis.ErrMissingField("spec.triggers")
}
for i, trigger := range s.Triggers {
if err := trigger.validate(ctx).ViaField(fmt.Sprintf("spec.triggers[%d]", i)); err != nil {
return err
}
}
return nil
}
func (t *EventListenerTrigger) validate(ctx context.Context) *apis.FieldError {
// Validate that only one of binding or bindings is set
if t.DeprecatedBinding != nil && len(t.Bindings) > 0 {
return apis.ErrMultipleOneOf("binding", "bindings")
}
// Validate that only one of inteceptor or interceptors is set
if t.DeprecatedInterceptor != nil && len(t.Interceptors) > 0 {
return apis.ErrMultipleOneOf("interceptor", "interceptors")
}
// Validate optional TriggerBinding
for i, b := range t.Bindings {
if b.Name == "" {
return apis.ErrMissingField(fmt.Sprintf("bindings[%d].name", i))
}
}
// Validate required TriggerTemplate
// Optional explicit match
if t.Template.APIVersion != "" {
if t.Template.APIVersion != "v1alpha1" {
return apis.ErrInvalidValue(fmt.Errorf("invalid apiVersion"), "template.apiVersion")
}
}
if t.Template.Name == "" {
return apis.ErrMissingField(fmt.Sprintf("template.name"))
}
for i, interceptor := range t.Interceptors {
if err := interceptor.validate(ctx).ViaField(fmt.Sprintf("interceptors[%d]", i)); err != nil {
return err
}
}
// The trigger name is added as a label value for 'tekton.dev/trigger' so it must follow the k8s label guidelines:
// https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#syntax-and-character-set
if errs := validation.IsValidLabelValue(t.Name); len(errs) > 0 {
return apis.ErrInvalidValue(fmt.Sprintf("trigger name '%s' must be a valid label value", t.Name), "name")
}
return nil
}
func (i *EventInterceptor) validate(ctx context.Context) *apis.FieldError {
if i.Webhook == nil && i.GitHub == nil && i.GitLab == nil && i.CEL == nil {
return apis.ErrMissingField("interceptor")
}
// Enforce oneof
numSet := 0
if i.Webhook != nil {
numSet++
}
if i.GitHub != nil {
numSet++
}
if i.GitLab != nil {
numSet++
}
if numSet > 1 {
return apis.ErrMultipleOneOf("interceptor.webhook", "interceptor.github", "interceptor.gitlab")
}
if i.Webhook != nil {
if i.Webhook.ObjectRef == nil || i.Webhook.ObjectRef.Name == "" {
return apis.ErrMissingField("interceptor.webhook.objectRef")
}
w := i.Webhook
if w.ObjectRef.Kind != "Service" {
return apis.ErrInvalidValue(fmt.Errorf("invalid kind"), "interceptor.webhook.objectRef.kind")
}
// Optional explicit match
if w.ObjectRef.APIVersion != "v1" {
return apis.ErrInvalidValue(fmt.Errorf("invalid apiVersion"), "interceptor.webhook.objectRef.apiVersion")
}
for i, header := range w.Header {
// Enforce non-empty canonical header keys
if len(header.Name) == 0 || http.CanonicalHeaderKey(header.Name) != header.Name {
return apis.ErrInvalidValue(fmt.Errorf("invalid header name"), fmt.Sprintf("interceptor.webhook.header[%d].name", i))
}
// Enforce non-empty header values
if header.Value.Type == pipelinev1.ParamTypeString {
if len(header.Value.StringVal) == 0 {
return apis.ErrInvalidValue(fmt.Errorf("invalid header value"), fmt.Sprintf("interceptor.webhook.header[%d].value", i))
}
} else if len(header.Value.ArrayVal) == 0 {
return apis.ErrInvalidValue(fmt.Errorf("invalid header value"), fmt.Sprintf("interceptor.webhook.header[%d].value", i))
}
}
}
// No github validation required yet.
// if i.GitHub != nil {
//
// }
// No gitlab validation required yet.
// if i.GitLab != nil {
//
// }
if i.CEL != nil {
if i.CEL.Filter == "" {
return apis.ErrMissingField("interceptor.cel.filter")
}
}
return nil
}