Blame SOURCES/TestSecurityProperties.java

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