Skip to content

Commit 258647f

Browse files
committed
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.
1 parent c03e2e6 commit 258647f

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

qa/evil-tests/src/test/java/org/elasticsearch/monitor/os/EvilOsProbeTests.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ public void testOsPrettyName() throws IOException {
3939
final List<String> lines = Files.readAllLines(PathUtils.get("/etc/os-release"));
4040
for (final String line : lines) {
4141
if (line != null && line.startsWith("PRETTY_NAME=")) {
42-
final Matcher matcher = Pattern.compile("PRETTY_NAME=(\"?|'?)?([^\"']+)\\1").matcher(line);
43-
assert matcher.matches() : line;
42+
final Matcher matcher = Pattern.compile("PRETTY_NAME=(\"?|'?)?([^\"']+)\\1").matcher(line.trim());
43+
final boolean matches = matcher.matches();
44+
assert matches : line;
45+
assert matcher.groupCount() == 2 : line;
4446
final String prettyName = matcher.group(2);
4547
assertThat(osInfo.getPrettyName(), equalTo(prettyName));
4648
return;

server/src/main/java/org/elasticsearch/monitor/os/OsProbe.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import java.util.List;
3838
import java.util.Map;
3939
import java.util.Optional;
40+
import java.util.regex.Matcher;
41+
import java.util.regex.Pattern;
4042
import java.util.stream.Collectors;
4143

4244
public class OsProbe {
@@ -547,16 +549,13 @@ private String getPrettyName() throws IOException {
547549
final Optional<String> maybePrettyNameLine =
548550
prettyNameLines.size() == 1 ? Optional.of(prettyNameLines.get(0)) : Optional.empty();
549551
if (maybePrettyNameLine.isPresent()) {
550-
final String prettyNameLine = maybePrettyNameLine.get();
551-
final String[] prettyNameFields = prettyNameLine.split("=");
552-
assert prettyNameFields.length == 2 : prettyNameLine;
553-
if (prettyNameFields[1].length() >= 3 &&
554-
(prettyNameFields[1].startsWith("\"") && prettyNameFields[1].endsWith("\"")) ||
555-
(prettyNameFields[1].startsWith("'") && prettyNameFields[1].endsWith("'"))) {
556-
return prettyNameFields[1].substring(1, prettyNameFields[1].length() - 1);
557-
} else {
558-
return prettyNameFields[1];
559-
}
552+
// we trim since some OS contain trailing space, for example, Oracle Linux Server 6.9 has a trailing space after the quote
553+
final String trimmedPrettyNameLine = maybePrettyNameLine.get().trim();
554+
final Matcher matcher = Pattern.compile("PRETTY_NAME=(\"?|'?)?([^\"']+)\\1").matcher(trimmedPrettyNameLine);
555+
final boolean matches = matcher.matches();
556+
assert matches : trimmedPrettyNameLine;
557+
assert matcher.groupCount() == 2 : trimmedPrettyNameLine;
558+
return matcher.group(2);
560559
} else {
561560
return Constants.OS_NAME;
562561
}

server/src/test/java/org/elasticsearch/monitor/os/OsProbeTests.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.Arrays;
2828
import java.util.Collections;
2929
import java.util.List;
30+
import java.util.Locale;
3031

3132
import static org.hamcrest.Matchers.allOf;
3233
import static org.hamcrest.Matchers.anyOf;
@@ -55,12 +56,10 @@ public void testOsInfo() throws IOException {
5556
List<String> readOsRelease() throws IOException {
5657
assert Constants.LINUX : Constants.OS_NAME;
5758
if (prettyName != null) {
58-
final String quote = randomFrom("\"", "'", null);
59-
if (quote == null) {
60-
return Arrays.asList("NAME=" + randomAlphaOfLength(16), "PRETTY_NAME=" + prettyName);
61-
} else {
62-
return Arrays.asList("NAME=" + randomAlphaOfLength(16), "PRETTY_NAME=" + quote + prettyName + quote);
63-
}
59+
final String quote = randomFrom("\"", "'", "");
60+
final String space = randomFrom(" ", "");
61+
final String prettyNameLine = String.format(Locale.ROOT, "PRETTY_NAME=%s%s%s%s", quote, prettyName, quote, space);
62+
return Arrays.asList("NAME=" + randomAlphaOfLength(16), prettyNameLine);
6463
} else {
6564
return Collections.singletonList("NAME=" + randomAlphaOfLength(16));
6665
}

0 commit comments

Comments
 (0)