Blame SOURCES/TestSecurityProperties.java

76107b
/* TestSecurityProperties -- Ensure system security properties can be used to
76107b
                             enable the crypto policies.
76107b
   Copyright (C) 2022 Red Hat, Inc.
76107b
76107b
This program is free software: you can redistribute it and/or modify
76107b
it under the terms of the GNU Affero General Public License as
76107b
published by the Free Software Foundation, either version 3 of the
76107b
License, or (at your option) any later version.
76107b
76107b
This program is distributed in the hope that it will be useful,
76107b
but WITHOUT ANY WARRANTY; without even the implied warranty of
76107b
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
76107b
GNU Affero General Public License for more details.
76107b
76107b
You should have received a copy of the GNU Affero General Public License
76107b
along with this program.  If not, see <http://www.gnu.org/licenses/>.
76107b
*/
53c576
import java.io.File;
53c576
import java.io.FileInputStream;
53c576
import java.security.Security;
53c576
import java.util.Properties;
53c576
53c576
public class TestSecurityProperties {
53c576
    // JDK 11
53c576
    private static final String JDK_PROPS_FILE_JDK_11 = System.getProperty("java.home") + "/conf/security/java.security";
53c576
    // JDK 8
53c576
    private static final String JDK_PROPS_FILE_JDK_8 = System.getProperty("java.home") + "/lib/security/java.security";
53c576
adf9a5
    private static final String POLICY_FILE = "/etc/crypto-policies/back-ends/java.config";
adf9a5
adf9a5
    private static final String MSG_PREFIX = "DEBUG: ";
adf9a5
53c576
    public static void main(String[] args) {
adf9a5
        if (args.length == 0) {
adf9a5
            System.err.println("TestSecurityProperties <true|false>");
adf9a5
            System.err.println("Invoke with 'true' if system security properties should be enabled.");
adf9a5
            System.err.println("Invoke with 'false' if system security properties should be disabled.");
adf9a5
            System.exit(1);
adf9a5
        }
adf9a5
        boolean enabled = Boolean.valueOf(args[0]);
adf9a5
        System.out.println(MSG_PREFIX + "System security properties enabled: " + enabled);
53c576
        Properties jdkProps = new Properties();
53c576
        loadProperties(jdkProps);
adf9a5
        if (enabled) {
adf9a5
            loadPolicy(jdkProps);
adf9a5
        }
53c576
        for (Object key: jdkProps.keySet()) {
53c576
            String sKey = (String)key;
53c576
            String securityVal = Security.getProperty(sKey);
53c576
            String jdkSecVal = jdkProps.getProperty(sKey);
53c576
            if (!securityVal.equals(jdkSecVal)) {
adf9a5
                String msg = "Expected value '" + jdkSecVal + "' for key '" +
53c576
                             sKey + "'" + " but got value '" + securityVal + "'";
53c576
                throw new RuntimeException("Test failed! " + msg);
53c576
            } else {
adf9a5
                System.out.println(MSG_PREFIX + sKey + " = " + jdkSecVal + " as expected.");
53c576
            }
53c576
        }
53c576
        System.out.println("TestSecurityProperties PASSED!");
53c576
    }
adf9a5
53c576
    private static void loadProperties(Properties props) {
53c576
        String javaVersion = System.getProperty("java.version");
adf9a5
        System.out.println(MSG_PREFIX + "Java version is " + javaVersion);
53c576
        String propsFile = JDK_PROPS_FILE_JDK_11;
53c576
        if (javaVersion.startsWith("1.8.0")) {
53c576
            propsFile = JDK_PROPS_FILE_JDK_8;
53c576
        }
adf9a5
        try (FileInputStream fin = new FileInputStream(propsFile)) {
adf9a5
            props.load(fin);
adf9a5
        } catch (Exception e) {
adf9a5
            throw new RuntimeException("Test failed!", e);
adf9a5
        }
adf9a5
    }
adf9a5
adf9a5
    private static void loadPolicy(Properties props) {
adf9a5
        try (FileInputStream fin = new FileInputStream(POLICY_FILE)) {
53c576
            props.load(fin);
53c576
        } catch (Exception e) {
53c576
            throw new RuntimeException("Test failed!", e);
53c576
        }
53c576
    }
adf9a5
53c576
}