File size: 4,466 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
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
package integrationkey

import (
	"context"
	"crypto/sha256"
	"database/sql"
	"encoding/json"
	"errors"
	"fmt"

	"github.com/google/uuid"
	"github.com/target/goalert/gadb"
	"github.com/target/goalert/permission"
	"github.com/target/goalert/util/log"
	"github.com/target/goalert/validation"
	"github.com/target/goalert/validation/validate"
)

const (
	MaxRules   = 100
	MaxActions = 10
	MaxParams  = 10
)

func destHash(dest gadb.DestV1) (hash [32]byte) {
	data, err := json.Marshal(dest)
	if err != nil {
		panic(err)
	}
	h := sha256.New()
	_, _ = h.Write(data)
	copy(hash[:], h.Sum(nil))
	return hash
}

func (s *Store) validateActions(ctx context.Context, fname string, actions []gadb.UIKActionV1) error {
	err := validate.Len(fname, actions, 0, MaxActions)
	if err != nil {
		return err
	}

	uniqDest := make(map[[32]byte]struct{}, len(actions))
	for i, a := range actions {
		err := s.reg.ValidateAction(ctx, a)
		if err != nil {
			return err
		}
		hash := destHash(a.Dest)
		if _, ok := uniqDest[hash]; ok {
			name, err := s.reg.LookupTypeName(ctx, a.Dest.Type)
			if err != nil {
				// Handle error by logging it and using the type ID as the name.
				// This is unlikely since the destination type should have been validated earlier.
				log.Log(ctx, err)
				name = a.Dest.Type
			}
			return validation.NewFieldErrorf(fmt.Sprintf("%s[%d]", fname, i), "duplicate destination '%s' not allowed", name)
		}
		uniqDest[hash] = struct{}{}
	}

	return nil
}

func (s *Store) ValidateUIKConfigV1(ctx context.Context, cfg gadb.UIKConfigV1) error {
	err := validate.Many(
		validate.Len("Rules", cfg.Rules, 0, MaxRules),
		s.validateActions(ctx, "DefaultActions", cfg.DefaultActions),
	)
	if err != nil {
		return err
	}

	for i, r := range cfg.Rules {
		field := fmt.Sprintf("Rules[%d]", i)
		err := validate.Many(
			validate.Name(field+".Name", r.Name),
			validate.Text(field+".Description", r.Description, 0, 255), // these are arbitrary and will likely change as the feature is developed
			validate.Text(field+".ConditionExpr", r.ConditionExpr, 1, 1024),
			s.validateActions(ctx, field+".Actions", r.Actions),
		)
		if err != nil {
			return err
		}
	}

	data, err := json.Marshal(cfg)
	if err != nil {
		return err
	}

	if len(data) > 64*1024 {
		return validation.NewFieldError("Config", "must be less than 64KiB in total")
	}

	return nil
}

func (s *Store) Config(ctx context.Context, db gadb.DBTX, keyID uuid.UUID) (*gadb.UIKConfigV1, error) {
	err := permission.LimitCheckAny(ctx, permission.User, permission.Service)
	if err != nil {
		return nil, err
	}

	cfg, err := gadb.New(db).IntKeyGetConfig(ctx, keyID)
	if errors.Is(err, sql.ErrNoRows) {
		return &gadb.UIKConfigV1{}, nil
	}
	if err != nil {
		return nil, err
	}

	if cfg.Version != 1 {
		return nil, fmt.Errorf("unsupported config version: %d", cfg.Version)
	}

	return &cfg.V1, nil
}

func (s *Store) SetConfig(ctx context.Context, db gadb.DBTX, keyID uuid.UUID, cfg *gadb.UIKConfigV1) error {
	err := permission.LimitCheckAny(ctx, permission.User)
	if err != nil {
		return err
	}

	if cfg != nil {
		err := s.ValidateUIKConfigV1(ctx, *cfg)
		if err != nil {
			return err
		}
	}

	gdb := gadb.New(db)
	keyType, err := gdb.IntKeyGetType(ctx, keyID)
	if err != nil {
		return err
	}
	if keyType != gadb.EnumIntegrationKeysTypeUniversal {
		return validation.NewGenericError("config only supported for universal keys")
	}

	if cfg == nil {
		return gdb.IntKeyDeleteConfig(ctx, keyID)
	}

	// ensure all rule IDs are set, and all actions have a channel
	for i := range cfg.Rules {
		if cfg.Rules[i].ID == uuid.Nil {
			cfg.Rules[i].ID = uuid.New()
		}
		err := s.setActionChannels(ctx, db, cfg.Rules[i].Actions)
		if err != nil {
			return err
		}
	}
	err = s.setActionChannels(ctx, db, cfg.DefaultActions)
	if err != nil {
		return err
	}

	err = gadb.New(db).IntKeySetConfig(ctx, gadb.IntKeySetConfigParams{
		ID:     keyID,
		Config: gadb.UIKConfig{Version: 1, V1: *cfg},
	})
	if err != nil {
		return err
	}

	return nil
}

func (s *Store) setActionChannels(ctx context.Context, tx gadb.DBTX, actions []gadb.UIKActionV1) error {
	for i, act := range actions {
		ok, err := s.reg.IsDynamicAction(ctx, act.Dest.Type)
		if err != nil {
			return err
		}

		if !ok {
			return validation.NewFieldError(fmt.Sprintf("Actions[%d]", i), "invalid destination type")
		}

		actions[i].ChannelID, err = s.ncStore.MapDestToID(ctx, tx, act.Dest)
		if err != nil {
			return err
		}
	}

	return nil
}