From 40ca62c298a5852a300bfc6c4bd81ba70e907285 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Mon, 12 Nov 2018 14:27:57 -0500 Subject: [PATCH] Address handling of OS pretty name on some OS (#35451) Some OS (e.g., Oracle Linux Server 6.9) have a trailing space at the end of the PRETTY_NAME line in /etc/os-release. This commit addresses this by accounting for this trailing space when extracting the pretty name. --- .../monitor/os/EvilOsProbeTests.java | 6 ++++-- .../org/elasticsearch/monitor/os/OsProbe.java | 19 +++++++++---------- .../monitor/os/OsProbeTests.java | 11 +++++------ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/qa/evil-tests/src/test/java/org/elasticsearch/monitor/os/EvilOsProbeTests.java b/qa/evil-tests/src/test/java/org/elasticsearch/monitor/os/EvilOsProbeTests.java index 429019b3a134..fed9b2cb535d 100644 --- a/qa/evil-tests/src/test/java/org/elasticsearch/monitor/os/EvilOsProbeTests.java +++ b/qa/evil-tests/src/test/java/org/elasticsearch/monitor/os/EvilOsProbeTests.java @@ -39,8 +39,10 @@ public void testOsPrettyName() throws IOException { final List lines = Files.readAllLines(PathUtils.get("/etc/os-release")); for (final String line : lines) { if (line != null && line.startsWith("PRETTY_NAME=")) { - final Matcher matcher = Pattern.compile("PRETTY_NAME=(\"?|'?)?([^\"']+)\\1").matcher(line); - assert matcher.matches() : line; + final Matcher matcher = Pattern.compile("PRETTY_NAME=(\"?|'?)?([^\"']+)\\1").matcher(line.trim()); + final boolean matches = matcher.matches(); + assert matches : line; + assert matcher.groupCount() == 2 : line; final String prettyName = matcher.group(2); assertThat(osInfo.getPrettyName(), equalTo(prettyName)); return; diff --git a/server/src/main/java/org/elasticsearch/monitor/os/OsProbe.java b/server/src/main/java/org/elasticsearch/monitor/os/OsProbe.java index 06a5aadd2294..dcc1dd2a581e 100644 --- a/server/src/main/java/org/elasticsearch/monitor/os/OsProbe.java +++ b/server/src/main/java/org/elasticsearch/monitor/os/OsProbe.java @@ -37,6 +37,8 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; public class OsProbe { @@ -547,16 +549,13 @@ private String getPrettyName() throws IOException { final Optional maybePrettyNameLine = prettyNameLines.size() == 1 ? Optional.of(prettyNameLines.get(0)) : Optional.empty(); if (maybePrettyNameLine.isPresent()) { - final String prettyNameLine = maybePrettyNameLine.get(); - final String[] prettyNameFields = prettyNameLine.split("="); - assert prettyNameFields.length == 2 : prettyNameLine; - if (prettyNameFields[1].length() >= 3 && - (prettyNameFields[1].startsWith("\"") && prettyNameFields[1].endsWith("\"")) || - (prettyNameFields[1].startsWith("'") && prettyNameFields[1].endsWith("'"))) { - return prettyNameFields[1].substring(1, prettyNameFields[1].length() - 1); - } else { - return prettyNameFields[1]; - } + // we trim since some OS contain trailing space, for example, Oracle Linux Server 6.9 has a trailing space after the quote + final String trimmedPrettyNameLine = maybePrettyNameLine.get().trim(); + final Matcher matcher = Pattern.compile("PRETTY_NAME=(\"?|'?)?([^\"']+)\\1").matcher(trimmedPrettyNameLine); + final boolean matches = matcher.matches(); + assert matches : trimmedPrettyNameLine; + assert matcher.groupCount() == 2 : trimmedPrettyNameLine; + return matcher.group(2); } else { return Constants.OS_NAME; } diff --git a/server/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java b/server/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java index 4e94e1352389..f76ac7289486 100644 --- a/server/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java +++ b/server/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java @@ -27,6 +27,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Locale; import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.anyOf; @@ -55,12 +56,10 @@ public void testOsInfo() throws IOException { List readOsRelease() throws IOException { assert Constants.LINUX : Constants.OS_NAME; if (prettyName != null) { - final String quote = randomFrom("\"", "'", null); - if (quote == null) { - return Arrays.asList("NAME=" + randomAlphaOfLength(16), "PRETTY_NAME=" + prettyName); - } else { - return Arrays.asList("NAME=" + randomAlphaOfLength(16), "PRETTY_NAME=" + quote + prettyName + quote); - } + final String quote = randomFrom("\"", "'", ""); + final String space = randomFrom(" ", ""); + final String prettyNameLine = String.format(Locale.ROOT, "PRETTY_NAME=%s%s%s%s", quote, prettyName, quote, space); + return Arrays.asList("NAME=" + randomAlphaOfLength(16), prettyNameLine); } else { return Collections.singletonList("NAME=" + randomAlphaOfLength(16)); }