BBlack has submitted this change and it was merged.

Change subject: move netmapper processing to common VCL
......................................................................


move netmapper processing to common VCL

(Note, this was originally these 3 commits
f092ee39eb89caf71fd34bd3a1f49e89b7a07e71
941b3e6e97f0b09954f6eb218b358f237196ebdb
e73eab0d80eeca86a764b8e689a8cba2e943edea
Then was reverted along with later stuff in:
e37e79857a6457578348451216d8efdfa465a814
, now re-applied)

This moves the netmapper "carriers" and "proxies" database lookups
to common VCL across all clusters, and integrates them with the
early vcl_recv IP processing done there to generate a standard set
of headers related to client IPs, trusted proxies, carriers, etc.

Note that new header names were invented here for the netmapper
results: "X-Carrier" contains MCC-MNC, and "X-Carrier-Meta"
contains the zeronet metadata like "wap".  Combined, these are
what what Zero calls "X-CS2".  "X-Trusted-Proxy" also contains
what Zero has traditionally calls "X-Forwarded-By".

This allows for an easier transition of the zero.inc.vcl code to
use the headers supplied from the common VCL, which copies the
data to its legacy headers and then possibly further
manipulates/uses/deletes those legacy headers.

Doing that also removes any fears about the sudden appearance of
previously mobile/zero -specific headers in text/upload requests
causing issues at the MediaWiki layer.

Bug: T89177
Bug: T96847
Bug: T109286
Change-Id: I8479f147a9c92cdba2733fcb8f1f92dec0bfaa47
---
M modules/varnish/templates/vcl/wikimedia.vcl.erb
M templates/varnish/zero.inc.vcl.erb
2 files changed, 167 insertions(+), 145 deletions(-)

Approvals:
  BBlack: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/modules/varnish/templates/vcl/wikimedia.vcl.erb 
b/modules/varnish/templates/vcl/wikimedia.vcl.erb
index 1257de3..7427e34 100644
--- a/modules/varnish/templates/vcl/wikimedia.vcl.erb
+++ b/modules/varnish/templates/vcl/wikimedia.vcl.erb
@@ -7,6 +7,11 @@
 #   being set from different code.
 import header;
 
+<% if @vcl_config.fetch("layer", "") == "frontend" -%>
+# only used in recv_fe_ip_processing on frontends
+import netmapper;
+<% end %>
+
 <%
 def backend_option(backend, option, default)
        if @varnish_backend_options.kind_of?(Array)
@@ -358,30 +363,143 @@
        }
 }
 
