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