mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-22 12:56:53 +00:00
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.
This commit is contained in:
parent
64e5c254c1
commit
40ca62c298
@ -39,8 +39,10 @@ public class EvilOsProbeTests extends ESTestCase {
|
||||
final List<String> 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;
|
||||
|
@ -37,6 +37,8 @@ import java.util.HashMap;
|
||||
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 @@ public class OsProbe {
|
||||
final Optional<String> 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;
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ import java.math.BigInteger;
|
||||
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 class OsProbeTests extends ESTestCase {
|
||||
List<String> 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));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user