Blame SOURCES/TestSecurityProperties.java

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