Handle OS pretty name on old OS without OS release (#35453)
Some very old ancient versions of Linux do not have /etc/os-release. For example, old Red Hat-like OS. This commit adds a fallback for handling pretty name for these OS.
This commit is contained in:
parent
71cfb730f6
commit
a18b599d64
|
@ -25,6 +25,7 @@ import org.elasticsearch.test.ESTestCase;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
@ -36,7 +37,15 @@ public class EvilOsProbeTests extends ESTestCase {
|
|||
public void testOsPrettyName() throws IOException {
|
||||
final OsInfo osInfo = OsProbe.getInstance().osInfo(randomLongBetween(1, 100), randomIntBetween(1, 8));
|
||||
if (Constants.LINUX) {
|
||||
final List<String> lines = Files.readAllLines(PathUtils.get("/etc/os-release"));
|
||||
final List<String> lines;
|
||||
if (Files.exists(PathUtils.get("/etc/os-release"))) {
|
||||
lines = Files.readAllLines(PathUtils.get("/etc/os-release"));
|
||||
} else if (Files.exists(PathUtils.get("/usr/lib/os-release"))) {
|
||||
lines = Files.readAllLines(PathUtils.get("/usr/lib/os-release"));
|
||||
} else {
|
||||
lines = Collections.singletonList(
|
||||
"PRETTY_NAME=\"" + Files.readAllLines(PathUtils.get("/etc/system-release")).get(0) + "\"");
|
||||
}
|
||||
for (final String line : lines) {
|
||||
if (line != null && line.startsWith("PRETTY_NAME=")) {
|
||||
final Matcher matcher = Pattern.compile("PRETTY_NAME=(\"?|'?)?([^\"']+)\\1").matcher(line.trim());
|
||||
|
|
|
@ -33,6 +33,7 @@ import java.lang.reflect.InvocationTargetException;
|
|||
import java.lang.reflect.Method;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -566,22 +567,33 @@ public class OsProbe {
|
|||
}
|
||||
|
||||
/**
|
||||
* The lines from {@code /etc/os-release} or {@code /usr/lib/os-release} as a fallback. These file represents identification of the
|
||||
* underlying operating system. The structure of the file is newlines of key-value pairs of shell-compatible variable assignments.
|
||||
* The lines from {@code /etc/os-release} or {@code /usr/lib/os-release} as a fallback, with an additional fallback to
|
||||
* {@code /etc/system-release}. These files represent identification of the underlying operating system. The structure of the file is
|
||||
* newlines of key-value pairs of shell-compatible variable assignments.
|
||||
*
|
||||
* @return the lines from {@code /etc/os-release} or {@code /usr/lib/os-release}
|
||||
* @throws IOException if an I/O exception occurs reading {@code /etc/os-release} or {@code /usr/lib/os-release}
|
||||
* @return the lines from {@code /etc/os-release} or {@code /usr/lib/os-release} or {@code /etc/system-release}
|
||||
* @throws IOException if an I/O exception occurs reading {@code /etc/os-release} or {@code /usr/lib/os-release} or
|
||||
* {@code /etc/system-release}
|
||||
*/
|
||||
@SuppressForbidden(reason = "access /etc/os-release or /usr/lib/os-release")
|
||||
@SuppressForbidden(reason = "access /etc/os-release or /usr/lib/os-release or /etc/system-release")
|
||||
List<String> readOsRelease() throws IOException {
|
||||
final List<String> lines;
|
||||
if (Files.exists(PathUtils.get("/etc/os-release"))) {
|
||||
lines = Files.readAllLines(PathUtils.get("/etc/os-release"));
|
||||
} else {
|
||||
assert lines != null && lines.isEmpty() == false;
|
||||
return lines;
|
||||
} else if (Files.exists(PathUtils.get("/usr/lib/os-release"))) {
|
||||
lines = Files.readAllLines(PathUtils.get("/usr/lib/os-release"));
|
||||
assert lines != null && lines.isEmpty() == false;
|
||||
return lines;
|
||||
} else if (Files.exists(PathUtils.get("/etc/system-release"))) {
|
||||
// fallback for older Red Hat-like OS
|
||||
lines = Files.readAllLines(PathUtils.get("/etc/system-release"));
|
||||
assert lines != null && lines.size() == 1;
|
||||
return Collections.singletonList("PRETTY_NAME=\"" + lines.get(0) + "\"");
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
assert lines != null && lines.isEmpty() == false;
|
||||
return lines;
|
||||
}
|
||||
|
||||
public OsStats osStats() {
|
||||
|
|
|
@ -127,6 +127,7 @@ grant {
|
|||
// OS release on Linux
|
||||
permission java.io.FilePermission "/etc/os-release", "read";
|
||||
permission java.io.FilePermission "/usr/lib/os-release", "read";
|
||||
permission java.io.FilePermission "/etc/system-release", "read";
|
||||
|
||||
// io stats on Linux
|
||||
permission java.io.FilePermission "/proc/self/mountinfo", "read";
|
||||
|
|
Loading…
Reference in New Issue