Apache Lucene open-source search software
SOLR-4394: Tests and example configs demonstrating SSL with both server and client certs
Chris M. Hostetter9e767a667d8c
diff --git a/lucene/tools/junit4/tests.policy b/lucene/tools/junit4/tests.policyindex d89461a42d4..545987d0132 100644--- a/lucene/tools/junit4/tests.policy+++ b/lucene/tools/junit4/tests.policy@@ -67,4 +67,7 @@ grant {permission java.security.SecurityPermission "getProperty.networkaddress.cache.ttl";permission java.security.SecurityPermission "getProperty.networkaddress.cache.negative.ttl";+ // SSL related properties for Solr tests+ permission java.security.SecurityPermission "getProperty.ssl.*";+};diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 63f70cab766..2be78baa651 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -41,6 +41,8 @@ Detailed Change ListOther Changes----------------------+* SOLR-4394: Tests and example configs demonstrating SSL with both server+ and client certs (hossman)================== 4.2.0 ==================diff --git a/solr/core/src/java/org/apache/solr/client/solrj/embedded/JettySolrRunner.java b/solr/core/src/java/org/apache/solr/client/solrj/embedded/JettySolrRunner.javaindex 1fdc3ea74d9..a87b1a115b3 100644--- a/solr/core/src/java/org/apache/solr/client/solrj/embedded/JettySolrRunner.java+++ b/solr/core/src/java/org/apache/solr/client/solrj/embedded/JettySolrRunner.java@@ -23,6 +23,9 @@ import java.util.LinkedList;import java.util.Random;import java.util.concurrent.atomic.AtomicLong;+import java.net.URL;+import java.net.MalformedURLException;+import javax.servlet.DispatcherType;import javax.servlet.Filter;import javax.servlet.FilterChain;@@ -38,8 +41,11 @@ import org.apache.solr.servlet.SolrDispatchFilter;import org.eclipse.jetty.server.Connector;import org.eclipse.jetty.server.Server;import org.eclipse.jetty.server.bio.SocketConnector;-import org.eclipse.jetty.server.handler.GzipHandler;import org.eclipse.jetty.server.nio.SelectChannelConnector;+import org.eclipse.jetty.server.ssl.SslConnector;+import org.eclipse.jetty.server.ssl.SslSocketConnector;+import org.eclipse.jetty.server.ssl.SslSelectChannelConnector;+import org.eclipse.jetty.server.handler.GzipHandler;import org.eclipse.jetty.server.session.HashSessionIdManager;import org.eclipse.jetty.servlet.FilterHolder;import org.eclipse.jetty.servlet.ServletContextHandler;@@ -47,6 +53,7 @@ import org.eclipse.jetty.util.component.LifeCycle;import org.eclipse.jetty.util.log.Logger;import org.eclipse.jetty.util.thread.QueuedThreadPool;import org.eclipse.jetty.util.thread.ThreadPool;+import org.eclipse.jetty.util.ssl.SslContextFactory;/*** Run solr using jetty@@ -155,22 +162,59 @@ public class JettySolrRunner {System.setProperty("solr.solr.home", solrHome);if (System.getProperty("jetty.testMode") != null) {final String connectorName = System.getProperty("tests.jettyConnector", "SelectChannel");++ // if this property is true, then jetty will be configured to use SSL+ // leveraging the same system properties as java to specify+ // the keystore/truststore if they are set+ //+ // This means we will use the same truststore, keystore (and keys) for+ // the server as well as any client actions taken by this JVM in+ // talking to that server, but for the purposes of testing that should+ // be good enough+ final boolean useSsl = Boolean.getBoolean("tests.jettySsl");+ final SslContextFactory sslcontext = new SslContextFactory(false);++ if (useSsl) {+ if (null != System.getProperty("javax.net.ssl.keyStore")) {+ sslcontext.setKeyStorePath+ (System.getProperty("javax.net.ssl.keyStore"));+ }+ if (null != System.getProperty("javax.net.ssl.keyStorePassword")) {+ sslcontext.setKeyStorePassword+ (System.getProperty("javax.net.ssl.keyStorePassword"));+ }+ if (null != System.getProperty("javax.net.ssl.trustStore")) {+ sslcontext.setTrustStore+ (System.getProperty("javax.net.ssl.trustStore"));+ }+ if (null != System.getProperty("javax.net.ssl.trustStorePassword")) {+ sslcontext.setTrustStorePassword+ (System.getProperty("javax.net.ssl.trustStorePassword"));+ }+ sslcontext.setNeedClientAuth(Boolean.getBoolean("tests.jettySsl.clientAuth"));+ }+final Connector connector;final QueuedThreadPool threadPool;if ("SelectChannel".equals(connectorName)) {- final SelectChannelConnector c = new SelectChannelConnector();+ final SelectChannelConnector c = useSsl+ ? new SslSelectChannelConnector(sslcontext)+ : new SelectChannelConnector();c.setReuseAddress(true);c.setLowResourcesMaxIdleTime(1500);connector = c;threadPool = (QueuedThreadPool) c.getThreadPool();} else if ("Socket".equals(connectorName)) {- final SocketConnector c = new SocketConnector();+ final SocketConnector c = useSsl+ ? new SslSocketConnector(sslcontext)+ : new SocketConnector();c.setReuseAddress(true);connector = c;threadPool = (QueuedThreadPool) c.getThreadPool();} else {throw new IllegalArgumentException("Illegal value for system property 'tests.jettyConnector': " + connectorName);}+connector.setPort(port);connector.setHost("127.0.0.1");if (threadPool != null) {@@ -346,6 +390,31 @@ public class JettySolrRunner {return lastPort;}+ /**+ * Returns a base URL consisting of the protocal, host, and port for a+ * Connector in use by the Jetty Server contained in this runner.+ */+ public URL getBaseUrl() {+ String protocol = null;+ try {+ Connector[] conns = server.getConnectors();+ if (0 == conns.length) {+ throw new IllegalStateException("Jetty Server has no Connectors");+ }+ Connector c = conns[0];+ if (c.getLocalPort() < 0) {+ throw new IllegalStateException("Jetty Connector is not open: " ++ c.getLocalPort());+ }+ protocol = (c instanceof SslConnector) ? "https" : "http";+ return new URL(protocol, c.getHost(), c.getLocalPort(), context);++ } catch (MalformedURLException e) {+ throw new IllegalStateException+ ("Java could not make sense of protocol: " + protocol, e);+ }+ }+public DebugFilter getDebugFilter() {return (DebugFilter)debugFilter.getFilter();}diff --git a/solr/example/etc/create-solrtest.keystore.sh b/solr/example/etc/create-solrtest.keystore.shnew file mode 100755index 00000000000..902a99609b4--- /dev/null+++ b/solr/example/etc/create-solrtest.keystore.sh@@ -0,0 +1,37 @@+#!/bin/bash -ex++# Licensed to the Apache Software Foundation (ASF) under one or more+# contributor license agreements. See the NOTICE file distributed with+# this work for additional information regarding copyright ownership.+# The ASF licenses this file to You under the Apache License, Version 2.0+# (the "License"); you may not use this file except in compliance with+# the License. You may obtain a copy of the License at+#+# http://www.apache.org/licenses/LICENSE-2.0+#+# Unless required by applicable law or agreed to in writing, software+# distributed under the License is distributed on an "AS IS" BASIS,+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+# See the License for the specific language governing permissions and+# limitations under the License.++############++# This script shows how the solrtest.keystore file used for solr tests+# and these example configs was generated.+#+# Running this script should only be neccessary if the keystore file+# needs to be replaced, which shouldn't be required until sometime arround+# the year 4751.+#+# NOTE: the "-ext" option used in the "keytool" command requires that you have+# the java7 version of keytool, but the generated key will work with any+# version of java++echo "### remove old keystore"+rm -f solrtest.keystore++echo "### create keystore and keys"+keytool -keystore solrtest.keystore -storepass "secret" -alias solrtest -keypass "secret" -genkey -keyalg RSA -dname "cn=localhost, ou=SolrTest, o=lucene.apache.org, c=US" -ext "san=ip:127.0.0.1" -validity 999999++diff --git a/solr/example/etc/jetty.xml b/solr/example/etc/jetty.xmlindex e2674125a84..7596a6cde7c 100644--- a/solr/example/etc/jetty.xml+++ b/solr/example/etc/jetty.xml@@ -60,6 +60,43 @@</Arg></Call>+ <!-- if the connector below is uncommented, then jetty will also accept SSL+ connections on port 8984, using a self signed certificate and can+ optionally require the client to authenticate with a certificate.+ (which can be the same as the server certificate_++ # Run solr example with SSL on port 8984+ java -jar start.jar+ #+ # Run post.jar so that it trusts the server cert...+ java -Djavax.net.ssl.trustStore=../etc/solrtest.keystore -Durl=https://localhost:8984/solr/update -jar post.jar *.xml++ # Run solr example with SSL requiring client certs on port 8984+ java -Djetty.ssl.clientAuth=true -jar start.jar+ #+ # Run post.jar so that it trusts the server cert,+ # and authenticates with a client cert+ java -Djavax.net.ssl.keyStorePassword=secret -Djavax.net.ssl.keyStore=../etc/solrtest.keystore -Djavax.net.ssl.trustStore=../etc/solrtest.keystore -Durl=https://localhost:8984/solr/update -jar post.jar *.xml++ -->+ <!--+ <Call name="addConnector">+ <Arg>+ <New class="org.eclipse.jetty.server.ssl.SslSelectChannelConnector">+ <Arg>+ <New class="org.eclipse.jetty.http.ssl.SslContextFactory">+ <Set name="keyStore"><SystemProperty name="jetty.home" default="."/>/etc/solrtest.keystore</Set>+ <Set name="keyStorePassword">secret</Set>+ <Set name="needClientAuth"><SystemProperty name="jetty.ssl.clientAuth" default="false"/></Set>+ </New>+ </Arg>+ <Set name="port"><SystemProperty name="jetty.ssl.port" default="8984"/></Set>+ <Set name="maxIdleTime">30000</Set>+ </New>+ </Arg>+ </Call>+ -->+<!-- =========================================================== --><!-- Set handler Collection Structure --><!-- =========================================================== -->diff --git a/solr/example/etc/solrtest.keystore b/solr/example/etc/solrtest.keystorenew file mode 100644index 00000000000..bcc6ec05d71Binary files /dev/null and b/solr/example/etc/solrtest.keystore differdiff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/SolrExampleJettyTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/SolrExampleJettyTest.javaindex 94e23d06e01..0803ce39ab1 100644--- a/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/SolrExampleJettyTest.java+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/embedded/SolrExampleJettyTest.java@@ -20,10 +20,19 @@ package org.apache.solr.client.solrj.embedded;import org.apache.solr.client.solrj.SolrExampleTests;import org.apache.solr.client.solrj.impl.HttpSolrServer;import org.apache.solr.util.ExternalPaths;++import java.io.File;+import java.util.Map;+import java.util.HashMap;+import org.junit.Assert;+import org.junit.AfterClass;import org.junit.BeforeClass;import org.junit.Test;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;+/*** TODO? perhaps use:* http://docs.codehaus.org/display/JETTY/ServletTester@@ -32,11 +41,73 @@ import org.junit.Test;*/public class SolrExampleJettyTest extends SolrExampleTests {+ private static Logger log = LoggerFactory.getLogger(SolrExampleJettyTest.class);++ private static boolean manageSslProps = true;++ private static final File TEST_KEYSTORE = new File(ExternalPaths.SOURCE_HOME,+ "example/etc/solrtest.keystore");++ private static final Map<String,String> SSL_PROPS = new HashMap<String,String>();+ static {+ SSL_PROPS.put("tests.jettySsl","false");+ SSL_PROPS.put("tests.jettySsl.clientAuth","false");+ SSL_PROPS.put("javax.net.ssl.keyStore", TEST_KEYSTORE.getAbsolutePath());+ SSL_PROPS.put("javax.net.ssl.keyStorePassword","secret");+ SSL_PROPS.put("javax.net.ssl.trustStore", TEST_KEYSTORE.getAbsolutePath());+ SSL_PROPS.put("javax.net.ssl.trustStorePassword","secret");+ }+@BeforeClasspublic static void beforeTest() throws Exception {++ // // //++ // :TODO: SOLR-4394 promote SSL up to SolrJettyTestBase?++ // consume the same amount of random no matter what+ final boolean trySsl = random().nextBoolean();+ final boolean trySslClientAuth = random().nextBoolean();++ // only randomize SSL if none of the SSL_PROPS are already set+ final Map<Object,Object> sysprops = System.getProperties();+ for (String prop : SSL_PROPS.keySet()) {+ if (sysprops.containsKey(prop)) {+ log.info("System property explicitly set, so skipping randomized ssl properties: " + prop);+ manageSslProps = false;+ break;+ }+ }++ assertTrue("test keystore does not exist, can't be used for randomized " ++ "ssl testing: " + TEST_KEYSTORE.getAbsolutePath(),+ TEST_KEYSTORE.exists() );++ if (manageSslProps) {+ log.info("Randomized ssl ({}) and clientAuth ({})", trySsl, trySslClientAuth);+ for (String prop : SSL_PROPS.keySet()) {+ System.setProperty(prop, SSL_PROPS.get(prop));+ }+ // now explicitly re-set the two random values+ System.setProperty("tests.jettySsl", String.valueOf(trySsl));+ System.setProperty("tests.jettySsl.clientAuth", String.valueOf(trySslClientAuth));+ }+ // // //++createJetty(ExternalPaths.EXAMPLE_HOME, null, null);}+ @AfterClass+ public static void afterTest() throws Exception {+ if (manageSslProps) {+ for (String prop : SSL_PROPS.keySet()) {+ System.clearProperty(prop);+ }+ }+ }++@Testpublic void testBadSetup(){diff --git a/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.java b/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.javaindex 9bd9ecb4ef6..69f9f8d6c82 100755--- a/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.java+++ b/solr/test-framework/src/java/org/apache/solr/SolrJettyTestBase.java@@ -86,7 +86,7 @@ abstract public class SolrJettyTestBase extends SolrTestCaseJ4if (jetty != null) {try {// setup the server...- String url = "http://127.0.0.1:"+port+context;+ String url = jetty.getBaseUrl().toString();HttpSolrServer s = new HttpSolrServer( url );s.setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT);s.setDefaultMaxConnectionsPerHost(100);