Class SystemUtilities
Security Configuration
SystemUtilities provides configurable security controls to prevent various attack vectors including information disclosure, resource exhaustion, and system manipulation attacks. All security features are disabled by default for backward compatibility.
Security controls can be enabled via system properties:
systemutilities.security.enabled=false
— Master switch for all security featuressystemutilities.environment.variable.validation.enabled=false
— Block sensitive environment variable accesssystemutilities.file.system.validation.enabled=false
— Validate file system operationssystemutilities.resource.limits.enabled=false
— Enforce resource usage limitssystemutilities.max.shutdown.hooks=100
— Maximum number of shutdown hookssystemutilities.max.temp.prefix.length=100
— Maximum temporary directory prefix lengthsystemutilities.sensitive.variable.patterns=password,secret,key,...
— Comma-separated sensitive variable patterns
Security Features
- Environment Variable Protection: Prevents access to sensitive environment variables (passwords, tokens, etc.)
- File System Validation: Validates temporary directory prefixes to prevent path traversal attacks
- Resource Limits: Configurable limits on shutdown hooks and other resources to prevent exhaustion
- Information Disclosure Prevention: Sanitizes variable names and prevents credential exposure
Usage Example
// Enable security with custom settings
System.setProperty("systemutilities.security.enabled", "true");
System.setProperty("systemutilities.environment.variable.validation.enabled", "true");
System.setProperty("systemutilities.file.system.validation.enabled", "true");
System.setProperty("systemutilities.max.shutdown.hooks", "50");
// These will now enforce security controls
String var = SystemUtilities.getExternalVariable("NORMAL_VAR"); // works
String pass = SystemUtilities.getExternalVariable("PASSWORD"); // returns null (filtered)
Key Features:
- System environment and property access
- Memory usage monitoring and management
- Network interface information retrieval
- Process management and identification
- Runtime environment analysis
- Temporary file management
Usage Examples:
// Get system environment variable with fallback to system property
String configPath = SystemUtilities.getExternalVariable("CONFIG_PATH");
// Check available system resources
int processors = SystemUtilities.getAvailableProcessors();
MemoryInfo memory = SystemUtilities.getMemoryInfo();
// Get network configuration
List<NetworkInfo> networks = SystemUtilities.getNetworkInterfaces();
All methods in this class are thread-safe unless otherwise noted. The class cannot be instantiated and provides only static utility methods.
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class
Simple container class describing the JVM memory usage at a given point in time.static class
Describes a network interface present on the host system.static class
Captures the results of executing an operating system process. -
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic void
addShutdownHook
(Runnable hook) Add shutdown hook with safe execution and resource limits.static File
createTempDirectory
(String prefix) Create temporary directory that will be deleted on JVM exit.static int
static int
Get available processors, considering Docker container limitsstatic long
Get process ID of current JVMgetEnvironmentVariables
(Predicate<String> filter) Get all environment variables with optional filtering and security protection.getEnvironmentVariablesUnsafe
(Predicate<String> filter) Get all environment variables with optional filtering, without security protection.static String
Fetch value from environment variable and if not set, then fetch from System properties.static String
Fetch value from environment variable and if not set, then fetch from System properties, without security filtering.static SystemUtilities.MemoryInfo
Get current JVM memory usage informationstatic List<SystemUtilities.NetworkInfo>
Get network interface informationstatic int
Get the current number of registered shutdown hooks.static double
Get system load average over last minutestatic TimeZone
Get system timezone, considering various sourcesstatic boolean
hasAvailableMemory
(long requiredBytes) Check if enough memory is availablestatic boolean
isJavaVersionAtLeast
(int major, int minor) Check if running on specific Java version or higher
-
Field Details
-
OS_NAME
-
JAVA_VERSION
-
USER_HOME
-
TEMP_DIR
-
-
Method Details
-
getExternalVariable
Fetch value from environment variable and if not set, then fetch from System properties. If neither available, return null.Security Note: This method filters out potentially sensitive variables such as passwords, tokens, and credentials to prevent information disclosure. Use
getExternalVariableUnsafe(String)
if you need access to sensitive variables and have verified the security requirements.- Parameters:
var
- String key of variable to return- Returns:
- variable value or null if not found or filtered for security
-
getExternalVariableUnsafe
Fetch value from environment variable and if not set, then fetch from System properties, without security filtering.Security Warning: This method bypasses security filtering and may return sensitive information such as passwords or tokens. Use with extreme caution and ensure proper access controls are in place.
- Parameters:
var
- String key of variable to return- Returns:
- variable value or null if not found
-
getAvailableProcessors
public static int getAvailableProcessors()Get available processors, considering Docker container limits -
getMemoryInfo
Get current JVM memory usage information -
getSystemLoadAverage
public static double getSystemLoadAverage()Get system load average over last minute- Returns:
- load average or -1.0 if not available
-
isJavaVersionAtLeast
public static boolean isJavaVersionAtLeast(int major, int minor) Check if running on specific Java version or higher -
currentJdkMajorVersion
public static int currentJdkMajorVersion()- Returns:
- current JDK major version
-
getCurrentProcessId
public static long getCurrentProcessId()Get process ID of current JVM- Returns:
- process ID for the current Java process
-
createTempDirectory
Create temporary directory that will be deleted on JVM exit.Security Note: The prefix parameter is validated to prevent path traversal attacks and ensure safe directory creation.
- Parameters:
prefix
- the prefix for the temporary directory name- Returns:
- the created temporary directory
- Throws:
IllegalArgumentException
- if the prefix contains invalid charactersIOException
- if the directory cannot be created (thrown as unchecked)
-
getSystemTimeZone
Get system timezone, considering various sources -
hasAvailableMemory
public static boolean hasAvailableMemory(long requiredBytes) Check if enough memory is available -
getEnvironmentVariables
Get all environment variables with optional filtering and security protection.Security Note: This method automatically filters out sensitive variables such as passwords, tokens, and credentials to prevent information disclosure. Use
getEnvironmentVariablesUnsafe(Predicate)
if you need access to sensitive variables and have verified the security requirements.- Parameters:
filter
- optional predicate to further filter variables (applied after security filtering)- Returns:
- map of non-sensitive environment variables
-
getEnvironmentVariablesUnsafe
Get all environment variables with optional filtering, without security protection.Security Warning: This method bypasses security filtering and may return sensitive information such as passwords or tokens. Use with extreme caution and ensure proper access controls are in place.
- Parameters:
filter
- optional predicate to filter variables- Returns:
- map of all environment variables matching the filter
-
getNetworkInterfaces
Get network interface information -
addShutdownHook
Add shutdown hook with safe execution and resource limits.Security Note: This method enforces a limit on the number of shutdown hooks to prevent resource exhaustion attacks. The current default limit is 100 hooks, configurable via system property.
- Parameters:
hook
- the runnable to execute during shutdown- Throws:
IllegalStateException
- if the maximum number of shutdown hooks is exceededIllegalArgumentException
- if hook is null
-
getShutdownHookCount
public static int getShutdownHookCount()Get the current number of registered shutdown hooks.- Returns:
- the number of shutdown hooks currently registered
-