SSLFactory implements ConnectionConfigurator { public static Configuration readSSLConfiguration(Configuration conf, Mode mode) { Configuration sslConf = new Configuration(false); sslConf.setBoolean(SSL_REQUIRE_CLIENT_CERT_KEY, conf.getBoolean( SSL_REQUIRE_CLIENT_CERT_KEY, SSL_REQUIRE_CLIENT_CERT_DEFAULT)); String sslConfResource; if (mode == Mode.CLIENT) { sslConfResource = conf.get(SSL_CLIENT_CONF_KEY, SSL_CLIENT_CONF_DEFAULT); } else { sslConfResource = conf.get(SSL_SERVER_CONF_KEY, SSL_SERVER_CONF_DEFAULT); } sslConf.addResource(sslConfResource); if (sslConf.getResource(sslConfResource) == null) { LOG.debug("{} can't be loaded form classpath, fallback using SSL" + " config from input configuration.", sslConfResource); sslConf = conf; } return sslConf; } SSLFactory(Mode mode, Configuration conf); static Configuration readSSLConfiguration(Configuration conf,
                                                   Mode mode); void init(); static HostnameVerifier getHostnameVerifier(String verifier); void destroy(); KeyStoresFactory getKeystoresFactory(); SSLEngine createSSLEngine(); SSLServerSocketFactory createSSLServerSocketFactory(); SSLSocketFactory createSSLSocketFactory(); HostnameVerifier getHostnameVerifier(); boolean isClientCertRequired(); @Override HttpURLConnection configure(HttpURLConnection conn); static final String SSL_CLIENT_CONF_KEY; static final String SSL_CLIENT_CONF_DEFAULT; static final String SSL_SERVER_CONF_KEY; static final String SSL_SERVER_CONF_DEFAULT; static final String SSL_REQUIRE_CLIENT_CERT_KEY; static final boolean SSL_REQUIRE_CLIENT_CERT_DEFAULT; static final String SSL_HOSTNAME_VERIFIER_KEY; static final String SSL_ENABLED_PROTOCOLS_KEY; static final String SSL_ENABLED_PROTOCOLS_DEFAULT; static final String SSL_SERVER_NEED_CLIENT_AUTH; static final boolean SSL_SERVER_NEED_CLIENT_AUTH_DEFAULT; static final String SSL_SERVER_KEYSTORE_LOCATION; static final String SSL_SERVER_KEYSTORE_PASSWORD; static final String SSL_SERVER_KEYSTORE_TYPE; static final String SSL_SERVER_KEYSTORE_TYPE_DEFAULT; static final String SSL_SERVER_KEYSTORE_KEYPASSWORD; static final String SSL_SERVER_TRUSTSTORE_LOCATION; static final String SSL_SERVER_TRUSTSTORE_PASSWORD; static final String SSL_SERVER_TRUSTSTORE_TYPE; static final String SSL_SERVER_TRUSTSTORE_TYPE_DEFAULT; static final String SSL_SERVER_EXCLUDE_CIPHER_LIST; static final String SSLCERTIFICATE; static final String KEYSTORES_FACTORY_CLASS_KEY; }
@Test public void testNonExistSslClientXml() throws Exception{ Configuration conf = new Configuration(false); conf.setBoolean(SSL_REQUIRE_CLIENT_CERT_KEY, false); conf.set(SSL_CLIENT_CONF_KEY, "non-exist-ssl-client.xml"); Configuration sslConf = SSLFactory.readSSLConfiguration(conf, SSLFactory.Mode.CLIENT); assertNull(sslConf.getResource("non-exist-ssl-client.xml")); assertNull(sslConf.get("ssl.client.truststore.location")); }
@Test public void testSslConfFallback() throws Exception { Configuration conf = new Configuration(FAKE_SSL_CONFIG); conf.set(SSL_CLIENT_CONF_KEY, "non-exist-ssl-client.xml"); Configuration sslConf = SSLFactory.readSSLConfiguration(conf, CLIENT); String clientTsLoc = sslConf.get(getClientTrustStoreKeyName()); assertEquals("trustStoreLocation", clientTsLoc); assertEquals(sslConf, conf); }
@Test public void testSslConfClassPathFirst() throws Exception { Configuration conf = createConfiguration(false, true); conf.addResource(FAKE_SSL_CONFIG); Configuration sslConfLoaded = SSLFactory.readSSLConfiguration(conf, CLIENT); String clientTsLoc = sslConfLoaded.get(getClientTrustStoreKeyName()); assertNotEquals("trustStoreLocation", clientTsLoc); assertNotEquals(conf, sslConfLoaded); }