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@c0bd7e94f6
This commit is contained in:
jaymode 2015-07-30 12:02:08 -04:00
parent ed6f38b3b0
commit 3713b3dfdd
3 changed files with 9 additions and 21 deletions

View File

@ -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();

View File

@ -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)) {

View File

@ -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());