This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.go
166 lines (143 loc) · 4.68 KB
/
middleware.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
package goatkeeper
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3filter"
"github.com/go-logr/logr"
logrtesting "github.com/go-logr/logr/testing"
"github.com/imdario/mergo"
"github.com/pkg/errors"
)
// MiddlewareResponse defines a response format that middleware will
// write to http requests if their request body or response do not
// adhere to defined OpenAPI specification.
type MiddlewareResponse struct {
Status int
Body []byte
Headers http.Header
}
// writeToResponse writes the response to an http.ResponseWriter.
func (response *MiddlewareResponse) writeToResponse(w http.ResponseWriter) error {
for key, values := range response.Headers {
for _, value := range values {
w.Header().Add(key, value)
}
}
w.WriteHeader(response.Status)
_, err := w.Write(response.Body)
return err
}
// MiddlewareOptions defines options for configuring the GoatKeeper middleware.
type MiddlewareOptions struct {
Logger logr.Logger
OpenAPISpec []byte
ValidateResponse bool // should goatkeeper also validate http responses
InvalidRequestResponse *MiddlewareResponse // response to write if request is invalid
InvalidResponseResponse *MiddlewareResponse // response to write if response is invalid
}
// DefaultMiddlewareOptions defines default options used by the middleware.
// These options are only used if a option isn't specified when initializing
// the middleware.
var DefaultMiddlewareOptions = MiddlewareOptions{
Logger: logrtesting.NullLogger{},
ValidateResponse: false,
InvalidRequestResponse: &MiddlewareResponse{
Headers: http.Header{},
Status: http.StatusBadRequest,
Body: []byte{},
},
InvalidResponseResponse: &MiddlewareResponse{
Status: http.StatusInternalServerError,
Headers: http.Header{},
Body: []byte{},
},
}
// middleware implements the goatkeeper middleware.
type middleware struct {
*MiddlewareOptions
*openapi3filter.Router
context.Context
}
// validateRequest validates the given request with data from the parsed
// OpenAPI specification.
func (m *middleware) validateRequest(r *http.Request) error {
route, pathParams, err := m.FindRoute(r.Method, r.URL)
if err != nil {
return err
}
return openapi3filter.ValidateRequest(m, &openapi3filter.RequestValidationInput{
Request: r,
PathParams: pathParams,
Route: route,
})
}
// validateResponse validates the given response with data from the parsed
// OpenAPI specification.
func (m *middleware) validateResponse(recorder *httpResponseRecorder, r *http.Request) error {
route, pathParams, err := m.FindRoute(r.Method, r.URL)
if err != nil {
return err
}
return openapi3filter.ValidateResponse(m, &openapi3filter.ResponseValidationInput{
RequestValidationInput: &openapi3filter.RequestValidationInput{
Request: r,
PathParams: pathParams,
Route: route,
},
Status: recorder.Status,
Header: recorder.Headers,
Body: ioutil.NopCloser(bytes.NewBuffer(recorder.Body.Bytes())),
})
}
// serve is the actual goatkeeper middleware.
func (m *middleware) serve(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logger := m.Logger.WithValues("Method", r.Method, "URL", r.URL)
err := m.validateRequest(r)
if err != nil {
logger.Error(err, "invalid request data")
if err = m.InvalidRequestResponse.writeToResponse(w); err != nil {
logger.Error(err, "unable to write response")
}
return
}
if !m.ValidateResponse {
next.ServeHTTP(w, r)
return
}
recorder := newHTTPResponseRecorder()
next.ServeHTTP(recorder, r)
err = m.validateResponse(recorder, r)
if err != nil {
logger.Error(err, "invalid response data")
if err = m.InvalidResponseResponse.writeToResponse(w); err != nil {
logger.Error(err, "unable to write response")
}
return
}
if err = recorder.writeToResponse(w); err != nil {
logger.Error(err, "unable to write response")
}
})
}
// NewMiddleware creates a new HTTP middleware that will use the given
// OpenAPI Spec to validate all requests and their responses.
func NewMiddleware(opts *MiddlewareOptions) (func(http.Handler) http.Handler, error) {
err := mergo.Merge(opts, DefaultMiddlewareOptions)
if err != nil {
return nil, errors.Wrap(err, "unable merge given options with defaults")
}
spec, err := openapi3.NewSwaggerLoader().LoadSwaggerFromData(opts.OpenAPISpec)
if err != nil {
return nil, errors.Wrap(err, "unable to parse openapi spec")
}
m := middleware{
Context: context.TODO(),
MiddlewareOptions: opts,
Router: openapi3filter.NewRouter().WithSwagger(spec),
}
return m.serve, nil
}