Class PrivilegedUtil
java.lang.Object
oshi.util.PrivilegedUtil
Utility class for privileged command execution and file reading. Provides methods to execute commands and read files
with optional privilege escalation via sudo when running as a non-root user.
-
Method Summary
Modifier and TypeMethodDescriptionGets the command allowlist, refreshing periodically from config.Gets the file allowlist, refreshing periodically from config.getKeyValueMapFromFilePrivileged(String filePath, String separator) Reads a file and returns a map of string keys to string values, with privileged fallback.static StringGets the configured sudo command prefix for the current platform.static StringgetStringFromFilePrivileged(String filePath) Reads a file and returns the first line as a String, with privileged fallback.static booleanisCommandAllowed(String command, Set<String> allowlist) Checks if a command is allowed for privileged execution.static booleanisFileAllowed(String filePath, Set<String> allowlist) Checks if a file path is allowed for privileged reading.static byte[]readAllBytesPrivileged(String filePath, boolean reportError) Reads all bytes from a file with privileged fallback.readFilePrivileged(String filePath) Reads a file with privileged fallback.
-
Method Details
-
isCommandAllowed
Checks if a command is allowed for privileged execution. Extracts the command name from the full command string and matches against the allowlist. Supports both bare names and full paths.- Parameters:
command- The full command string (e.g., "dmidecode -t system")allowlist- Set of allowed command names or paths- Returns:
- true if the command is in the allowlist, false otherwise
-
isFileAllowed
Checks if a file path is allowed for privileged reading. Supports Java glob pattern matching.- Parameters:
filePath- The file path to checkallowlist- Set of allowed file paths or glob patterns- Returns:
- true if the file path matches an entry in the allowlist, false otherwise
-
getCommandAllowlist
-
getFileAllowlist
-
getPrefix
Gets the configured sudo command prefix for the current platform.- Returns:
- The prefix string, or empty string if not configured or not supported on this platform
-
readFilePrivileged
Reads a file with privileged fallback. First attempts normal read, then falls back to sudo cat if the file exists but is not readable and the file is in the allowlist.- Parameters:
filePath- The file to read- Returns:
- A list of Strings representing each line of the file, or empty list if unreadable
-
getStringFromFilePrivileged
-
getKeyValueMapFromFilePrivileged
public static Map<String,String> getKeyValueMapFromFilePrivileged(String filePath, String separator) Reads a file and returns a map of string keys to string values, with privileged fallback.- Parameters:
filePath- The file to readseparator- Character(s) in each line of the file that separate the key and the value- Returns:
- The map contained in the file, delimited by the separator, with the value whitespace trimmed
-
readAllBytesPrivileged
Reads all bytes from a file with privileged fallback.- Parameters:
filePath- The file to readreportError- Whether to log errors reading the file- Returns:
- A byte array representing the file contents, or empty array if unreadable
-