-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipeline_test.go
More file actions
303 lines (265 loc) · 8.96 KB
/
Copy pathpipeline_test.go
File metadata and controls
303 lines (265 loc) · 8.96 KB
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
package dyfields_test
import (
"encoding/json"
"os"
"testing"
"github.com/BrobridgeOrg/dyfields"
)
// The pipeline scenario is the hard case the design was stress-tested against:
// two levels of object_list, a cross-level `^.` reference, secrets at both
// levels, and an edit that deletes an entry from the middle of a list.
func readFile(t *testing.T, name string) []byte {
t.Helper()
b, err := os.ReadFile("testdata/" + name)
if err != nil {
t.Fatalf("read %s: %v", name, err)
}
return b
}
func readDoc(t *testing.T, name string) dyfields.ValueDocument {
t.Helper()
var d dyfields.ValueDocument
if err := json.Unmarshal(readFile(t, name), &d); err != nil {
t.Fatalf("unmarshal %s: %v", name, err)
}
return d
}
func canonical(t *testing.T, v any) string {
t.Helper()
b, err := json.MarshalIndent(v, "", " ")
if err != nil {
t.Fatalf("marshal: %v", err)
}
return string(b)
}
func pipelineCompiled(t *testing.T) *dyfields.Compiled {
t.Helper()
c, err := dyfields.Load(readFile(t, "pipeline_schema.json"))
if err != nil {
t.Fatalf("load pipeline schema: %v", err)
}
return c
}
func TestPipelineSchemaCompiles(t *testing.T) {
c := pipelineCompiled(t)
// Both list levels hold a secret somewhere below them, so both need a
// client-generated $id: the secret key cannot be written without one.
if !c.RequiresItemID("sinks") {
t.Error("sinks should require a client-generated $id")
}
if !c.RequiresItemID("sinks.mappings") {
t.Error("sinks.mappings should require a client-generated $id")
}
}
func TestPipelineSchemaRoundTrip(t *testing.T) {
original := readFile(t, "pipeline_schema.json")
s, err := dyfields.Parse(original)
if err != nil {
t.Fatalf("parse: %v", err)
}
out, err := s.MarshalIndent()
if err != nil {
t.Fatalf("marshal: %v", err)
}
again, err := dyfields.Parse(out)
if err != nil {
t.Fatalf("re-parse: %v", err)
}
if canonical(t, s) != canonical(t, again) {
t.Fatal("schema changed when written out and read back")
}
}
func TestPipelineExistingDocumentValidates(t *testing.T) {
c := pipelineCompiled(t)
before := readDoc(t, "pipeline_before.json")
out, errs := c.Validate(before)
if errs.Len() != 0 {
t.Fatalf("stored document should validate, got: %v", errs)
}
// A document that already conforms must come back unchanged; otherwise
// every round trip through the server would rewrite stored data.
if got, want := canonical(t, out), canonical(t, before); got != want {
t.Errorf("Validate changed a conforming document\n--- got\n%s\n--- want\n%s", got, want)
}
}
func TestPipelineApplyMatchesScenario(t *testing.T) {
c := pipelineCompiled(t)
before := readDoc(t, "pipeline_before.json")
var patch dyfields.Patch
if err := json.Unmarshal(readFile(t, "pipeline_patch.json"), &patch); err != nil {
t.Fatalf("unmarshal patch: %v", err)
}
got, errs := c.Apply(before, patch)
if errs.Len() != 0 {
t.Fatalf("apply should succeed, got: %v", errs)
}
want := readDoc(t, "pipeline_after.json")
if canonical(t, got) != canonical(t, want) {
t.Fatalf("apply result differs\n--- got\n%s\n--- want\n%s", canonical(t, got), canonical(t, want))
}
}
// The single most important guarantee in the design: deleting an entry from the
// middle of a list must not shift anyone else's secret onto the wrong entry.
func TestPipelineSecretsStayAlignedAcrossDeletion(t *testing.T) {
c := pipelineCompiled(t)
before := readDoc(t, "pipeline_before.json")
var patch dyfields.Patch
if err := json.Unmarshal(readFile(t, "pipeline_patch.json"), &patch); err != nil {
t.Fatalf("unmarshal patch: %v", err)
}
out, _ := c.Apply(before, patch)
if got := out.Secrets["sinks.s_k1.mappings.m_3.secret_salt"]; got != "salt-for-phone" {
t.Errorf("m_3 lost its own salt: %q", got)
}
for _, gone := range []string{
"sinks.s_k1.mappings.m_2.secret_salt", // the deleted mapping
"sinks.s_h1.auth_token", // the deleted sink
"webhook_secret", // the switched-off group
} {
if _, ok := out.Secrets[gone]; ok {
t.Errorf("secret %q should have been removed", gone)
}
}
if got := out.Secrets["source_password"]; got != "pg-secret" {
// An untouched secret comes back as an empty string from the form,
// which must mean "unchanged", not "clear it".
t.Errorf("untouched secret was not preserved: %q", got)
}
}
func TestPipelineImpactOfWarnsBeforeDataIsLost(t *testing.T) {
c := pipelineCompiled(t)
before := readDoc(t, "pipeline_before.json")
// Switching a sink from kafka to http invalidates a whole subtree.
patch := dyfields.Patch{Values: map[string]any{
"sinks": []any{map[string]any{
"$id": "s_k1",
"type": "http",
"endpoint": "https://elsewhere.internal/ingest",
}},
}}
lost := c.ImpactOf(before, patch)
want := map[string]bool{
"sinks.s_k1.brokers": true,
"sinks.s_k1.topic": true,
"sinks.s_k1.mappings.m_1.partition_key": true,
"sinks.s_k1.mappings.m_2.partition_key": true,
"sinks.s_k1.mappings.m_3.partition_key": true,
}
got := map[string]bool{}
for _, p := range lost {
got[p] = true
}
for p := range want {
if !got[p] {
t.Errorf("ImpactOf did not report %q as lost (got %v)", p, lost)
}
}
}
func TestPipelineReadOnlyIsEnforcedOnApply(t *testing.T) {
c := pipelineCompiled(t)
before := readDoc(t, "pipeline_before.json")
// is_running is true, so source_type is locked.
patch := dyfields.Patch{Values: map[string]any{"source_type": "mysql"}}
_, errs := c.Apply(before, patch)
if !errs.Has("readonly") {
t.Fatalf("changing a locked field should be rejected, got: %v", errs)
}
// Validate alone cannot see this, because it has nothing to compare
// against. That is a documented boundary, not an oversight.
tampered := before.Clone()
tampered.Values["source_type"] = "mysql"
if _, errs := c.Validate(tampered); errs.Len() != 0 {
t.Fatalf("Validate does not check readonly, got: %v", errs)
}
}
func TestPipelineMissingItemIDForSecretBearingEntry(t *testing.T) {
c := pipelineCompiled(t)
doc := readDoc(t, "pipeline_before.json")
sinks := doc.Values["sinks"].([]any)
sink := sinks[0].(map[string]any)
maps := sink["mappings"].([]any)
maps = append(maps, map[string]any{
"source_field": "ip", "target_field": "client_ip", "transform": "mask",
})
sink["mappings"] = maps
_, errs := c.Validate(doc)
if !errs.Has("missing_item_id") {
t.Fatalf("an entry with a secret must carry a $id, got: %v", errs)
}
}
func TestPipelineDuplicateIsReportedWithoutLeakingTheValue(t *testing.T) {
c := pipelineCompiled(t)
doc := readDoc(t, "pipeline_before.json")
sinks := doc.Values["sinks"].([]any)
sink := sinks[0].(map[string]any)
maps := sink["mappings"].([]any)
first := maps[0].(map[string]any)
maps[2].(map[string]any)["target_field"] = first["target_field"]
_, errs := c.Validate(doc)
if !errs.Has("duplicate") {
t.Fatalf("duplicate target_field should be rejected, got: %v", errs)
}
for _, e := range errs {
if e.Code == "duplicate" && e.ItemPath == "" {
t.Error("a duplicate error must say which entry it is about")
}
}
}
func TestPipelineCrossLevelReferenceControlsVisibility(t *testing.T) {
c := pipelineCompiled(t)
doc := readDoc(t, "pipeline_before.json")
vis := c.Visible(doc)
// The kafka sink's mappings can be partition keys; the http sink's cannot,
// because partition_key reads `^.type` from the sink it belongs to.
if !vis.Fields["sinks[0].mappings[0].partition_key"] {
t.Error("partition_key should be visible under a kafka sink")
}
if vis.Fields["sinks[1].mappings[0].partition_key"] {
t.Error("partition_key should be hidden under an http sink")
}
}
func TestPipelineHiddenValuesAreRemoved(t *testing.T) {
c := pipelineCompiled(t)
doc := readDoc(t, "pipeline_before.json")
// A partition_key on the http sink's mapping is not merely ignored: an
// invisible field has no value at all.
sinks := doc.Values["sinks"].([]any)
http := sinks[1].(map[string]any)
http["mappings"].([]any)[0].(map[string]any)["partition_key"] = true
out, errs := c.Validate(doc)
if errs.Len() != 0 {
t.Fatalf("unexpected errors: %v", errs)
}
m := out.Values["sinks"].([]any)[1].(map[string]any)["mappings"].([]any)[0].(map[string]any)
if _, ok := m["partition_key"]; ok {
t.Error("an invisible field kept its value")
}
}
func TestPipelineRedactHidesEverySecret(t *testing.T) {
c := pipelineCompiled(t)
doc := readDoc(t, "pipeline_before.json")
pub := c.Redact(doc)
b, err := json.Marshal(pub)
if err != nil {
t.Fatalf("marshal: %v", err)
}
for _, secret := range []string{"pg-secret", "salt-for-email", "bearer-xyz", "hook-abc"} {
if contains(string(b), secret) {
t.Errorf("redacted document still contains %q", secret)
}
}
if len(pub.SecretsSet) != len(doc.Secrets) {
t.Errorf("expected %d secret paths, got %d", len(doc.Secrets), len(pub.SecretsSet))
}
}
func contains(haystack, needle string) bool {
return len(needle) > 0 && len(haystack) >= len(needle) &&
func() bool {
for i := 0; i+len(needle) <= len(haystack); i++ {
if haystack[i:i+len(needle)] == needle {
return true
}
}
return false
}()
}