Blame SOURCES/TestSecurityProperties.java

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