Support Httpd filter initParam configs

Gerrit supports a setting named 'httpd.filterClass', 'filterClass' is a
Class that implements the `javax.servlet.Filter` to do filtering works.
It's very convenient to add customized filters for Gerrit for user needs.

Sometimes, the customized filter integrates to Gerrit have it's own
init param to be setup when a filter is initializing. This is
mentioned as `init-param` .

So one of the solutions is to support a config of filter init params.
When Gerrit Web Container is on a startup, check and read the config,
then inject the params into the specified filter if it's needed. This
is helpful in flexibility and scalability for Gerrit httpd filter
integration.

For example, we have a filterClass config now:

    filterClass = com.anyorg.sso.filter.SSOFilter

`SSOFilter` requires two 'init-param' on init: 'PARAM-1' and 'PARAM-2':

Then, add `filterClass.<className>.initParam` settings for them:

    [filterClass "com.company.buc.sso.client.filter.SSOFilter"]
        PARAM-1 = hello
        PARAM-2 = world

(About Git Config legal key scope: https://github.com/git/git/blob/v2.23.0/config.c#L347)
(About Servlet Filter: https://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/FilterConfig.html)

Signed-off-by: Dyrone Teng <dyroneteng@gmail.com>
Reviewed-by: Gert van Dijk <gertvdijk@gmail.com>
Reviewed-by: Luca Milanesio <luca.milanesio@gmail.com>
Change-Id: Ibbc7a6db91f66006c7478ffd6c08190dffcb1ee1
This commit is contained in:
Teng Long
2019-09-23 11:22:03 +08:00
committed by David Pursehouse
parent 3d481e3713
commit c192ca9574
6 changed files with 215 additions and 4 deletions

View File

@@ -0,0 +1,22 @@
load("//javatests/com/google/gerrit/acceptance:tests.bzl", "acceptance_tests")
acceptance_tests(
srcs = glob([
"*IT.java",
]),
group = "filter",
labels = ["filter"],
deps = [
":util",
],
)
java_library(
name = "util",
testonly = True,
srcs = [
"FakeMustInitParamsFilter.java",
"FakeNoInitParamsFilter.java",
],
deps = ["//java/com/google/gerrit/acceptance:lib"],
)

View File

@@ -0,0 +1,56 @@
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.acceptance.filter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class FakeMustInitParamsFilter implements Filter {
// `PARAM_X` and `PARAM_Y` are init param keys
private static final String INIT_PARAM_1 = "PARAM-1";
private static final String INIT_PARAM_2 = "PARAM-2";
// the map is used for testing
private static final Map<String, String> initParams = new HashMap<>();
@Override
public void init(FilterConfig filterConfig) throws ServletException {
initParams.put(INIT_PARAM_1, filterConfig.getInitParameter(INIT_PARAM_1));
initParams.put(INIT_PARAM_2, filterConfig.getInitParameter(INIT_PARAM_2));
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(request, response);
}
@Override
public void destroy() {
// do nothing.
}
// the function is used for testing
Map<String, String> getInitParams() {
return initParams;
}
}

View File

@@ -0,0 +1,42 @@
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.acceptance.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class FakeNoInitParamsFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// no init params in this filter.
// do nothing.
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
chain.doFilter(request, response);
}
@Override
public void destroy() {
// do nothing.
}
}

View File

@@ -0,0 +1,57 @@
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.acceptance.filter;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.testing.ConfigSuite;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.Config;
import org.junit.Assert;
import org.junit.Test;
public class FilterClassIT extends AbstractDaemonTest {
@ConfigSuite.Default
public static Config enableFilter() throws ConfigInvalidException {
Config cfg = new Config();
cfg.fromText(
""
+ "[httpd]\n"
+ " filterClass = com.google.gerrit.acceptance.filter.FakeNoInitParamsFilter\n"
+ " filterClass = com.google.gerrit.acceptance.filter.FakeMustInitParamsFilter\n"
+ "[filterClass \"com.google.gerrit.acceptance.filter.FakeMustInitParamsFilter\"]\n"
+ " PARAM-1 = hello\n"
+ " PARAM-2 = world\n");
return cfg;
}
@Test
public void filterLoad() {
FakeNoInitParamsFilter fakeNoInitParamsFilter =
server.getTestInjector().getBinding(FakeNoInitParamsFilter.class).getProvider().get();
Assert.assertNotNull(fakeNoInitParamsFilter);
FakeMustInitParamsFilter fakeMustInitParamsFilter =
server.getTestInjector().getBinding(FakeMustInitParamsFilter.class).getProvider().get();
Assert.assertNotNull(fakeMustInitParamsFilter);
}
@Test
public void filterInitParams() {
FakeMustInitParamsFilter fakeMustInitParamsFilter =
server.getTestInjector().getBinding(FakeMustInitParamsFilter.class).getProvider().get();
Assert.assertEquals(2, fakeMustInitParamsFilter.getInitParams().size());
Assert.assertEquals("hello", fakeMustInitParamsFilter.getInitParams().get("PARAM-1"));
Assert.assertEquals("world", fakeMustInitParamsFilter.getInitParams().get("PARAM-2"));
}
}