From b5c3e6f9aae9791b749ba3d908ce1b6672ee2124 Mon Sep 17 00:00:00 2001 From: Mark Payne Date: Wed, 15 Nov 2023 14:35:03 -0500 Subject: [PATCH] NIFI-12358 Fixed NPE in HostHeaderHandler Fixed NPE in ThreadDumpTask and TrackedLeaderElectionManager This closes #8032 Signed-off-by: David Handermann --- .../nifi/diagnostics/ThreadDumpTask.java | 9 ++-- .../TrackedLeaderElectionManager.java | 2 +- .../nifi/web/server/HostHeaderHandler.java | 41 +++++++++++-------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/nifi-framework-api/src/main/java/org/apache/nifi/diagnostics/ThreadDumpTask.java b/nifi-framework-api/src/main/java/org/apache/nifi/diagnostics/ThreadDumpTask.java index d5765fdf9781..74228a2d0aa6 100644 --- a/nifi-framework-api/src/main/java/org/apache/nifi/diagnostics/ThreadDumpTask.java +++ b/nifi-framework-api/src/main/java/org/apache/nifi/diagnostics/ThreadDumpTask.java @@ -25,6 +25,7 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Objects; public class ThreadDumpTask implements DiagnosticTask { @Override @@ -37,7 +38,7 @@ public DiagnosticsDumpElement captureDump(boolean verbose) { final List sortedInfos = new ArrayList<>(infos.length); Collections.addAll(sortedInfos, infos); - sortedInfos.sort(new Comparator() { + sortedInfos.sort(new Comparator<>() { @Override public int compare(ThreadInfo o1, ThreadInfo o2) { return o1.getThreadName().toLowerCase().compareTo(o2.getThreadName().toLowerCase()); @@ -69,7 +70,7 @@ public int compare(ThreadInfo o1, ThreadInfo o2) { sb.append(" (in native code)"); } - if (deadlockedThreadIds != null && deadlockedThreadIds.length > 0) { + if (deadlockedThreadIds != null) { for (final long id : deadlockedThreadIds) { if (id == info.getThreadId()) { sb.append(" ** DEADLOCKED THREAD **"); @@ -77,7 +78,7 @@ public int compare(ThreadInfo o1, ThreadInfo o2) { } } - if (monitorDeadlockThreadIds != null && monitorDeadlockThreadIds.length > 0) { + if (monitorDeadlockThreadIds != null) { for (final long id : monitorDeadlockThreadIds) { if (id == info.getThreadId()) { sb.append(" ** MONITOR-DEADLOCKED THREAD **"); @@ -91,7 +92,7 @@ public int compare(ThreadInfo o1, ThreadInfo o2) { final MonitorInfo[] monitors = info.getLockedMonitors(); for (final MonitorInfo monitor : monitors) { - if (monitor.getLockedStackFrame().equals(element)) { + if (Objects.equals(monitor.getLockedStackFrame(), element)) { sb.append("\n\t- waiting on ").append(monitor); } } diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-leader-election-shared/src/main/java/org/apache/nifi/controller/leader/election/TrackedLeaderElectionManager.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-leader-election-shared/src/main/java/org/apache/nifi/controller/leader/election/TrackedLeaderElectionManager.java index 25f15bddd0f9..2f34b5bc2ad6 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-leader-election-shared/src/main/java/org/apache/nifi/controller/leader/election/TrackedLeaderElectionManager.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-leader-election-shared/src/main/java/org/apache/nifi/controller/leader/election/TrackedLeaderElectionManager.java @@ -57,7 +57,7 @@ public Map getLeadershipChangeCount(final long duration, final final TimedBuffer buffer = entry.getValue(); final TimestampedLong aggregateValue = buffer.getAggregateValue(System.currentTimeMillis() - TimeUnit.MILLISECONDS.convert(duration, unit)); - final int leadershipChanges = aggregateValue.getValue().intValue(); + final int leadershipChanges = (aggregateValue == null) ? 0 : aggregateValue.getValue().intValue(); leadershipChangesPerRole.put(roleName, leadershipChanges); } diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-jetty/src/main/java/org/apache/nifi/web/server/HostHeaderHandler.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-jetty/src/main/java/org/apache/nifi/web/server/HostHeaderHandler.java index 62348bb53b67..1b0c8135005e 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-jetty/src/main/java/org/apache/nifi/web/server/HostHeaderHandler.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-jetty/src/main/java/org/apache/nifi/web/server/HostHeaderHandler.java @@ -16,6 +16,18 @@ */ package org.apache.nifi.web.server; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.text.StringEscapeUtils; +import org.apache.http.conn.util.InetAddressUtils; +import org.apache.nifi.util.NiFiProperties; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.server.handler.ScopedHandler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.net.InetAddress; @@ -28,17 +40,6 @@ import java.util.Map; import java.util.Objects; import java.util.stream.Collectors; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.text.StringEscapeUtils; -import org.apache.http.conn.util.InetAddressUtils; -import org.apache.nifi.util.NiFiProperties; -import org.eclipse.jetty.server.Request; -import org.eclipse.jetty.server.handler.ScopedHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public class HostHeaderHandler extends ScopedHandler { private static final Logger logger = LoggerFactory.getLogger(HostHeaderHandler.class); @@ -233,7 +234,7 @@ public static List generateDefaultHostnames(NiFiProperties niFiPropertie try { final int lambdaPort = serverPort; List customIPs = extractIPsFromNetworkInterfaces(niFiProperties); - customIPs.stream().forEach(ip -> { + customIPs.forEach(ip -> { validHosts.add(ip); validHosts.add(ip + ":" + lambdaPort); }); @@ -284,16 +285,20 @@ static List extractIPsFromNetworkInterfaces(NiFiProperties niFiPropertie Map networkInterfaces = niFiProperties.isHTTPSConfigured() ? niFiProperties.getHttpsNetworkInterfaces() : niFiProperties.getHttpNetworkInterfaces(); if (isNotDefined(networkInterfaces)) { // No custom interfaces defined - return new ArrayList<>(0); + return List.of(); } else { - List allIPAddresses = new ArrayList<>(); + final List allIPAddresses = new ArrayList<>(); for (Map.Entry entry : networkInterfaces.entrySet()) { final String networkInterfaceName = entry.getValue(); try { - NetworkInterface ni = NetworkInterface.getByName(networkInterfaceName); - List ipAddresses = Collections.list(ni.getInetAddresses()).stream().map(inetAddress -> inetAddress.getHostAddress().toLowerCase()).collect(Collectors.toList()); - logger.debug("Resolved the following IP addresses for network interface {}: {}", networkInterfaceName, StringUtils.join(ipAddresses, ", ")); - allIPAddresses.addAll(ipAddresses); + final NetworkInterface ni = NetworkInterface.getByName(networkInterfaceName); + if (ni == null) { + logger.warn("Cannot resolve network interface named " + networkInterfaceName); + } else { + final List ipAddresses = Collections.list(ni.getInetAddresses()).stream().map(inetAddress -> inetAddress.getHostAddress().toLowerCase()).collect(Collectors.toList()); + logger.debug("Resolved the following IP addresses for network interface {}: {}", networkInterfaceName, StringUtils.join(ipAddresses, ", ")); + allIPAddresses.addAll(ipAddresses); + } } catch (SocketException e) { logger.warn("Cannot resolve network interface named " + networkInterfaceName); }