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