Skip to content

Commit 9143088

Browse files
authored
HADOOP-17665 Ignore missing keystore configuration in reloading mechanism
1 parent 8d5cc98 commit 9143088

File tree

3 files changed

+66
-27
lines changed

3 files changed

+66
-27
lines changed

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/http/HttpServer2.java

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.net.MalformedURLException;
2828
import java.net.URI;
2929
import java.net.URL;
30+
import java.nio.file.Path;
3031
import java.nio.file.Paths;
3132
import java.util.List;
3233
import java.util.ArrayList;
@@ -587,7 +588,8 @@ private ServerConnector createHttpsChannelConnector(
587588
conf.getLong(FileBasedKeyStoresFactory.SSL_STORES_RELOAD_INTERVAL_TPL_KEY,
588589
FileBasedKeyStoresFactory.DEFAULT_SSL_STORES_RELOAD_INTERVAL);
589590

590-
if (storesReloadInterval > 0) {
591+
if (storesReloadInterval > 0 &&
592+
(keyStore != null || trustStore != null)) {
591593
this.configurationChangeMonitor = Optional.of(
592594
this.makeConfigurationChangeMonitor(storesReloadInterval, sslContextFactory));
593595
}
@@ -601,22 +603,30 @@ private ServerConnector createHttpsChannelConnector(
601603
private Timer makeConfigurationChangeMonitor(long reloadInterval,
602604
SslContextFactory.Server sslContextFactory) {
603605
java.util.Timer timer = new java.util.Timer(FileBasedKeyStoresFactory.SSL_MONITORING_THREAD_NAME, true);
606+
ArrayList<Path> locations = new ArrayList<Path>();
607+
if (keyStore != null) {
608+
locations.add(Paths.get(keyStore));
609+
}
610+
if (trustStore != null) {
611+
locations.add(Paths.get(trustStore));
612+
}
604613
//
605614
// The Jetty SSLContextFactory provides a 'reload' method which will reload both
606615
// truststore and keystore certificates.
607616
//
608617
timer.schedule(new FileMonitoringTimerTask(
609-
Paths.get(keyStore),
610-
path -> {
611-
LOG.info("Reloading certificates from store keystore " + keyStore);
612-
try {
613-
sslContextFactory.reload(factory -> { });
614-
} catch (Exception ex) {
615-
LOG.error("Failed to reload SSL keystore certificates", ex);
616-
}
617-
},null),
618-
reloadInterval,
619-
reloadInterval
618+
locations,
619+
path -> {
620+
LOG.info("Reloading keystore and truststore certificates.");
621+
try {
622+
sslContextFactory.reload(factory -> { });
623+
} catch (Exception ex) {
624+
LOG.error("Failed to reload SSL keystore " +
625+
"and truststore certificates", ex);
626+
}
627+
},null),
628+
reloadInterval,
629+
reloadInterval
620630
);
621631
return timer;
622632
}

hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/ssl/FileMonitoringTimerTask.java

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import org.slf4j.LoggerFactory;
2525

2626
import java.nio.file.Path;
27+
import java.util.ArrayList;
28+
import java.util.Collections;
29+
import java.util.List;
2730
import java.util.TimerTask;
2831
import java.util.function.Consumer;
2932

@@ -42,34 +45,59 @@ public class FileMonitoringTimerTask extends TimerTask {
4245
static final String PROCESS_ERROR_MESSAGE =
4346
"Could not process file change : ";
4447

45-
final private Path filePath;
48+
final private List<Path> filePaths;
4649
final private Consumer<Path> onFileChange;
4750
final Consumer<Throwable> onChangeFailure;
48-
private long lastProcessed;
51+
private List<Long> lastProcessed;
4952

5053
/**
51-
* Create file monitoring task to be scheduled using a standard Java {@link java.util.Timer}
52-
* instance.
54+
* See {@link #FileMonitoringTimerTask(List, Consumer, Consumer)}.
5355
*
54-
* @param filePath The path to the file to monitor.
55-
* @param onFileChange The function to call when the file has changed.
56-
* @param onChangeFailure The function to call when an exception is thrown during the
57-
* file change processing.
56+
* @param filePath The file to monitor.
57+
* @param onFileChange What to do when the file changes.
58+
* @param onChangeFailure What to do when <code>onFileChange</code>
59+
* throws an exception.
5860
*/
5961
public FileMonitoringTimerTask(Path filePath, Consumer<Path> onFileChange,
60-
Consumer<Throwable> onChangeFailure) {
61-
Preconditions.checkNotNull(filePath, "path to monitor disk file is not set");
62-
Preconditions.checkNotNull(onFileChange, "action to monitor disk file is not set");
62+
Consumer<Throwable> onChangeFailure) {
63+
this(Collections.singletonList(filePath), onFileChange, onChangeFailure);
64+
}
6365

64-
this.filePath = filePath;
65-
this.lastProcessed = filePath.toFile().lastModified();
66+
/**
67+
* Create file monitoring task to be scheduled using a standard
68+
* Java {@link java.util.Timer} instance.
69+
*
70+
* @param filePaths The path to the file to monitor.
71+
* @param onFileChange The function to call when the file has changed.
72+
* @param onChangeFailure The function to call when an exception is
73+
* thrown during the file change processing.
74+
*/
75+
public FileMonitoringTimerTask(List<Path> filePaths,
76+
Consumer<Path> onFileChange,
77+
Consumer<Throwable> onChangeFailure) {
78+
Preconditions.checkNotNull(filePaths,
79+
"path to monitor disk file is not set");
80+
Preconditions.checkNotNull(onFileChange,
81+
"action to monitor disk file is not set");
82+
83+
this.filePaths = new ArrayList<Path>(filePaths);
84+
this.lastProcessed = new ArrayList<Long>();
85+
this.filePaths.forEach(path ->
86+
this.lastProcessed.add(path.toFile().lastModified()));
6687
this.onFileChange = onFileChange;
6788
this.onChangeFailure = onChangeFailure;
6889
}
6990

7091
@Override
7192
public void run() {
72-
if (lastProcessed != filePath.toFile().lastModified()) {
93+
int modified = -1;
94+
for (int i = 0; i < filePaths.size() && modified < 0; i++) {
95+
if (lastProcessed.get(i) != filePaths.get(i).toFile().lastModified()) {
96+
modified = i;
97+
}
98+
}
99+
if (modified > -1) {
100+
Path filePath = filePaths.get(modified);
73101
try {
74102
onFileChange.accept(filePath);
75103
} catch (Throwable t) {
@@ -79,7 +107,7 @@ public void run() {
79107
LOG.error(PROCESS_ERROR_MESSAGE + filePath.toString(), t);
80108
}
81109
}
82-
lastProcessed = filePath.toFile().lastModified();
110+
lastProcessed.set(modified, filePath.toFile().lastModified());
83111
}
84112
}
85113
}

hadoop-yarn-project/hadoop-yarn/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,5 @@
246246
<module>hadoop-yarn-ui</module>
247247
<module>hadoop-yarn-csi</module>
248248
</modules>
249+
<!-- -->
249250
</project>

0 commit comments

Comments
 (0)