/
webhook_validation.go
229 lines (209 loc) · 8.24 KB
/
webhook_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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package graphql
import (
"encoding/json"
"net/url"
"unsafe"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
"github.com/kyma-incubator/compass/components/director/internal/model"
"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
"github.com/kyma-incubator/compass/components/director/pkg/webhook"
)
var emptyApplicationLifecycleWebhookRequestObject = &webhook.ApplicationLifecycleWebhookRequestObject{
Application: &Application{
BaseEntity: &BaseEntity{},
},
}
var emptyFormationConfigurationChangeInput = &webhook.FormationConfigurationChangeInput{
Assignment: &webhook.FormationAssignment{},
ReverseAssignment: &webhook.FormationAssignment{},
Formation: &model.Formation{
Error: json.RawMessage{},
},
ApplicationTemplate: &webhook.ApplicationTemplateWithLabels{
ApplicationTemplate: &model.ApplicationTemplate{},
Labels: map[string]string{},
Tenant: &webhook.TenantWithLabels{
BusinessTenantMapping: &model.BusinessTenantMapping{},
Labels: map[string]string{},
},
TrustDetails: &webhook.TrustDetails{
Subjects: []string{},
},
},
Application: &webhook.ApplicationWithLabels{
Application: &model.Application{
BaseEntity: &model.BaseEntity{},
},
Labels: map[string]string{},
Tenant: &webhook.TenantWithLabels{
BusinessTenantMapping: &model.BusinessTenantMapping{},
Labels: map[string]string{},
},
},
Runtime: &webhook.RuntimeWithLabels{
Runtime: &model.Runtime{},
Labels: map[string]string{},
Tenant: &webhook.TenantWithLabels{
BusinessTenantMapping: &model.BusinessTenantMapping{},
Labels: map[string]string{},
},
TrustDetails: &webhook.TrustDetails{
Subjects: []string{},
},
},
RuntimeContext: &webhook.RuntimeContextWithLabels{
RuntimeContext: &model.RuntimeContext{},
Labels: map[string]string{},
Tenant: &webhook.TenantWithLabels{
BusinessTenantMapping: &model.BusinessTenantMapping{},
Labels: map[string]string{},
},
},
CustomerTenantContext: &webhook.CustomerTenantContext{},
}
var emptyApplicationTenantMappingInput = &webhook.ApplicationTenantMappingInput{
Assignment: &webhook.FormationAssignment{},
ReverseAssignment: &webhook.FormationAssignment{},
Formation: &model.Formation{
Error: json.RawMessage{},
},
SourceApplicationTemplate: &webhook.ApplicationTemplateWithLabels{
ApplicationTemplate: &model.ApplicationTemplate{},
Labels: map[string]string{},
Tenant: &webhook.TenantWithLabels{
BusinessTenantMapping: &model.BusinessTenantMapping{},
Labels: map[string]string{},
},
TrustDetails: &webhook.TrustDetails{
Subjects: []string{},
},
},
SourceApplication: &webhook.ApplicationWithLabels{
Application: &model.Application{
BaseEntity: &model.BaseEntity{},
},
Labels: map[string]string{},
Tenant: &webhook.TenantWithLabels{
BusinessTenantMapping: &model.BusinessTenantMapping{},
Labels: map[string]string{},
},
},
TargetApplicationTemplate: &webhook.ApplicationTemplateWithLabels{
ApplicationTemplate: &model.ApplicationTemplate{},
Labels: map[string]string{},
Tenant: &webhook.TenantWithLabels{
BusinessTenantMapping: &model.BusinessTenantMapping{},
Labels: map[string]string{},
},
TrustDetails: &webhook.TrustDetails{
Subjects: []string{},
},
},
TargetApplication: &webhook.ApplicationWithLabels{
Application: &model.Application{
BaseEntity: &model.BaseEntity{},
},
Labels: map[string]string{},
Tenant: &webhook.TenantWithLabels{
BusinessTenantMapping: &model.BusinessTenantMapping{},
Labels: map[string]string{},
},
},
CustomerTenantContext: &webhook.CustomerTenantContext{},
}
var emptyFormationLifecycleInput = &webhook.FormationLifecycleInput{
Formation: &model.Formation{},
CustomerTenantContext: &webhook.CustomerTenantContext{},
}
var webhookTemplateInputByType = map[WebhookType]webhook.TemplateInput{
WebhookTypeRegisterApplication: emptyApplicationLifecycleWebhookRequestObject,
WebhookTypeUnregisterApplication: emptyApplicationLifecycleWebhookRequestObject,
WebhookTypeConfigurationChanged: emptyFormationConfigurationChangeInput,
WebhookTypeApplicationTenantMapping: emptyApplicationTenantMappingInput,
WebhookTypeFormationLifecycle: emptyFormationLifecycleInput,
}
// Validate missing godoc
func (i WebhookInput) Validate() error {
if err := validation.ValidateStruct(&i,
validation.Field(&i.Type, validation.Required, validation.In(WebhookTypeConfigurationChanged, WebhookTypeApplicationTenantMapping, WebhookTypeRegisterApplication, WebhookTypeUnregisterApplication, WebhookTypeOpenResourceDiscovery, WebhookTypeOpenResourceDiscoveryStatic, WebhookTypeFormationLifecycle, WebhookTypeSystemFieldDiscovery)),
validation.Field(&i.URL, is.URL, validation.RuneLength(0, longStringLengthLimit)),
validation.Field(&i.CorrelationIDKey, validation.RuneLength(0, longStringLengthLimit)),
validation.Field(&i.Mode, validation.In(WebhookModeSync, WebhookModeAsync, WebhookModeAsyncCallback), validation.When(i.Type == WebhookTypeConfigurationChanged || i.Type == WebhookTypeApplicationTenantMapping || i.Type == WebhookTypeFormationLifecycle, validation.In(WebhookModeSync, WebhookModeAsyncCallback)).Else(validation.NotIn(WebhookModeAsyncCallback))),
validation.Field(&i.RetryInterval, validation.Min(0)),
validation.Field(&i.Timeout, validation.Min(0)),
validation.Field(&i.Auth),
); err != nil {
return err
}
if i.URL == nil && i.URLTemplate == nil {
return apperrors.NewInvalidDataError("missing webhook url")
}
if i.URL != nil && i.URLTemplate != nil {
return apperrors.NewInvalidDataError("cannot provide both webhook url and url template")
}
if i.URL != nil {
_, err := url.ParseRequestURI(*i.URL)
if err != nil {
return apperrors.NewInvalidDataError("failed to parse webhook url: %s", err)
}
}
requestObject := webhookTemplateInputByType[i.Type]
if i.URLTemplate != nil {
if requestObject == nil {
return apperrors.NewInvalidDataError("missing template input for type: %s", i.Type)
}
if _, err := requestObject.ParseURLTemplate(i.URLTemplate); err != nil {
return apperrors.NewInvalidDataError("failed to parse webhook url template: %s", err)
}
}
if i.InputTemplate != nil {
if requestObject == nil {
return apperrors.NewInvalidDataError("missing template input for type: %s", i.Type)
}
if _, err := requestObject.ParseInputTemplate(i.InputTemplate); err != nil {
return apperrors.NewInvalidDataError("failed to parse webhook input template: %s", err)
}
}
if i.HeaderTemplate != nil {
if requestObject == nil {
return apperrors.NewInvalidDataError("missing template input for type: %s", i.Type)
}
if _, err := requestObject.ParseHeadersTemplate(i.HeaderTemplate); err != nil {
return apperrors.NewInvalidDataError("failed to parse webhook headers template: %s", err)
}
}
if i.OutputTemplate == nil && isOutTemplateMandatory(i.Type) {
return apperrors.NewInvalidDataError("outputTemplate is required for type: %v", i.Type)
}
var responseObject webhook.ResponseObject
if i.OutputTemplate != nil {
if _, err := responseObject.ParseOutputTemplate(i.OutputTemplate); err != nil {
return apperrors.NewInvalidDataError("failed to parse webhook output template: %s", err)
}
}
if i.Mode != nil && *i.Mode == WebhookModeAsync {
if i.StatusTemplate != nil {
if _, err := responseObject.ParseStatusTemplate(i.StatusTemplate); err != nil {
return apperrors.NewInvalidDataError("failed to parse webhook status template: %s", err)
}
} else {
return apperrors.NewInvalidDataError("missing webhook status template")
}
}
return nil
}
// GetAddress returns the memory address of the Webhook in the form of an uninterpreted type(integer number)
// Currently, it's used in some formation constraints input templates, so we could propagate the memory address to the formation constraints operators and later on to modify/update it.
func (w *Webhook) GetAddress() uintptr {
return uintptr(unsafe.Pointer(w))
}
func isOutTemplateMandatory(webhookType WebhookType) bool {
switch webhookType {
case WebhookTypeRegisterApplication,
WebhookTypeUnregisterApplication:
return true
default:
return false
}
}