-
Notifications
You must be signed in to change notification settings - Fork 552
/
config.go
446 lines (395 loc) · 16.4 KB
/
config.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Package config defines the config structs and some config parser interfaces and implementations
package config
import (
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log"
"regexp"
"strings"
"time"
"github.com/devopsfaith/krakend/encoding"
)
const (
// BracketsRouterPatternBuilder uses brackets as route params delimiter
BracketsRouterPatternBuilder = iota
// ColonRouterPatternBuilder use a colon as route param delimiter
ColonRouterPatternBuilder
// DefaultMaxIdleConnsPerHost is the default value for the MaxIdleConnsPerHost param
DefaultMaxIdleConnsPerHost = 250
// DefaultTimeout is the default value to use for the ServiceConfig.Timeout param
DefaultTimeout = 2 * time.Second
// ConfigVersion is the current version of the config struct
ConfigVersion = 2
)
// RoutingPattern to use during route conversion. By default, use the colon router pattern
var RoutingPattern = ColonRouterPatternBuilder
// ServiceConfig defines the krakend service
type ServiceConfig struct {
// name of the service
Name string `mapstructure:"name"`
// set of endpoint definitions
Endpoints []*EndpointConfig `mapstructure:"endpoints"`
// defafult timeout
Timeout time.Duration `mapstructure:"timeout"`
// default TTL for GET
CacheTTL time.Duration `mapstructure:"cache_ttl"`
// default set of hosts
Host []string `mapstructure:"host"`
// port to bind the krakend service
Port int `mapstructure:"port"`
// version code of the configuration
Version int `mapstructure:"version"`
// OutputEncoding defines the default encoding strategy to use for the endpoint responses
OutputEncoding string `mapstructure:"output_encoding"`
// Extra configuration for customized behaviour
ExtraConfig ExtraConfig `mapstructure:"extra_config"`
// ReadTimeout is the maximum duration for reading the entire
// request, including the body.
//
// Because ReadTimeout does not let Handlers make per-request
// decisions on each request body's acceptable deadline or
// upload rate, most users will prefer to use
// ReadHeaderTimeout. It is valid to use them both.
ReadTimeout time.Duration `mapstructure:"read_timeout"`
// WriteTimeout is the maximum duration before timing out
// writes of the response. It is reset whenever a new
// request's header is read. Like ReadTimeout, it does not
// let Handlers make decisions on a per-request basis.
WriteTimeout time.Duration `mapstructure:"write_timeout"`
// IdleTimeout is the maximum amount of time to wait for the
// next request when keep-alives are enabled. If IdleTimeout
// is zero, the value of ReadTimeout is used. If both are
// zero, ReadHeaderTimeout is used.
IdleTimeout time.Duration `mapstructure:"idle_timeout"`
// ReadHeaderTimeout is the amount of time allowed to read
// request headers. The connection's read deadline is reset
// after reading the headers and the Handler can decide what
// is considered too slow for the body.
ReadHeaderTimeout time.Duration `mapstructure:"read_header_timeout"`
// DisableKeepAlives, if true, prevents re-use of TCP connections
// between different HTTP requests.
DisableKeepAlives bool `mapstructure:"disable_keep_alives"`
// DisableCompression, if true, prevents the Transport from
// requesting compression with an "Accept-Encoding: gzip"
// request header when the Request contains no existing
// Accept-Encoding value. If the Transport requests gzip on
// its own and gets a gzipped response, it's transparently
// decoded in the Response.Body. However, if the user
// explicitly requested gzip it is not automatically
// uncompressed.
DisableCompression bool `mapstructure:"disable_compression"`
// MaxIdleConns controls the maximum number of idle (keep-alive)
// connections across all hosts. Zero means no limit.
MaxIdleConns int `mapstructure:"max_idle_connections"`
// MaxIdleConnsPerHost, if non-zero, controls the maximum idle
// (keep-alive) connections to keep per-host. If zero,
// DefaultMaxIdleConnsPerHost is used.
MaxIdleConnsPerHost int `mapstructure:"max_idle_connections_per_host"`
// IdleConnTimeout is the maximum amount of time an idle
// (keep-alive) connection will remain idle before closing
// itself.
// Zero means no limit.
IdleConnTimeout time.Duration `mapstructure:"idle_connection_timeout"`
// ResponseHeaderTimeout, if non-zero, specifies the amount of
// time to wait for a server's response headers after fully
// writing the request (including its body, if any). This
// time does not include the time to read the response body.
ResponseHeaderTimeout time.Duration `mapstructure:"response_header_timeout"`
// ExpectContinueTimeout, if non-zero, specifies the amount of
// time to wait for a server's first response headers after fully
// writing the request headers if the request has an
// "Expect: 100-continue" header. Zero means no timeout and
// causes the body to be sent immediately, without
// waiting for the server to approve.
// This time does not include the time to send the request header.
ExpectContinueTimeout time.Duration `mapstructure:"expect_continue_timeout"`
// DialerTimeout is the maximum amount of time a dial will wait for
// a connect to complete. If Deadline is also set, it may fail
// earlier.
//
// The default is no timeout.
//
// When using TCP and dialing a host name with multiple IP
// addresses, the timeout may be divided between them.
//
// With or without a timeout, the operating system may impose
// its own earlier timeout. For instance, TCP timeouts are
// often around 3 minutes.
DialerTimeout time.Duration `mapstructure:"dialer_timeout"`
// DialerFallbackDelay specifies the length of time to wait before
// spawning a fallback connection, when DualStack is enabled.
// If zero, a default delay of 300ms is used.
DialerFallbackDelay time.Duration `mapstructure:"dialer_fallback_delay"`
// DialerKeepAlive specifies the keep-alive period for an active
// network connection.
// If zero, keep-alives are not enabled. Network protocols
// that do not support keep-alives ignore this field.
DialerKeepAlive time.Duration `mapstructure:"dialer_keep_alive"`
// DisableStrictREST flags if the REST enforcement is disabled
DisableStrictREST bool `mapstructure:"disable_rest"`
// Plugin defines the configuration for the plugin loader
Plugin *Plugin `mapstructure:"plugin"`
// TLS defines the configuration params for enabling TLS (HTTPS & HTTP/2) at
// the router layer
TLS *TLS `mapstructure:"tls"`
// run krakend in debug mode
Debug bool
uriParser URIParser
}
// EndpointConfig defines the configuration of a single endpoint to be exposed
// by the krakend service
type EndpointConfig struct {
// url pattern to be registered and exposed to the world
Endpoint string `mapstructure:"endpoint"`
// HTTP method of the endpoint (GET, POST, PUT, etc)
Method string `mapstructure:"method"`
// set of definitions of the backends to be linked to this endpoint
Backend []*Backend `mapstructure:"backend"`
// number of concurrent calls this endpoint must send to the backends
ConcurrentCalls int `mapstructure:"concurrent_calls"`
// timeout of this endpoint
Timeout time.Duration `mapstructure:"timeout"`
// duration of the cache header
CacheTTL time.Duration `mapstructure:"cache_ttl"`
// list of query string params to be extracted from the URI
QueryString []string `mapstructure:"querystring_params"`
// Endpoint Extra configuration for customized behaviour
ExtraConfig ExtraConfig `mapstructure:"extra_config"`
// HeadersToPass defines the list of headers to pass to the backends
HeadersToPass []string `mapstructure:"headers_to_pass"`
// OutputEncoding defines the encoding strategy to use for the endpoint responses
OutputEncoding string `mapstructure:"output_encoding"`
}
// Backend defines how krakend should connect to the backend service (the API resource to consume)
// and how it should process the received response
type Backend struct {
// the name of the group the response should be moved to. If empty, the response is
// not changed
Group string `mapstructure:"group"`
// HTTP method of the request to send to the backend
Method string `mapstructure:"method"`
// Set of hosts of the API
Host []string `mapstructure:"host"`
// False if the hostname should be sanitized
HostSanitizationDisabled bool `mapstructure:"disable_host_sanitize"`
// URL pattern to use to locate the resource to be consumed
URLPattern string `mapstructure:"url_pattern"`
// set of response fields to remove. If empty, the filter id not used
Blacklist []string `mapstructure:"blacklist"`
// set of response fields to allow. If empty, the filter id not used
Whitelist []string `mapstructure:"whitelist"`
// map of response fields to be renamed and their new names
Mapping map[string]string `mapstructure:"mapping"`
// the encoding format
Encoding string `mapstructure:"encoding"`
// the response to process is a collection
IsCollection bool `mapstructure:"is_collection"`
// name of the field to extract to the root. If empty, the formater will do nothing
Target string `mapstructure:"target"`
// name of the service discovery driver to use
SD string `mapstructure:"sd"`
// list of keys to be replaced in the URLPattern
URLKeys []string
// number of concurrent calls this endpoint must send to the API
ConcurrentCalls int
// timeout of this backend
Timeout time.Duration
// decoder to use in order to parse the received response from the API
Decoder encoding.Decoder `json:"-"`
// Backend Extra configuration for customized behaviours
ExtraConfig ExtraConfig `mapstructure:"extra_config"`
}
// Plugin contains the config required by the plugin module
type Plugin struct {
Folder string `mapstructure:"folder"`
Pattern string `mapstructure:"pattern"`
}
// TLS defines the configuration params for enabling TLS (HTTPS & HTTP/2) at the router layer
type TLS struct {
IsDisabled bool `mapstructure:"disabled"`
PublicKey string `mapstructure:"public_key"`
PrivateKey string `mapstructure:"private_key"`
MinVersion string `mapstructure:"min_version"`
MaxVersion string `mapstructure:"max_version"`
CurvePreferences []uint16 `mapstructure:"curve_preferences"`
PreferServerCipherSuites bool `mapstructure:"prefer_server_cipher_suites"`
CipherSuites []uint16 `mapstructure:"cipher_suites"`
}
// ExtraConfig is a type to store extra configurations for customized behaviours
type ExtraConfig map[string]interface{}
func (e *ExtraConfig) sanitize() {
for module, extra := range *e {
switch extra := extra.(type) {
case map[interface{}]interface{}:
sanitized := map[string]interface{}{}
for k, v := range extra {
sanitized[fmt.Sprintf("%v", k)] = v
}
(*e)[module] = sanitized
}
}
}
// ConfigGetter is a function for parsing ExtraConfig into a previously know type
type ConfigGetter func(ExtraConfig) interface{}
// DefaultConfigGetter is the Default implementation for ConfigGetter, it just returns the ExtraConfig map.
func DefaultConfigGetter(extra ExtraConfig) interface{} { return extra }
const defaultNamespace = "github.com/devopsfaith/krakend/config"
// ConfigGetters map than match namespaces and ConfigGetter so the components knows which type to expect returned by the
// ConfigGetter ie: if we look for the defaultNamespace in the map, we will get the DefaultConfigGetter implementation
// which will return a ExtraConfig when called
var ConfigGetters = map[string]ConfigGetter{defaultNamespace: DefaultConfigGetter}
var (
simpleURLKeysPattern = regexp.MustCompile(`\{([a-zA-Z\-_0-9]+)\}`)
sequentialParamsPattern = regexp.MustCompile(`^resp[\d]+_.*$`)
debugPattern = "^[^/]|/__debug(/.*)?$"
errInvalidHost = errors.New("invalid host")
errInvalidNoOpEncoding = errors.New("can not use NoOp encoding with more than one backends connected to the same endpoint")
defaultPort = 8080
)
// Hash returns the sha 256 hash of the configuration in a standard base64 encoded string
func (s *ServiceConfig) Hash() (string, error) {
b, err := json.Marshal(s)
if err != nil {
return "", err
}
sum := sha256.Sum256(b)
return base64.StdEncoding.EncodeToString(sum[:]), nil
}
// Init initializes the configuration struct and its defined endpoints and backends.
// Init also sanitizes the values, applies the default ones whenever necessary and
// normalizes all the things.
func (s *ServiceConfig) Init() error {
s.uriParser = NewURIParser()
if s.Version != ConfigVersion {
return fmt.Errorf("Unsupported version: %d (want: %d)", s.Version, ConfigVersion)
}
if s.Port == 0 {
s.Port = defaultPort
}
if s.MaxIdleConnsPerHost == 0 {
s.MaxIdleConnsPerHost = DefaultMaxIdleConnsPerHost
}
if s.Timeout == 0 {
s.Timeout = DefaultTimeout
}
s.Host = s.uriParser.CleanHosts(s.Host)
s.ExtraConfig.sanitize()
for i, e := range s.Endpoints {
e.Endpoint = s.uriParser.CleanPath(e.Endpoint)
if err := e.validate(); err != nil {
return err
}
inputParams := s.extractPlaceHoldersFromURLTemplate(e.Endpoint, s.paramExtractionPattern())
inputSet := map[string]interface{}{}
for ip := range inputParams {
inputSet[inputParams[ip]] = nil
}
e.Endpoint = s.uriParser.GetEndpointPath(e.Endpoint, inputParams)
s.initEndpointDefaults(i)
if e.OutputEncoding == encoding.NOOP && len(e.Backend) > 1 {
return errInvalidNoOpEncoding
}
e.ExtraConfig.sanitize()
for j, b := range e.Backend {
s.initBackendDefaults(i, j)
if err := s.initBackendURLMappings(i, j, inputSet); err != nil {
return err
}
b.ExtraConfig.sanitize()
}
}
return nil
}
func (s *ServiceConfig) paramExtractionPattern() *regexp.Regexp {
if s.DisableStrictREST {
return simpleURLKeysPattern
}
return endpointURLKeysPattern
}
func (s *ServiceConfig) extractPlaceHoldersFromURLTemplate(subject string, pattern *regexp.Regexp) []string {
matches := pattern.FindAllStringSubmatch(subject, -1)
keys := make([]string, len(matches))
for k, v := range matches {
keys[k] = v[1]
}
return keys
}
func (s *ServiceConfig) initEndpointDefaults(e int) {
endpoint := s.Endpoints[e]
if endpoint.Method == "" {
endpoint.Method = "GET"
}
if s.CacheTTL != 0 && endpoint.CacheTTL == 0 {
endpoint.CacheTTL = s.CacheTTL
}
if s.Timeout != 0 && endpoint.Timeout == 0 {
endpoint.Timeout = s.Timeout
}
if endpoint.ConcurrentCalls == 0 {
endpoint.ConcurrentCalls = 1
}
if endpoint.OutputEncoding == "" {
if s.OutputEncoding != "" {
endpoint.OutputEncoding = s.OutputEncoding
} else {
endpoint.OutputEncoding = encoding.JSON
}
}
}
func (s *ServiceConfig) initBackendDefaults(e, b int) {
endpoint := s.Endpoints[e]
backend := endpoint.Backend[b]
if len(backend.Host) == 0 {
backend.Host = s.Host
} else if !backend.HostSanitizationDisabled {
backend.Host = s.uriParser.CleanHosts(backend.Host)
}
if backend.Method == "" {
backend.Method = endpoint.Method
}
backend.Timeout = endpoint.Timeout
backend.ConcurrentCalls = endpoint.ConcurrentCalls
backend.Decoder = encoding.Get(strings.ToLower(backend.Encoding))(backend.IsCollection)
}
func (s *ServiceConfig) initBackendURLMappings(e, b int, inputParams map[string]interface{}) error {
backend := s.Endpoints[e].Backend[b]
backend.URLPattern = s.uriParser.CleanPath(backend.URLPattern)
outputParams := s.extractPlaceHoldersFromURLTemplate(backend.URLPattern, simpleURLKeysPattern)
outputSet := map[string]interface{}{}
for op := range outputParams {
outputSet[outputParams[op]] = nil
}
if len(outputSet) > len(inputParams) {
return fmt.Errorf("Too many output params! input: %v, output: %v\n", outputSet, outputParams)
}
tmp := backend.URLPattern
backend.URLKeys = make([]string, len(outputParams))
for o := range outputParams {
if !sequentialParamsPattern.MatchString(outputParams[o]) {
if _, ok := inputParams[outputParams[o]]; !ok {
return fmt.Errorf("Undefined output param [%s]! input: %v, output: %v\n", outputParams[o], inputParams, outputParams)
}
}
tmp = strings.Replace(tmp, "{"+outputParams[o]+"}", "{{."+strings.Title(outputParams[o])+"}}", -1)
backend.URLKeys = append(backend.URLKeys, strings.Title(outputParams[o]))
}
backend.URLPattern = tmp
return nil
}
func (e *EndpointConfig) validate() error {
matched, err := regexp.MatchString(debugPattern, e.Endpoint)
if err != nil {
log.Printf("ERROR: parsing the endpoint url [%s]: %s. Ignoring\n", e.Endpoint, err.Error())
return err
}
if matched {
return fmt.Errorf("ERROR: the endpoint url path [%s] is not a valid one!!! Ignoring\n", e.Endpoint)
}
if len(e.Backend) == 0 {
return fmt.Errorf("WARNING: the [%s] endpoint has 0 backends defined! Ignoring\n", e.Endpoint)
}
return nil
}