-
Notifications
You must be signed in to change notification settings - Fork 0
/
validators.go
629 lines (562 loc) · 20.4 KB
/
validators.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
package validation
import (
"context"
"errors"
"fmt"
"io"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/dcarbone/terraform-plugin-framework-utils/v3/conv"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
)
type TestFunc func(context.Context, GenericRequest, *GenericResponse)
// GenericConfig describes the configuration of a Generic
type GenericConfig struct {
Description string
MarkdownDescription string
Describer validator.Describer
TestFunc TestFunc
SkipWhenNull bool
SkipWhenUnknown bool
}
// Generic a validator that can be applied to variable attribute types
type Generic struct {
validator.Describer
d string
md string
fn TestFunc
skipNull bool
skipUnknown bool
}
// NewGenericValidator returns a type implementing every validator interface
func NewGenericValidator(conf GenericConfig) Generic {
if conf.TestFunc == nil {
panic("test function cannot be nil")
}
v := Generic{
Describer: conf.Describer,
d: conf.Description,
md: conf.MarkdownDescription,
fn: conf.TestFunc,
skipNull: conf.SkipWhenNull,
skipUnknown: conf.SkipWhenUnknown,
}
return v
}
func (g Generic) Validate(ctx context.Context, req GenericRequest, resp *GenericResponse) {
err := conv.TestAttributeValueState(req.ConfigValue)
if g.skipUnknown && conv.IsValueIsUnknownError(err) {
return
}
if g.skipNull && conv.IsValueIsNullError(err) {
return
}
// otherwise, fire away!
g.fn(ctx, req, resp)
}
func (g Generic) Description(ctx context.Context) string {
if g.Describer != nil {
return g.Describer.Description(ctx)
}
return g.d
}
func (g Generic) MarkdownDescription(ctx context.Context) string {
if g.Describer != nil {
return g.Describer.MarkdownDescription(ctx)
}
return g.md
}
func (g Generic) ValidateBool(ctx context.Context, req validator.BoolRequest, resp *validator.BoolResponse) {
rq, rp, err := toGenericTypes(req, resp)
if err != nil {
resp.Diagnostics.AddError("conversion error", err.Error())
return
}
g.Validate(ctx, rq, rp)
}
func (g Generic) ValidateFloat64(ctx context.Context, req validator.Float64Request, resp *validator.Float64Response) {
rq, rp, err := toGenericTypes(req, resp)
if err != nil {
resp.Diagnostics.AddError("conversion error", err.Error())
return
}
g.Validate(ctx, rq, rp)
}
func (g Generic) ValidateInt64(ctx context.Context, req validator.Int64Request, resp *validator.Int64Response) {
rq, rp, err := toGenericTypes(req, resp)
if err != nil {
resp.Diagnostics.AddError("conversion error", err.Error())
return
}
g.Validate(ctx, rq, rp)
}
func (g Generic) ValidateList(ctx context.Context, req validator.ListRequest, resp *validator.ListResponse) {
rq, rp, err := toGenericTypes(req, resp)
if err != nil {
resp.Diagnostics.AddError("conversion error", err.Error())
return
}
g.Validate(ctx, rq, rp)
}
func (g Generic) ValidateMap(ctx context.Context, req validator.MapRequest, resp *validator.MapResponse) {
rq, rp, err := toGenericTypes(req, resp)
if err != nil {
resp.Diagnostics.AddError("conversion error", err.Error())
return
}
g.Validate(ctx, rq, rp)
}
func (g Generic) ValidateNumber(ctx context.Context, req validator.NumberRequest, resp *validator.NumberResponse) {
rq, rp, err := toGenericTypes(req, resp)
if err != nil {
resp.Diagnostics.AddError("conversion error", err.Error())
return
}
g.Validate(ctx, rq, rp)
}
func (g Generic) ValidateObject(ctx context.Context, req validator.ObjectRequest, resp *validator.ObjectResponse) {
rq, rp, err := toGenericTypes(req, resp)
if err != nil {
resp.Diagnostics.AddError("conversion error", err.Error())
return
}
g.Validate(ctx, rq, rp)
}
func (g Generic) ValidateSet(ctx context.Context, req validator.SetRequest, resp *validator.SetResponse) {
rq, rp, err := toGenericTypes(req, resp)
if err != nil {
resp.Diagnostics.AddError("conversion error", err.Error())
return
}
g.Validate(ctx, rq, rp)
}
func (g Generic) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
rq, rp, err := toGenericTypes(req, resp)
if err != nil {
resp.Diagnostics.AddError("conversion error", err.Error())
return
}
g.Validate(ctx, rq, rp)
}
// RequiredTest is an Generic implementation that will register an error if the attribute has no value of
// any kind
func RequiredTest() TestFunc {
return func(ctx context.Context, req GenericRequest, resp *GenericResponse) {
if conv.TestAttributeValueState(req.ConfigValue) == nil {
return
}
resp.Diagnostics.AddAttributeError(
req.Path,
"Attribute must be valued",
"Attribute must have a value configured",
)
}
}
var requiredValidator = NewGenericValidator(GenericConfig{
Description: "Asserts the attribute is defined and non-null",
MarkdownDescription: "Asserts the attribute is defined and non-null",
TestFunc: RequiredTest(),
SkipWhenNull: false,
SkipWhenUnknown: true,
})
// Required returns a validator that asserts a field is configured with any value at all.
func Required() Generic {
return requiredValidator
}
// RegexpMatchTest is an Generic implementation that will first attempt to convert the value of
// a field to a string, then see if that resulting string matches the provided expression.
func RegexpMatchTest(r string) TestFunc {
re := regexp.MustCompile(r)
return func(ctx context.Context, req GenericRequest, resp *GenericResponse) {
str := conv.AttributeValueToString(req.ConfigValue)
if !re.MatchString(str) {
resp.Diagnostics.AddAttributeError(
req.Path,
"Field value does not match expression",
fmt.Sprintf("Field value %q does not match expression %q", str, r),
)
}
}
}
// RegexpMatch returns a validator that asserts an attribute's value matches the provided expression
func RegexpMatch(r string) Generic {
v := NewGenericValidator(GenericConfig{
Description: fmt.Sprintf("Asserts attribute string value matches expression %q", r),
MarkdownDescription: fmt.Sprintf("Asserts attribute string value matches expression %q", r),
TestFunc: RegexpMatchTest(r),
SkipWhenNull: true,
SkipWhenUnknown: true,
})
return v
}
// RegexpNotMatchTest is an Generic implementation that will first attempt to convert the value of
// a field to a string, then see if that resulting string matches the provided expression.
func RegexpNotMatchTest(r string) TestFunc {
re := regexp.MustCompile(r)
return func(ctx context.Context, req GenericRequest, resp *GenericResponse) {
str := conv.AttributeValueToString(req.ConfigValue)
if re.MatchString(str) {
resp.Diagnostics.AddAttributeError(
req.Path,
"Field value must NOT match expression",
fmt.Sprintf("Field value %q matches expression %q, indicating it contains invalid characters", str, r),
)
}
}
}
// RegexpNotMatch returns a validator that asserts an attribute's value does not match the provided expression
func RegexpNotMatch(r string) Generic {
v := NewGenericValidator(GenericConfig{
Description: fmt.Sprintf("Assert attribute string value does not match expression %q", r),
MarkdownDescription: fmt.Sprintf("Assert attribute string value does not match expression %q", r),
TestFunc: RegexpNotMatchTest(r),
SkipWhenNull: true,
SkipWhenUnknown: true,
})
return v
}
// LengthTest is an Generic implementation that attempts to find a length value appropriate
// for the attribute value type, asserting that it is within the specified bounds.
//
// If either min or max is set to -1, then that value is unbounded and thus not verified.
func LengthTest(minL, maxL int) TestFunc {
return func(ctx context.Context, req GenericRequest, resp *GenericResponse) {
// perform some basic sanity checking
if minL == -1 && maxL == -1 {
resp.Diagnostics.AddAttributeWarning(
req.Path,
"Length validation is unbounded, there is nothing to verify",
"Both minL and maxL variables were set to -1. This has no purpose and should be rectified.",
)
return
} else if minL < -1 || maxL < -1 {
resp.Diagnostics.AddAttributeError(
req.Path,
"Cannot use negative value for length check",
fmt.Sprintf("The provided minL value of %q and / or the provided maxL value %q are negative."+
" The only valid negative value is -1 to indicate \"unbounded\". This should be rectified.",
minL,
maxL,
))
return
} else if maxL != -1 && minL > maxL {
resp.Diagnostics.AddAttributeError(
req.Path,
"Minimum length value is greater than maximum length value",
fmt.Sprintf(
"The provided minimum length %q is greater than the provided maximum length of %q."+
" This should be rectified.",
minL,
maxL,
))
return
}
fl := conv.AttributeValueLength(req.ConfigValue)
if minL > -1 && fl < minL {
resp.Diagnostics.AddAttributeError(
req.Path,
"Field value length is below minimum threshold",
fmt.Sprintf("Field value length %d is less than mininum allowed of %d", fl, minL),
)
}
if maxL > -1 && fl > maxL {
resp.Diagnostics.AddAttributeError(
req.Path,
"Field value length is above maximum threshold",
fmt.Sprintf("Field value length %d is greater than the maximum allowed of %d", fl, maxL),
)
}
}
}
// Length asserts an attribute's length is limited to the specified bounds. The allowed types are:
// - string
// - set
// - map
// - list
func Length(minL, maxL int) Generic {
v := NewGenericValidator(GenericConfig{
Description: fmt.Sprintf("Asserts an attribute's value contains no less than %d and no more than %d elements, with -1 meaning unbounded", minL, maxL),
MarkdownDescription: fmt.Sprintf("Asserts an attribute's value contains no less than %d and no more than %d elements, with -1 meaning unbounded", minL, maxL),
TestFunc: LengthTest(minL, maxL),
SkipWhenNull: true,
SkipWhenUnknown: true,
})
return v
}
// CompareTest executes a registered comparison function against the target attribute's value
func CompareTest(op CompareOp, target interface{}, meta ...interface{}) TestFunc {
return func(ctx context.Context, req GenericRequest, resp *GenericResponse) {
err := CompareAttrValues(ctx, req.ConfigValue, op, target, meta...)
if err != nil {
switch true {
case errors.Is(err, ErrComparisonFailed):
addComparisonFailedDiagnostic(op, target, req, resp, err)
case errors.Is(err, ErrTypeConversionFailed):
resp.Diagnostics.AddAttributeError(
req.Path,
"Could not convert attribute to target type for comparison",
fmt.Sprintf("Unable to convert attribute value type %T to %T for %q copmarison: %v", req, target, op, err))
default:
resp.Diagnostics.AddAttributeError(
req.Path,
"Unexpected error during comparison",
fmt.Sprintf("Unexpected error during comparison: %v", err),
)
}
}
}
}
// Compare executes the specified comparison to the target value for an attribute.
//
// Type comparisons
func Compare(op CompareOp, target interface{}, meta ...interface{}) Generic {
v := NewGenericValidator(GenericConfig{
Description: fmt.Sprintf("Asserts an attribute is %q to %T(%[2]v)", op, target),
MarkdownDescription: fmt.Sprintf("Asserts an attribute is %q to %T(%[2]v)", op, target),
TestFunc: CompareTest(op, target, meta...),
SkipWhenNull: true,
SkipWhenUnknown: true,
})
return v
}
// TestIsURL asserts that the provided value can be parsed by url.Parse()
func TestIsURL(requireScheme string, requirePort int) TestFunc {
return func(ctx context.Context, req GenericRequest, resp *GenericResponse) {
requireScheme := requireScheme
requirePort := strconv.Itoa(requirePort)
validateURL := func(v string) {
if purl, err := url.Parse(v); err != nil {
resp.Diagnostics.AddAttributeError(
req.Path,
"Value is not parseable as URL",
fmt.Sprintf("Value is not parseable as url.URL: %v", err),
)
} else {
if requireScheme != "" && purl.Scheme != requireScheme {
resp.Diagnostics.AddAttributeError(
req.Path,
"URL scheme mismatch",
fmt.Sprintf("Defined scheme %q does not match required %q", purl.Scheme, requireScheme),
)
}
if requirePort != "" && purl.Port() != requirePort {
resp.Diagnostics.AddAttributeError(
req.Path,
"URL port mismatch",
fmt.Sprintf("Defined port %q does not match required %q", purl.Port(), requirePort),
)
}
}
}
if lv, ok := req.ConfigValue.(types.List); ok {
for _, v := range lv.Elements() {
validateURL(conv.AttributeValueToString(v))
}
} else if sv, ok := req.ConfigValue.(types.Set); ok {
for _, v := range sv.Elements() {
validateURL(conv.AttributeValueToString(v))
}
} else if mv, ok := req.ConfigValue.(types.Map); ok {
for _, v := range mv.Elements() {
validateURL(conv.AttributeValueToString(v))
}
} else {
validateURL(conv.AttributeValueToString(req.ConfigValue))
}
}
}
// IsURLWith returns a validator that asserts a given attribute's value(s) are parseable as an URL, and that it / they
// have a specific scheme and / or port
func IsURLWith(requiredScheme string, requiredPort int) Generic {
return NewGenericValidator(GenericConfig{
Description: "Tests if provided value is parseable as url.URL",
MarkdownDescription: "Tests if provided value is parseable as url.URL",
TestFunc: TestIsURL(requiredScheme, requiredPort),
SkipWhenNull: true,
SkipWhenUnknown: true,
})
}
// IsURL returns a validator that asserts a given attribute's value(s) are parseable as an URL
func IsURL() Generic {
return IsURLWith("", 0)
}
// TestIsDurationString asserts that a given attribute's value is a valid time.Duration string
func TestIsDurationString() TestFunc {
return func(ctx context.Context, req GenericRequest, resp *GenericResponse) {
if _, err := time.ParseDuration(conv.AttributeValueToString(req.ConfigValue)); err != nil {
resp.Diagnostics.AddAttributeError(
req.Path,
"Value is not parseable as time.Duration",
fmt.Sprintf("Value is not parseable as time.Duration: %v", err),
)
}
}
}
var isDurationStringValidator = NewGenericValidator(GenericConfig{
Description: "Tests if value is a valid time.Duration string",
MarkdownDescription: "Tests if value is a valid time.Duration string",
TestFunc: TestIsDurationString(),
SkipWhenNull: true,
SkipWhenUnknown: true,
})
// IsDurationString returns a validator that asserts a given attribute's value is a valid time.Duration string
func IsDurationString() Generic {
return isDurationStringValidator
}
// TestEnvVarValued asserts that a given attribute value is the name of an environment variable that is valued
// at runtime
func TestEnvVarValued() TestFunc {
return func(ctx context.Context, req GenericRequest, resp *GenericResponse) {
varName := conv.AttributeValueToString(req.ConfigValue)
if v, ok := os.LookupEnv(varName); !ok || strings.TrimSpace(v) == "" {
resp.Diagnostics.AddAttributeError(
req.Path,
"Environment variable is either undefined or empty",
fmt.Sprintf("The provided environment variable %q is not defined or empty", varName),
)
}
}
}
var envVarValuedValidator = NewGenericValidator(GenericConfig{
Description: "Tests if value is an environment variable name that itself is valued",
MarkdownDescription: "Tests if value is an environment variable name that itself is valued",
TestFunc: TestEnvVarValued(),
SkipWhenNull: true,
SkipWhenUnknown: true,
})
// EnvVarValued returns a validator that asserts a given attribute's value is the name of an environment variable that
// is valued at runtime
func EnvVarValued() Generic {
return envVarValuedValidator
}
// TestFileIsReadable attempts to open and subsequently read a single byte from the file at the path specified by the
// attribute value.
func TestFileIsReadable() TestFunc {
return func(ctx context.Context, req GenericRequest, resp *GenericResponse) {
fname := conv.AttributeValueToString(req.ConfigValue)
fh, err := os.Open(fname)
if fh != nil {
// always try to close handle
defer func() {
_ = fh.Close()
}()
}
// if we weren't able to open the file
if err != nil {
resp.Diagnostics.AddAttributeError(
req.Path,
"File could not be opened for reading",
fmt.Sprintf("File %q could not be opened for reading: %v", fname, err),
)
return
}
// attempt to read 1 byte from the file
if _, err = io.LimitReader(fh, 1).Read(make([]byte, 1, 1)); err != nil && !errors.Is(err, io.EOF) {
resp.Diagnostics.AddAttributeError(
req.Path,
"File is not readable",
fmt.Sprintf("File %q could not be read from: %v", fname, err),
)
}
}
}
var fileIsReadableValidator = NewGenericValidator(GenericConfig{
Description: "Tests if value is a file that exists and is readable",
MarkdownDescription: "Tests if value is a file that exists and is readable",
TestFunc: TestFileIsReadable(),
SkipWhenNull: true,
SkipWhenUnknown: true,
})
// FileIsReadable returns a validator that asserts a given attribute's value is a local file that is readable.
func FileIsReadable() Generic {
return fileIsReadableValidator
}
// MutuallyExclusiveSiblingTest ensures that a given attribute is not valued when another one is, and vice versa.
func MutuallyExclusiveSiblingTest(siblingAttr string) TestFunc {
return func(ctx context.Context, req GenericRequest, resp *GenericResponse) {
// if the attribute is not valued, for whatever reason, move on.
if conv.TestAttributeValueState(req.ConfigValue) != nil {
return
}
siblingAttrValue := types.String{}
siblingAttrPath := req.Path.ParentPath().AtName(siblingAttr)
// try to fetch value of sibling attribute
diags := req.Config.GetAttribute(ctx, siblingAttrPath, &siblingAttrValue)
if diags.HasError() {
return
}
// if the sibling attribute is not valued, move on
if conv.TestAttributeValueState(siblingAttrValue) != nil {
return
}
// yell about things
resp.Diagnostics.AddAttributeError(
req.Path,
"Mutually exclusive value error",
fmt.Sprintf(
"Cannot provide value to both %q and %q",
conv.FormatPathPathSteps(req.Path.Steps()...),
conv.FormatPathPathSteps(siblingAttrPath.Steps()...),
),
)
}
}
// MutuallyExclusiveSibling returns a validator that ensures that an attribute only carries a value when another
// sibling attribute does not.
//
// Sibling is defined as another attribute that is at the same step depth as the source attribute
func MutuallyExclusiveSibling(siblingAttr string) Generic {
v := NewGenericValidator(GenericConfig{
Description: fmt.Sprintf("Ensures attribute is only valued if sibling attribute %q is empty", siblingAttr),
MarkdownDescription: fmt.Sprintf("Ensures attribute is only valued if sibling attribute %q is empty", siblingAttr),
TestFunc: MutuallyExclusiveSiblingTest(siblingAttr),
SkipWhenNull: true,
SkipWhenUnknown: true,
})
return v
}
// MutuallyInclusiveSiblingTest ensures that a given attribute is valued when a sibling attribute is also valued
func MutuallyInclusiveSiblingTest(siblingAttr string) TestFunc {
return func(ctx context.Context, req GenericRequest, resp *GenericResponse) {
// if this attribute is valued, move on
if conv.TestAttributeValueState(req.ConfigValue) == nil {
return
}
siblingAttrValue := types.String{}
siblingAttrPath := req.Path.ParentPath().AtName(siblingAttr)
// try to fetch value of sibling attribute
diags := req.Config.GetAttribute(ctx, siblingAttrPath, &siblingAttrValue)
if diags.HasError() {
return
}
// if the sibling attribute is not valued, move on
if conv.TestAttributeValueState(siblingAttrValue) != nil {
return
}
// yell about things
resp.Diagnostics.AddAttributeError(
req.Path,
"Mutually inclusive value error",
fmt.Sprintf(
"Attribute %q is required when sibling attribute %q is valued",
conv.FormatPathPathSteps(req.Path.Steps()...),
conv.FormatPathPathSteps(siblingAttrPath.Steps()...),
),
)
}
}
func MutuallyInclusiveSibling(siblingAttr string) Generic {
v := NewGenericValidator(GenericConfig{
Description: fmt.Sprintf("Ensure attribute is valued when sibling attribute %q is also valued", siblingAttr),
MarkdownDescription: fmt.Sprintf("Ensure attribute is valued when sibling attribute %q is also valued", siblingAttr),
TestFunc: MutuallyInclusiveSiblingTest(siblingAttr),
SkipWhenNull: false,
SkipWhenUnknown: false,
})
return v
}