Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

SOLR-16491: Create v2 equivalent for /admin/info/key (#1123)

Jason Gerlowskic86128855807
diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.java
index ec5f4fc3002..00eb7454b8d 100644
--- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java
+++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java
@@ -40,7 +40,6 @@ import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.nio.file.Path;
import java.nio.file.Paths;
-import java.security.spec.InvalidKeySpecException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
@@ -147,6 +146,7 @@ import org.apache.solr.security.HttpClientBuilderPlugin;
import org.apache.solr.security.PKIAuthenticationPlugin;
import org.apache.solr.security.PublicKeyHandler;
import org.apache.solr.security.SecurityPluginHolder;
+import org.apache.solr.security.SolrNodeKeyPair;
import org.apache.solr.update.SolrCoreState;
import org.apache.solr.update.UpdateShardHandler;
import org.apache.solr.util.OrderedExecutor;
@@ -236,6 +236,8 @@ public class CoreContainer {
protected final Path solrHome;
+ protected final SolrNodeKeyPair nodeKeyPair;
+
protected final CoresLocator coresLocator;
private volatile String hostName;
@@ -388,12 +390,8 @@ public class CoreContainer {
this.cfg = requireNonNull(config);
this.loader = config.getSolrResourceLoader();
this.solrHome = config.getSolrHome();
-
- try {
- containerHandlers.put(PublicKeyHandler.PATH, new PublicKeyHandler(cfg.getCloudConfig()));
- } catch (IOException | InvalidKeySpecException e) {
- throw new RuntimeException("Bad PublicKeyHandler configuration.", e);
- }
+ this.nodeKeyPair = new SolrNodeKeyPair(cfg.getCloudConfig());
+ containerHandlers.put(PublicKeyHandler.PATH, new PublicKeyHandler(nodeKeyPair));
if (null != this.cfg.getBooleanQueryMaxClauseCount()) {
IndexSearcher.setMaxClauseCount(this.cfg.getBooleanQueryMaxClauseCount());
}
@@ -638,6 +636,7 @@ public class CoreContainer {
*/
protected CoreContainer(Object testConstructor) {
solrHome = null;
+ nodeKeyPair = null;
loader = null;
coresLocator = null;
cfg = null;
@@ -1069,6 +1068,15 @@ public class CoreContainer {
.to(CoreContainer.class)
.in(Singleton.class);
}
+ })
+ .register(
+ new AbstractBinder() {
+ @Override
+ protected void configure() {
+ bindFactory(new InjectionFactories.SingletonFactory<>(nodeKeyPair))
+ .to(SolrNodeKeyPair.class)
+ .in(Singleton.class);
+ }
});
jerseyAppHandler = new ApplicationHandler(containerHandlers.getJerseyEndpoints());
diff --git a/solr/core/src/java/org/apache/solr/security/PKIAuthenticationPlugin.java b/solr/core/src/java/org/apache/solr/security/PKIAuthenticationPlugin.java
index 68b28c7592d..a126bfaa3a6 100644
--- a/solr/core/src/java/org/apache/solr/security/PKIAuthenticationPlugin.java
+++ b/solr/core/src/java/org/apache/solr/security/PKIAuthenticationPlugin.java
@@ -103,6 +103,9 @@ public class PKIAuthenticationPlugin extends AuthenticationPlugin
return interceptorRegistered;
}
+ // TODO We only use PublicKeyHandler here to gain access to the underlying keypair; let's nuke the
+ // indirection and
+ // just pass in the SolrNodeKeyPair instance directly
public PKIAuthenticationPlugin(
CoreContainer cores, String nodeName, PublicKeyHandler publicKeyHandler) {
this.publicKeyHandler = publicKeyHandler;
@@ -461,7 +464,7 @@ public class PKIAuthenticationPlugin extends AuthenticationPlugin
String s = usr + " " + System.currentTimeMillis();
byte[] payload = s.getBytes(UTF_8);
- byte[] payloadCipher = publicKeyHandler.keyPair.encrypt(ByteBuffer.wrap(payload));
+ byte[] payloadCipher = publicKeyHandler.getKeyPair().encrypt(ByteBuffer.wrap(payload));
String base64Cipher = Base64.getEncoder().encodeToString(payloadCipher);
log.trace("generateToken: usr={} token={}", usr, base64Cipher);
return Optional.of(myNodeName + " " + base64Cipher);
@@ -476,7 +479,7 @@ public class PKIAuthenticationPlugin extends AuthenticationPlugin
String s = myNodeName + " " + user + " " + Instant.now().toEpochMilli();
byte[] payload = s.getBytes(UTF_8);
- byte[] signature = publicKeyHandler.keyPair.signSha256(payload);
+ byte[] signature = publicKeyHandler.getKeyPair().signSha256(payload);
String base64Signature = Base64.getEncoder().encodeToString(signature);
return Optional.of(s + " " + base64Signature);
}
@@ -505,7 +508,7 @@ public class PKIAuthenticationPlugin extends AuthenticationPlugin
@VisibleForTesting
public String getPublicKey() {
- return publicKeyHandler.getPublicKey();
+ return publicKeyHandler.getKeyPair().getPublicKeyStr();
}
public static final String HEADER = "SolrAuth";
diff --git a/solr/core/src/java/org/apache/solr/security/PublicKeyAPI.java b/solr/core/src/java/org/apache/solr/security/PublicKeyAPI.java
new file mode 100644
index 00000000000..94ee5b099ac
--- /dev/null
+++ b/solr/core/src/java/org/apache/solr/security/PublicKeyAPI.java
@@ -0,0 +1,61 @@
+/*
+ * 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.security;
+
+import static org.apache.solr.client.solrj.impl.BinaryResponseParser.BINARY_CONTENT_TYPE_V2;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import io.swagger.v3.oas.annotations.media.Schema;
+import javax.inject.Inject;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import org.apache.solr.api.JerseyResource;
+import org.apache.solr.jersey.PermissionName;
+import org.apache.solr.jersey.SolrJerseyResponse;
+
+/**
+ * V2 API for fetching the public key of the receiving node.
+ *
+ * <p>This API is analogous to the v1 /admin/info/key endpoint.
+ */
+@Path("/node/key")
+public class PublicKeyAPI extends JerseyResource {
+
+ private final SolrNodeKeyPair nodeKeyPair;
+
+ @Inject
+ public PublicKeyAPI(SolrNodeKeyPair nodeKeyPair) {
+ this.nodeKeyPair = nodeKeyPair;
+ }
+
+ @GET
+ @Produces({"application/json", "application/xml", BINARY_CONTENT_TYPE_V2})
+ @PermissionName(PermissionNameProvider.Name.ALL)
+ public PublicKeyResponse getPublicKey() {
+ final PublicKeyResponse response = instantiateJerseyResponse(PublicKeyResponse.class);
+ response.key = nodeKeyPair.getKeyPair().getPublicKeyStr();
+ return response;
+ }
+
+ public static class PublicKeyResponse extends SolrJerseyResponse {
+ @JsonProperty("key")
+ @Schema(description = "The public key of the receiving Solr node.")
+ public String key;
+ }
+}
diff --git a/solr/core/src/java/org/apache/solr/security/PublicKeyHandler.java b/solr/core/src/java/org/apache/solr/security/PublicKeyHandler.java
index 257ddd32d25..9dbde9d0966 100644
--- a/solr/core/src/java/org/apache/solr/security/PublicKeyHandler.java
+++ b/solr/core/src/java/org/apache/solr/security/PublicKeyHandler.java
@@ -18,12 +18,13 @@
package org.apache.solr.security;
import com.google.common.annotations.VisibleForTesting;
-import java.io.IOException;
-import java.net.URL;
-import java.security.spec.InvalidKeySpecException;
-import org.apache.solr.common.StringUtils;
-import org.apache.solr.core.CloudConfig;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import org.apache.solr.api.Api;
+import org.apache.solr.api.JerseyResource;
import org.apache.solr.handler.RequestHandlerBase;
+import org.apache.solr.handler.api.V2ApiUtils;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.response.SolrQueryResponse;
import org.apache.solr.util.CryptoKeys;
@@ -31,41 +32,25 @@ import org.apache.solr.util.CryptoKeys;
public class PublicKeyHandler extends RequestHandlerBase {
public static final String PATH = "/admin/info/key";
- final CryptoKeys.RSAKeyPair keyPair;
+ private final SolrNodeKeyPair nodeKeyPair;
@VisibleForTesting
public PublicKeyHandler() {
- keyPair = new CryptoKeys.RSAKeyPair();
+ this(new SolrNodeKeyPair(null));
}
- public PublicKeyHandler(CloudConfig config) throws IOException, InvalidKeySpecException {
- keyPair = createKeyPair(config);
+ public PublicKeyHandler(SolrNodeKeyPair nodeKeyPair) {
+ this.nodeKeyPair = nodeKeyPair;
}
- private CryptoKeys.RSAKeyPair createKeyPair(CloudConfig config)
- throws IOException, InvalidKeySpecException {
- if (config == null) {
- return new CryptoKeys.RSAKeyPair();
- }
-
- String publicKey = config.getPkiHandlerPublicKeyPath();
- String privateKey = config.getPkiHandlerPrivateKeyPath();
-
- // If both properties unset, then we fall back to generating a new key pair
- if (StringUtils.isEmpty(publicKey) && StringUtils.isEmpty(privateKey)) {
- return new CryptoKeys.RSAKeyPair();
- }
-
- return new CryptoKeys.RSAKeyPair(new URL(privateKey), new URL(publicKey));
- }
-
- public String getPublicKey() {
- return keyPair.getPublicKeyStr();
+ public CryptoKeys.RSAKeyPair getKeyPair() {
+ return nodeKeyPair.getKeyPair();
}
@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
- rsp.add("key", keyPair.getPublicKeyStr());
+ V2ApiUtils.squashIntoSolrResponseWithoutHeader(
+ rsp, new PublicKeyAPI(nodeKeyPair).getPublicKey());
}
@Override
@@ -82,4 +67,19 @@ public class PublicKeyHandler extends RequestHandlerBase {
public Name getPermissionName(AuthorizationContext request) {
return Name.ALL;
}
+
+ @Override
+ public Boolean registerV2() {
+ return Boolean.TRUE;
+ }
+
+ @Override
+ public Collection<Api> getApis() {
+ return new ArrayList<>();
+ }
+
+ @Override
+ public Collection<Class<? extends JerseyResource>> getJerseyResources() {
+ return List.of(PublicKeyAPI.class);
+ }
}
diff --git a/solr/core/src/java/org/apache/solr/security/SolrNodeKeyPair.java b/solr/core/src/java/org/apache/solr/security/SolrNodeKeyPair.java
new file mode 100644
index 00000000000..9eeb6b5f68e
--- /dev/null
+++ b/solr/core/src/java/org/apache/solr/security/SolrNodeKeyPair.java
@@ -0,0 +1,63 @@
+/*
+ * 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.security;
+
+import java.io.IOException;
+import java.net.URL;
+import java.security.spec.InvalidKeySpecException;
+import org.apache.solr.common.StringUtils;
+import org.apache.solr.core.CloudConfig;
+import org.apache.solr.util.CryptoKeys;
+
+/**
+ * Creates and mediates access to the CryptoKeys.RSAKeyPair used by this Solr node.
+ *
+ * <p>Expected to be created once on each Solr node for the life of that process.
+ */
+public class SolrNodeKeyPair {
+
+ private final CryptoKeys.RSAKeyPair keyPair;
+
+ public SolrNodeKeyPair(CloudConfig cloudConfig) {
+ keyPair = createKeyPair(cloudConfig);
+ }
+
+ public CryptoKeys.RSAKeyPair getKeyPair() {
+ return keyPair;
+ }
+
+ private static CryptoKeys.RSAKeyPair createKeyPair(CloudConfig config) {
+ if (config == null) {
+ return new CryptoKeys.RSAKeyPair();
+ }
+
+ String publicKey = config.getPkiHandlerPublicKeyPath();
+ String privateKey = config.getPkiHandlerPrivateKeyPath();
+
+ // If both properties unset, then we fall back to generating a new key pair
+ if (StringUtils.isEmpty(publicKey) && StringUtils.isEmpty(privateKey)) {
+ return new CryptoKeys.RSAKeyPair();
+ }
+
+ try {
+ return new CryptoKeys.RSAKeyPair(new URL(privateKey), new URL(publicKey));
+ } catch (IOException | InvalidKeySpecException e) {
+ throw new RuntimeException("Bad PublicKeyHandler configuration.", e);
+ }
+ }
+}
diff --git a/solr/core/src/test/org/apache/solr/security/PublicKeyAPITest.java b/solr/core/src/test/org/apache/solr/security/PublicKeyAPITest.java
new file mode 100644
index 00000000000..f75b32b85d9
--- /dev/null
+++ b/solr/core/src/test/org/apache/solr/security/PublicKeyAPITest.java
@@ -0,0 +1,34 @@
+/*
+ * 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.security;
+
+import org.apache.solr.SolrTestCaseJ4;
+import org.junit.Test;
+
+/** Unit test for {@link PublicKeyAPI} */
+public class PublicKeyAPITest extends SolrTestCaseJ4 {
+
+ @Test
+ public void testRetrievesPublicKey() {
+ final SolrNodeKeyPair nodeKeyPair = new SolrNodeKeyPair(null);
+
+ final PublicKeyAPI.PublicKeyResponse response = new PublicKeyAPI(nodeKeyPair).getPublicKey();
+
+ assertEquals(nodeKeyPair.getKeyPair().getPublicKeyStr(), response.key);
+ }
+}