origin icon indicating copy to clipboard operation
origin copied to clipboard

SSL termination before Openshift (proxies chain) and X-Forwarded headers

Open strowk opened this issue 9 years ago • 15 comments

Hi!

We use Openshift Origin with separate haproxy that terminates ssl. Here is the basic scheme of connection flow: Client -> Haproxy1(SSL terminator) -> Haproxy2(Openshift Router) -> Container

Configuration for Haproxy1(SSL terminator) is very simple:

global
   log /dev/log local0

frontend webFrontend
bind *:443 ssl crt /etc/ssl/mytest.loc.pem
mode http
option http-server-close
option forwardfor
http-request set-header X-Forwarded-Proto https if { ssl_fc }
option httpclose
use_backend webBackend

backend webBackend
mode http
balance roundrobin
server shiftWebServer 127.0.0.1:180

It takes https from input and forward it as http to output appending forward headers to it. The one we are interested here is X-Forwarded-Proto, which's needed for application in container to create correct(https) links to another resources. So we have https between Client and Haproxy1 and http between Haproxy1 and Openshift Router: Client -(https)-> Haproxy1(SSL terminator) -(http)-> Haproxy2(Openshift Router) -> Container

The problem is that these lines https://github.com/openshift/origin/blob/release-1.3/images/router/haproxy/conf/haproxy-config.template#L289 (we use Openshift Origin 1.3) and the same in https://github.com/openshift/origin/blob/master/images/router/haproxy/conf/haproxy-config.template#L457 http-request set-header X-Forwarded-Proto http if !{ ssl_fc } configure haproxy to set X-Forwarded-Proto as http if incoming connection was http. "Set" here means delete existing header and append new one. No matter if there was already X-Forwarded-Proto or not. That means Openshift Router does not support such proxy chain correctly and I haven't found how this issue can be resolved another way without customization haproxy configuration of router.

I know that Openshift itself provide support of https, but the thing is that we want to have one place where SSL termination happens and we want it to be outside of Openshift in a separate environment. That's why we decided to configure additional outside haproxy as SSL terminator.

Now you can say that X-Forwarded-Proto is not good for chain of proxy at all, we should use Forwarded header, it has the standard - https://tools.ietf.org/html/rfc7239 In RFC we can read that "If the request is passing through several proxies, each proxy can add a set of parameters; it can also remove previously added "Forwarded" header fields." As I can see in both master and release 1.3 Forwarded as well as X-Forwarded-Proto being set, not added http-request set-header Forwarded for=%[src];host=%[req.hdr(host)];proto=%[req.hdr(X-Forwarded-Proto)]

So standard says we can do anything we like - add new Forwarded or change existing. Current Openshift Router configuration deletes all "forwarded" information from incoming request and information that client has https connection between browser and first proxy disappears.

I think some changes in the haproxy-config.template is required to resolve the issue. Would be great if we could for example configure another behavior as a mode "outside SSL termination" and that would cause Openshift to create another haproxy configuration where there is no such issue.

I think the most valuable information in X-Forwarded-Proto is a type of connection between browser and the first proxy in the chain. So it would be great if it wasn't changed at all in case if it already does exists in request in case option "outside SSL termination" is enabled.

I think for Forwarded header it could be supported that Openshift would just add new header after existing and all information about proxies in chain could be processed by application.

Maybe both changes for the X-Forwarded-Proto and for Forwarded could be enabled by default and there is no need for option "outside SSL termination" at all. In that case current behavior would be considered as a Bug.

Version

openshift v1.3.3 kubernetes v1.3.0+52492b4

strowk avatar Apr 14 '17 17:04 strowk

/cc @knobunc

stevekuznetsov avatar Apr 18 '17 00:04 stevekuznetsov

Issues go stale after 90d of inactivity.

Mark the issue as fresh by commenting /remove-lifecycle stale. Stale issues rot after an additional 30d of inactivity and eventually close. Exclude this issue from closing by commenting /lifecycle frozen.

If this issue is safe to close now please do so with /close.

/lifecycle stale

openshift-bot avatar Feb 10 '18 04:02 openshift-bot

@knobunc is this still valid?

stevekuznetsov avatar Feb 13 '18 23:02 stevekuznetsov

We put an aws elb before openshift router and use elb for ssl termination and high availability. Now we have same issue for https redirect

