/
sslservernamedotyaml.go
399 lines (340 loc) · 13.3 KB
/
sslservernamedotyaml.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
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"
"sort"
"strconv"
"strings"
"github.com/apache/trafficcontrol/lib/go-tc"
)
const ContentTypeYAML = "application/yaml; charset=us-ascii" // Note YAML has no IANA standard mime type. This is one of several common usages, and is likely to be the standardized value. If you're reading this, please check IANA to see if YAML has been added, and change this to the IANA definition if so. Also note we include 'charset=us-ascii' because YAML is commonly UTF-8, but ATS is likely to be unable to handle UTF.
const LineCommentYAML = LineCommentHash
const SSLServerNameYAMLFileName = "ssl_server_name.yaml"
const ContentTypeSSLServerNameYAML = ContentTypeYAML
const LineCommentSSLServerNameYAML = LineCommentYAML
// DefaultDefaultEnableH2 is whether Delivery Services will have HTTP/2 enabled by default if they don't have an explicit Parameter, and no Opt is passed to the Make func.
// We disable by default, to prevent potentially enabling broken clients.
const DefaultDefaultEnableH2 = false
type TLSVersion string
const TLSVersion1p0 = TLSVersion("1.0")
const TLSVersion1p1 = TLSVersion("1.1")
const TLSVersion1p2 = TLSVersion("1.2")
const TLSVersion1p3 = TLSVersion("1.3")
const TLSVersionInvalid = TLSVersion("")
// StringToTLSVersion returns the TLSVersion or TLSVersionInvalid if the string is not a TLS Version enum.
func StringToTLSVersion(st string) TLSVersion {
switch TLSVersion(st) {
case TLSVersion1p0:
return TLSVersion1p0
case TLSVersion1p1:
return TLSVersion1p1
case TLSVersion1p2:
return TLSVersion1p2
case TLSVersion1p3:
return TLSVersion1p3
}
return TLSVersionInvalid
}
// tlsVersionsToATS maps TLS version strings to the string used by ATS in ssl_server_name.yaml.
var tlsVersionsToATS = map[TLSVersion]string{
TLSVersion1p0: "TLSv1",
TLSVersion1p1: "TLSv1_1",
TLSVersion1p2: "TLSv1_2",
TLSVersion1p3: "TLSv1_3",
}
const SSLServerNameYAMLParamEnableH2 = "enable_h2"
const SSLServerNameYAMLParamTLSVersions = "tls_versions"
// DefaultDefaultTLSVersions is the list of TLS versions to enable by default, if no Parameter exists and no Opt is passed to the Make func.
// By default, we enable all, even insecure versions.
// As a CDN, Traffic Control assumes it should not break clients, and it's the client's responsibility to use secure protocols.
// Note this enables certain downgrade attacks. Operators or tenants concerned about these attacks should disable older TLS versions.
var DefaultDefaultTLSVersions = []TLSVersion{
TLSVersion1p0,
TLSVersion1p1,
TLSVersion1p2,
TLSVersion1p3,
}
// SSLServerNameYAMLOpts contains settings to configure ssl_server_name.yaml generation options.
type SSLServerNameYAMLOpts struct {
// VerboseComments is whether to add informative comments to the generated file, about what was generated and why.
// Note this does not include the header comment, which is configured separately with HdrComment.
// These comments are human-readable and not guaranteed to be consistent between versions. Automating anything based on them is strongly discouraged.
VerboseComments bool
// HdrComment is the header comment to include at the beginning of the file.
// This should be the text desired, without comment syntax (like # or //). The file's comment syntax will be added.
// To omit the header comment, pass the empty string.
HdrComment string
// DefaultTLSVersions is the list of TLS versions to enable on delivery services with no Parameter.
DefaultTLSVersions []TLSVersion
// DefaultEnableH2 is whether to disable H2 on delivery services with no Parameter.
DefaultEnableH2 bool
}
func MakeSSLServerNameYAML(
server *Server,
dses []DeliveryService,
dss []DeliveryServiceServer,
dsRegexArr []tc.DeliveryServiceRegexes,
tcParentConfigParams []tc.Parameter,
cdn *tc.CDN,
topologies []tc.Topology,
cacheGroupArr []tc.CacheGroupNullable,
serverCapabilities map[int]map[ServerCapability]struct{},
dsRequiredCapabilities map[int]map[ServerCapability]struct{},
opt *SSLServerNameYAMLOpts,
) (Cfg, error) {
if opt == nil {
opt = &SSLServerNameYAMLOpts{}
}
if len(opt.DefaultTLSVersions) == 0 {
opt.DefaultTLSVersions = DefaultDefaultTLSVersions
}
sslDatas, warnings, err := GetServerSSLData(
server,
dses,
dss,
dsRegexArr,
tcParentConfigParams,
cdn,
topologies,
cacheGroupArr,
serverCapabilities,
dsRequiredCapabilities,
opt.DefaultTLSVersions,
opt.DefaultEnableH2,
)
if err != nil {
return Cfg{}, makeErr(warnings, "getting ssl data: "+err.Error())
}
txt := ""
if opt.HdrComment != "" {
txt += makeHdrComment(opt.HdrComment)
}
seenFQDNs := map[string]struct{}{}
for _, sslData := range sslDatas {
tlsVersionsATS := []string{}
for _, tlsVersion := range sslData.TLSVersions {
tlsVersionsATS = append(tlsVersionsATS, `'`+tlsVersionsToATS[tlsVersion]+`'`)
}
for _, requestFQDN := range sslData.RequestFQDNs {
// TODO let active DSes take precedence?
if _, ok := seenFQDNs[requestFQDN]; ok {
warnings = append(warnings, "ds '"+sslData.DSName+"' had the same FQDN '"+requestFQDN+"' as some other delivery service, skipping!")
continue
}
seenFQDNs[requestFQDN] = struct{}{}
dsTxt := "\n"
if opt.VerboseComments {
dsTxt += LineCommentYAML + ` ds '` + sslData.DSName + `'` + "\n"
}
dsTxt += `- fqdn: '` + requestFQDN + `'`
dsTxt += "\n" + ` disable_h2: ` + strconv.FormatBool(!sslData.EnableH2)
dsTxt += "\n" + ` valid_tls_versions_in: [` + strings.Join(tlsVersionsATS, `,`) + `]`
txt += dsTxt + "\n"
}
}
return Cfg{
Text: txt,
ContentType: ContentTypeSSLServerNameYAML,
LineComment: LineCommentSSLServerNameYAML,
Warnings: warnings,
}, nil
}
// SSLData has the DS data needed for both sni.yaml (ATS 9+) and ssl_server_name.yaml (ATS 8).
type SSLData struct {
DSName string
RequestFQDNs []string
EnableH2 bool
TLSVersions []TLSVersion
}
// GetServerSSLData gets the SSLData for all Delivery Services assigned to the given Server, any warnings, and any error.
func GetServerSSLData(
server *Server,
dses []DeliveryService,
dss []DeliveryServiceServer,
dsRegexArr []tc.DeliveryServiceRegexes,
tcParentConfigParams []tc.Parameter,
cdn *tc.CDN,
topologies []tc.Topology,
cacheGroupArr []tc.CacheGroupNullable,
serverCapabilities map[int]map[ServerCapability]struct{},
dsRequiredCapabilities map[int]map[ServerCapability]struct{},
defaultTLSVersions []TLSVersion,
defaultEnableH2 bool,
) ([]SSLData, []string, error) {
warnings := []string{}
if len(server.ProfileNames) == 0 {
return nil, warnings, errors.New("this server missing Profiles")
}
dsRegexes := makeDSRegexMap(dsRegexArr)
parentConfigParamsWithProfiles, err := tcParamsToParamsWithProfiles(tcParentConfigParams)
if err != nil {
warnings = append(warnings, "error getting profiles from Traffic Ops Parameters, Parameters will not be considered for generation! : "+err.Error())
parentConfigParamsWithProfiles = []parameterWithProfiles{}
}
profileParentConfigParams := map[string]map[string]string{} // map[profileName][paramName]paramVal
for _, param := range parentConfigParamsWithProfiles {
for _, profile := range param.ProfileNames {
if _, ok := profileParentConfigParams[profile]; !ok {
profileParentConfigParams[profile] = map[string]string{}
}
profileParentConfigParams[profile][param.Name] = param.Value
}
}
cacheGroups, err := makeCGMap(cacheGroupArr)
if err != nil {
return nil, warnings, errors.New("making cachegroup map: " + err.Error())
}
nameTopologies := makeTopologyNameMap(topologies)
sort.Sort(dsesSortByName(dses))
sslDatas := []SSLData{}
for _, ds := range dses {
hasDS, err := dsUsesServer(&ds, server, dss, nameTopologies, cacheGroups, serverCapabilities, dsRequiredCapabilities)
if err != nil {
warnings = append(warnings, "error checking if ds uses this server, considering false! Error: "+err.Error())
continue
}
if !hasDS {
continue
}
dsParentConfigParams := map[string]string{}
if ds.ProfileName != nil {
dsParentConfigParams = profileParentConfigParams[*ds.ProfileName]
}
requestFQDNs, err := getDSRequestFQDNs(&ds, dsRegexes[tc.DeliveryServiceName(*ds.XMLID)], server, cdn.DomainName)
if err != nil {
warnings = append(warnings, "error getting ds '"+*ds.XMLID+"' request fqdns, skipping! Error: "+err.Error())
continue
}
enableH2 := defaultEnableH2
tlsVersions := defaultTLSVersions
dsTLSVersions := []TLSVersion{}
for _, tlsVersion := range ds.TLSVersions {
if _, ok := tlsVersionsToATS[TLSVersion(tlsVersion)]; !ok {
warnings = append(warnings, "ds '"+*ds.XMLID+"' had unknown TLS Version '"+tlsVersion+"' - ignoring!")
continue
}
dsTLSVersions = append(dsTLSVersions, TLSVersion(tlsVersion))
}
if len(dsTLSVersions) > 0 {
tlsVersions = dsTLSVersions
}
paramValEnableH2 := dsParentConfigParams[SSLServerNameYAMLParamEnableH2]
paramValEnableH2 = strings.TrimSpace(paramValEnableH2)
paramValEnableH2 = strings.ToLower(paramValEnableH2)
if paramValEnableH2 != "" {
enableH2 = strings.HasPrefix(paramValEnableH2, "t") || strings.HasPrefix(paramValEnableH2, "y")
}
paramValTLSVersions := dsParentConfigParams[SSLServerNameYAMLParamTLSVersions]
paramValTLSVersions = strings.Replace(paramValTLSVersions, " ", "", -1)
paramValTLSVersions = strings.TrimSpace(paramValTLSVersions)
paramTLSVersions := []TLSVersion{}
if paramValTLSVersions != "" {
// Allow delimiting with commas, semicolons, spaces, or newlines.
delim := ","
if !strings.Contains(paramValTLSVersions, delim) {
delim = ";"
}
if !strings.Contains(paramValTLSVersions, delim) {
delim = " "
}
if !strings.Contains(paramValTLSVersions, delim) {
delim = "\n"
}
tlsVersionsParamArr := strings.Split(paramValTLSVersions, delim)
for _, tlsVersion := range tlsVersionsParamArr {
if _, ok := tlsVersionsToATS[TLSVersion(tlsVersion)]; !ok {
warnings = append(warnings, "ds '"+*ds.XMLID+"' had unknown "+SSLServerNameYAMLParamTLSVersions+" parameter '"+tlsVersion+"' - ignoring!")
continue
}
paramTLSVersions = append(paramTLSVersions, TLSVersion(tlsVersion))
}
}
// let Parameters override the Delivery Service field, for backward-compatibility,
// and also because this lets tenants who own multiple DSes set them all in a single
// place, instead of duplicating for every DS (which will be even more de-duplicated
// when Layered Profiles are implemented)
if len(paramTLSVersions) != 0 {
tlsVersions = paramTLSVersions
}
sslDatas = append(sslDatas, SSLData{
DSName: *ds.XMLID,
RequestFQDNs: requestFQDNs,
EnableH2: enableH2,
TLSVersions: tlsVersions,
})
}
return sslDatas, warnings, nil
}
func dsUsesServer(
ds *DeliveryService,
server *Server,
dss []DeliveryServiceServer,
nameTopologies map[TopologyName]tc.Topology,
cacheGroups map[tc.CacheGroupName]tc.CacheGroupNullable,
serverCapabilities map[int]map[ServerCapability]struct{},
dsRequiredCapabilities map[int]map[ServerCapability]struct{},
) (bool, error) {
if ds.XMLID == nil || *ds.XMLID == "" {
return false, errors.New("ds missing xmlId")
} else if ds.ID == nil {
return false, errors.New("ds missing id")
} else if server.ID == nil {
return false, errors.New("server missing id")
} else if ds.Type == nil {
return false, errors.New("ds missing type")
}
if !hasRequiredCapabilities(serverCapabilities[*server.ID], dsRequiredCapabilities[*ds.ID]) {
return false, nil
}
serverParentCGData, err := getParentCacheGroupData(server, cacheGroups)
if err != nil {
return false, errors.New("getting server parent cachegroup data: " + err.Error())
}
cacheIsTopLevel := isTopLevelCache(serverParentCGData)
if !cacheIsTopLevel && (ds.Topology == nil || *ds.Topology == "") {
if !dsAssignedServer(*ds.ID, *server.ID, dss) {
return false, nil
}
}
if ds.Topology != nil && *ds.Topology != "" {
topology, ok := nameTopologies[TopologyName(*ds.Topology)]
if !ok {
return false, errors.New("ds topology '" + *ds.Topology + "' not found in topologies")
}
serverPlacement, err := getTopologyPlacement(tc.CacheGroupName(*server.Cachegroup), topology, cacheGroups, ds)
if err != nil {
return false, errors.New("getting topology placement: " + err.Error())
}
if !serverPlacement.InTopology {
return false, nil
}
}
return true, nil
}
// dsAssignedServer returns whether the Delivery Service Servers has an assignment between the server and the DS.
// Does not check Topologies, or parentage. Only useful for Edges and pre-topology DSS.
func dsAssignedServer(dsID int, serverID int, dsses []DeliveryServiceServer) bool {
for _, dss := range dsses {
if dss.Server == serverID && dss.DeliveryService == dsID {
return true
}
}
return false
}