From 8e930d357e8d25f57168c744c93cc95d486d2bc4 Mon Sep 17 00:00:00 2001 From: Robert Butts Date: Wed, 15 Jan 2020 17:49:11 -0700 Subject: [PATCH] Fix atscfg not adding hdr_rw_mid files to meta cfg --- lib/go-atscfg/meta.go | 3 + traffic_ops/ort/atstccfg/cfgfile/meta.go | 71 +++++++++++++------ .../testing/api/v14/atsconfig_meta_test.go | 61 ++++++++++++++++ traffic_ops/testing/api/v14/tc-fixtures.json | 67 +++++++++++++++++ traffic_ops/traffic_ops_golang/ats/db.go | 21 +++++- 5 files changed, 199 insertions(+), 24 deletions(-) diff --git a/lib/go-atscfg/meta.go b/lib/go-atscfg/meta.go index fbcb841b80..804c0e704a 100644 --- a/lib/go-atscfg/meta.go +++ b/lib/go-atscfg/meta.go @@ -88,11 +88,13 @@ 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") @@ -100,6 +102,7 @@ locationParamsFor: log.Warnln("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. } } } diff --git a/traffic_ops/ort/atstccfg/cfgfile/meta.go b/traffic_ops/ort/atstccfg/cfgfile/meta.go index 18dd27e343..313c08e34f 100644 --- a/traffic_ops/ort/atstccfg/cfgfile/meta.go +++ b/traffic_ops/ort/atstccfg/cfgfile/meta.go @@ -183,31 +183,58 @@ func GetConfigFileMeta(cfg config.TCCfg, serverNameOrID string) (string, error) return "", errors.New("getting delivery services: " + err.Error()) } - dsIDs := []int{} - for _, ds := range deliveryServices { - if ds.ID == nil { - // TODO log error? - continue + dsNames := map[tc.DeliveryServiceName]struct{}{} + if tc.CacheTypeFromString(server.Type) != tc.CacheTypeMid { + dsIDs := []int{} + for _, ds := range deliveryServices { + if ds.ID == nil { + // TODO log error? + continue + } + dsIDs = append(dsIDs, *ds.ID) } - dsIDs = append(dsIDs, *ds.ID) - } - serverIDs := []int{server.ID} + serverIDs := []int{server.ID} - dsServers, err := toreq.GetDeliveryServiceServers(cfg, dsIDs, serverIDs) - if err != nil { - return "", errors.New("getting meta config delivery service servers: " + err.Error()) - } + dsServers, err := toreq.GetDeliveryServiceServers(cfg, dsIDs, serverIDs) + if err != nil { + return "", errors.New("getting meta config delivery service servers: " + err.Error()) + } - dssMap := map[int]struct{}{} // set of map[dsID]. We know we only asked for our own server, so we don't care about the servers returned. - for _, dss := range dsServers { - if dss.DeliveryService == nil { - continue // TODO log? + dssMap := map[int]struct{}{} // set of map[dsID]. We know we only asked for our own server, so we don't care about the servers returned. + for _, dss := range dsServers { + if dss.DeliveryService == nil { + continue // TODO log? + } + dssMap[*dss.DeliveryService] = struct{}{} + } + for _, ds := range deliveryServices { + if ds.ID == nil { + continue + } + if ds.XMLID == nil { + continue // TODO log? + } + if _, ok := dssMap[*ds.ID]; !ok { + continue + } + dsNames[tc.DeliveryServiceName(*ds.XMLID)] = struct{}{} + } + } else { + for _, ds := range deliveryServices { + if ds.ID == nil { + continue + } + if ds.XMLID == nil { + continue // TODO log? + } + if ds.CDNID == nil || *ds.CDNID != server.CDNID { + continue + } + dsNames[tc.DeliveryServiceName(*ds.XMLID)] = struct{}{} } - dssMap[*dss.DeliveryService] = struct{}{} } - dsNames := map[tc.DeliveryServiceName]struct{}{} uriSignedDSes := []tc.DeliveryServiceName{} for _, ds := range deliveryServices { if ds.ID == nil { @@ -216,13 +243,13 @@ func GetConfigFileMeta(cfg config.TCCfg, serverNameOrID string) (string, error) if ds.XMLID == nil { continue // TODO log? } - if _, ok := dssMap[*ds.ID]; !ok { + if _, ok := dsNames[tc.DeliveryServiceName(*ds.XMLID)]; !ok { continue } - dsNames[tc.DeliveryServiceName(*ds.XMLID)] = struct{}{} - if ds.SigningAlgorithm != nil && *ds.SigningAlgorithm == tc.SigningAlgorithmURISigning { - uriSignedDSes = append(uriSignedDSes, tc.DeliveryServiceName(*ds.XMLID)) + if ds.SigningAlgorithm == nil || *ds.SigningAlgorithm != tc.SigningAlgorithmURISigning { + continue } + uriSignedDSes = append(uriSignedDSes, tc.DeliveryServiceName(*ds.XMLID)) } return atscfg.MakeMetaConfig(tc.CacheName(serverHostName), &serverInfo, toURL, toReverseProxyURL, locationParams, uriSignedDSes, scopeParams, dsNames), nil diff --git a/traffic_ops/testing/api/v14/atsconfig_meta_test.go b/traffic_ops/testing/api/v14/atsconfig_meta_test.go index 378c797431..089cb732f7 100644 --- a/traffic_ops/testing/api/v14/atsconfig_meta_test.go +++ b/traffic_ops/testing/api/v14/atsconfig_meta_test.go @@ -27,6 +27,7 @@ func TestATSConfigMeta(t *testing.T) { defer DeleteTestDeliveryServiceServersCreated(t) CreateTestDeliveryServiceServers(t) GetTestATSConfigMeta(t) + GetTestATSConfigMetaMidHdrRw(t) }) } @@ -82,3 +83,63 @@ func GetTestATSConfigMeta(t *testing.T) { t.Errorf("Getting server '"+server.HostName+"' config list: expected: %+v actual: %+v\n", expected, *actual) } } + +func GetTestATSConfigMetaMidHdrRw(t *testing.T) { + testServer := tc.Server{} + for _, sv := range testData.Servers { + if tc.CacheTypeFromString(sv.Type) == tc.CacheTypeMid { + + testServer = sv + break + } + } + if testServer.HostName == "" { + t.Fatal("cannot GET Server: no test data") + } + + serverList, _, err := TOSession.GetServerByHostName(testServer.HostName) + if err != nil { + t.Fatalf("cannot GET Server: %v", err) + } + if len(serverList) < 1 { + t.Fatalf("cannot GET Server '" + testServer.HostName + "', returned no servers\n") + } + server := serverList[0] + + lst, _, err := TOSession.GetATSServerConfigList(server.ID) + if err != nil { + t.Fatalf("Getting server '" + server.HostName + "' config list: " + err.Error() + "\n") + } + + expected := tc.ATSConfigMetaDataConfigFile{ + FileNameOnDisk: "hdr_rw_mid_ds1nat.config", + Location: "/remap/config/location/parameter", + APIURI: "cdns/cdn1/configfiles/ats/hdr_rw_mid_ds1nat.config", // expected suffix; config gen doesn't care about API version + URL: "", + Scope: "cdns", + } + + actual := (*tc.ATSConfigMetaDataConfigFile)(nil) + for _, cfg := range lst.ConfigFiles { + if cfg.FileNameOnDisk == expected.FileNameOnDisk { + actual = &cfg + break + } + } + if actual == nil { + t.Fatalf("Getting server '"+server.HostName+"' config list: expected: %+v actual: not found\n", expected.FileNameOnDisk) + } + + if expected.FileNameOnDisk != actual.FileNameOnDisk { + t.Errorf("Getting server '"+server.HostName+"' config list: expected: %+v actual: %+v\n", expected, *actual) + } + if expected.Location != actual.Location { + t.Errorf("Getting server '"+server.HostName+"' config list: expected: %+v actual: %+v\n", expected, *actual) + } + if !strings.HasSuffix(actual.APIURI, expected.APIURI) { + t.Errorf("Getting server '"+server.HostName+"' config list: expected: %+v actual: %+v\n", expected, *actual) + } + if actual.Scope != expected.Scope { + t.Errorf("Getting server '"+server.HostName+"' config list: expected: %+v actual: %+v\n", expected, *actual) + } +} diff --git a/traffic_ops/testing/api/v14/tc-fixtures.json b/traffic_ops/testing/api/v14/tc-fixtures.json index d8097b5d61..a1d6ce8096 100644 --- a/traffic_ops/testing/api/v14/tc-fixtures.json +++ b/traffic_ops/testing/api/v14/tc-fixtures.json @@ -630,6 +630,73 @@ "type": "HTTP_LIVE", "xmlId": "msods1", "anonymousBlockingEnabled": true + }, + { + "active": true, + "cdnName": "cdn1", + "cacheurl": "cacheUrl1", + "ccrDnsTtl": 3600, + "checkPath": "", + "consistentHashQueryParams": [], + "deepCachingType": "NEVER", + "displayName": "ds1natDisplayName", + "dnsBypassCname": null, + "dnsBypassIp": "", + "dnsBypassIp6": "", + "dnsBypassTtl": 30, + "dscp": 40, + "edgeHeaderRewrite": "edgeHeader1", + "exampleURLs": [ + "http://ccr.ds1nat.example.net", + "https://ccr.ds1nat.example.net" + ], + "fqPacingRate": 0, + "geoLimit": 0, + "geoLimitCountries": "", + "geoLimitRedirectURL": null, + "geoProvider": 0, + "globalMaxMbps": 0, + "globalMaxTps": 0, + "httpBypassFqdn": "", + "infoUrl": "TBD", + "initialDispersion": 1, + "ipv6RoutingEnabled": true, + "lastUpdated": "2018-04-06 16:48:51+00", + "logsEnabled": false, + "longDesc": "d s 1", + "longDesc1": "ds1nat", + "longDesc2": "ds1nat", + "matchList": [ + { + "pattern": ".*\\.ds1nat\\..*", + "setNumber": 0, + "type": "HOST_REGEXP" + } + ], + "maxDnsAnswers": 0, + "midHeaderRewrite": "midHeader1", + "missLat": 41.881944, + "missLong": -87.627778, + "multiSiteOrigin": false, + "orgServerFqdn": "http://origin.example.net", + "originShield": null, + "profileDescription": null, + "profileName": null, + "protocol": 2, + "qstringIgnore": 1, + "rangeRequestHandling": 0, + "regexRemap": "rr1", + "regionalGeoBlocking": false, + "remapText": "@plugin=tslua.so @pparam=/opt/trafficserver/etc/trafficserver/remapPlugin1.lua", + "routingName": "ccr-ds1nat", + "signed": false, + "signingAlgorithm": "url_sig", + "sslKeyVersion": 2, + "tenant": "tenant1", + "tenantName": "tenant1", + "type": "HTTP_LIVE_NATNL", + "xmlId": "ds1nat", + "anonymousBlockingEnabled": true } ], "deliveryservicesRequiredCapabilities": [ diff --git a/traffic_ops/traffic_ops_golang/ats/db.go b/traffic_ops/traffic_ops_golang/ats/db.go index 26fe44b023..ac30dcf2ce 100644 --- a/traffic_ops/traffic_ops_golang/ats/db.go +++ b/traffic_ops/traffic_ops_golang/ats/db.go @@ -750,17 +750,34 @@ WHERE } // GetDSNames returns a set of delivery service names which have the given server assigned, and any error. +// For edges, this returns all manually-assigned delivery services. For Mids, it returns all delivery services on the Mid's CDN. func GetDSNames(tx *sql.Tx, serverHostName string, serverPort int) (map[tc.DeliveryServiceName]struct{}, error) { qry := ` +WITH s_name_port AS ( + SELECT $1::text as name, $2::integer as port +) SELECT ds.xml_id FROM deliveryservice ds JOIN deliveryservice_server dss ON ds.id = dss.deliveryservice JOIN server s ON s.id = dss.server + JOIN type tp on s.type = tp.id WHERE - s.host_name = $1 - AND s.tcp_port = $2 + s.host_name = (select name from s_name_port) + AND s.tcp_port = (select port from s_name_port) + AND tp.name <> '` + tc.CacheTypeMid.String() + `' +UNION ALL +SELECT + ds.xml_id +FROM + deliveryservice ds + JOIN server s ON s.cdn_id = ds.cdn_id + JOIN type tp on s.type = tp.id +WHERE + s.host_name = (select name from s_name_port) + AND s.tcp_port = (select port from s_name_port) + AND tp.name = '` + tc.CacheTypeMid.String() + `' ` rows, err := tx.Query(qry, serverHostName, serverPort) if err != nil {