File size: 2,635 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package nfydest

import (
	"context"
	"errors"
	"fmt"
	"reflect"
	"slices"

	"github.com/expr-lang/expr"
	"github.com/target/goalert/gadb"
	"github.com/target/goalert/validation"
)

type ParamValidator interface {
	ValidateParam(ctx context.Context, paramID, value string) error
}

type ActionValidator interface {
	ValidateAction(ctx context.Context, act gadb.UIKActionV1) error
}

type ActionParamError struct {
	ParamID string
	Err     error
}

func (e *ActionParamError) Error() string { return fmt.Sprintf("parameter %s: %s", e.ParamID, e.Err) }

func (e *ActionParamError) ClientError() bool { return true }

func (r *Registry) ValidateAction(ctx context.Context, act gadb.UIKActionV1) error {
	p := r.Provider(act.Dest.Type)
	if p == nil {
		return ErrUnknownType
	}

	info, err := p.TypeInfo(ctx)
	if err != nil {
		return err
	}

	if !info.Enabled {
		return ErrNotEnabled
	}

	err = r.ValidateDest(ctx, act.Dest)
	if err != nil {
		return err
	}

	if act.Params == nil {
		act.Params = make(map[string]string)
	}

	if v, ok := p.(ActionValidator); ok {
		// Some providers may need/want to validate all params at once.
		err := v.ValidateAction(ctx, act)
		// If we get `ErrUnsupported`, we'll fall back to the field-by-field validation, this can happen if the provider implements the interface, but the backing implementation (e.g., external plugin) doesn't support it.
		if !errors.Is(err, ErrUnsupported) {
			return err
		}
	}

	knownParams := make([]string, 0, len(info.DynamicParams))
	for _, f := range info.DynamicParams {
		knownParams = append(knownParams, f.ParamID)
	}

	// Make sure we reject any params that are not expected.
	for paramID := range act.Params {
		if slices.Contains(knownParams, paramID) {
			continue
		}

		return &ActionParamError{
			ParamID: paramID,
			Err:     fmt.Errorf("unexpected param"),
		}
	}

	for _, param := range info.DynamicParams {
		if act.Params[param.ParamID] != "" {
			val := "string(" + act.Params[param.ParamID] + ")"
			_, err = expr.Compile(val, expr.AsKind(reflect.String))
			if err != nil {
				return &ActionParamError{
					ParamID: param.ParamID,
					Err:     validation.WrapError(err),
				}
			}
		}

		paramV, ok := p.(ParamValidator)
		if !ok {
			continue
		}

		err := paramV.ValidateParam(ctx, param.ParamID, act.Params[param.ParamID])
		if validation.IsClientError(err) {
			return &ActionParamError{
				ParamID: param.ParamID,
				Err:     err,
			}
		}
		if err != nil && !errors.Is(err, ErrUnsupported) {
			return err
		}
	}

	// Since we have no extra/unknown fields, and all required fields are valid, we've validated the Action.
	return nil
}