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 429019b3a134b..fed9b2cb535d8 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 06a5aadd22945..dcc1dd2a581e8 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 4e94e13523897..f76ac7289486d 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)); }