forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
430 lines (376 loc) · 11.5 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
// Package config loads and understands the tegola config format.
package config
import (
"fmt"
"io"
"net/http"
"os"
"strings"
"time"
"github.com/BurntSushi/toml"
"github.com/go-spatial/tegola"
"github.com/go-spatial/tegola/internal/env"
"github.com/go-spatial/tegola/internal/log"
"github.com/go-spatial/tegola/provider"
)
const (
BboxToken = "!BBOX!"
ZoomToken = "!ZOOM!"
XToken = "!X!"
YToken = "!Y!"
ZToken = "!Z!"
ScaleDenominatorToken = "!SCALE_DENOMINATOR!"
PixelWidthToken = "!PIXEL_WIDTH!"
PixelHeightToken = "!PIXEL_HEIGHT!"
IdFieldToken = "!ID_FIELD!"
GeomFieldToken = "!GEOM_FIELD!"
GeomTypeToken = "!GEOM_TYPE!"
)
// ReservedTokens for query injection
var ReservedTokens = map[string]struct{}{
BboxToken: {},
ZoomToken: {},
XToken: {},
YToken: {},
ZToken: {},
ScaleDenominatorToken: {},
PixelWidthToken: {},
PixelHeightToken: {},
IdFieldToken: {},
GeomFieldToken: {},
GeomTypeToken: {},
}
var blacklistHeaders = []string{"content-encoding", "content-length", "content-type"}
// Config represents a tegola config file.
type Config struct {
// the tile buffer to use
TileBuffer *env.Int `toml:"tile_buffer"`
// LocationName is the file name or http server that the config was read from.
// If this is an empty string, it means that the location was unknown. This is the case if
// the Parse() function is used directly.
LocationName string
Webserver Webserver `toml:"webserver"`
Cache env.Dict `toml:"cache"`
Observer env.Dict `toml:"observer"`
// Map of providers.
// all providers must have at least two entries.
// 1. name -- this is the name that is referenced in
// the maps section
// 2. type -- this is the name the provider modules register
// themselves under. (e.g. postgis, gpkg, mvt_postgis )
// Note: Use the type to figure out if the provider is a mvt or std provider
Providers []env.Dict `toml:"providers"`
Maps []provider.Map `toml:"maps"`
}
// Webserver represents the config options for the webserver part of Tegola
type Webserver struct {
HostName env.String `toml:"hostname"`
Port env.String `toml:"port"`
URIPrefix env.String `toml:"uri_prefix"`
Headers env.Dict `toml:"headers"`
SSLCert env.String `toml:"ssl_cert"`
SSLKey env.String `toml:"ssl_key"`
}
// ValidateAndRegisterParams ensures configured params don't conflict with existing
// query tokens or have overlapping names
func ValidateAndRegisterParams(mapName string, params []provider.QueryParameter) error {
if len(params) == 0 {
return nil
}
usedNames := make(map[string]struct{})
usedTokens := make(map[string]struct{})
for _, param := range params {
if _, ok := provider.ParamTypeDecoders[param.Type]; !ok {
return ErrParamUnknownType{
MapName: string(mapName),
Parameter: param,
}
}
if len(param.DefaultSQL) > 0 && len(param.DefaultValue) > 0 {
return ErrParamTwoDefaults{
MapName: string(mapName),
Parameter: param,
}
}
if len(param.DefaultValue) > 0 {
decoderFn := provider.ParamTypeDecoders[param.Type]
if _, err := decoderFn(param.DefaultValue); err != nil {
return ErrParamInvalidDefault{
MapName: string(mapName),
Parameter: param,
}
}
}
if _, ok := ReservedTokens[param.Token]; ok {
return ErrParamTokenReserved{
MapName: string(mapName),
Parameter: param,
}
}
if !provider.ParameterTokenRegexp.MatchString(param.Token) {
return ErrParamBadTokenName{
MapName: string(mapName),
Parameter: param,
}
}
if _, ok := usedNames[param.Name]; ok {
return ErrParamDuplicateName{
MapName: string(mapName),
Parameter: param,
}
}
if _, ok := usedTokens[param.Token]; ok {
return ErrParamDuplicateToken{
MapName: string(mapName),
Parameter: param,
}
}
usedNames[param.Name] = struct{}{}
usedTokens[param.Token] = struct{}{}
}
// Mark all used tokens as reserved
for token := range usedTokens {
ReservedTokens[token] = struct{}{}
}
return nil
}
// Validate checks the config for issues
func (c *Config) Validate() error {
var knownTypes []string
drivers := make(map[string]int)
for _, name := range provider.Drivers(provider.TypeStd) {
drivers[name] = int(provider.TypeStd)
knownTypes = append(knownTypes, name)
}
for _, name := range provider.Drivers(provider.TypeMvt) {
drivers[name] = int(provider.TypeMvt)
knownTypes = append(knownTypes, name)
}
// mvtproviders maps a known provider name to whether that provider is
// an mvt provider or not.
mvtproviders := make(map[string]bool, len(c.Providers))
for i, prvd := range c.Providers {
name, _ := prvd.String("name", nil)
if name == "" {
return ErrProviderNameRequired{Pos: i}
}
typ, _ := prvd.String("type", nil)
if typ == "" {
return ErrProviderTypeRequired{Pos: i}
}
// Check to see if the name has already been seen before.
if _, ok := mvtproviders[name]; ok {
return ErrProviderNameDuplicate{Pos: i}
}
drv, ok := drivers[typ]
if !ok {
return ErrUnknownProviderType{
Name: name,
Type: typ,
KnownProviders: knownTypes,
}
}
mvtproviders[name] = drv == int(provider.TypeMvt)
}
// check for map layer name / zoom collisions
// map of layers to providers
mapLayers := map[string]map[string]provider.MapLayer{}
// maps with configured parameters for logging
mapsWithCustomParams := []string{}
for mapKey, m := range c.Maps {
// validate any declared query parameters
if err := ValidateAndRegisterParams(string(m.Name), m.Parameters); err != nil {
return err
}
if len(m.Parameters) > 0 {
mapsWithCustomParams = append(mapsWithCustomParams, string(m.Name))
}
if _, ok := mapLayers[string(m.Name)]; !ok {
mapLayers[string(m.Name)] = map[string]provider.MapLayer{}
}
// Set current provider to empty, for MVT providers
// we can only have the same provider for all layers.
// This allow us to track what the first found provider
// is.
currentProvider := ""
isMVTProvider := false
for layerKey, l := range m.Layers {
pname, _, err := l.ProviderLayerName()
if err != nil {
return err
}
if currentProvider == "" {
// This is the first provider we found.
// For MVTProviders all others need to be the same, so store it
// so we can check later
currentProvider = pname
}
isMvt, doesExists := mvtproviders[pname]
if !doesExists {
return ErrInvalidProviderForMap{
MapName: string(m.Name),
ProviderName: pname,
}
}
// check to see if any of the prior provider or this one is
// an mvt provider. If it is, then the mvtProvider check needs
// to be done
isMVTProvider = isMVTProvider || isMvt
// only need to do this check if we are dealing with MVTProviders
if isMVTProvider && pname != currentProvider {
// for mvt_providers we can only have the same provider
// for all layers
// check to see
if mvtproviders[pname] || isMVTProvider {
return ErrMVTDifferentProviders{
Original: currentProvider,
Current: pname,
}
}
}
name, err := l.GetName()
if err != nil {
return err
}
// MaxZoom default
if l.MaxZoom == nil {
ph := env.Uint(tegola.MaxZ)
// set in iterated value
l.MaxZoom = &ph
// set in underlying config struct
c.Maps[mapKey].Layers[layerKey].MaxZoom = &ph
}
// MinZoom default
if l.MinZoom == nil {
ph := env.Uint(0)
// set in iterated value
l.MinZoom = &ph
// set in underlying config struct
c.Maps[mapKey].Layers[layerKey].MinZoom = &ph
}
if int(*l.MaxZoom) == 0 {
log.Warn("max_zoom of 0 is not supported. adjusting to '1'")
ph := env.Uint(1)
// set in iterated value
l.MaxZoom = &ph
// set in underlying config struct
c.Maps[mapKey].Layers[layerKey].MaxZoom = &ph
}
// check if we already have this layer
if val, ok := mapLayers[string(m.Name)][name]; ok {
// we have a hit. check for zoom range overlap
if uint(*val.MinZoom) <= uint(*l.MaxZoom) && uint(*l.MinZoom) <= uint(*val.MaxZoom) {
return ErrOverlappingLayerZooms{
ProviderLayer1: string(val.ProviderLayer),
ProviderLayer2: string(l.ProviderLayer),
}
}
continue
}
// add the MapLayer to our map
mapLayers[string(m.Name)][name] = l
}
}
if len(mapsWithCustomParams) > 0 {
log.Infof(
"Caching is disabled for these maps, since they have configured custom parameters: %s",
strings.Join(mapsWithCustomParams, ", "),
)
}
// check for blacklisted headers
for k := range c.Webserver.Headers {
for _, v := range blacklistHeaders {
if v == strings.ToLower(k) {
return ErrInvalidHeader{Header: k}
}
}
}
// check if webserver.uri_prefix is set and if so
// confirm it starts with a forward slash "/"
if string(c.Webserver.URIPrefix) != "" {
uriPrefix := string(c.Webserver.URIPrefix)
if string(uriPrefix[0]) != "/" {
return ErrInvalidURIPrefix(uriPrefix)
}
}
return nil
}
// ConfigureTileBuffers handles setting the tile buffer for a Map
func (c *Config) ConfigureTileBuffers() {
// range our configured maps
for mapKey, m := range c.Maps {
// if there is a tile buffer config for this map, use it
if m.TileBuffer != nil {
c.Maps[mapKey].TileBuffer = m.TileBuffer
continue
}
// if there is a global tile buffer config, use it
if c.TileBuffer != nil {
c.Maps[mapKey].TileBuffer = c.TileBuffer
continue
}
// tile buffer is not configured, use default
c.Maps[mapKey].TileBuffer = env.IntPtr(env.Int(tegola.DefaultTileBuffer))
}
}
// Parse will parse the Tegola config file provided by the io.Reader.
func Parse(reader io.Reader, location string) (conf Config, err error) {
// decode conf file, don't care about the meta data.
_, err = toml.NewDecoder(reader).Decode(&conf)
if err != nil {
return conf, err
}
for _, m := range conf.Maps {
for k, p := range m.Parameters {
p.Normalize()
m.Parameters[k] = p
}
}
conf.LocationName = location
conf.ConfigureTileBuffers()
return conf, nil
}
// Load will load and parse the config file from the given location.
func Load(location string) (conf Config, err error) {
var reader io.Reader
// check for http prefix
if strings.HasPrefix(location, "http") {
log.Infof("loading remote config (%v)", location)
// setup http client with a timeout
var httpClient = &http.Client{
Timeout: time.Second * 10,
}
// make the http request
res, err := httpClient.Get(location)
if err != nil {
return conf, fmt.Errorf("error fetching remote config file (%v): %v ", location, err)
}
// set the reader to the response body
reader = res.Body
} else if location == "-" {
log.Infof("loading local config from stdin")
reader = os.Stdin
} else {
log.Infof("loading local config (%v)", location)
// check the conf file exists
if _, err := os.Stat(location); os.IsNotExist(err) {
return conf, fmt.Errorf("config file at location (%v) not found", location)
}
// open the config file
reader, err = os.Open(location)
if err != nil {
return conf, fmt.Errorf("error opening local config file (%v): %v ", location, err)
}
}
return Parse(reader, location)
}
// LoadAndValidate will load the config from the given filename and validate it if it was
// able to load the file
func LoadAndValidate(filename string) (cfg Config, err error) {
cfg, err = Load(filename)
if err != nil {
return cfg, err
}
// validate our config
return cfg, cfg.Validate()
}