This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
validation.go
325 lines (295 loc) · 10.3 KB
/
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package validation
import (
"strconv"
"strings"
"github.com/golang/protobuf/proto"
"k8s.io/apimachinery/pkg/util/validation"
"github.com/flyteorg/flyteadmin/pkg/common"
"github.com/flyteorg/flyteadmin/pkg/errors"
"github.com/flyteorg/flyteadmin/pkg/manager/impl/shared"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/admin"
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/core"
"github.com/flyteorg/flytepropeller/pkg/compiler/validators"
"google.golang.org/grpc/codes"
)
var entityToResourceType = map[common.Entity]core.ResourceType{
common.Task: core.ResourceType_TASK,
common.Workflow: core.ResourceType_WORKFLOW,
common.LaunchPlan: core.ResourceType_LAUNCH_PLAN,
}
func ValidateEmptyStringField(field, fieldName string) error {
if field == "" {
return shared.GetMissingArgumentError(fieldName)
}
return nil
}
// ValidateMaxLengthStringField Validates that a string field does not exceed a certain character count
func ValidateMaxLengthStringField(field string, fieldName string, limit int) error {
if len(field) > limit {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "%s cannot exceed %d characters", fieldName, limit)
}
return nil
}
// ValidateMaxMapLengthField Validates that a map field does not exceed a certain amount of entries
func ValidateMaxMapLengthField(m map[string]string, fieldName string, limit int) error {
if len(m) > limit {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "%s map cannot exceed %d entries", fieldName, limit)
}
return nil
}
func validateLabels(labels *admin.Labels) error {
if labels == nil || len(labels.Values) == 0 {
return nil
}
if err := ValidateMaxMapLengthField(labels.Values, "labels", maxLabelArrayLength); err != nil {
return err
}
if err := validateLabelsAlphanumeric(labels); err != nil {
return err
}
return nil
}
// Given an admin.Labels, checks if the labels exist or not and if it does, checks if the labels are K8s compliant,
// i.e. alphanumeric + - and _
func validateLabelsAlphanumeric(labels *admin.Labels) error {
for key, value := range labels.Values {
if errs := validation.IsQualifiedName(key); len(errs) > 0 {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "invalid label key [%s]: %v", key, errs)
}
if errs := validation.IsValidLabelValue(value); len(errs) > 0 {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "invalid label value [%s]: %v", value, errs)
}
}
return nil
}
func ValidateIdentifierFieldsSet(id *core.Identifier) error {
if id == nil {
return shared.GetMissingArgumentError(shared.ID)
}
if err := ValidateEmptyStringField(id.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(id.Domain, shared.Domain); err != nil {
return err
}
if err := ValidateEmptyStringField(id.Name, shared.Name); err != nil {
return err
}
if err := ValidateEmptyStringField(id.Version, shared.Version); err != nil {
return err
}
return nil
}
// ValidateIdentifier Validates that all required fields for an identifier are present.
func ValidateIdentifier(id *core.Identifier, expectedType common.Entity) error {
if id == nil {
return shared.GetMissingArgumentError(shared.ID)
}
if entityToResourceType[expectedType] != id.ResourceType {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"unexpected resource type %s for identifier [%+v], expected %s instead",
strings.ToLower(id.ResourceType.String()), id, strings.ToLower(entityToResourceType[expectedType].String()))
}
return ValidateIdentifierFieldsSet(id)
}
// ValidateNamedEntityIdentifier Validates that all required fields for an identifier are present.
func ValidateNamedEntityIdentifier(id *admin.NamedEntityIdentifier) error {
if id == nil {
return shared.GetMissingArgumentError(shared.ID)
}
if err := ValidateEmptyStringField(id.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(id.Domain, shared.Domain); err != nil {
return err
}
if err := ValidateEmptyStringField(id.Name, shared.Name); err != nil {
return err
}
return nil
}
func ValidateResourceType(resourceType core.ResourceType) error {
if resourceType == core.ResourceType_UNSPECIFIED || core.ResourceType.String(resourceType) == "" {
return shared.GetInvalidArgumentError(shared.ResourceType)
}
return nil
}
func ValidateVersion(version string) error {
if err := ValidateEmptyStringField(version, shared.Version); err != nil {
return err
}
return nil
}
func ValidateResourceListRequest(request admin.ResourceListRequest) error {
if request.Id == nil {
return shared.GetMissingArgumentError(shared.ID)
}
if err := ValidateEmptyStringField(request.Id.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Id.Domain, shared.Domain); err != nil {
return err
}
if err := ValidateLimit(request.Limit); err != nil {
return err
}
return nil
}
func ValidateDescriptionEntityListRequest(request admin.DescriptionEntityListRequest) error {
if request.Id == nil {
return shared.GetMissingArgumentError(shared.ID)
}
if err := ValidateEmptyStringField(request.Id.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Id.Domain, shared.Domain); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Id.Name, shared.Name); err != nil {
return err
}
if err := ValidateLimit(request.Limit); err != nil {
return err
}
return nil
}
func ValidateActiveLaunchPlanRequest(request admin.ActiveLaunchPlanRequest) error {
if err := ValidateEmptyStringField(request.Id.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Id.Domain, shared.Domain); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Id.Name, shared.Name); err != nil {
return err
}
return nil
}
func ValidateActiveLaunchPlanListRequest(request admin.ActiveLaunchPlanListRequest) error {
if err := ValidateEmptyStringField(request.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Domain, shared.Domain); err != nil {
return err
}
if err := ValidateLimit(request.Limit); err != nil {
return err
}
return nil
}
func ValidateNamedEntityIdentifierListRequest(request admin.NamedEntityIdentifierListRequest) error {
if err := ValidateEmptyStringField(request.Project, shared.Project); err != nil {
return err
}
if err := ValidateEmptyStringField(request.Domain, shared.Domain); err != nil {
return err
}
if err := ValidateLimit(request.Limit); err != nil {
return err
}
return nil
}
func ValidateDescriptionEntityGetRequest(request admin.ObjectGetRequest) error {
if err := ValidateResourceType(request.Id.ResourceType); err != nil {
return err
}
if err := ValidateIdentifierFieldsSet(request.Id); err != nil {
return err
}
return nil
}
func validateLiteralMap(inputMap *core.LiteralMap, fieldName string) error {
if inputMap != nil && len(inputMap.Literals) > 0 {
for name, fixedInput := range inputMap.Literals {
if name == "" {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "missing key in %s", fieldName)
}
if fixedInput == nil || fixedInput.GetValue() == nil {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "missing valid literal in %s %s", fieldName, name)
}
if isDateTime(fixedInput) {
// Make datetime specific validations
return ValidateDatetime(fixedInput)
}
}
}
return nil
}
func validateParameterMap(inputMap *core.ParameterMap, fieldName string) error {
if inputMap != nil && len(inputMap.Parameters) > 0 {
for name, defaultInput := range inputMap.Parameters {
if name == "" {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "missing key in %s", fieldName)
}
if defaultInput.GetVar() == nil || defaultInput.GetVar().GetType() == nil {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"The Variable component of the Parameter %s in %s either is missing, or has a missing Type",
name, fieldName)
}
if defaultInput.GetDefault() == nil && !defaultInput.GetRequired() {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"Invalid variable %s in %s - variable has neither default, nor is required. "+
"One must be specified", name, fieldName)
}
defaultValue := defaultInput.GetDefault()
if defaultValue != nil {
inputType := validators.LiteralTypeForLiteral(defaultValue)
if !validators.AreTypesCastable(inputType, defaultInput.GetVar().GetType()) {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument,
"Type mismatch for Parameter %s in %s has type %s, expected %s", name, fieldName,
defaultInput.GetVar().GetType().String(), inputType.String())
}
if defaultInput.GetVar().GetType().GetSimple() == core.SimpleType_DATETIME {
// Make datetime specific validations
return ValidateDatetime(defaultValue)
}
}
}
}
return nil
}
// ValidateToken Offsets are encoded as string tokens to enable future api pagination changes. In addition to validating that an
// offset is a valid integer, we assert that it is non-negative.
func ValidateToken(token string) (int, error) {
if token == "" {
return 0, nil
}
offset, err := strconv.Atoi(token)
if err != nil {
return 0, err
}
if offset < 0 {
return 0, errors.NewFlyteAdminErrorf(codes.InvalidArgument, "Invalid token value: %s", token)
}
return offset, nil
}
func ValidateLimit(limit uint32) error {
if limit == 0 {
return shared.GetInvalidArgumentError(shared.Limit)
}
return nil
}
func ValidateOutputData(outputData *core.LiteralMap, maxSizeInBytes int64) error {
if outputData == nil {
return nil
}
outputSizeInBytes := int64(proto.Size(outputData))
if outputSizeInBytes <= maxSizeInBytes {
return nil
}
return errors.NewFlyteAdminErrorf(codes.ResourceExhausted, "Output data size exceeds platform configured threshold (%+v > %v)", outputSizeInBytes, maxSizeInBytes)
}
func ValidateDatetime(literal *core.Literal) error {
if literal == nil {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, "Found invalid nil datetime")
}
timestamp := literal.GetScalar().GetPrimitive().GetDatetime()
err := timestamp.CheckValid()
if err != nil {
return errors.NewFlyteAdminErrorf(codes.InvalidArgument, err.Error())
}
return nil
}
func isDateTime(input *core.Literal) bool {
return input.GetScalar() != nil && input.GetScalar().GetPrimitive() != nil && input.GetScalar().GetPrimitive().GetDatetime() != nil
}