-
Notifications
You must be signed in to change notification settings - Fork 347
/
filters.go
350 lines (305 loc) · 16.4 KB
/
filters.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
package filters
import (
"errors"
"io"
"net/http"
"time"
log "github.com/sirupsen/logrus"
"github.com/opentracing/opentracing-go"
)
const (
// DynamicBackendHostKey is the key used in the state bag to pass host to the proxy.
DynamicBackendHostKey = "backend:dynamic:host"
// DynamicBackendSchemeKey is the key used in the state bag to pass scheme to the proxy.
DynamicBackendSchemeKey = "backend:dynamic:scheme"
// DynamicBackendURLKey is the key used in the state bag to pass url to the proxy.
DynamicBackendURLKey = "backend:dynamic:url"
// BackendIsProxyKey is the key used in the state bag to notify proxy that the backend is also a proxy.
BackendIsProxyKey = "backend:isproxy"
// BackendTimeout is the key used in the state bag to configure backend timeout in proxy
BackendTimeout = "backend:timeout"
// ReadTimeout is the key used in the state bag to configure read request body timeout in proxy
ReadTimeout = "read:timeout"
// WriteTimeout is the key used in the state bag to configure write response body timeout in proxy
WriteTimeout = "write:timeout"
// BackendRatelimit is the key used in the state bag to configure backend ratelimit in proxy
BackendRatelimit = "backend:ratelimit"
)
// FilterContext object providing state and information that is unique to a request.
type FilterContext interface {
// The response writer object belonging to the incoming request. Used by
// filters that handle the requests themselves.
// Deprecated: use Response() or Serve()
ResponseWriter() http.ResponseWriter
// The incoming request object. It is forwarded to the route endpoint
// with its properties changed by the filters.
Request() *http.Request
// The response object. It is returned to the client with its
// properties changed by the filters.
Response() *http.Response
// The copy (deep) of the original incoming request or nil if the
// implementation does not provide it.
//
// The object received from this method contains an empty body, and all
// payload related properties have zero value.
OriginalRequest() *http.Request
// The copy (deep) of the original incoming response or nil if the
// implementation does not provide it.
//
// The object received from this method contains an empty body, and all
// payload related properties have zero value.
OriginalResponse() *http.Response
// This method is deprecated. A FilterContext implementation should flag this state
// internally
Served() bool
// This method is deprecated. You should call Serve providing the desired response
MarkServed()
// Serve a request with the provided response. It can be used by filters that handle the requests
// themselves. FilterContext implementations should flag this state and prevent the filter chain
// from continuing
Serve(*http.Response)
// Provides the wildcard parameter values from the request path by their
// name as the key.
PathParam(string) string
// Provides a read-write state bag, unique to a request and shared by all
// the filters in the route.
StateBag() map[string]interface{}
// Gives filters access to the backend url specified in the route or an empty
// value in case it's a shunt, loopback. In case of dynamic backend is empty.
BackendUrl() string
// Returns the host that will be set for the outgoing proxy request as the
// 'Host' header.
OutgoingHost() string
// Allows explicitly setting the Host header to be sent to the backend, overriding the
// strategy used by the implementation, which can be either the Host header from the
// incoming request or the host fragment of the backend url.
//
// Filters that need to modify the outgoing 'Host' header, need to use
// this method instead of setting the Request().Headers["Host"] value.
// (The requestHeader filter automatically detects if the header name
// is 'Host' and calls this method.)
SetOutgoingHost(string)
// Allow filters to collect metrics other than the default metrics (Filter Request, Filter Response methods)
Metrics() Metrics
// Allow filters to add Tags, Baggage to the trace or set the ComponentName.
Tracer() opentracing.Tracer
// Allow filters to create their own spans
ParentSpan() opentracing.Span
// Returns a clone of the FilterContext including a brand new request object.
// The stream body of the new request is shared with the original.
// Whenever the request body of the original request is read, the body of the
// new request body is written.
// The StateBag and filterMetrics object are not preserved in the new context.
// Therefore, you can't access state bag values set in the previous context.
Split() (FilterContext, error)
// Performs a new route lookup and executes the matched route if any
Loopback()
Logger() FilterContextLogger
}
// FilterContextLogger is the logger which logs messages with additional context information.
type FilterContextLogger interface {
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
}
// Metrics provides possibility to use custom metrics from filter implementations. The custom metrics will
// be exposed by the common metrics endpoint exposed by the proxy, where they can be accessed by the custom
// key prefixed by the filter name and the string 'custom'. E.g: <filtername>.custom.<customkey>.
type Metrics interface {
// MeasureSince adds values to a timer with a custom key.
MeasureSince(key string, start time.Time)
// IncCounter increments a custom counter identified by its key.
IncCounter(key string)
// IncCounterBy increments a custom counter identified by its key by a certain value.
IncCounterBy(key string, value int64)
// IncFloatCounterBy increments a custom counter identified by its key by a certain
// float (decimal) value. IMPORTANT: Not all Metrics implementation support float
// counters. In that case, a call to IncFloatCounterBy is dropped.
IncFloatCounterBy(key string, value float64)
}
// Filters are created by the Spec components, optionally using filter
// specific settings. When implementing filters, it needs to be taken
// into consideration, that filter instances are route specific and not
// request specific, so any state stored with a filter is shared between
// all requests for the same route and can cause concurrency issues.
type Filter interface {
// The Request method is called while processing the incoming request.
Request(FilterContext)
// The Response method is called while processing the response to be
// returned.
Response(FilterContext)
}
// FilterCloser are Filters that need to cleanup resources after
// filter termination. For example Filters, that create a goroutine
// for some reason need to cleanup their goroutine or they would leak
// goroutines.
type FilterCloser interface {
Filter
io.Closer
}
// Spec objects are specifications for filters. When initializing the routes,
// the Filter instances are created using the Spec objects found in the
// registry.
type Spec interface {
// Name gives the name of the Spec. It is used to identify filters in a route definition.
Name() string
// CreateFilter creates a Filter instance. Called with the parameters in the route
// definition while initializing a route.
CreateFilter(config []interface{}) (Filter, error)
}
// Registry used to lookup Spec objects while initializing routes.
type Registry map[string]Spec
// ErrInvalidFilterParameters is used in case of invalid filter parameters.
var ErrInvalidFilterParameters = errors.New("invalid filter parameters")
// Registers a filter specification.
func (r Registry) Register(s Spec) {
name := s.Name()
if _, ok := r[name]; ok {
log.Infof("Replacing %s filter specification", name)
}
r[name] = s
}
// All Skipper filter names
const (
BackendIsProxyName = "backendIsProxy"
ModRequestHeaderName = "modRequestHeader"
SetRequestHeaderName = "setRequestHeader"
AppendRequestHeaderName = "appendRequestHeader"
DropRequestHeaderName = "dropRequestHeader"
ModResponseHeaderName = "modResponseHeader"
SetResponseHeaderName = "setResponseHeader"
AppendResponseHeaderName = "appendResponseHeader"
DropResponseHeaderName = "dropResponseHeader"
SetContextRequestHeaderName = "setContextRequestHeader"
AppendContextRequestHeaderName = "appendContextRequestHeader"
SetContextResponseHeaderName = "setContextResponseHeader"
AppendContextResponseHeaderName = "appendContextResponseHeader"
CopyRequestHeaderName = "copyRequestHeader"
CopyResponseHeaderName = "copyResponseHeader"
ModPathName = "modPath"
SetPathName = "setPath"
RedirectToName = "redirectTo"
RedirectToLowerName = "redirectToLower"
StaticName = "static"
StripQueryName = "stripQuery"
PreserveHostName = "preserveHost"
StatusName = "status"
CompressName = "compress"
DecompressName = "decompress"
SetQueryName = "setQuery"
DropQueryName = "dropQuery"
InlineContentName = "inlineContent"
InlineContentIfStatusName = "inlineContentIfStatus"
FlowIdName = "flowId"
XforwardName = "xforward"
XforwardFirstName = "xforwardFirst"
RandomContentName = "randomContent"
RepeatContentName = "repeatContent"
RepeatContentHexName = "repeatContentHex"
WrapContentName = "wrapContent"
WrapContentHexName = "wrapContentHex"
BackendTimeoutName = "backendTimeout"
ReadTimeoutName = "readTimeout"
WriteTimeoutName = "writeTimeout"
BlockName = "blockContent"
BlockHexName = "blockContentHex"
LatencyName = "latency"
BandwidthName = "bandwidth"
ChunksName = "chunks"
BackendLatencyName = "backendLatency"
BackendBandwidthName = "backendBandwidth"
BackendChunksName = "backendChunks"
AbsorbName = "absorb"
AbsorbSilentName = "absorbSilent"
UniformRequestLatencyName = "uniformRequestLatency"
UniformResponseLatencyName = "uniformResponseLatency"
NormalRequestLatencyName = "normalRequestLatency"
NormalResponseLatencyName = "normalResponseLatency"
HistogramRequestLatencyName = "histogramRequestLatency"
HistogramResponseLatencyName = "histogramResponseLatency"
LogHeaderName = "logHeader"
TeeName = "tee"
TeenfName = "teenf"
TeeLoopbackName = "teeLoopback"
SedName = "sed"
SedDelimName = "sedDelim"
SedRequestName = "sedRequest"
SedRequestDelimName = "sedRequestDelim"
BasicAuthName = "basicAuth"
WebhookName = "webhook"
OAuthTokeninfoAnyScopeName = "oauthTokeninfoAnyScope"
OAuthTokeninfoAllScopeName = "oauthTokeninfoAllScope"
OAuthTokeninfoAnyKVName = "oauthTokeninfoAnyKV"
OAuthTokeninfoAllKVName = "oauthTokeninfoAllKV"
OAuthTokenintrospectionAnyClaimsName = "oauthTokenintrospectionAnyClaims"
OAuthTokenintrospectionAllClaimsName = "oauthTokenintrospectionAllClaims"
OAuthTokenintrospectionAnyKVName = "oauthTokenintrospectionAnyKV"
OAuthTokenintrospectionAllKVName = "oauthTokenintrospectionAllKV"
SecureOAuthTokenintrospectionAnyClaimsName = "secureOauthTokenintrospectionAnyClaims"
SecureOAuthTokenintrospectionAllClaimsName = "secureOauthTokenintrospectionAllClaims"
SecureOAuthTokenintrospectionAnyKVName = "secureOauthTokenintrospectionAnyKV"
SecureOAuthTokenintrospectionAllKVName = "secureOauthTokenintrospectionAllKV"
ForwardTokenName = "forwardToken"
ForwardTokenFieldName = "forwardTokenField"
OAuthGrantName = "oauthGrant"
GrantCallbackName = "grantCallback"
GrantLogoutName = "grantLogout"
GrantClaimsQueryName = "grantClaimsQuery"
JwtValidationName = "jwtValidation"
OAuthOidcUserInfoName = "oauthOidcUserInfo"
OAuthOidcAnyClaimsName = "oauthOidcAnyClaims"
OAuthOidcAllClaimsName = "oauthOidcAllClaims"
OidcClaimsQueryName = "oidcClaimsQuery"
DropRequestCookieName = "dropRequestCookie"
DropResponseCookieName = "dropResponseCookie"
RequestCookieName = "requestCookie"
ResponseCookieName = "responseCookie"
JsCookieName = "jsCookie"
ConsecutiveBreakerName = "consecutiveBreaker"
RateBreakerName = "rateBreaker"
DisableBreakerName = "disableBreaker"
AdmissionControlName = "admissionControl"
ClientRatelimitName = "clientRatelimit"
RatelimitName = "ratelimit"
ClusterClientRatelimitName = "clusterClientRatelimit"
ClusterRatelimitName = "clusterRatelimit"
ClusterLeakyBucketRatelimitName = "clusterLeakyBucketRatelimit"
BackendRateLimitName = "backendRatelimit"
RatelimitFailClosedName = "ratelimitFailClosed"
LuaName = "lua"
CorsOriginName = "corsOrigin"
HeaderToQueryName = "headerToQuery"
QueryToHeaderName = "queryToHeader"
DisableAccessLogName = "disableAccessLog"
EnableAccessLogName = "enableAccessLog"
AuditLogName = "auditLog"
UnverifiedAuditLogName = "unverifiedAuditLog"
SetDynamicBackendHostFromHeader = "setDynamicBackendHostFromHeader"
SetDynamicBackendSchemeFromHeader = "setDynamicBackendSchemeFromHeader"
SetDynamicBackendUrlFromHeader = "setDynamicBackendUrlFromHeader"
SetDynamicBackendHost = "setDynamicBackendHost"
SetDynamicBackendScheme = "setDynamicBackendScheme"
SetDynamicBackendUrl = "setDynamicBackendUrl"
ApiUsageMonitoringName = "apiUsageMonitoring"
FifoName = "fifo"
LifoName = "lifo"
LifoGroupName = "lifoGroup"
RfcPathName = "rfcPath"
RfcHostName = "rfcHost"
BearerInjectorName = "bearerinjector"
TracingBaggageToTagName = "tracingBaggageToTag"
StateBagToTagName = "stateBagToTag"
TracingTagName = "tracingTag"
TracingSpanNameName = "tracingSpanName"
OriginMarkerName = "originMarker"
FadeInName = "fadeIn"
EndpointCreatedName = "endpointCreated"
ConsistentHashKeyName = "consistentHashKey"
ConsistentHashBalanceFactorName = "consistentHashBalanceFactor"
// Undocumented filters
HealthCheckName = "healthcheck"
SetFastCgiFilenameName = "setFastCgiFilename"
DisableRatelimitName = "disableRatelimit"
UnknownRatelimitName = "unknownRatelimit"
)