forked from elastic/apm-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_schema.go
422 lines (371 loc) · 11.9 KB
/
json_schema.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package tests
import (
"bytes"
"encoding/json"
"fmt"
"io"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/elastic/apm-server/processor"
"github.com/elastic/apm-server/tests/loader"
"github.com/elastic/beats/libbeat/common"
)
type ProcessorSetup struct {
Proc processor.Processor
// path to payload that should be a full and valid example
FullPayloadPath string
// path to ES template definitions
TemplatePaths []string
// json schema string
Schema string
}
type SchemaTestData struct {
Key string
Valid []interface{}
Invalid []Invalid
Condition Condition
}
type Invalid struct {
Msg string
Values []interface{}
}
type Condition struct {
// If requirements for a field apply in case of anothers key absence,
// add the key.
Absence []string
// If requirements for a field apply in case of anothers key specific values,
// add the key and its values.
Existence map[string]interface{}
}
type obj = map[string]interface{}
var (
Str1024 = createStr(1024, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 _-")
Str1024Special = createStr(1024, `⌘ `)
Str1025 = createStr(1025, "")
)
// This test checks
// * that all payload attributes are reflected in the json Schema, except for
// dynamic attributes not be specified in the schema;
// * that all attributes in the json schema are also included in the payload,
// to ensure full test coverage.
// Parameters:
// - payloadAttrsNotInSchema: attributes sent with the payload but should not be
// specified in the schema.
// - schemaAttrsNotInPayload: attributes that are reflected in the json schema but are
// not part of the payload.
func (ps *ProcessorSetup) PayloadAttrsMatchJsonSchema(t *testing.T, payloadAttrsNotInSchema, schemaAttrsNotInPayload *Set) {
require.NotNil(t, ps.Schema)
// check payload attrs in json schema
payload, err := loader.LoadData(ps.FullPayloadPath)
require.NoError(t, err, fmt.Sprintf("File %s not loaded", ps.FullPayloadPath))
payloadAttrs := NewSet()
flattenJsonKeys(payload, "", payloadAttrs)
schemaStruct, _ := schemaStruct(strings.NewReader(ps.Schema))
schemaKeys := NewSet()
flattenSchemaNames(schemaStruct, "", schemaKeys)
missing := Difference(payloadAttrs, schemaKeys)
missing = differenceWithGroup(missing, payloadAttrsNotInSchema)
assertEmptySet(t, missing, fmt.Sprintf("Json payload fields missing in schema %v", missing))
missing = Difference(schemaKeys, payloadAttrs)
missing = differenceWithGroup(missing, schemaAttrsNotInPayload)
assertEmptySet(t, missing, fmt.Sprintf("Json schema fields missing in payload %v", missing))
}
// Test that payloads missing `required `attributes fail validation.
// - `required`: ensure required keys must not be missing or nil
// - `conditionally required`: prepare payload according to conditions, then
// ensure required keys must not be missing
func (ps *ProcessorSetup) AttrsPresence(t *testing.T, requiredKeys *Set, condRequiredKeys map[string]Condition) {
required := Union(requiredKeys, NewSet(
"service",
"service.name",
"service.agent",
"service.agent.name",
"service.agent.version",
"service.language.name",
"service.runtime.name",
"service.runtime.version",
"service.framework.name",
"service.framework.version",
"process.pid",
))
payload, err := loader.LoadData(ps.FullPayloadPath)
require.NoError(t, err)
payloadKeys := NewSet()
flattenJsonKeys(payload, "", payloadKeys)
for _, k := range payloadKeys.Array() {
key := k.(string)
_, keyLast := splitKey(key)
//test sending nil value for key
ps.changePayload(t, key, nil, Condition{}, upsertFn,
func(k string) (bool, string) {
return !required.ContainsStrPattern(k), keyLast
},
)
//test removing key from payload
cond, _ := condRequiredKeys[key]
ps.changePayload(t, key, nil, cond, deleteFn,
func(k string) (bool, string) {
if required.ContainsStrPattern(k) {
return false, fmt.Sprintf("missing properties: \"%s\"", keyLast)
} else if _, ok := condRequiredKeys[k]; ok {
return false, fmt.Sprintf("missing properties: \"%s\"", keyLast)
}
return true, ""
},
)
}
}
// Test that field names indexed as `keywords` in Elasticsearch, have the same
// length limitation on the Intake API.
// APM Server has set all keyword restrictions to length 1024.
//
// keywordExceptionKeys: attributes defined as keywords in the ES template, but
// do not require a length restriction in the json schema, e.g. due to regex
// patterns defining a more specific restriction,
// templateToSchema: mapping for fields that are nested or named different on
// ES level than on intake API
func (ps *ProcessorSetup) KeywordLimitation(t *testing.T, keywordExceptionKeys *Set, templateToSchema map[string]string) {
// fetch keyword restricted field names from ES template
keywordFields, err := fetchFlattenedFieldNames(ps.TemplatePaths, addKeywordFields)
assert.NoError(t, err)
for _, k := range keywordFields.Array() {
key := k.(string)
if keywordExceptionKeys.Contains(key) {
continue
}
for from, to := range templateToSchema {
if strings.HasPrefix(key, from) {
key = strings.Replace(key, from, to, 1)
break
}
}
testAttrs := func(val interface{}, valid bool, msg string) {
ps.changePayload(t, key, val, Condition{}, upsertFn,
func(k string) (bool, string) { return valid, msg })
}
testAttrs(createStr(1025, ""), false, "maxlength")
testAttrs(createStr(1024, ""), true, "")
}
}
// Test that specified values for attributes fail or pass
// the validation accordingly.
// The configuration and testing of valid attributes here is intended
// to ensure correct setup and configuration to avoid false negatives.
func (ps *ProcessorSetup) DataValidation(t *testing.T, testData []SchemaTestData) {
for _, d := range testData {
testAttrs := func(val interface{}, valid bool, msg string) {
ps.changePayload(t, d.Key, val, d.Condition,
upsertFn, func(k string) (bool, string) {
return valid, msg
})
}
for _, invalid := range d.Invalid {
for _, v := range invalid.Values {
testAttrs(v, false, invalid.Msg)
}
}
for _, v := range d.Valid {
testAttrs(v, true, "")
}
}
}
func logPayload(t *testing.T, payload map[string]interface{}) {
j, _ := json.MarshalIndent(payload, "", " ")
t.Log("payload:", string(j))
}
func (ps *ProcessorSetup) changePayload(
t *testing.T,
key string,
val interface{},
condition Condition,
changeFn func(interface{}, string, interface{}) interface{},
validateFn func(string) (bool, string),
) {
// load payload
payload, err := loader.LoadData(ps.FullPayloadPath)
require.NoError(t, err)
// prepare payload according to conditions:
// - ensure specified keys being present
for k, val := range condition.Existence {
fnKey, keyToChange := splitKey(k)
payload = iterateMap(payload, "", fnKey, keyToChange, val, upsertFn).(obj)
}
err = ps.Proc.Validate(payload)
assert.NoError(t, err)
// - ensure specified keys being absent
for _, k := range condition.Absence {
fnKey, keyToChange := splitKey(k)
payload = iterateMap(payload, "", fnKey, keyToChange, nil, deleteFn).(obj)
}
// change payload for key to test
fnKey, keyToChange := splitKey(key)
payload = iterateMap(payload, "", fnKey, keyToChange, val, changeFn).(obj)
wantLog := false
defer func() {
if wantLog {
logPayload(t, payload)
}
}()
// run actual validation
err = ps.Proc.Validate(payload)
if shouldValidate, errMsg := validateFn(key); shouldValidate {
wantLog = !assert.NoError(t, err, fmt.Sprintf("Expected <%v> for key <%s> to be valid", val, key))
_, _, err = ps.Proc.Decode(payload)
assert.NoError(t, err)
} else {
if assert.Error(t, err, fmt.Sprintf(`Expected error for key <%v> with msg "%s", but received no error.`, key, errMsg)) {
wantLog = !assert.Contains(t, strings.ToLower(err.Error()), errMsg)
} else {
wantLog = true
}
}
}
func createStr(n int, start string) string {
buf := bytes.NewBufferString(start)
for buf.Len() < n {
buf.WriteString("a")
}
return buf.String()
}
func splitKey(s string) (string, string) {
idx := strings.LastIndex(s, ".")
if idx == -1 {
return "", s
}
return s[:idx], s[idx+1:]
}
func upsertFn(m interface{}, k string, v interface{}) interface{} {
fn := func(o obj, key string, val interface{}) obj { o[key] = val; return o }
return applyFn(m, k, v, fn)
}
func deleteFn(m interface{}, k string, v interface{}) interface{} {
fn := func(o obj, key string, _ interface{}) obj { delete(o, key); return o }
return applyFn(m, k, v, fn)
}
func applyFn(m interface{}, k string, val interface{}, fn func(obj, string, interface{}) obj) interface{} {
switch m.(type) {
case obj:
fn(m.(obj), k, val)
case []interface{}:
for _, e := range m.([]interface{}) {
if eObj, ok := e.(obj); ok {
fn(eObj, k, val)
}
}
}
return m
}
func iterateMap(m interface{}, prefix, fnKey, xKey string, val interface{}, fn func(interface{}, string, interface{}) interface{}) interface{} {
re := regexp.MustCompile(fmt.Sprintf("^%s$", fnKey))
if d, ok := m.(obj); ok {
ma := d
if prefix == "" && fnKey == "" {
ma = fn(ma, xKey, val).(obj)
}
for k, v := range d {
key := strConcat(prefix, k, ".")
ma[k] = iterateMap(v, key, fnKey, xKey, val, fn)
if key == fnKey || re.MatchString(key) {
ma[k] = fn(ma[k], xKey, val)
}
}
if len(ma) > 0 {
return ma
}
return nil
} else if d, ok := m.([]interface{}); ok {
var ma []interface{}
for _, i := range d {
if r := iterateMap(i, prefix, fnKey, xKey, val, fn); r != nil {
ma = append(ma, r)
}
}
return ma
} else {
return m
}
}
type Schema struct {
Title string
Properties map[string]*Schema
AdditionalProperties obj
PatternProperties obj
Items *Schema
AllOf []*Schema
OneOf []*Schema
AnyOf []*Schema
MaxLength int
}
type Mapping struct {
from string
to string
}
func schemaStruct(reader io.Reader) (*Schema, error) {
decoder := json.NewDecoder(reader)
var schema Schema
err := decoder.Decode(&schema)
return &schema, err
}
func flattenSchemaNames(s *Schema, prefix string, flattened *Set) {
if len(s.Properties) > 0 {
for k, v := range s.Properties {
key := strConcat(prefix, k, ".")
flattened.Add(key)
flattenSchemaNames(v, key, flattened)
}
} else if s.Items != nil {
flattenSchemaNames(s.Items, prefix, flattened)
} else {
for _, schemas := range [][]*Schema{s.AllOf, s.OneOf, s.AnyOf} {
if schemas != nil {
for _, e := range schemas {
flattenSchemaNames(e, prefix, flattened)
}
}
}
}
}
func flattenJsonKeys(data interface{}, prefix string, flattened *Set) {
if d, ok := data.(obj); ok {
for k, v := range d {
key := strConcat(prefix, k, ".")
flattened.Add(key)
flattenJsonKeys(v, key, flattened)
}
} else if d, ok := data.([]interface{}); ok {
for _, v := range d {
flattenJsonKeys(v, prefix, flattened)
}
}
}
func addKeywordFields(f common.Field) bool {
if f.Type == "keyword" || f.ObjectType == "keyword" {
return true
} else if len(f.MultiFields) > 0 {
for _, mf := range f.MultiFields {
if mf.Type == "keyword" {
return true
}
}
}
return false
}