From 3713b3dfdd5cd481b1e3435cad255475a2415b64 Mon Sep 17 00:00:00 2001 From: jaymode Date: Thu, 30 Jul 2015 12:02:08 -0400 Subject: [PATCH] use NetworkUtils instead of InetAddress.getLocalHost Prior to this commit, we were InetAddress.getLocalHost() to get the hostname and host address when auditing. This is different than how we report the node's hostname and host address in other places where we use NetworkUtils. This caused false failures to be seen with the IndexAuditTrail tests. This commit switches the audit trails to use the NetworkUtils methods. Closes elastic/elasticsearch#285 Original commit: elastic/x-pack-elasticsearch@c0bd7e94f6ed574585bc0f1592ee277363a4ae3e --- .../shield/audit/index/IndexAuditTrail.java | 12 ++---------- .../shield/audit/logfile/LoggingAuditTrail.java | 13 ++++--------- .../shield/audit/index/IndexAuditTrailTests.java | 5 +++-- 3 files changed, 9 insertions(+), 21 deletions(-) diff --git a/shield/src/main/java/org/elasticsearch/shield/audit/index/IndexAuditTrail.java b/shield/src/main/java/org/elasticsearch/shield/audit/index/IndexAuditTrail.java index 88e8b169787..9542e63db55 100644 --- a/shield/src/main/java/org/elasticsearch/shield/audit/index/IndexAuditTrail.java +++ b/shield/src/main/java/org/elasticsearch/shield/audit/index/IndexAuditTrail.java @@ -237,16 +237,8 @@ public class IndexAuditTrail extends AbstractComponent implements AuditTrail { */ public void start(boolean master) { if (state.compareAndSet(State.INITIALIZED, State.STARTING)) { - String hostname = "n/a"; - String hostaddr = "n/a"; - try { - hostname = InetAddress.getLocalHost().getHostName(); - hostaddr = InetAddress.getLocalHost().getHostAddress(); - } catch (UnknownHostException e) { - logger.warn("unable to resolve local host name", e); - } - this.nodeHostName = hostname; - this.nodeHostAddress = hostaddr; + this.nodeHostName = NetworkUtils.getLocalHostName("n/a"); + this.nodeHostAddress = NetworkUtils.getLocalHostAddress("n/a"); if (client == null) { initializeClient(); diff --git a/shield/src/main/java/org/elasticsearch/shield/audit/logfile/LoggingAuditTrail.java b/shield/src/main/java/org/elasticsearch/shield/audit/logfile/LoggingAuditTrail.java index 18493da4db2..4da4c7b6edd 100644 --- a/shield/src/main/java/org/elasticsearch/shield/audit/logfile/LoggingAuditTrail.java +++ b/shield/src/main/java/org/elasticsearch/shield/audit/logfile/LoggingAuditTrail.java @@ -24,7 +24,6 @@ import org.elasticsearch.transport.TransportRequest; import java.net.InetAddress; import java.net.InetSocketAddress; -import java.net.UnknownHostException; import static org.elasticsearch.common.Strings.arrayToCommaDelimitedString; import static org.elasticsearch.shield.audit.AuditUtil.indices; @@ -274,19 +273,15 @@ public class LoggingAuditTrail implements AuditTrail { static String resolvePrefix(Settings settings) { StringBuilder builder = new StringBuilder(); if (settings.getAsBoolean("shield.audit.logfile.prefix.emit_node_host_address", false)) { - try { - String address = InetAddress.getLocalHost().getHostAddress(); + String address = NetworkUtils.getLocalHostAddress(null); + if (address != null) { builder.append("[").append(address).append("] "); - } catch (UnknownHostException e) { - // ignore } } if (settings.getAsBoolean("shield.audit.logfile.prefix.emit_node_host_name", false)) { - try { - String hostName = InetAddress.getLocalHost().getHostName(); + String hostName = NetworkUtils.getLocalHostName(null); + if (hostName != null) { builder.append("[").append(hostName).append("] "); - } catch (UnknownHostException e) { - // ignore } } if (settings.getAsBoolean("shield.audit.logfile.prefix.emit_node_name", true)) { diff --git a/shield/src/test/java/org/elasticsearch/shield/audit/index/IndexAuditTrailTests.java b/shield/src/test/java/org/elasticsearch/shield/audit/index/IndexAuditTrailTests.java index fe45f033599..f34268ce12b 100644 --- a/shield/src/test/java/org/elasticsearch/shield/audit/index/IndexAuditTrailTests.java +++ b/shield/src/test/java/org/elasticsearch/shield/audit/index/IndexAuditTrailTests.java @@ -14,6 +14,7 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.Client; import org.elasticsearch.common.inject.util.Providers; +import org.elasticsearch.common.network.NetworkUtils; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.transport.LocalTransportAddress; @@ -586,8 +587,8 @@ public class IndexAuditTrailTests extends ShieldIntegrationTest { DateTime dateTime = ISODateTimeFormat.dateTimeParser().withZoneUTC().parseDateTime((String) hit.field("@timestamp").getValue()); assertThat(dateTime.isBefore(DateTime.now(DateTimeZone.UTC)), is(true)); - assertThat(clusterService().localNode().getHostName(), equalTo(hit.field("node_host_name").getValue())); - assertThat(clusterService().localNode().getHostAddress(), equalTo(hit.field("node_host_address").getValue())); + assertThat(NetworkUtils.getLocalHostName("n/a"), equalTo(hit.field("node_host_name").getValue())); + assertThat(NetworkUtils.getLocalHostAddress("n/a"), equalTo(hit.field("node_host_address").getValue())); assertEquals(layer, hit.field("layer").getValue()); assertEquals(type, hit.field("event_type").getValue());