-
Notifications
You must be signed in to change notification settings - Fork 339
/
meta.go
475 lines (431 loc) · 16.3 KB
/
meta.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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package atscfg
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import (
"errors"
"path/filepath"
"strings"
"github.com/apache/trafficcontrol/lib/go-tc"
)
type CfgMeta struct {
Name string
Path string
}
// ConfigFilesListOpts contains settings to configure generation options.
type ConfigFilesListOpts struct {
}
// MakeMetaObj returns the list of config files, any warnings, and any errors.
func MakeConfigFilesList(
configDir string,
server *Server,
serverParams []tc.Parameter,
deliveryServices []DeliveryService,
deliveryServiceServers []DeliveryServiceServer,
globalParams []tc.Parameter,
cacheGroupArr []tc.CacheGroupNullable,
topologies []tc.Topology,
opt *ConfigFilesListOpts,
) ([]CfgMeta, []string, error) {
if opt == nil {
opt = &ConfigFilesListOpts{}
}
warnings := []string{}
if server.Cachegroup == nil {
return nil, warnings, errors.New("this server missing Cachegroup")
} else if server.CachegroupID == nil {
return nil, warnings, errors.New("this server missing CachegroupID")
} else if server.TCPPort == nil {
return nil, warnings, errors.New("server missing TCPPort")
} else if server.HostName == nil {
return nil, warnings, errors.New("server missing HostName")
} else if server.CDNID == nil {
return nil, warnings, errors.New("server missing CDNID")
} else if server.CDNName == nil {
return nil, warnings, errors.New("server missing CDNName")
} else if server.ID == nil {
return nil, warnings, errors.New("server missing ID")
} else if len(server.ProfileNames) == 0 {
return nil, warnings, errors.New("server missing Profile")
}
atsMajorVer, verWarns := getATSMajorVersion(serverParams)
warnings = append(warnings, verWarns...)
dses, dsWarns := filterConfigFileDSes(server, deliveryServices, deliveryServiceServers)
warnings = append(warnings, dsWarns...)
locationParams := getLocationParams(serverParams)
uriSignedDSes, signDSWarns := getURISignedDSes(dses)
warnings = append(warnings, signDSWarns...)
configFiles := []CfgMeta{}
if locationParams["remap.config"].Path != "" {
configLocation := locationParams["remap.config"].Path
for _, ds := range uriSignedDSes {
cfgName := "uri_signing_" + string(ds) + ".config"
// If there's already a parameter for it, don't clobber it. The user may wish to override the location.
if _, ok := locationParams[cfgName]; !ok {
p := locationParams[cfgName]
p.Name = cfgName
p.Path = configLocation
locationParams[cfgName] = p
}
}
}
locationParamsFor:
for cfgFile, cfgParams := range locationParams {
if strings.HasSuffix(cfgFile, ".config") {
dsConfigFilePrefixes := []string{
"hdr_rw_mid_", // must come before hdr_rw_, to avoid thinking we have a "hdr_rw_" with a ds of "mid_x"
"hdr_rw_",
"regex_remap_",
"url_sig_",
"uri_signing_",
}
prefixFor:
for _, prefix := range dsConfigFilePrefixes {
if strings.HasPrefix(cfgFile, prefix) {
dsName := strings.TrimSuffix(strings.TrimPrefix(cfgFile, prefix), ".config")
if _, ok := dses[tc.DeliveryServiceName(dsName)]; !ok {
warnings = append(warnings, "server profile had 'location' Parameter '"+cfgFile+"', but delivery Service '"+dsName+"' is not assigned to this Server! Not including in meta config!")
continue locationParamsFor
}
break prefixFor // if it has a prefix, don't check the next prefix. This is important for hdr_rw_mid_, which will match hdr_rw_ and result in a "ds name" of "mid_x" if we don't continue here.
}
}
}
atsCfg := CfgMeta{
Name: cfgParams.Name,
Path: cfgParams.Path,
}
configFiles = append(configFiles, atsCfg)
}
configFiles, configDirWarns, err := addMetaObjConfigDir(configFiles, configDir, server, locationParams, uriSignedDSes, dses, cacheGroupArr, topologies, atsMajorVer)
warnings = append(warnings, configDirWarns...)
return configFiles, warnings, err
}
// addMetaObjConfigDir takes the Meta Object generated from TO data, and the ATS config directory
// and prepends the config directory to all relative paths,
// and creates MetaObj entries for all required config files which have no location parameter.
// If configDir is empty and any location Parameters have relative paths, returns an error.
// Returns the amended config files list, any warnings, and any error.
func addMetaObjConfigDir(
configFiles []CfgMeta,
configDir string,
server *Server,
locationParams map[string]configProfileParams, // map[configFile]params; 'location' and 'URL' Parameters on serverHostName's Profile
uriSignedDSes []tc.DeliveryServiceName,
dses map[tc.DeliveryServiceName]DeliveryService,
cacheGroupArr []tc.CacheGroupNullable,
topologies []tc.Topology,
atsMajorVer int,
) ([]CfgMeta, []string, error) {
warnings := []string{}
if server.Cachegroup == nil {
return nil, warnings, errors.New("server missing Cachegroup")
}
cacheGroups, err := makeCGMap(cacheGroupArr)
if err != nil {
return nil, warnings, errors.New("making CG map: " + err.Error())
}
// Note there may be multiple files with the same name in different directories.
configFilesM := map[string][]CfgMeta{} // map[fileShortName]CfgMeta
for _, fi := range configFiles {
configFilesM[fi.Name] = append(configFilesM[fi.Path], fi)
}
// add all strictly required files, all of which should be in the base config directory.
// If they don't exist, create them.
// If they exist with a relative path, prepend configDir.
// If any exist with a relative path, or don't exist, and configDir is empty, return an error.
for _, fileName := range requiredFiles(atsMajorVer) {
if _, ok := configFilesM[fileName]; ok {
continue
}
if configDir == "" {
return nil, warnings, errors.New("required file '" + fileName + "' has no location Parameter, and ATS config directory not found.")
}
configFilesM[fileName] = []CfgMeta{{
Name: fileName,
Path: configDir,
}}
}
for fileName, fis := range configFilesM {
newFis := []CfgMeta{}
for _, fi := range fis {
if !filepath.IsAbs(fi.Path) {
if configDir == "" {
return nil, warnings, errors.New("file '" + fileName + "' has location Parameter with relative path '" + fi.Path + "', but ATS config directory was not found.")
}
absPath := filepath.Join(configDir, fi.Path)
fi.Path = absPath
}
newFis = append(newFis, fi)
}
configFilesM[fileName] = newFis
}
nameTopologies := makeTopologyNameMap(topologies)
for _, ds := range dses {
if ds.XMLID == nil {
warnings = append(warnings, "got Delivery Service with nil XMLID - not considering!")
continue
}
err := error(nil)
// Note we log errors, but don't return them.
// If an individual DS has an error, we don't want to break the rest of the CDN.
if ds.Topology != nil && *ds.Topology != "" {
topology := nameTopologies[TopologyName(*ds.Topology)]
placement, err := getTopologyPlacement(tc.CacheGroupName(*server.Cachegroup), topology, cacheGroups, &ds)
if err != nil {
return nil, warnings, errors.New("getting topology placement: " + err.Error())
}
if placement.IsFirstCacheTier {
if (ds.FirstHeaderRewrite != nil && *ds.FirstHeaderRewrite != "") || ds.MaxOriginConnections != nil || ds.ServiceCategory != nil {
fileName := FirstHeaderRewriteConfigFileName(*ds.XMLID)
if configFilesM, err = ensureConfigFile(configFilesM, fileName, configDir); err != nil {
warnings = append(warnings, "ensuring config file '"+fileName+"': "+err.Error())
}
}
}
if placement.IsInnerCacheTier {
if (ds.InnerHeaderRewrite != nil && *ds.InnerHeaderRewrite != "") || ds.MaxOriginConnections != nil || ds.ServiceCategory != nil {
fileName := InnerHeaderRewriteConfigFileName(*ds.XMLID)
if configFilesM, err = ensureConfigFile(configFilesM, fileName, configDir); err != nil {
warnings = append(warnings, "ensuring config file '"+fileName+"': "+err.Error())
}
}
}
if placement.IsLastCacheTier {
if (ds.LastHeaderRewrite != nil && *ds.LastHeaderRewrite != "") || ds.MaxOriginConnections != nil || ds.ServiceCategory != nil {
fileName := LastHeaderRewriteConfigFileName(*ds.XMLID)
if configFilesM, err = ensureConfigFile(configFilesM, fileName, configDir); err != nil {
warnings = append(warnings, "ensuring config file '"+fileName+"': "+err.Error())
}
}
}
} else if strings.HasPrefix(server.Type, tc.EdgeTypePrefix) {
if (ds.EdgeHeaderRewrite != nil || ds.MaxOriginConnections != nil || ds.ServiceCategory != nil) &&
strings.HasPrefix(server.Type, tc.EdgeTypePrefix) {
fileName := "hdr_rw_" + *ds.XMLID + ".config"
if configFilesM, err = ensureConfigFile(configFilesM, fileName, configDir); err != nil {
warnings = append(warnings, "ensuring config file '"+fileName+"': "+err.Error())
}
}
} else if strings.HasPrefix(server.Type, tc.MidTypePrefix) {
if (ds.MidHeaderRewrite != nil || ds.MaxOriginConnections != nil || ds.ServiceCategory != nil) &&
ds.Type != nil && ds.Type.UsesMidCache() &&
strings.HasPrefix(server.Type, tc.MidTypePrefix) {
fileName := "hdr_rw_mid_" + *ds.XMLID + ".config"
if configFilesM, err = ensureConfigFile(configFilesM, fileName, configDir); err != nil {
warnings = append(warnings, "ensuring config file '"+fileName+"': "+err.Error())
}
}
}
if ds.RegexRemap != nil {
configFile := "regex_remap_" + *ds.XMLID + ".config"
if configFilesM, err = ensureConfigFile(configFilesM, configFile, configDir); err != nil {
warnings = append(warnings, "ensuring config file '"+configFile+"': "+err.Error())
}
}
if ds.SigningAlgorithm != nil && *ds.SigningAlgorithm == tc.SigningAlgorithmURLSig {
configFile := "url_sig_" + *ds.XMLID + ".config"
if configFilesM, err = ensureConfigFile(configFilesM, configFile, configDir); err != nil {
warnings = append(warnings, "ensuring config file '"+configFile+"': "+err.Error())
}
}
if ds.SigningAlgorithm != nil && *ds.SigningAlgorithm == tc.SigningAlgorithmURISigning {
configFile := "uri_signing_" + *ds.XMLID + ".config"
if configFilesM, err = ensureConfigFile(configFilesM, configFile, configDir); err != nil {
warnings = append(warnings, "ensuring config file '"+configFile+"': "+err.Error())
}
}
}
newFiles := []CfgMeta{}
for _, fis := range configFilesM {
for _, fi := range fis {
newFiles = append(newFiles, fi)
}
}
return newFiles, warnings, nil
}
// getURISignedDSes returns the URI-signed Delivery Services, and any warnings.
func getURISignedDSes(dses map[tc.DeliveryServiceName]DeliveryService) ([]tc.DeliveryServiceName, []string) {
warnings := []string{}
uriSignedDSes := []tc.DeliveryServiceName{}
for _, ds := range dses {
if ds.ID == nil {
warnings = append(warnings, "got delivery service with no id, skipping!")
continue
}
if ds.XMLID == nil {
warnings = append(warnings, "got delivery service with no xmlId (name), skipping!")
continue
}
if _, ok := dses[tc.DeliveryServiceName(*ds.XMLID)]; !ok {
continue // skip: this ds isn't assigned to this server, this is normal
}
if ds.SigningAlgorithm == nil || *ds.SigningAlgorithm != tc.SigningAlgorithmURISigning {
continue // not signed, so not in our list of signed dses to make config files for.
}
uriSignedDSes = append(uriSignedDSes, tc.DeliveryServiceName(*ds.XMLID))
}
return uriSignedDSes, warnings
}
// filterConfigFileDSes returns the DSes that should have config files for the given server.
// Returns the delivery services and any warnings.
func filterConfigFileDSes(server *Server, deliveryServices []DeliveryService, deliveryServiceServers []DeliveryServiceServer) (map[tc.DeliveryServiceName]DeliveryService, []string) {
warnings := []string{}
dses := map[tc.DeliveryServiceName]DeliveryService{}
if tc.CacheTypeFromString(server.Type) != tc.CacheTypeMid {
dsIDs := map[int]struct{}{}
for _, ds := range deliveryServices {
if ds.ID == nil {
warnings = append(warnings, "got delivery service with no ID, skipping!")
continue
}
dsIDs[*ds.ID] = struct{}{}
}
// TODO verify?
// serverIDs := []int{server.ID}
dssMap := map[int]struct{}{}
for _, dss := range deliveryServiceServers {
if dss.Server != *server.ID {
continue
}
if _, ok := dsIDs[dss.DeliveryService]; !ok {
continue
}
dssMap[dss.DeliveryService] = struct{}{}
}
for _, ds := range deliveryServices {
if ds.ID == nil {
warnings = append(warnings, "got deliveryservice with nil id, skipping!")
continue
}
if ds.XMLID == nil {
warnings = append(warnings, "got deliveryservice with nil xmlId (name), skipping!")
continue
}
if _, ok := dssMap[*ds.ID]; !ok && ds.Topology == nil {
continue
}
dses[tc.DeliveryServiceName(*ds.XMLID)] = ds
}
} else {
for _, ds := range deliveryServices {
if ds.ID == nil {
warnings = append(warnings, "got deliveryservice with nil id, skipping!")
continue
}
if ds.XMLID == nil {
warnings = append(warnings, "got deliveryservice with nil xmlId (name), skipping!")
continue
}
if ds.CDNID == nil || *ds.CDNID != *server.CDNID {
continue
}
dses[tc.DeliveryServiceName(*ds.XMLID)] = ds
}
}
return dses, warnings
}
// getTOURLAndReverseProxy returns the toURL and toReverseProxyURL if they exist, or empty strings if they don't.
func getTOURLAndReverseProxy(globalParams []tc.Parameter) (string, string) {
toReverseProxyURL := ""
toURL := ""
for _, param := range globalParams {
if param.Name == "tm.rev_proxy.url" {
toReverseProxyURL = param.Value
} else if param.Name == "tm.url" {
toURL = param.Value
}
if toReverseProxyURL != "" && toURL != "" {
break
}
}
return toURL, toReverseProxyURL
}
func getLocationParams(serverParams []tc.Parameter) map[string]configProfileParams {
locationParams := map[string]configProfileParams{}
for _, param := range serverParams {
if param.Name == "location" {
p := locationParams[param.ConfigFile]
p.Name = param.ConfigFile
p.Path = param.Value
locationParams[param.ConfigFile] = p
}
}
return locationParams
}
type configProfileParams struct {
Name string
Path string
}
func requiredFiles(atsMajorVer int) []string {
if atsMajorVer >= 9 {
return requiredFiles9()
}
return requiredFiles8()
}
// requiredFiles8 the list of config files required by ATS 8.
// Note these are not exhaustive. This is only used to error if these are missing.
// The presence of these is no guarantee the location Parameters are complete and correct.
func requiredFiles8() []string {
return []string{
"cache.config",
"hosting.config",
"ip_allow.config",
"parent.config",
"plugin.config",
"records.config",
"remap.config",
"ssl_server_name.yaml",
"storage.config",
"volume.config",
}
}
// requiredFiles9 is the list of config files required by ATS 9.
// Note these are not exhaustive. This is only used to error if these are missing.
// The presence of these is no guarantee the location Parameters are complete and correct.
func requiredFiles9() []string {
return []string{
"cache.config",
"hosting.config",
"ip_allow.yaml",
"parent.config",
"plugin.config",
"records.config",
"remap.config",
"sni.yaml",
"storage.config",
"volume.config",
"strategies.yaml",
}
}
// ensureConfigFile ensures files contains the given fileName. If so, returns files unmodified.
// If not, if configDir is empty, returns an error.
// If not, and configDir is nonempty, creates the given file, configDir location, and returns files.
func ensureConfigFile(files map[string][]CfgMeta, fileName string, configDir string) (map[string][]CfgMeta, error) {
if _, ok := files[fileName]; ok {
return files, nil
}
if configDir == "" {
return files, errors.New("required file '" + fileName + "' has no location Parameter, and ATS config directory not found.")
}
files[fileName] = []CfgMeta{{
Name: fileName,
Path: configDir,
}}
return files, nil
}