Fix OutOfScopeException getting the serverName from HTTP request

Whenever the caller was coming from an HTTP thread that was not
coming from a Guice filter (e.g. GitHub OAuth filter) the request
for the canonicalWebUrl was throwing an OutOfScopeException.

One sample scenario was the following stack of calls:

c.g.g.m.v.VirtualHostHttpCanonicalWebUrlProvider.lambda$getServerName
java.base/java.util.Optional.map
c.g.g.m.v.VirtualHostHttpCanonicalWebUrlProvider.getServerName
c.g.g.m.v.VirtualHostHttpCanonicalWebUrlProvider.get
c.g.g.p.g.o.CanonicalWebUrls.getCannonicalWebUrl
c.g.g.p.g.o.CanonicalWebUrls.getOAuthFinalRedirectUrl
c.g.g.p.g.o.OAuthProtocol.getAuthorizationUrl
c.g.g.p.g.o.OAuthProtocol.loginPhase1
c.g.g.p.g.o.GitHubLogin.login
c.g.g.p.g.o.OAuthWebFilter.login
c.g.g.p.g.o.OAuthWebFilter.doFilter
c.g.g.p.g.o.OAuthFilter.doFilter
org.eclipse.jetty.servlet.FilterHolder.doFilter

Because the request for a canonical web url was coming
from OAuthFilter.doFilter invoked directly from Jetty,
the Guice filter did not manage to inject the current
request, making the lamba execution of the map to
throw an unchecked exception.

By swapping the check between the local thread the scoped HTTP
request and catching the OutOfScopeException, the above condition
would just return into an empty server name.

Change-Id: Ie645fa3e07d128d6fbdbd0624196e7c43d35929c
1 file changed
tree: 2ad597b4679c049e0847e8c8011c860de72c69e5
  1. src/
  2. .gitignore
  3. BUILD
  4. LICENSE
  5. README.md
README.md

Gerrit VirtualHost

Gerrit lib module to split the projects' space into virtual hosts similarly of what you would do with an HTTP Server and different domain names.

How to build

Build this module as it was a Gerrit plugin:

  • Clone Gerrit source tree
  • Clone the virtualhost source tree
  • Link the virtualhost directory to Gerrit /plugins/virtualhost
  • From Gerrit source tree run bazel build plugins/virtualhost
  • The virtualhost.jar module is generated under /bazel-genfiles/plugins/virtualhost/

How install

Copy virtualhost.jar library to Gerrit /lib and add the following two extra settings to gerrit.config:

[gerrit]
  installModule = com.gerritforge.gerrit.modules.virtualhost.GuiceModule

[httpd]
  filterClass = com.gerritforge.gerrit.modules.virtualhost.VirtualHostFilter

Propagation of the X-Forwarded-Host Header:

When Gerrit is hidden behind multiple service layers (eg. reverse-proxy and load balancer), it is essential to ensure the propagation from the upstream proxy of the header X-Forwarded-Host from the upstream proxy.

How to define virtual hosts

/etc/virtualhost.config contains the definition of the virtual hosts and the set of projects included.

Each server section defines a virtual host and contains a set of projects included. Projects are defined using Gerrit ref-matching expressions and can be repeated multiple times to include multiple matchers.

Example to include all the projects starting with team1/ and the ones starting with the username:

[server "team1.mycompany.com"]
  projects = team1/*
  projects = ${username}/*

Default host

For all the other server names that are not defined and for SSH access, there is a special default section that lists of visible projects.

Example to include all the projects by default:

[default]
  projects = ^.*