-
Notifications
You must be signed in to change notification settings - Fork 7
/
jsonschema.go
110 lines (95 loc) · 2.61 KB
/
jsonschema.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
package jsonschema
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/luraproject/lura/v2/config"
"github.com/luraproject/lura/v2/logging"
"github.com/luraproject/lura/v2/proxy"
"github.com/xeipuuv/gojsonschema"
)
const Namespace = "github.com/devopsfaith/krakend-jsonschema"
var ErrEmptyBody = &malformedError{err: errors.New("could not validate an empty body")}
// ProxyFactory creates an proxy factory over the injected one adding a JSON Schema
// validator middleware to the pipe when required
func ProxyFactory(logger logging.Logger, pf proxy.Factory) proxy.FactoryFunc {
return proxy.FactoryFunc(func(cfg *config.EndpointConfig) (proxy.Proxy, error) {
next, err := pf.New(cfg)
if err != nil {
return proxy.NoopProxy, err
}
schemaLoader, ok := configGetter(cfg.ExtraConfig).(gojsonschema.JSONLoader)
if !ok || schemaLoader == nil {
return next, nil
}
schema, err := gojsonschema.NewSchema(schemaLoader)
if err != nil {
logger.Error("[ENDPOINT: " + cfg.Endpoint + "][JSONSchema] Parsing the definition:" + err.Error())
return next, nil
}
logger.Debug("[ENDPOINT: " + cfg.Endpoint + "][JSONSchema] Validator enabled")
return newProxy(schema, next), nil
})
}
func newProxy(schema *gojsonschema.Schema, next proxy.Proxy) proxy.Proxy {
return func(ctx context.Context, r *proxy.Request) (*proxy.Response, error) {
if r.Body == nil {
return nil, ErrEmptyBody
}
body, err := io.ReadAll(r.Body)
if err != nil {
return nil, err
}
r.Body.Close()
if len(body) == 0 {
return nil, ErrEmptyBody
}
r.Body = io.NopCloser(bytes.NewBuffer(body))
result, err := schema.Validate(gojsonschema.NewBytesLoader(body))
if err != nil {
return nil, &malformedError{err: err}
}
if !result.Valid() {
return nil, &validationError{errs: result.Errors()}
}
return next(ctx, r)
}
}
func configGetter(cfg config.ExtraConfig) interface{} {
v, ok := cfg[Namespace]
if !ok {
return nil
}
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(v); err != nil {
return nil
}
return gojsonschema.NewBytesLoader(buf.Bytes())
}
type validationError struct {
errs []gojsonschema.ResultError
}
func (v *validationError) Error() string {
errs := make([]string, len(v.errs))
for i, desc := range v.errs {
errs[i] = fmt.Sprintf("- %s", desc)
}
return strings.Join(errs, "\n")
}
func (*validationError) StatusCode() int {
return http.StatusBadRequest
}
type malformedError struct {
err error
}
func (m *malformedError) Error() string {
return m.err.Error()
}
func (*malformedError) StatusCode() int {
return http.StatusBadRequest
}