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