-
Notifications
You must be signed in to change notification settings - Fork 4.8k
HIVE-29636: Add SSL keystore auto-reloading for HiveServer2 WebUI #6514
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:ssl-auto-reload
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.
+247
−0
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
206 changes: 206 additions & 0 deletions
206
common/src/test/org/apache/hive/http/TestHttpServer.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,206 @@ | ||
| /* | ||
| * 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.apache.hadoop.hive.conf.HiveConf.ConfVars; | ||
| import org.eclipse.jetty.util.ssl.SslContextFactory; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.Method; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.attribute.FileTime; | ||
| import java.util.Optional; | ||
| import java.util.Timer; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.CALLS_REAL_METHODS; | ||
| import static org.mockito.Mockito.atLeastOnce; | ||
| import static org.mockito.Mockito.doAnswer; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.withSettings; | ||
|
|
||
| /** | ||
| * Tests for the SSL keystore auto-reload feature wired in via | ||
| * {@code HttpServer#makeConfigurationChangeMonitor} and the surrounding | ||
| * {@code configurationChangeMonitor} field. See HiveConf | ||
| * {@code hive.server2.webui.keystore.reload.interval}. | ||
| */ | ||
| public class TestHttpServer { | ||
|
|
||
| private Path keystore; | ||
| private Timer timer; | ||
|
|
||
| @Before | ||
| public void setUp() throws Exception { | ||
| keystore = Files.createTempFile("test-keystore-", ".jks"); | ||
| Files.write(keystore, "initial-content".getBytes()); | ||
| } | ||
|
|
||
| @After | ||
| public void tearDown() throws Exception { | ||
| if (timer != null) { | ||
| timer.cancel(); | ||
| } | ||
| if (keystore != null) { | ||
| Files.deleteIfExists(keystore); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * The reload-interval ConfVar defaults to 60s so the feature is on out of the box. | ||
| * Wiring in {@code createAndAddChannelConnector} treats <= 0 as "disabled". | ||
| */ | ||
| @Test | ||
| public void testReloadIntervalConfDefaultIs60Seconds() { | ||
| HiveConf conf = new HiveConf(); | ||
| long ms = conf.getTimeVar( | ||
| ConfVars.HIVE_SERVER2_WEBUI_SSL_KEYSTORE_RELOAD_INTERVAL, TimeUnit.MILLISECONDS); | ||
| assertEquals(60_000L, ms); | ||
| } | ||
|
|
||
| /** | ||
| * When the watched keystore file is modified, the scheduled | ||
| * {@code FileMonitoringTimerTask} must invoke | ||
| * {@code SslContextFactory#reload}. | ||
| */ | ||
| @Test(timeout = 10_000) | ||
| public void testMonitorReloadsSslContextOnKeystoreModification() throws Exception { | ||
| SslContextFactory sslContextFactory = mock(SslContextFactory.class); | ||
| CountDownLatch reloadCalled = new CountDownLatch(1); | ||
| doAnswer(invocation -> { | ||
| reloadCalled.countDown(); | ||
| return null; | ||
| }).when(sslContextFactory).reload(any()); | ||
|
|
||
| timer = invokeMakeMonitor(100L, keystore.toString(), sslContextFactory); | ||
|
|
||
| // Bump mtime to guarantee a detected change (FileMonitoringTimerTask compares mtimes). | ||
| Files.setLastModifiedTime(keystore, FileTime.fromMillis(System.currentTimeMillis() + 5_000)); | ||
|
|
||
| assertTrue("SslContextFactory#reload was not called within 5s of keystore mtime change", | ||
| reloadCalled.await(5, TimeUnit.SECONDS)); | ||
| verify(sslContextFactory, atLeastOnce()).reload(any()); | ||
| } | ||
|
|
||
| /** | ||
| * Reload failures must be swallowed so a transient bad keystore can't take HS2 down; | ||
| * the next mtime change should still trigger another reload attempt. | ||
| */ | ||
| @Test(timeout = 10_000) | ||
| public void testMonitorSurvivesReloadException() throws Exception { | ||
| SslContextFactory sslContextFactory = mock(SslContextFactory.class); | ||
| CountDownLatch reloadCalled = new CountDownLatch(2); | ||
| doAnswer(invocation -> { | ||
| reloadCalled.countDown(); | ||
| throw new RuntimeException("simulated keystore reload failure"); | ||
| }).when(sslContextFactory).reload(any()); | ||
|
|
||
| timer = invokeMakeMonitor(100L, keystore.toString(), sslContextFactory); | ||
|
|
||
| Files.setLastModifiedTime(keystore, FileTime.fromMillis(System.currentTimeMillis() + 5_000)); | ||
| Thread.sleep(300); | ||
| Files.setLastModifiedTime(keystore, FileTime.fromMillis(System.currentTimeMillis() + 10_000)); | ||
|
|
||
| assertTrue("Monitor should keep firing reload attempts even after exceptions", | ||
| reloadCalled.await(5, TimeUnit.SECONDS)); | ||
| } | ||
|
|
||
| /** | ||
| * {@code stop()} must cancel the monitor Timer when one was installed, | ||
| * so the daemon thread does not outlive HS2. | ||
| */ | ||
| @Test | ||
| public void testStopCancelsConfigurationChangeMonitor() throws Exception { | ||
| HttpServer server = mock(HttpServer.class, withSettings().defaultAnswer(CALLS_REAL_METHODS)); | ||
|
|
||
| // Track whether cancel() was invoked on the installed timer. | ||
| boolean[] cancelled = {false}; | ||
| Timer installed = new Timer("test-monitor", true) { | ||
| @Override | ||
| public void cancel() { | ||
| cancelled[0] = true; | ||
| super.cancel(); | ||
| } | ||
| }; | ||
| setField(server, "configurationChangeMonitor", Optional.of(installed)); | ||
|
|
||
| // stop() also calls webServer.stop(); webServer is null on a mock, so we expect | ||
| // a NullPointerException after the cancel path runs. | ||
| try { | ||
| server.stop(); | ||
| } catch (NullPointerException expected) { | ||
| // intentionally ignored — we only assert the monitor was cancelled | ||
| } | ||
| assertTrue("Timer#cancel should have been invoked from stop()", cancelled[0]); | ||
| } | ||
|
|
||
| /** | ||
| * No monitor installed → stop() must not blow up trying to cancel a missing Timer. | ||
| * (Mockito skips field initializers, so we re-establish the production default | ||
| * {@code Optional.empty()} on the mock before exercising stop().) | ||
| */ | ||
| @Test | ||
| public void testStopWithoutMonitorDoesNotThrowFromCancelPath() throws Exception { | ||
| HttpServer server = mock(HttpServer.class, withSettings().defaultAnswer(CALLS_REAL_METHODS)); | ||
| setField(server, "configurationChangeMonitor", Optional.empty()); | ||
|
|
||
| Optional<Timer> current = getField(server, "configurationChangeMonitor"); | ||
| assertFalse("configurationChangeMonitor should be empty for this case", current.isPresent()); | ||
|
|
||
| try { | ||
| server.stop(); | ||
| } catch (NullPointerException expectedFromWebServerStop) { | ||
| // ok — the monitor branch must not have thrown before reaching webServer.stop() | ||
| } | ||
| } | ||
|
|
||
| // ---- reflection helpers ------------------------------------------------ | ||
|
|
||
| private static Timer invokeMakeMonitor(long intervalMs, String keystorePath, | ||
| SslContextFactory sslContextFactory) throws Exception { | ||
| HttpServer server = mock(HttpServer.class, withSettings().defaultAnswer(CALLS_REAL_METHODS)); | ||
| Method m = HttpServer.class.getDeclaredMethod( | ||
| "makeConfigurationChangeMonitor", long.class, String.class, SslContextFactory.class); | ||
| m.setAccessible(true); | ||
| return (Timer) m.invoke(server, intervalMs, keystorePath, sslContextFactory); | ||
| } | ||
|
|
||
| private static void setField(Object target, String name, Object value) throws Exception { | ||
| Field f = HttpServer.class.getDeclaredField(name); | ||
| f.setAccessible(true); | ||
| f.set(target, value); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| private static <T> T getField(Object target, String name) throws Exception { | ||
| Field f = HttpServer.class.getDeclaredField(name); | ||
| f.setAccessible(true); | ||
| return (T) f.get(target); | ||
| } | ||
| } |
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.
i think name is very generic