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