jhaohai avatar Mar 12 '18 07:03 jhaohai

@stevekuznetsov this effects almost all of our deployments with an enterprise LB terminating SSL in the first place, so I should say it is still valid.

Most of time we deploy a customized router - i.e. modify the config template and inject it as a configmap, but it would be simpler to be able to use a relevant option like "outside SSL termination".

ekinmeroglu avatar Mar 12 '18 08:03 ekinmeroglu

@knobunc can you triage this please?

stevekuznetsov avatar Mar 12 '18 17:03 stevekuznetsov

Stale issues rot after 30d of inactivity.

Mark the issue as fresh by commenting /remove-lifecycle rotten. Rotten issues close after an additional 30d of inactivity. Exclude this issue from closing by commenting /lifecycle frozen.

If this issue is safe to close now please do so with /close.

/lifecycle rotten /remove-lifecycle stale

openshift-bot avatar Apr 11 '18 22:04 openshift-bot

@stevekuznetsov @knobunc Any update on this? Also a relevant and annoying issue for some of our use cases that these headers simply get overridden even if they are already set.

nickdk avatar May 08 '18 14:05 nickdk

@nickdk you can customize the router template (https://docs.openshift.com/container-platform/3.3/install_config/router/customized_haproxy_router.html)

I'll have to check, but I thought our default configuration added another x-forwarded for header, so you could look N back to see the right one for your environment.

knobunc avatar May 08 '18 15:05 knobunc

@nickdk you can customize the router template (https://docs.openshift.com/container-platform/3.3/install_config/router/customized_haproxy_router.html)

Well, yes we do that, but it's a hack, which needs to be maintained during upgrades. Not cool.

I'll have to check, but I thought our default configuration added another x-forwarded for header, so you could look N back to see the right one for your environment.

Well if you mean the default one - https://github.com/openshift/origin/blob/master/images/router/haproxy/conf/haproxy-config.template so configuration by default is http-request set-header X-Forwarded-Proto http if !{ ssl_fc } where set-header means - "HAProxy can set a Header field in the request from the client using the following directive:" and "set-header removes first any header in existing field that matches ". ( https://www.haproxy.com/documentation/aloha/7-5/traffic-management/lb-layer7/http-rewrite/ ) So that's it - remove really valuable header (first one) and set an internal one. In our custom template we just introduced acl check if the header is already present - we just do not set it again.

strowk avatar May 08 '18 20:05 strowk

Rotten issues close after 30d of inactivity.

Reopen the issue by commenting /reopen. Mark the issue as fresh by commenting /remove-lifecycle rotten. Exclude this issue from closing again by commenting /lifecycle frozen.

/close

openshift-bot avatar Jun 08 '18 00:06 openshift-bot

Hi, we also run into this issue that the headers are simply replaced. Are there plans to change the default template to keep existing headers?

tinexw avatar Aug 15 '18 20:08 tinexw

In addition to X-Forwarded-Proto , later we discovered that for some applications out-Openshift proxy also requires setting X-Forwarded-Host and X-Forwarded-Port, so I renamed the issue, because all three of them are overwritten by Openshift router in one place.

I think I'll try to work on introducing a fix probably later on next week in case if contribution rules are not very complex.

/reopen /remove-lifecycle rotten

strowk avatar Aug 16 '18 04:08 strowk

I created a PR (#20667) that adds the forwarded header instead of replacing it. As noted in the PR I didn't touch the X-Forwarded-* headers because I didn't know what the desired behavior is.

tinexw avatar Aug 16 '18 12:08 tinexw

I have the same exact issue too. Base on @strowk idea, i use the following code to avoid the issue

--- a/images/router/haproxy/conf/haproxy-config.template
+++ b/images/router/haproxy/conf/haproxy-config.template
@@ -426,6 +426,8 @@ backend {{genBackendNamePrefix $cfg.TLSTermination}}:{{$cfgIdx}}
   timeout check 5000ms
   http-request set-header X-Forwarded-Host %[req.hdr(host)]
   http-request set-header X-Forwarded-Port %[dst_port]
+  acl existing-x-forwarded-proto req.hdr(X-Forwarded-Proto) -m found
+  http-request set-header X-Forwarded-Proto http if !existing-x-forwarded-proto !{ ssl_fc }

jeffrey4l avatar Aug 20 '18 06:08 jeffrey4l