BBlack has uploaded a new change for review.
https://gerrit.wikimedia.org/r/243395
Change subject: move netmapper processing to common VCL
......................................................................
move netmapper processing to common VCL
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 what Zero has traditionally called
"X-CS2", and "X-Trusted-Proxy" contains what Zero has
traditionally called "X-Forwarded-By" - this allows for an easier
transition of the zero.inc.vcl code to use the headers supplied
from the common VCL (it currently ends up deleting the XFB header
for its own reasons, for instance), which copies the data to its
legacy header names.
Doing that also removes any fears about the sudden appearance of
previously mobile/zero -specific headers appearing in text/upload
requests causing issues at the MediaWiki layer.
Bug: T89177
Bug: T96847
Bug: T109286
Change-Id: Ib92353d478c06548c4082af970f4561f2c6332f3
---
M modules/varnish/templates/vcl/wikimedia.vcl.erb
M templates/varnish/zero.inc.vcl.erb
2 files changed, 116 insertions(+), 93 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/operations/puppet
refs/changes/95/243395/1
diff --git a/modules/varnish/templates/vcl/wikimedia.vcl.erb
b/modules/varnish/templates/vcl/wikimedia.vcl.erb
index 1e2eb16..1b21bc0 100644
--- a/modules/varnish/templates/vcl/wikimedia.vcl.erb
+++ b/modules/varnish/templates/vcl/wikimedia.vcl.erb
@@ -7,6 +7,8 @@
# being set from different code.
import header;
+import netmapper;
+
<%
def backend_option(backend, option, default)
if @varnish_backend_options.kind_of?(Array)
@@ -358,47 +360,122 @@
}
}
-// 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
+// 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 3 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;
+
+ 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
+ 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
+ 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);
+
+ // 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 label for that carrier. Commonly this is
+ // just a raw MCC-MNC code, but sometimes has additional
+ // Zero-specific flags appended, e.g.: "123-45|wap|residential"
+}
+
+sub vcl_init {
+ // 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);
+}
+
+sub vcl_recv {
+ // IP processing is req->req mangling that doesn't need to 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") == "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 {
set req.http.X-Forwarded-For = client.ip;
}
}
-
- // 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 (client.ip !~ wikimedia_nets) {
diff --git a/templates/varnish/zero.inc.vcl.erb
b/templates/varnish/zero.inc.vcl.erb
index 2e005ab..2a28f47 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;
- }
-
- //
*****************************************************************************
- // * 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;
- }
-
- // 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, "^([^,]+, ?)+", ""));
+ // Use standard XC to set the tag_carrier-specific X-CS2 header...
+ set req.http.X-CS2 = req.http.X-Carrier;
// 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
--
To view, visit https://gerrit.wikimedia.org/r/243395
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib92353d478c06548c4082af970f4561f2c6332f3
Gerrit-PatchSet: 1
Gerrit-Project: operations/puppet
Gerrit-Branch: production
Gerrit-Owner: BBlack <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits