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