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