alficles commented on a change in pull request #3688: Add TO Go remap.config
URL: https://github.com/apache/trafficcontrol/pull/3688#discussion_r321574341
 
 

 ##########
 File path: lib/go-atscfg/remapdotconfig.go
 ##########
 @@ -0,0 +1,369 @@
+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-log"
+       "github.com/apache/trafficcontrol/lib/go-tc"
+)
+
+type RemapConfigDSData struct {
+       ID                       int
+       Type                     tc.DSType
+       OriginFQDN               *string
+       MidHeaderRewrite         *string
+       CacheURL                 *string
+       RangeRequestHandling     *int
+       CacheKeyConfigParams     map[string]string
+       RemapText                *string
+       EdgeHeaderRewrite        *string
+       SigningAlgorithm         *string
+       Name                     string
+       QStringIgnore            *int
+       RegexRemap               *string
+       FQPacingRate             *int
+       DSCP                     int
+       RoutingName              *string
+       MultiSiteOrigin          *string
+       Pattern                  *string
+       RegexType                *string
+       Domain                   *string
+       RegexSetNumber           *string
+       OriginShield             *string
+       ProfileID                *int
+       Protocol                 *int
+       AnonymousBlockingEnabled *bool
+       Active                   bool
+}
+
+func MakeRemapDotConfig(
+       serverName tc.CacheName,
+       toToolName string, // tm.toolname global parameter (TODO: cache itself?)
+       toURL string, // tm.url global parameter (TODO: cache itself?)
+       atsMajorVersion int,
+       cacheURLConfigParams map[string]string, // map[name]value for this 
server's profile and config file 'cacheurl.config'
+       dsProfilesCacheKeyConfigParams map[int]map[string]string, // 
map[profileID][paramName]paramVal for this server's profile, config file 
'cachekey.config'
+       serverPackageParamData map[string]string, // map[paramName]paramVal for 
this server, config file 'package'
+       serverInfo *ServerInfo, // ServerInfo for this server
+       remapDSData []RemapConfigDSData,
+) string {
+       hdr := GenericHeaderComment(string(serverName), toToolName, toURL)
+       text := ""
+       if tc.CacheTypeFromString(serverInfo.Type) == tc.CacheTypeMid {
+               text = GetServerConfigRemapDotConfigForMid(atsMajorVersion, 
dsProfilesCacheKeyConfigParams, serverInfo, remapDSData, hdr)
+       } else {
+               text = 
GetServerConfigRemapDotConfigForEdge(cacheURLConfigParams, 
dsProfilesCacheKeyConfigParams, serverPackageParamData, serverInfo, 
remapDSData, atsMajorVersion, hdr)
+       }
+       return text
+}
+
+func GetServerConfigRemapDotConfigForMid(
+       atsMajorVersion int,
+       profilesCacheKeyConfigParams map[int]map[string]string,
+       server *ServerInfo,
+       dses []RemapConfigDSData,
+       header string,
+) string {
+       midRemaps := map[string]string{}
+       for _, ds := range dses {
+               if ds.Type.IsLive() && !ds.Type.IsNational() {
+                       continue // Live local delivery services skip mids
+               }
+
+               if ds.OriginFQDN == nil || *ds.OriginFQDN == "" {
+                       log.Warnf("GetServerConfigRemapDotConfigForMid ds '" + 
ds.Name + "' has no origin fqdn, skipping!") // TODO confirm - Perl uses 
without checking!
+                       continue
+               }
+
+               if midRemaps[*ds.OriginFQDN] != "" {
+                       continue // skip remap rules from extra HOST_REGEXP 
entries
+               }
+
+               midRemap := ""
+               if ds.MidHeaderRewrite != nil && *ds.MidHeaderRewrite != "" {
+                       midRemap += ` @plugin=header_rewrite.so @pparam=` + 
MidHeaderRewriteConfigFileName(ds.Name)
+               }
+               if ds.QStringIgnore != nil && *ds.QStringIgnore == 
tc.QueryStringIgnoreIgnoreInCacheKeyAndPassUp {
+                       midRemap += GetQStringIgnoreRemap(atsMajorVersion)
+               }
+               if ds.CacheURL != nil && *ds.CacheURL != "" {
+                       midRemap += ` @plugin=cacheurl.so @pparam=` + 
CacheURLConfigFileName(ds.Name)
+               }
+
+               if ds.ProfileID != nil && 
len(profilesCacheKeyConfigParams[*ds.ProfileID]) > 0 {
+                       midRemap += ` @plugin=cachekey.so`
+                       for name, val := range 
profilesCacheKeyConfigParams[*ds.ProfileID] {
+                               midRemap += ` @pparam=--` + name + "=" + val
+                       }
+               }
+               if ds.RangeRequestHandling != nil && *ds.RangeRequestHandling 
== tc.RangeRequestHandlingCacheRangeRequest {
+                       midRemap += ` @plugin=cache_range_requests.so`
+               }
+
+               if midRemap != "" {
+                       midRemaps[*ds.OriginFQDN] = midRemap
+               }
+       }
+
+       textLines := []string{}
+       for originFQDN, midRemap := range midRemaps {
+               textLines = append(textLines, "map "+originFQDN+" 
"+originFQDN+midRemap+"\n")
+       }
+       sort.Sort(sort.StringSlice(textLines))
+
+       text := header
+       for _, line := range textLines {
+               text += line
+       }
+       return text
+}
+
+func GetServerConfigRemapDotConfigForEdge(
+       cacheURLConfigParams map[string]string,
+       profilesCacheKeyConfigParams map[int]map[string]string,
+       serverPackageParamData map[string]string, // map[paramName]paramVal for 
this server, config file 'package'
+       server *ServerInfo,
+       dses []RemapConfigDSData,
+       atsMajorVersion int,
+       header string,
+) string {
+       for name, val := range serverPackageParamData {
+               serverPackageParamData[name] = strings.Replace(val, 
"__HOSTNAME__", server.HostName+"."+server.DomainName, -1)
+       }
+
+       textLines := []string{}
+
+       for _, ds := range dses {
+               remapText := ""
+               if ds.Type == tc.DSTypeAnyMap {
+                       if ds.RemapText == nil {
+                               log.Errorln("ds '" + ds.Name + "' is ANY_MAP, 
but has no remap text - skipping")
+                               continue
+                       }
+                       remapText = *ds.RemapText + "\n"
+                       textLines = append(textLines, remapText)
+                       continue
+               }
+
+               remapLines, err := MakeEdgeDSDataRemapLines(ds, server)
+               if err != nil {
+                       log.Errorln("making remap lines for DS '" + ds.Name + 
"' - skipping! : " + err.Error())
+                       continue
+               }
+
+               for _, line := range remapLines {
+                       profilecacheKeyConfigParams := (map[string]string)(nil)
+                       if ds.ProfileID != nil {
+                               profilecacheKeyConfigParams = 
profilesCacheKeyConfigParams[*ds.ProfileID]
+                       }
+                       remapText = BuildRemapLine(cacheURLConfigParams, 
atsMajorVersion, server, serverPackageParamData, remapText, ds, line.From, 
line.To, profilecacheKeyConfigParams)
+               }
+               textLines = append(textLines, remapText)
+       }
+
+       text := header
+       sort.Sort(sort.StringSlice(textLines))
+       for _, line := range textLines {
+               text += line
+       }
+       return text
+}
+
+// BuildRemapLine builds the remap line for the given server and delivery 
service.
+// The cacheKeyConfigParams map may be nil, if this ds profile had no cache 
key config params.
+func BuildRemapLine(cacheURLConfigParams map[string]string, atsMajorVersion 
int, server *ServerInfo, pData map[string]string, text string, ds 
RemapConfigDSData, mapFrom string, mapTo string, cacheKeyConfigParams 
map[string]string) string {
+       // ds = 'remap' in perl
+
+       mapFrom = strings.Replace(mapFrom, `__http__`, server.HostName, -1)
+
+       if _, hasDSCPRemap := pData["dscp_remap"]; hasDSCPRemap {
+               text += "map    " + mapFrom + "     " + mapTo + ` 
@plugin=dscp_remap.so @pparam=` + strconv.Itoa(ds.DSCP)
+       } else {
+               text += "map    " + mapFrom + "     " + mapTo + ` 
@plugin=header_rewrite.so @pparam=dscp/set_dscp_` + strconv.Itoa(ds.DSCP) + 
".config"
+       }
+
+       if ds.EdgeHeaderRewrite != nil && *ds.EdgeHeaderRewrite != "" {
+               text += ` @plugin=header_rewrite.so @pparam=` + 
EdgeHeaderRewriteConfigFileName(ds.Name)
+       }
+
+       if ds.SigningAlgorithm != nil && *ds.SigningAlgorithm != "" {
+               if *ds.SigningAlgorithm == tc.SigningAlgorithmURLSig {
+                       text += ` @plugin=url_sig.so @pparam=url_sig_` + 
ds.Name + ".config"
+               } else if *ds.SigningAlgorithm == tc.SigningAlgorithmURISigning 
{
+                       text += ` @plugin=uri_signing.so @pparam=uri_signing_` 
+ ds.Name + ".config"
+               }
+       }
+
+       if ds.QStringIgnore != nil {
+               if *ds.QStringIgnore == tc.QueryStringIgnoreDropAtEdge {
+                       dqsFile := "drop_qstring.config"
+                       text += ` @plugin=regex_remap.so @pparam=` + dqsFile
+               } else if *ds.QStringIgnore == 
tc.QueryStringIgnoreIgnoreInCacheKeyAndPassUp {
+                       if _, globalExists := cacheURLConfigParams["location"]; 
globalExists {
+                               log.Debugln("qstring_ignore == 1, but global 
cacheurl.config param exists, so skipping remap rename 
config_file=cacheurl.config parameter") // TODO warn? Perl was a debug
 
 Review comment:
   Is debug the right place to log this? This is a pretty serious configuration 
snafu. The DS is configured to ignore the query string, but this is ignoring 
the request to ignore it. I worry that a debug log will wind up ignored.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to