Skip to content

Commit f28c1cd

Browse files
committed
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.
1 parent 2528eed commit f28c1cd

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.io.IOException;
2727
import java.nio.file.Files;
28+
import java.util.Collections;
2829
import java.util.List;
2930
import java.util.regex.Matcher;
3031
import java.util.regex.Pattern;
@@ -36,7 +37,15 @@ public class EvilOsProbeTests extends ESTestCase {
3637
public void testOsPrettyName() throws IOException {
3738
final OsInfo osInfo = OsProbe.getInstance().osInfo(randomLongBetween(1, 100), randomIntBetween(1, 8));
3839
if (Constants.LINUX) {
39-
final List<String> lines = Files.readAllLines(PathUtils.get("/etc/os-release"));
40+
final List<String> lines;
41+
if (Files.exists(PathUtils.get("/etc/os-release"))) {
42+
lines = Files.readAllLines(PathUtils.get("/etc/os-release"));
43+
} else if (Files.exists(PathUtils.get("/usr/lib/os-release"))) {
44+
lines = Files.readAllLines(PathUtils.get("/usr/lib/os-release"));
45+
} else {
46+
lines = Collections.singletonList(
47+
"PRETTY_NAME=\"" + Files.readAllLines(PathUtils.get("/etc/system-release")).get(0) + "\"");
48+
}
4049
for (final String line : lines) {
4150
if (line != null && line.startsWith("PRETTY_NAME=")) {
4251
final Matcher matcher = Pattern.compile("PRETTY_NAME=(\"?|'?)?([^\"']+)\\1").matcher(line.trim());

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.lang.reflect.Method;
3434
import java.nio.file.Files;
3535
import java.nio.file.Path;
36+
import java.util.Collections;
3637
import java.util.HashMap;
3738
import java.util.List;
3839
import java.util.Map;
@@ -566,22 +567,33 @@ private String getPrettyName() throws IOException {
566567
}
567568

568569
/**
569-
* The lines from {@code /etc/os-release} or {@code /usr/lib/os-release} as a fallback. These file represents identification of the
570-
* underlying operating system. The structure of the file is newlines of key-value pairs of shell-compatible variable assignments.
570+
* The lines from {@code /etc/os-release} or {@code /usr/lib/os-release} as a fallback, with an additional fallback to
571+
* {@code /etc/system-release}. These files represent identification of the underlying operating system. The structure of the file is
572+
* newlines of key-value pairs of shell-compatible variable assignments.
571573
*
572-
* @return the lines from {@code /etc/os-release} or {@code /usr/lib/os-release}
573-
* @throws IOException if an I/O exception occurs reading {@code /etc/os-release} or {@code /usr/lib/os-release}
574+
* @return the lines from {@code /etc/os-release} or {@code /usr/lib/os-release} or {@code /etc/system-release}
575+
* @throws IOException if an I/O exception occurs reading {@code /etc/os-release} or {@code /usr/lib/os-release} or
576+
* {@code /etc/system-release}
574577
*/
575-
@SuppressForbidden(reason = "access /etc/os-release or /usr/lib/os-release")
578+
@SuppressForbidden(reason = "access /etc/os-release or /usr/lib/os-release or /etc/system-release")
576579
List<String> readOsRelease() throws IOException {
577580
final List<String> lines;
578581
if (Files.exists(PathUtils.get("/etc/os-release"))) {
579582
lines = Files.readAllLines(PathUtils.get("/etc/os-release"));
580-
} else {
583+
assert lines != null && lines.isEmpty() == false;
584+
return lines;
585+
} else if (Files.exists(PathUtils.get("/usr/lib/os-release"))) {
581586
lines = Files.readAllLines(PathUtils.get("/usr/lib/os-release"));
587+
assert lines != null && lines.isEmpty() == false;
588+
return lines;
589+
} else if (Files.exists(PathUtils.get("/etc/system-release"))) {
590+
// fallback for older Red Hat-like OS
591+
lines = Files.readAllLines(PathUtils.get("/etc/system-release"));
592+
assert lines != null && lines.size() == 1;
593+
return Collections.singletonList("PRETTY_NAME=\"" + lines.get(0) + "\"");
594+
} else {
595+
return Collections.emptyList();
582596
}
583-
assert lines != null && lines.isEmpty() == false;
584-
return lines;
585597
}
586598

587599
public OsStats osStats() {

server/src/main/resources/org/elasticsearch/bootstrap/security.policy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ grant {
127127
// OS release on Linux
128128
permission java.io.FilePermission "/etc/os-release", "read";
129129
permission java.io.FilePermission "/usr/lib/os-release", "read";
130+
permission java.io.FilePermission "/etc/system-release", "read";
130131

131132
// io stats on Linux
132133
permission java.io.FilePermission "/proc/self/mountinfo", "read";

0 commit comments

Comments
 (0)