Apache Solr open-source search software
SOLR-15657: Allow setting any and all standard SecureRandom / Security Provider usage to a non secure SecureRandom.
[email protected]26cf3f1fa0dd
diff --git a/gradle/testing/randomization/policies/solr-tests.policy b/gradle/testing/randomization/policies/solr-tests.policyindex ed8313f47d3..297647aa2ae 100644--- a/gradle/testing/randomization/policies/solr-tests.policy+++ b/gradle/testing/randomization/policies/solr-tests.policy@@ -40,6 +40,8 @@ grant {permission java.nio.file.LinkPermission "hard";+ permission java.security.SecurityPermission "putProviderProperty.SolrTestNonSecure";+// all possibilities of accepting/binding/connections on localhost with ports >=1024:permission java.net.SocketPermission "localhost:1024-", "accept,listen,connect,resolve";permission java.net.SocketPermission "127.0.0.1:1024-", "accept,listen,connect,resolve";diff --git a/solr/benchmark/src/java/org/apache/solr/bench/MiniClusterState.java b/solr/benchmark/src/java/org/apache/solr/bench/MiniClusterState.javaindex 18f3c2ca0f6..33c7a651c18 100755--- a/solr/benchmark/src/java/org/apache/solr/bench/MiniClusterState.java+++ b/solr/benchmark/src/java/org/apache/solr/bench/MiniClusterState.java@@ -50,6 +50,7 @@ import org.apache.solr.common.util.IOUtils;import org.apache.solr.common.util.NamedList;import org.apache.solr.common.util.SolrNamedThreadFactory;import org.apache.solr.common.util.SuppressForbidden;+import org.apache.solr.util.SolrTestNonSecureRandomProvider;import org.openjdk.jmh.annotations.Level;import org.openjdk.jmh.annotations.Scope;import org.openjdk.jmh.annotations.Setup;@@ -97,6 +98,9 @@ public class MiniClusterState {/** The Delete mini cluster. */boolean deleteMiniCluster = true;+ /** Unless overridden we ensure SecureRandoms do not block. */+ boolean doNotWeakenSecureRandom = Boolean.getBoolean("doNotWeakenSecureRandom");+/** The Mini cluster base dir. */Path miniClusterBaseDir;@@ -175,6 +179,11 @@ public class MiniClusterState {public void doSetup(BenchmarkParams benchmarkParams, BaseBenchState baseBenchState)throws Exception {+ if (!doNotWeakenSecureRandom) {+ // remove all blocking from all secure randoms+ SolrTestNonSecureRandomProvider.injectProvider();+ }+workDir = System.getProperty("workBaseDir", "build/work");log("");diff --git a/solr/test-framework/src/java/org/apache/solr/util/NotSecurePseudoRandomSpi.java b/solr/test-framework/src/java/org/apache/solr/util/NotSecurePseudoRandomSpi.javanew file mode 100644index 00000000000..f77287efc9e--- /dev/null+++ b/solr/test-framework/src/java/org/apache/solr/util/NotSecurePseudoRandomSpi.java@@ -0,0 +1,89 @@+/*+ * 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.+ */+package org.apache.solr.util;++import java.security.SecureRandomParameters;+import java.security.SecureRandomSpi;+import java.util.Random;+import java.util.concurrent.ThreadLocalRandom;++/**+ * A mocked up instance of SecureRandom that just uses {@link Random} under the covers.+ * This is to prevent blocking issues that arise in platform default+ * SecureRandom instances due to too many instances / not enough random entropy.+ * Tests do not need secure SSL.+ */+public class NotSecurePseudoRandomSpi extends SecureRandomSpi {++ @Override+ protected void engineSetSeed(byte[] seed) {++ }++ @Override+ protected void engineNextBytes(byte[] bytes) {+ fillData(bytes);+ }++ @Override+ protected byte[] engineGenerateSeed(int numBytes) {+ return new byte[numBytes];+ }++ /**+ * Helper method that can be used to fill an array with non-zero data.+ * (Attempted workarround of Solaris SSL Padding bug: SOLR-9068)+ */+ private static final byte[] fillData(byte[] data) {+ ThreadLocalRandom.current().nextBytes(data);+ return data;+ }+++ public NotSecurePseudoRandomSpi() {+ super();+ }++ /** returns a new byte[] filled with static data */+ public byte[] generateSeed(int numBytes) {+ return fillData(new byte[numBytes]);+ }++ /** fills the byte[] with static data */+ public void nextBytes(byte[] bytes) {+ fillData(bytes);+ }++ public void nextBytes(byte[] bytes, SecureRandomParameters params) {+ fillData(bytes);+ }++ /** NOOP */+ public void setSeed(byte[] seed) { /* NOOP */ }++ /** NOOP */+ public void setSeed(long seed) { /* NOOP */ }++ public void reseed() {+ /* NOOP */+ }++ public void reseed(SecureRandomParameters params) {+ /* NOOP */+ }++}diff --git a/solr/test-framework/src/java/org/apache/solr/util/SolrTestNonSecureRandomProvider.java b/solr/test-framework/src/java/org/apache/solr/util/SolrTestNonSecureRandomProvider.javanew file mode 100644index 00000000000..ee7ce7a8e76--- /dev/null+++ b/solr/test-framework/src/java/org/apache/solr/util/SolrTestNonSecureRandomProvider.java@@ -0,0 +1,79 @@+/*+ * 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.+ */+package org.apache.solr.util;++import java.security.NoSuchAlgorithmException;+import java.security.Provider;+import java.security.SecureRandom;+import java.security.Security;++public class SolrTestNonSecureRandomProvider extends Provider {++ public SolrTestNonSecureRandomProvider() {+ super("SolrTestNonSecure",+ "1.0",+ "A Test only, non secure provider");+ put("SecureRandom.SHA1PRNG", NotSecurePseudoRandomSpi.class.getName());+ put("SecureRandom.NativePRNG", NotSecurePseudoRandomSpi.class.getName());+ put("SecureRandom.DRBG", NotSecurePseudoRandomSpi.class.getName());++ put("SecureRandom.SHA1PRNG ThreadSafe", "true");+ put("SecureRandom.NativePRNG ThreadSafe", "true");+ put("SecureRandom.DRBG ThreadSafe", "true");++ put("SecureRandom.SHA1PRNG ImplementedIn", "Software");+ put("SecureRandom.NativePRNG ImplementedIn", "Software");+ put("SecureRandom.DRBG ImplementedIn", "Software");+ }++ public static void injectProvider() {+ // Install our non secure solr test secure random provider+ Provider[] secureRandomProviders =+ Security.getProviders("SecureRandom.SHA1PRNG");+ if ((secureRandomProviders == null)+ || (secureRandomProviders.length < 1)+ || (!SolrTestNonSecureRandomProvider.class.equals(+ secureRandomProviders[0].getClass()))) {+ Security.insertProviderAt(new SolrTestNonSecureRandomProvider(), 1);+ }++ // Assert that new SecureRandom() and+ // SecureRandom.getInstance("SHA1PRNG") return a SecureRandom backed+ // by our non secure test provider.+ SecureRandom rng1 = new SecureRandom();+ if (!SolrTestNonSecureRandomProvider.class.equals(+ rng1.getProvider().getClass())) {+ throw new SecurityException(+ "new SecureRandom() backed by wrong Provider: "+ + rng1.getProvider().getClass());+ }++ boolean skipCheck = false;+ SecureRandom rng2 = null;+ try {+ rng2 = SecureRandom.getInstance("SHA1PRNG");+ } catch (NoSuchAlgorithmException e) {+ skipCheck = true;+ }+ if (!skipCheck && !SolrTestNonSecureRandomProvider.class.equals(+ rng2.getProvider().getClass())) {+ throw new SecurityException(+ "SecureRandom.getInstance(\"SHA1PRNG\") backed by wrong"+ + " Provider: " + rng2.getProvider().getClass());+ }+ }+}\ No newline at end of file