Blame SOURCES/TestSecurityProperties.java

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