-
Notifications
You must be signed in to change notification settings - Fork 4.8k
HIVE-29639: Support a pluggable authentication filter for the HiveServer2 WebUI #6518
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
magnuma3
wants to merge
1
commit into
apache:master
Choose a base branch
from
magnuma3:hs2-webui-auth-filter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+376
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
231 changes: 231 additions & 0 deletions
231
common/src/test/org/apache/hive/http/TestHttpServerCustomAuthFilter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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 org.apache.hive.http; | ||
|
|
||
| import org.apache.hadoop.hive.conf.HiveConf; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import javax.servlet.Filter; | ||
| import javax.servlet.FilterChain; | ||
| import javax.servlet.FilterConfig; | ||
| import javax.servlet.ServletException; | ||
| import javax.servlet.ServletRequest; | ||
| import javax.servlet.ServletResponse; | ||
| import javax.servlet.http.HttpServletResponse; | ||
| import java.io.IOException; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.ServerSocket; | ||
| import java.net.URL; | ||
| import java.util.Enumeration; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| /** | ||
| * End-to-end test that wires a sample {@link Filter} into HttpServer via | ||
| * the custom-auth-filter Builder API and verifies that real HTTP requests | ||
| * to the running server flow through the filter — and that the filter's | ||
| * decision (pass through or short-circuit with 401) is honored. | ||
| */ | ||
| public class TestHttpServerCustomAuthFilter { | ||
|
|
||
| private HttpServer server; | ||
|
|
||
| @Before | ||
| public void resetCounters() { | ||
| RecordingFilter.reset(); | ||
| BlockingFilter.reset(); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() throws Exception { | ||
| if (server != null) { | ||
| server.stop(); | ||
| server = null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * With {@code useCustomAuthFilter=true} and a passthrough filter, requests | ||
| * reach the underlying servlet AND the filter sees them, including the | ||
| * configured init parameters. | ||
| */ | ||
| @Test(timeout = 30_000) | ||
| public void testCustomAuthFilterInterceptsRequests() throws Exception { | ||
| Map<String, String> params = new HashMap<>(); | ||
| params.put("realm", "hive"); | ||
| params.put("ttl", "600"); | ||
|
|
||
| int port = freePort(); | ||
| server = new HttpServer.Builder("test") | ||
| .setConf(new HiveConf()) | ||
| .setHost("localhost") | ||
| .setPort(port) | ||
| .setUseCustomAuthFilter(true) | ||
| .setCustomAuthFilter(RecordingFilter.class.getName()) | ||
| .setCustomAuthFilterParams(params) | ||
| .build(); | ||
| server.start(); | ||
|
|
||
| int code = doGet("/jmx"); | ||
| assertEquals("Passthrough filter should not block the request", 200, code); | ||
|
|
||
| assertTrue("Filter should have been invoked at least once; was " | ||
| + RecordingFilter.callCount.get(), RecordingFilter.callCount.get() >= 1); | ||
| assertEquals("Init params should be threaded through to the filter", | ||
| "hive", RecordingFilter.initParams.get("realm")); | ||
| assertEquals("600", RecordingFilter.initParams.get("ttl")); | ||
| } | ||
|
|
||
| /** | ||
| * The filter's decision is authoritative: when the filter short-circuits | ||
| * with a 401, the underlying servlet is never reached and the client | ||
| * receives the 401 response code. | ||
| */ | ||
| @Test(timeout = 30_000) | ||
| public void testCustomAuthFilterCanBlockRequest() throws Exception { | ||
| int port = freePort(); | ||
| server = new HttpServer.Builder("test") | ||
| .setConf(new HiveConf()) | ||
| .setHost("localhost") | ||
| .setPort(port) | ||
| .setUseCustomAuthFilter(true) | ||
| .setCustomAuthFilter(BlockingFilter.class.getName()) | ||
| .setCustomAuthFilterParams(new HashMap<>()) | ||
| .build(); | ||
| server.start(); | ||
|
|
||
| int code = doGet("/jmx"); | ||
| assertEquals("Blocking filter should short-circuit with 401", 401, code); | ||
| assertTrue("Filter should have run before blocking", | ||
| BlockingFilter.callCount.get() >= 1); | ||
| } | ||
|
|
||
| /** | ||
| * Without {@code useCustomAuthFilter=true}, no custom filter is installed | ||
| * and requests proceed normally; the recording filter sees nothing even | ||
| * though it is present on the classpath. | ||
| */ | ||
| @Test(timeout = 30_000) | ||
| public void testCustomAuthFilterNotInstalledWhenDisabled() throws Exception { | ||
| int port = freePort(); | ||
| server = new HttpServer.Builder("test") | ||
| .setConf(new HiveConf()) | ||
| .setHost("localhost") | ||
| .setPort(port) | ||
| .build(); | ||
| server.start(); | ||
|
|
||
| int code = doGet("/jmx"); | ||
| assertEquals(200, code); | ||
| assertEquals("Filter must not be installed when useCustomAuthFilter is off", | ||
| 0, RecordingFilter.callCount.get()); | ||
| } | ||
|
|
||
| // ---- helpers ------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Picks a currently-free port. HttpServer's PortHandlerWrapper keys handlers | ||
| * by the configured port, so we cannot pass 0 (dynamic) — the actual bound | ||
| * port would not match the registered handler. There is a small race window | ||
| * between closing this socket and Jetty binding, which is acceptable for a | ||
| * unit test. | ||
| */ | ||
| private static int freePort() throws IOException { | ||
| try (ServerSocket s = new ServerSocket(0)) { | ||
| return s.getLocalPort(); | ||
| } | ||
| } | ||
|
|
||
| private int doGet(String path) throws IOException { | ||
| URL url = new URL("http://localhost:" + server.getPort() + path); | ||
| HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
| try { | ||
| conn.setRequestMethod("GET"); | ||
| conn.setConnectTimeout(5_000); | ||
| conn.setReadTimeout(5_000); | ||
| return conn.getResponseCode(); | ||
| } finally { | ||
| conn.disconnect(); | ||
| } | ||
| } | ||
|
|
||
| // ---- sample filters ------------------------------------------------------ | ||
|
|
||
| /** Records every invocation and the init parameters seen at startup. */ | ||
| public static class RecordingFilter implements Filter { | ||
| static final AtomicInteger callCount = new AtomicInteger(0); | ||
| static volatile Map<String, String> initParams = new HashMap<>(); | ||
|
|
||
| static void reset() { | ||
| callCount.set(0); | ||
| initParams = new HashMap<>(); | ||
| } | ||
|
|
||
| @Override | ||
| public void init(FilterConfig fc) { | ||
| Map<String, String> seen = new HashMap<>(); | ||
| Enumeration<String> names = fc.getInitParameterNames(); | ||
| while (names.hasMoreElements()) { | ||
| String n = names.nextElement(); | ||
| seen.put(n, fc.getInitParameter(n)); | ||
| } | ||
| initParams = seen; | ||
| } | ||
|
|
||
| @Override | ||
| public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) | ||
| throws IOException, ServletException { | ||
| callCount.incrementAndGet(); | ||
| chain.doFilter(req, resp); | ||
| } | ||
|
|
||
| @Override | ||
| public void destroy() { | ||
| } | ||
| } | ||
|
|
||
| /** Short-circuits every request with 401, like a deny-by-default auth filter. */ | ||
| public static class BlockingFilter implements Filter { | ||
| static final AtomicInteger callCount = new AtomicInteger(0); | ||
|
|
||
| static void reset() { | ||
| callCount.set(0); | ||
| } | ||
|
|
||
| @Override | ||
| public void init(FilterConfig fc) { | ||
| } | ||
|
|
||
| @Override | ||
| public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) | ||
| throws IOException, ServletException { | ||
| callCount.incrementAndGet(); | ||
| ((HttpServletResponse) resp).sendError(HttpServletResponse.SC_UNAUTHORIZED, "blocked"); | ||
| } | ||
|
|
||
| @Override | ||
| public void destroy() { | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <html><body>test webapp root</body></html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of adding a new config, why not extend the existing config to support custom_auth? https://github.com/apache/hive/blob/master/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java#L3914C5-L3914C35
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review @saihemanth-cloudera!
I initially followed the pattern of the existing hive.server2.webui.use.spnego and hive.server2.webui.use.pam configs.
I noticed that hive.server2.webui.auth.method was introduced more recently in HIVE-28457 (4.1.0) but currently only supports NONE and LDAP, so the WebUI auth configuration is a bit split between the two styles right now.
Is the long-term plan to eventually consolidate use.spnego and use.pam into hive.server2.webui.auth.method as well? That context would help me align this PR with the intended direction.