This is an automated email from the ASF dual-hosted git repository.

rob pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficcontrol.git


The following commit(s) were added to refs/heads/master by this push:
     new 053e9fb9d6 add support for __REGEX_REMAP_DIRECTIVE__ to remap.config 
processing (#6993)
053e9fb9d6 is described below

commit 053e9fb9d66d0e75baea7b7559d72e8042511ee2
Author: Brian Olsen <[email protected]>
AuthorDate: Mon Aug 1 10:21:31 2022 -0600

    add support for __REGEX_REMAP_DIRECTIVE__ to remap.config processing (#6993)
    
    * add support for __REGEX_REMAP_DIRECTIVE__ to remap.config processing
    
    * fix docs table
    
    * prefix table with tabs
---
 CHANGELOG.md                               |   1 +
 docs/source/overview/delivery_services.rst |  16 +++-
 lib/go-atscfg/remapdotconfig.go            |  20 +++--
 lib/go-atscfg/remapdotconfig_test.go       | 133 +++++++++++++++++++++++++++++
 4 files changed, 163 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index efff8be2bd..29699346da 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,6 +32,7 @@ The format is based on [Keep a 
Changelog](http://keepachangelog.com/en/1.0.0/).
 - Change to t3c diff to flag a config file for replacement if owner/group 
settings are not `ats` 
[#6879](https://github.com/apache/trafficcontrol/issues/6879).
 - t3c now looks in the executable dir path for t3c- utilities
 - Added support for parent.config markdown/retry DS parameters using 
first./inner./last. prefixes.  mso. and <null> prefixes should be deprecated.
+- Add new __REGEX_REMAP_DIRECTIVE__ support to raw remap text to allow moving 
the regex_remap placement.
 
 ### Fixed
 - Fixed TO to default route ID to 0, if it is not present in the request 
context.
diff --git a/docs/source/overview/delivery_services.rst 
b/docs/source/overview/delivery_services.rst
index d043383f35..e08b3d4f34 100644
--- a/docs/source/overview/delivery_services.rst
+++ b/docs/source/overview/delivery_services.rst
@@ -675,11 +675,23 @@ For HTTP and DNS-:ref:`Routed <ds-types>` Delivery 
Services, this will be added
 Directives
 """
 
-The Raw Remap text is ordinarily added at the end of the line, after 
everything else. However, it may be necessary to add Range Request Handling 
after the Raw Remap. For example, if you have a plugin which manipulates the 
Range header. In this case, you can insert the text ``__RANGE_DIRECTIVE__`` in 
the Raw Remap text, and the range request handling directives will be added at 
that point.
+The Raw Remap text is ordinarily added at the end of the line, after 
everything else. However, it may be necessary to change the normal arg 
ordering, especially if the user needs to modify the cachekey, range headers or 
URI in a way that may change cachekey or regex_remap behaviors.
+
+It may be necessary to add Range Request Handling after the Raw Remap. For 
example, if you have a plugin which manipulates the Range header. In this case, 
you can insert the text ``__RANGE_DIRECTIVE__`` in the Raw Remap text, and the 
range request handling directives will be added at that point.
 
 For example, if you have an Apache Traffic Server lua plugin which manipulates 
the range, and are using Slice Range Request Handling which needs to run after 
your plugin, you can set a Raw Remap, ``@plugin=tslua.so @pparam=range.lua 
__RANGE_DIRECTIVE__``, and the ``@plugin=slice.so`` range directive will be 
inserted after your plugin.
 
-Similarly the text ``__CACHEKEY_DIRECTIVE__`` can be moved into the Raw Remap 
text to allow the Raw Remap text to manipulate the uri contents before the 
cachekey is generated.
+Another example might be a Delivery Service which modifies the uri in a way 
that changes the cachey key (cachekey), and affects parent routing 
(regex_remap) with the possibility of range request handling via background 
fetch.  Raw Remap Text might then look like: ``@plugin=tslua.so 
@pparam=uri-manip.lua __CACHEKEY_DIRECTIVE__ __REGEX_REMAP_DIRECTIVE__ 
__RANGE_DIRECTIVE__``.  This would set things up such that background_fetch 
would issue a request to the proper remap parent.
+
+.. table:: Supported Raw Remap Directives
+
+       
+---------------------------+-------------------------------------------+
+       | Name                      | Use(s)                                    
|
+       
+===========================+===========================================+
+       | __CACHEKEY_DIRECTIVE__    | Inserts cachekey plugin and args (if any) 
|
+       | __RANGE_DIRECTIVE__       | Inserts range directive args (if any)     
|
+       | __REGEX_REMAP_DIRECTIVE__ | Inserts regex_remap directive (if any)    
|
+       
+---------------------------+-------------------------------------------+
 
 .. _ds-regex-remap:
 
diff --git a/lib/go-atscfg/remapdotconfig.go b/lib/go-atscfg/remapdotconfig.go
index bf7aab24bd..4c82418894 100644
--- a/lib/go-atscfg/remapdotconfig.go
+++ b/lib/go-atscfg/remapdotconfig.go
@@ -35,8 +35,9 @@ const CacheKeyParameterConfigFile = "cachekey.config"
 const ContentTypeRemapDotConfig = ContentTypeTextASCII
 const LineCommentRemapDotConfig = LineCommentHash
 
-const RemapConfigRangeDirective = `__RANGE_DIRECTIVE__`
 const RemapConfigCachekeyDirective = `__CACHEKEY_DIRECTIVE__`
+const RemapConfigRangeDirective = `__RANGE_DIRECTIVE__`
+const RemapConfigRegexRemapDirective = `__REGEX_REMAP_DIRECTIVE__`
 
 // RemapDotConfigOpts contains settings to configure generation options.
 type RemapDotConfigOpts struct {
@@ -558,7 +559,7 @@ func buildEdgeRemapLine(
                }
        }
 
-       // This allows the Range and Cachekey directive hacks
+       // Raw remap text, this allows the directive hacks
        remapText := ""
        if ds.RemapText != nil {
                remapText = *ds.RemapText
@@ -586,16 +587,25 @@ func buildEdgeRemapLine(
                cachekeyTxt = " @plugin=cachekey.so" + cachekeyArgs
        }
 
-       // Temporary hack for moving the cachekey directive into the raw remap 
text
+       // Hack for moving the cachekey directive into the raw remap text
        if strings.Contains(remapText, RemapConfigCachekeyDirective) {
                remapText = strings.Replace(remapText, 
RemapConfigCachekeyDirective, cachekeyTxt, 1)
        } else {
                text += cachekeyTxt
        }
 
+       regexRemapTxt := ""
+
        // Note: should use full path here?
        if ds.RegexRemap != nil && *ds.RegexRemap != "" {
-               text += ` @plugin=regex_remap.so @pparam=regex_remap_` + 
*ds.XMLID + ".config"
+               regexRemapTxt = ` @plugin=regex_remap.so @pparam=regex_remap_` 
+ *ds.XMLID + ".config"
+       }
+
+       // Hack for moving the regex_remap directive into the raw remap text
+       if strings.Contains(remapText, RemapConfigRegexRemapDirective) {
+               remapText = strings.Replace(remapText, 
RemapConfigRegexRemapDirective, regexRemapTxt, 1)
+       } else {
+               text += regexRemapTxt
        }
 
        rangeReqTxt := ""
@@ -620,7 +630,7 @@ func buildEdgeRemapLine(
                }
        }
 
-       // Temporary hack for moving the range directive into the raw remap text
+       // Hack for moving the range directive into the raw remap text
        if strings.Contains(remapText, RemapConfigRangeDirective) {
                remapText = strings.Replace(remapText, 
RemapConfigRangeDirective, rangeReqTxt, 1)
        } else {
diff --git a/lib/go-atscfg/remapdotconfig_test.go 
b/lib/go-atscfg/remapdotconfig_test.go
index 20bfa4fe0c..ef838ad864 100644
--- a/lib/go-atscfg/remapdotconfig_test.go
+++ b/lib/go-atscfg/remapdotconfig_test.go
@@ -5859,6 +5859,139 @@ func TestMakeRemapDotConfigRawRemapCachekeyDirective(t 
*testing.T) {
        }
 }
 
+func TestMakeRemapDotConfigRawRemapRegexRemapDirective(t *testing.T) {
+       hdr := "myHeaderComment"
+
+       server := makeTestRemapServer()
+       server.Type = "EDGE"
+
+       ds := DeliveryService{}
+       ds.ID = util.IntPtr(48)
+       dsType := tc.DSType("HTTP_LIVE_NATNL")
+       ds.Type = &dsType
+       ds.OrgServerFQDN = util.StrPtr("origin.example.test")
+       ds.MidHeaderRewrite = util.StrPtr("")
+       ds.RemapText = util.StrPtr("@plugin=tslua.so 
@pparam=uri-manipulator.lua __REGEX_REMAP_DIRECTIVE__")
+       ds.EdgeHeaderRewrite = nil
+       ds.SigningAlgorithm = util.StrPtr("foo")
+       ds.XMLID = util.StrPtr("mydsname")
+       ds.QStringIgnore = 
util.IntPtr(int(tc.QueryStringIgnoreIgnoreInCacheKeyAndPassUp))
+       ds.RegexRemap = util.StrPtr("myregexremap")
+       ds.FQPacingRate = util.IntPtr(0)
+       ds.DSCP = util.IntPtr(0)
+       ds.RoutingName = util.StrPtr("myroutingname")
+       ds.MultiSiteOrigin = util.BoolPtr(false)
+       ds.OriginShield = util.StrPtr("myoriginshield")
+       ds.ProfileID = util.IntPtr(49)
+       ds.ProfileName = util.StrPtr("dsprofile")
+       ds.Protocol = util.IntPtr(int(tc.DSProtocolHTTPAndHTTPS))
+       ds.AnonymousBlockingEnabled = util.BoolPtr(false)
+       ds.Active = util.BoolPtr(true)
+       ds.RangeSliceBlockSize = util.IntPtr(262144)
+
+       dses := []DeliveryService{ds}
+
+       dss := []DeliveryServiceServer{
+               DeliveryServiceServer{
+                       Server:          *server.ID,
+                       DeliveryService: *ds.ID,
+               },
+       }
+
+       dsRegexes := []tc.DeliveryServiceRegexes{
+               tc.DeliveryServiceRegexes{
+                       DSName: *ds.XMLID,
+                       Regexes: []tc.DeliveryServiceRegex{
+                               tc.DeliveryServiceRegex{
+                                       Type:      
string(tc.DSMatchTypeHostRegex),
+                                       SetNumber: 0,
+                                       Pattern:   
`myliteralpattern__http__foo`,
+                               },
+                       },
+               },
+       }
+
+       serverParams := []tc.Parameter{
+               tc.Parameter{
+                       Name:       "trafficserver",
+                       ConfigFile: "package",
+                       Value:      "7",
+                       Profiles:   []byte(`["global"]`),
+               },
+               tc.Parameter{
+                       Name:       "serverpkgval",
+                       ConfigFile: "package",
+                       Value:      "serverpkgval __HOSTNAME__ foo",
+                       Profiles:   []byte(server.ProfileNames[0]),
+               },
+               tc.Parameter{
+                       Name:       "dscp_remap_no",
+                       ConfigFile: "package",
+                       Value:      "notused",
+                       Profiles:   []byte(server.ProfileNames[0]),
+               },
+       }
+
+       remapConfigParams := []tc.Parameter{
+               tc.Parameter{
+                       Name:       "not_location",
+                       ConfigFile: "cachekey.config",
+                       Value:      "notinconfig",
+                       Profiles:   []byte(`["global"]`),
+               },
+       }
+
+       cdn := &tc.CDN{
+               DomainName: "cdndomain.example",
+               Name:       "my-cdn-name",
+       }
+
+       topologies := []tc.Topology{}
+       cgs := []tc.CacheGroupNullable{}
+       serverCapabilities := map[int]map[ServerCapability]struct{}{}
+       dsRequiredCapabilities := map[int]map[ServerCapability]struct{}{}
+       configDir := `/opt/trafficserver/etc/trafficserver`
+
+       cfg, err := MakeRemapDotConfig(server, dses, dss, dsRegexes, 
serverParams, cdn, remapConfigParams, topologies, cgs, serverCapabilities, 
dsRequiredCapabilities, configDir, &RemapDotConfigOpts{HdrComment: hdr})
+       if err != nil {
+               t.Fatal(err)
+       }
+       txt := cfg.Text
+
+       txt = strings.TrimSpace(txt)
+
+       testComment(t, txt, hdr)
+
+       txtLines := strings.Split(txt, "\n")
+
+       if len(txtLines) != 4 { // 2 remaps plus header comment plus blank
+               t.Fatalf("expected a comment header, a blank line, and 2 remaps 
from HTTP_AND_HTTPS DS, actual: '%v' count %v", txt, len(txtLines))
+       }
+
+       remapLine := txtLines[2]
+
+       if !strings.HasPrefix(remapLine, "map") {
+               t.Errorf("expected to start with 'map', actual '%v'", txt)
+       }
+
+       if 1 != strings.Count(remapLine, "regex_remap.so") {
+               t.Errorf("expected remap on edge server to contain a single 
regex_remap plugin, actual '%v'", txt)
+       }
+
+       if !strings.Contains(remapLine, "@plugin=tslua.so 
@pparam=uri-manipulator.lua  @plugin=regex_remap.so") {
+               t.Errorf("expected regex_remap to come after tslua, actual 
'%v'", txt)
+       }
+       if strings.Contains(remapLine, "__REGEX_REMAP_DIRECTIVE__") {
+               t.Errorf("expected raw remap regex_remap directive to be 
replaced, actual '%v'", txt)
+       }
+       if count := strings.Count(remapLine, "regex_remap.so"); count != 1 { // 
Individual line should only have 1 regex_remap.so
+               t.Errorf("expected raw remap regex_remap directive to be 
replaced not duplicated, actual count %v '%v'", count, txt)
+       }
+       if count := strings.Count(txt, "regex_remap.so"); count != 2 { // All 
lines should have 2 regex_remap.so - HTTP and HTTPS lines
+               t.Errorf("expected raw remap regex_remap directive to have one 
regex_remap.so for HTTP and one for HTTPS remap, actual count %v '%v'", count, 
txt)
+       }
+}
+
 func TestMakeRemapDotConfigRawRemapWithoutRangeDirective(t *testing.T) {
        hdr := "myHeaderComment"
 

Reply via email to