-// XFF / XFP / XRIP -handling (order is important below, should be at start of 
recv)
-// TODO: Integration with zero-like XFF-handling, to skip over authorized 
proxies for XRIP
-sub recv_ip_processing {
-       // Everything here is req-mangling based on the req
-       // itself, and some parts are non-idempotent, so do not
-       // re-run this on a request restart
+<% if @vcl_config.fetch("layer", "") == "frontend" -%>
+// Must be done at the top of vcl_recv, in our varnish-frontend layer only,
+// and should be guarded against running on request restarts.
+sub recv_fe_ip_processing {
+       // this subroutine "owns" these 4 headers - nothing else in our VCL or
+       // anywhere in our network should be setting them.
+       unset req.http.X-Trusted-Proxy;
+       unset req.http.X-Client-IP;
+       unset req.http.X-Carrier;
+       unset req.http.X-Carrier-Meta;
+
+       if (client.ip !~ wikimedia_nets) {
+               // Ensure we only accept XFP from our own networks.  Ideally
+               // it should only be set by our nginx TLS terminator
+               // specifically, but there are known cases where internal apps
+               // set XFP to fake HTTPS when making a request to our public
+               // endpoints from the inside.
+               unset req.http.X-Forwarded-Proto;
+               // only the nginx TLS terminator should be setting this one at
+               // all - there are no other internal exceptions to that rule
+               unset req.http.X-Real-IP;
+       }
+
+       // There are two possible cases here: either nginx acted as our TLS
+       // proxy and already set X-Real-IP (as well as appended the same value
+       // as XFF), or the traffic was direct to varnish-fe, in which case
+       // XRIP is not yet set and XFF is directly from external.
+       if (!req.http.X-Real-IP) {
+               // direct-to-port-80 case, set XRIP ourselves
+               set req.http.X-Real-IP = client.ip;
+               set req.http.X-Trusted-Proxy = netmapper.map("proxies", 
req.http.X-Real-IP);
+               // normalize to boolean post-netmapper (varnish-3.0.4...)
+               if (req.http.X-Trusted-Proxy == "") {
+                       unset req.http.X-Trusted-Proxy;
+               }
+               if (req.http.X-Trusted-Proxy && req.http.X-Forwarded-For) {
+                       // get last from trusted-proxy-supplied XFF
+                       set req.http.X-Client-IP = 
regsub(req.http.X-Forwarded-For, "^([^,]+, ?)+", "");
+               }
+       } else {
+               // XRIP from nginx, XFF assumed to be set and contain at least
+               // XRIP at the end, possibly prepended by other addrs set
+               // externally by some proxy.
+               set req.http.X-Trusted-Proxy = netmapper.map("proxies", 
req.http.X-Real-IP);
+               // normalize to boolean post-netmapper (varnish-3.0.4...)
+               if (req.http.X-Trusted-Proxy == "") {
+                       unset req.http.X-Trusted-Proxy;
+               }
+               if (req.http.X-Trusted-Proxy) {
+                       // strip off final XFF entry (should alias XRIP, both 
set
+                       // by nginx) to set xffce, then take the final entry
+                       // of xffce as the client IP.  Note that if the
+                       // trusted proxy did not set an XFF header nginx would
+                       // have set it to just XRIP with no commas, and the
+                       // regsubs would result in setting XCIP = XRIP.
+                       set req.http.xffce = regsub(req.http.X-Forwarded-For, 
",[^,]+$", "");
+                       set req.http.X-Client-IP = regsub(req.http.xffce, 
"^([^,]+, ?)+", "");
+                       unset req.http.xffce; // clean up temp var
+               }
+       }
+
+       // If XCIP not set above, or looks invalid due to bad external XFF
+       // input, just alias to XRIP
+       if (!req.http.X-Client-IP || req.http.X-Client-IP !~ 
"^[0-9a-fA-F:.]+$") {
+               set req.http.X-Client-IP = req.http.X-Real-IP;
+       }
+
+       // Now check carrier database for setting X-Carrier based on XCIP
+       set req.http.X-Carrier = netmapper.map("carriers", 
req.http.X-Client-IP);
+       // normalize to boolean post-netmapper (varnish-3.0.4...)
+       if (req.http.X-Carrier == "") {
+               unset req.http.X-Carrier;
+       }
+       else {
+               // Split X-Carrier data from raw form with optional trailing 
metadata,
+               // such as "123-45|wap|mobile", so that X-Carrier contains only
+               // MCC-MNC and X-Carrier-Meta contains the trailing attributes
+               set req.http.X-Carrier-Meta = regsub(req.http.X-Carrier, 
"^[^|]*\|?", "");
+               if (req.http.X-Carrier-Meta != "") {
+                       set req.http.X-Carrier = regsub(req.http.X-Carrier, 
"\|.*$", "");
+               }
+               else {
+                       unset req.http.X-Carrier-Meta;
+               }
+       }
+
+       // From this (very early) point forward, regardless of cache tier/layer:
+       // client.ip   ->
+       //     the network-level source address, hop-by-hop - could be an
+       //     internal address within our infrastructure as we traverse
+       //     various cache/proxy layers.
+       // req.http.X-Real-IP   ->
+       //     the network-level source address when this request first
+       //     entered our public traffic infrastructure at the edge, with no
+       //     other decoding.  Could still be a trusted external proxy.
+       // req.http.X-Trusted-Proxy ->
+       //     If X-Real-IP mapped to the address of a trusted proxy in our
+       //     "proxies" database (such as OperaMini), this will be the
+       //     official name of the trusted proxy.  Otherwise it will be unset
+       //     (boolean false).
+       // req.http.X-Client-IP ->
+       //     Iff XTP above is set, *and* the trusted-proxy-supplied X-F-F
+       //     had a legitimate-looking address at the end, X-Client-IP will
+       //     contain the claimed client IP directly behind the trusted
+       //     proxy.  Otherwise this will alias X-Real-IP.
+       // req.http.X-Carrier ->
+       //     If X-Client-IP matches a network in our "carriers" database,
+       //     this will contain the MCC-MNC code for that carrier.  Otherwise
+       //     it will be undefined.
+       // req.http.X-Carrier-Meta ->
+       //     If X-Carrier is defined: for some carriers, the database
+       //     contains extra metadata in the form of one or more labels like
+       //     "wap" or "residential".  They'll be separated by "|" if more
+       //     than one, and this header is undefined if there was no such
+       //     metadata.
+}
+
+<% end %>
+
+sub vcl_init {
+<% if @vcl_config.fetch("layer", "") == "frontend" -%>
+       // again, netmapper only used in frontends, for recv_fe_ip_processing
+       // args here are map-name (for .map()), data file, and seconds between 
mtime checks for reload
+       netmapper.init("proxies", "/var/netmapper/proxies.json", 89);
+       netmapper.init("carriers", "/var/netmapper/carriers.json", 89);
+<% end %>
+}
+
+sub vcl_recv {
+       // IP processing is req->req mangling that shouldn't be re-done on
+       // restart, and XFF-appending is non-idempotent for restart purposes..
        if (req.restarts == 0) {
-               if (client.ip !~ wikimedia_nets) {
-                       // Ensure we only accept XFP and XRIP from our nets.
-                       // Note this *should* be just the local tlsproxy
-                       // terminator, or passing through layers of varnish,
-                       // but we have known cases where applayer internal
-                       // things are faking XFP...
-                       unset req.http.X-Forwarded-Proto;
-                       unset req.http.X-Real-IP;
-               }
-
-               // Ensure XRIP is always set, in the case of varnish-fe and a
-               // direct external request (otherwise nginx will have set it).
-               if (!req.http.X-Real-IP) {
-                       set req.http.X-Real-IP = client.ip;
-               }
-
-               // All proxies need to update XFF with client.ip hop-by-hop
+<% if @vcl_config.fetch("layer", "") == "frontend" -%>
+               call recv_fe_ip_processing;
+<% end %>
+               // All layers need to update XFF with client.ip hop-by-hop so 
that it
+               // looks right to layers beneath, including the app layer
                if (req.http.X-Forwarded-For) {
                        set req.http.X-Forwarded-For = req.http.X-Forwarded-For 
+ ", " + client.ip;
                } else {
@@ -389,18 +507,7 @@
                }
        }
 
-       // From this (very early) point forward:
-       // client.ip -> the network-level source address, hop-by-hop
-       // X-Real-IP -> the network-level source address when this
-       // request first entered our infrastructure at the edge, without any
-       // regard to external XFF header processing, and does not become
-       // "wrong" from traversing our internal proxy layers.
-}
-
-sub vcl_recv {
-       call recv_ip_processing;
-
-<% if @vcl_config.fetch("layer", "frontend") != "frontend" -%>
+<% if @vcl_config.fetch("layer", "") != "frontend" -%>
        if (client.ip !~ wikimedia_nets) {
                // Do not allow direct access to non-frontend layers
                error 403 "Access denied";
diff --git a/templates/varnish/zero.inc.vcl.erb 
b/templates/varnish/zero.inc.vcl.erb
index 2e005ab..58968e9 100644
--- a/templates/varnish/zero.inc.vcl.erb
+++ b/templates/varnish/zero.inc.vcl.erb
@@ -2,73 +2,19 @@
 
 // Note: This requires "import header" in the including VCL
 
-import netmapper;
-
-sub vcl_init {
-    // args here are map-name (for .map()), data file, and seconds between 
mtime checks for reload
-    netmapper.init("carriers", "/var/netmapper/carriers.json", 89);
-    netmapper.init("proxies", "/var/netmapper/proxies.json", 89);
-}
-
 sub tag_carrier {
-    // This is what things look like on entry in four basic scenarios
-    // ("both" here means something MITM-proxied the SSL connection, like 
OperaMini,
-    //   but was kind enough to set an XFF header on the way)
-    // (the leading "..." in XFF is that sometimes there are other local
-    //   proxies on 127.0.0.1 or whatever, according to some OperaMini docs...)
+    // XTP and XC are set up in the common wikimedia.vcl now from netmapper 
data
 
-    // *********************************************************
-    // *    v> | client.ip | XFF                        | XFP
-    // * direct| client    | (...,)? client             |
-    // * ssl   | ssl       | (...,)? client, ssl        | https
-    // * proxy | proxy     | (...,)? client, proxy      |
-    // * both  | ssl       | (...,)? client, proxy, ssl | https
-    // *********************************************************
+    // Use standard XTP to set the tag_carrier-specific XFB header...
+    set req.http.X-Forwarded-By = req.http.X-Trusted-Proxy;
 
-    // first, strip the SSL entry from XFF if applicable
-    if (req.http.X-Forwarded-Proto) {
-        set req.http.XFF-NoSSL = regsub(req.http.X-Forwarded-For, ",[^,]+$", 
""); // strips final entry
-    } else {
-        set req.http.XFF-NoSSL = req.http.X-Forwarded-For;
+    // Use standard XC/XCM to set the tag_carrier-specific X-CS2 header...
+    if (req.http.X-Carrier-Meta) {
+        set req.http.X-CS2 = req.http.X-Carrier + "|" + 
req.http.X-Carrier-Meta;
     }
-
-    // 
*****************************************************************************
-    // *    v>      | XFF-NoSSL             | map("proxies") input from regsub 
below
-    // * direct/ssl | (...,)? client        | client
-    // * proxy/both | (...,)? client, proxy | proxy
-    // 
*****************************************************************************
-
-    // now get the trusted proxy into XFB, if any - the regsub grabs the final 
entry in the list
-    set req.http.X-Forwarded-By = netmapper.map("proxies", 
regsub(req.http.XFF-NoSSL, "^([^,]+, ?)+", ""));
-
-    // normalize to boolean again...
-    if (req.http.X-Forwarded-By == "") {
-        unset req.http.X-Forwarded-By;
+    else {
+        set req.http.X-CS2 = req.http.X-Carrier;
     }
-
-    // now, iff XFB was set, strip away one more layer to put the supposed 
client IP at the end
-    if (req.http.X-Forwarded-By) {
-        set req.http.XFF-ClientEnd = regsub(req.http.XFF-NoSSL, ",[^,]+$", "");
-    } else {
-        set req.http.XFF-ClientEnd = req.http.XFF-NoSSL;
-    }
-
-    unset req.http.XFF-NoSSL; // clean up temp var
-
-    // ****************************************************************
-    // *    v>  | XFF-CE         | map("carriers") input from regsub below
-    // *   all  | (...,)? client | client
-    // *****************************************************************
-
-    // Grab client IP from the end of the XFF-CE list and feed to map("zero")
-    set req.http.X-CS2 = netmapper.map("carriers", 
regsub(req.http.XFF-ClientEnd, "^([^,]+, ?)+", ""));
-
-    // normalize to boolean (varnish-3.0.4...)
-    if (req.http.X-CS2 == "") {
-        unset req.http.X-CS2;
-    }
-
-    unset req.http.XFF-ClientEnd; // clean up temp var
 
     // Now the per-carrier stuff...
     // Short of having netmapper (or equivalent) return a set of match 
parameters, I can't see any
@@ -115,55 +61,24 @@
     }
 
     // Prepare for outbound X-CS and X-Analytics header assembly.
-    // See following section where actual outbound X-CS and X-Analytics are 
set.
-    // Some of the headers used for X-Analytics are not varied on, so add them 
after the backend processing.
-    // Note that vcl_deliver in other files may also modify X-Analytics
-    if (req.http.X-CS == "ON") {
-        set req.http.X-CS = req.http.X-CS2;
-    }
+    if (req.http.X-CS2) {
+       // Regardless of X-CS transform to "ON" for Vary and/or server-side
+       // purposes, if a carrier was detected at all, set X-CS as an outbound
+       // response header for the application to consume.
+       set resp.http.X-CS = req.http.X-CS2;
 
-    // Now that we've updated the req.http.X-CS for postprocessing, set 
outbound headers
-    // and X-Analytics.
-    if (req.http.X-CS) {
-
-        // All mobile responses need to have the X-CS set if it came from a 
zero network.
-        // This allows apps to detect when we switch from zero to non-zero and 
back,
-        // or when the user switches from one network to another and both are 
zero (multi-SIM phones).
-        // The client should constantly check each response for this header, 
and if it changes,
-        // re-request the partner's message info.
-        // This value is only based on Varnish's zero detection, not the 
backend processing of any sort.
-        // When apps see an outbound X-CS, they ask the API for the formal 
enabled/disabled status in a JSON object.
-        // We are in vcl_deliver, so setting the resp.http.X-CS should not 
impact caching; it's postprocessing.
-        set resp.http.X-CS = req.http.X-CS;
-
-        // Some carriers have multiple ip ranges with different rules. IP set 
name is added at
-        // the end of the X-CS, e.g. "123-45|wap" to indicate a wap gateway 
with their own IPs.
-        // To minimize analytics impact, the 'zero' value in X-Analytics will 
stay as carrier id,
-        // and the additional suffix will go into the 'zeronet=xxx' if it was 
present.
-
-        // The following is strictly related to X-Analytics, not outbound X-CS 
for apps.
-        // Remove everything before and including the first '|' char
-        set req.http.X-ZEROSFX = regsub(req.http.X-CS, "^[^\|]*\|?", "");
-        if (req.http.X-ZEROSFX != "") {
-            // Remove everything on and after the first '|' char
-            set req.http.X-CS = "zero=" + regsub(req.http.X-CS, "\|.*", "") + 
";zeronet=" + req.http.X-ZEROSFX;
-        } else if (req.http.X-CS == "404-01b") {
-            // For a short time we had this workaround. This "else if" can be 
removed after 2014-10-15
-            // We only had one carrier, "404-01b", whom we temporarily had to 
create an extra entry until backend was ready.
-            // In order for analytics to report correct stats, we used to 
remove "b" before modifying X-Analytics.
-            set req.http.X-CS = "zero=404-01;zeronet=b";
-        } else {
-            set req.http.X-CS = "zero=" + req.http.X-CS;
-        }
-
-        if (resp.http.X-Analytics) {
-            set resp.http.X-Analytics = resp.http.X-Analytics + ";" + 
req.http.X-CS;
-        } else {
-            set resp.http.X-Analytics = req.http.X-CS;
-        }
-    } else {
-        // Let's ensure no outbound X-CS if pre-analytics X-CS (as determined 
in corresponding if) was not set.
-        unset resp.http.X-CS;
+       // X-C and X-C-M are set/split in the common recv code in
+       // wikimedia.vcl; X-CS2 is aggregated from them in recv
+       if (req.http.X-Carrier) {
+               if (resp.http.X-Analytics) {
+                       set resp.http.X-Analytics = resp.http.X-Analytics + 
";zero=" + req.http.X-Carrier;
+               } else {
+                       set resp.http.X-Analytics = "zero=" + 
req.http.X-Carrier;
+               }
+               if (req.http.X-Carrier-Meta) {
+                       set resp.http.X-Analytics = resp.http.X-Analytics + 
";zeronet=" + req.http.X-Carrier-Meta;
+               }
+       }
     }
     if (req.http.X-Forwarded-By2) {
         if (resp.http.X-Analytics) {

-- 
To view, visit https://gerrit.wikimedia.org/r/243976
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I8479f147a9c92cdba2733fcb8f1f92dec0bfaa47
Gerrit-PatchSet: 2
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: BBlack <[email protected]>
Gerrit-Reviewer: BBlack <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to