2022-05-17 08:43:53 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.security.Security;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
|
|
|
public class TestSecurityProperties {
|
|
|
|
// JDK 11
|
|
|
|
private static final String JDK_PROPS_FILE_JDK_11 = System.getProperty("java.home") + "/conf/security/java.security";
|
|
|
|
// JDK 8
|
|
|
|
private static final String JDK_PROPS_FILE_JDK_8 = System.getProperty("java.home") + "/lib/security/java.security";
|
|
|
|
|
2022-07-25 13:41:47 +00:00
|
|
|
private static final String POLICY_FILE = "/etc/crypto-policies/back-ends/java.config";
|
|
|
|
|
|
|
|
private static final String MSG_PREFIX = "DEBUG: ";
|
|
|
|
|
2022-05-17 08:43:53 +00:00
|
|
|
public static void main(String[] args) {
|
2022-07-25 13:41:47 +00:00
|
|
|
if (args.length == 0) {
|
|
|
|
System.err.println("TestSecurityProperties <true|false>");
|
|
|
|
System.err.println("Invoke with 'true' if system security properties should be enabled.");
|
|
|
|
System.err.println("Invoke with 'false' if system security properties should be disabled.");
|
|
|
|
System.exit(1);
|
|
|
|
}
|
|
|
|
boolean enabled = Boolean.valueOf(args[0]);
|
|
|
|
System.out.println(MSG_PREFIX + "System security properties enabled: " + enabled);
|
2022-05-17 08:43:53 +00:00
|
|
|
Properties jdkProps = new Properties();
|
|
|
|
loadProperties(jdkProps);
|
2022-07-25 13:41:47 +00:00
|
|
|
if (enabled) {
|
|
|
|
loadPolicy(jdkProps);
|
|
|
|
}
|
2022-05-17 08:43:53 +00:00
|
|
|
for (Object key: jdkProps.keySet()) {
|
|
|
|
String sKey = (String)key;
|
|
|
|
String securityVal = Security.getProperty(sKey);
|
|
|
|
String jdkSecVal = jdkProps.getProperty(sKey);
|
|
|
|
if (!securityVal.equals(jdkSecVal)) {
|
2022-07-25 13:41:47 +00:00
|
|
|
String msg = "Expected value '" + jdkSecVal + "' for key '" +
|
2022-05-17 08:43:53 +00:00
|
|
|
sKey + "'" + " but got value '" + securityVal + "'";
|
|
|
|
throw new RuntimeException("Test failed! " + msg);
|
|
|
|
} else {
|
2022-07-25 13:41:47 +00:00
|
|
|
System.out.println(MSG_PREFIX + sKey + " = " + jdkSecVal + " as expected.");
|
2022-05-17 08:43:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
System.out.println("TestSecurityProperties PASSED!");
|
|
|
|
}
|
2022-07-25 13:41:47 +00:00
|
|
|
|
2022-05-17 08:43:53 +00:00
|
|
|
private static void loadProperties(Properties props) {
|
|
|
|
String javaVersion = System.getProperty("java.version");
|
2022-07-25 13:41:47 +00:00
|
|
|
System.out.println(MSG_PREFIX + "Java version is " + javaVersion);
|
2022-05-17 08:43:53 +00:00
|
|
|
String propsFile = JDK_PROPS_FILE_JDK_11;
|
|
|
|
if (javaVersion.startsWith("1.8.0")) {
|
|
|
|
propsFile = JDK_PROPS_FILE_JDK_8;
|
|
|
|
}
|
2022-07-25 13:41:47 +00:00
|
|
|
try (FileInputStream fin = new FileInputStream(propsFile)) {
|
|
|
|
props.load(fin);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException("Test failed!", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void loadPolicy(Properties props) {
|
|
|
|
try (FileInputStream fin = new FileInputStream(POLICY_FILE)) {
|
2022-05-17 08:43:53 +00:00
|
|
|
props.load(fin);
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new RuntimeException("Test failed!", e);
|
|
|
|
}
|
|
|
|
}
|
2022-07-25 13:41:47 +00:00
|
|
|
|
2022-05-17 08:43:53 +00:00
|
|
|
}
|