Skip to content

Commit 82e8dff

Browse files
committed
Handle OS pretty name on old OS without OS release
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 eb8f346 commit 82e8dff

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-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);

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
import java.lang.reflect.Method;
3434
import java.nio.file.Files;
3535
import java.nio.file.Path;
36+
import java.util.Arrays;
37+
import java.util.Collections;
3638
import java.util.HashMap;
3739
import java.util.List;
3840
import java.util.Map;
@@ -567,22 +569,33 @@ private String getPrettyName() throws IOException {
567569
}
568570

569571
/**
570-
* The lines from {@code /etc/os-release} or {@code /usr/lib/os-release} as a fallback. These file represents identification of the
571-
* underlying operating system. The structure of the file is newlines of key-value pairs of shell-compatible variable assignments.
572+
* The lines from {@code /etc/os-release} or {@code /usr/lib/os-release} as a fallback, with an additional fallback to
573+
* {@code /etc/system-release}. These file represents identification of the underlying operating system. The structure of the file is
574+
* newlines of key-value pairs of shell-compatible variable assignments.
572575
*
573-
* @return the lines from {@code /etc/os-release} or {@code /usr/lib/os-release}
574-
* @throws IOException if an I/O exception occurs reading {@code /etc/os-release} or {@code /usr/lib/os-release}
576+
* @return the lines from {@code /etc/os-release} or {@code /usr/lib/os-release} or {@code /etc/system-release}
577+
* @throws IOException if an I/O exception occurs reading {@code /etc/os-release} or {@code /usr/lib/os-release} or
578+
* {@code /etc/system-release}
575579
*/
576-
@SuppressForbidden(reason = "access /etc/os-release or /usr/lib/os-release")
580+
@SuppressForbidden(reason = "access /etc/os-release or /usr/lib/os-release or /etc/system-release")
577581
List<String> readOsRelease() throws IOException {
578582
final List<String> lines;
579583
if (Files.exists(PathUtils.get("/etc/os-release"))) {
580584
lines = Files.readAllLines(PathUtils.get("/etc/os-release"));
581-
} else {
585+
assert lines != null && lines.isEmpty() == false;
586+
return lines;
587+
} else if (Files.exists(PathUtils.get("/usr/lib/os-release"))) {
582588
lines = Files.readAllLines(PathUtils.get("/usr/lib/os-release"));
589+
assert lines != null && lines.isEmpty() == false;
590+
return lines;
591+
} else if (Files.exists(PathUtils.get("/etc/system-release"))) {
592+
// fallback for older Red Hat-like OS
593+
lines = Files.readAllLines(PathUtils.get("/etc/system-release"));
594+
assert lines != null && lines.size() == 1;
595+
return Collections.singletonList("PRETTY_NAME=\"" + lines.get(0) + "\"");
596+
} else {
597+
return Collections.emptyList();
583598
}
584-
assert lines != null && lines.isEmpty() == false;
585-
return lines;
586599
}
587600

588601
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)