Blame SOURCES/TestSecurityProperties.java

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