This is an automated email from the ASF dual-hosted git repository.
broustant pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr-sandbox.git
The following commit(s) were added to refs/heads/main by this push:
new c1c6a8e Add tests for EncryptionRequestHandler request params. (#122)
c1c6a8e is described below
commit c1c6a8e8cafcf8cf7ea3bdb5b4e6a81483b55c47
Author: Bruno Roustant <[email protected]>
AuthorDate: Tue Aug 12 12:17:14 2025 +0200
Add tests for EncryptionRequestHandler request params. (#122)
---
.../solr/encryption/EncryptionRequestHandler.java | 30 ++++----
.../encryption/EncryptionRequestHandlerTest.java | 85 ++++++++++++++++++++++
2 files changed, 101 insertions(+), 14 deletions(-)
diff --git
a/encryption/src/main/java/org/apache/solr/encryption/EncryptionRequestHandler.java
b/encryption/src/main/java/org/apache/solr/encryption/EncryptionRequestHandler.java
index d1ada0a..018e8e7 100644
---
a/encryption/src/main/java/org/apache/solr/encryption/EncryptionRequestHandler.java
+++
b/encryption/src/main/java/org/apache/solr/encryption/EncryptionRequestHandler.java
@@ -267,7 +267,8 @@ public class EncryptionRequestHandler extends
RequestHandlerBase {
}
}
- private static String getRequestKeyId(SolrQueryRequest req,
SolrQueryResponse rsp) throws IOException {
+ // Visible for testing.
+ static String getRequestKeyId(SolrQueryRequest req, SolrQueryResponse rsp)
throws IOException {
String keyId = req.getParams().get(PARAM_KEY_ID);
if (keyId == null || keyId.isEmpty()) {
rsp.add(STATUS, STATUS_FAILURE);
@@ -293,7 +294,7 @@ public class EncryptionRequestHandler extends
RequestHandlerBase {
log.debug("Encrypt request for keyId={}", keyId);
RequestStatus requestStatus = RequestStatus.ERROR;
try {
- requestStatus = handleRequestLocallyInner(req, rsp, keyId);
+ requestStatus = localRequestInner(req, rsp, keyId);
} finally {
String statusValue = requestStatus.success ? STATUS_SUCCESS :
STATUS_FAILURE;
rsp.add(STATUS, statusValue);
@@ -305,7 +306,7 @@ public class EncryptionRequestHandler extends
RequestHandlerBase {
}
}
- private RequestStatus handleRequestLocallyInner(
+ private RequestStatus localRequestInner(
SolrQueryRequest req,
SolrQueryResponse rsp,
String keyId)
@@ -351,7 +352,7 @@ public class EncryptionRequestHandler extends
RequestHandlerBase {
}
}
- private void ensureNonEmptyCommitDataForEmptyCommit(Map<String, String>
commitData) {
+ private static void ensureNonEmptyCommitDataForEmptyCommit(Map<String,
String> commitData) {
if (commitData.isEmpty()) {
// Ensure that there is some data in the commit user data so that an
empty commit
// (with no change) is allowed.
@@ -359,7 +360,7 @@ public class EncryptionRequestHandler extends
RequestHandlerBase {
}
}
- private boolean checkEncryptionComplete(
+ private static boolean checkEncryptionComplete(
SolrQueryRequest req,
String keyId,
SegmentInfos segmentInfos)
@@ -380,13 +381,13 @@ public class EncryptionRequestHandler extends
RequestHandlerBase {
return encryptionComplete;
}
- private boolean isCommitActiveKeyId(String keyId, SegmentInfos segmentInfos)
{
+ private static boolean isCommitActiveKeyId(String keyId, SegmentInfos
segmentInfos) {
String keyRef = getActiveKeyRefFromCommit(segmentInfos.getUserData());
String activeKeyId = keyRef == null ? null : getKeyIdFromCommit(keyRef,
segmentInfos.getUserData());
return Objects.equals(keyId, activeKeyId);
}
- private boolean areAllSegmentsEncryptedWithKeyId(@Nullable String keyId,
+ private static boolean areAllSegmentsEncryptedWithKeyId(@Nullable String
keyId,
SolrCore core,
SegmentInfos segmentInfos)
throws IOException {
DirectoryFactory directoryFactory = core.getDirectoryFactory();
@@ -404,13 +405,13 @@ public class EncryptionRequestHandler extends
RequestHandlerBase {
}
}
- private boolean areAllLogsEncryptedWithKeyId(String keyId, SolrCore core,
SegmentInfos segmentInfos)
+ private static boolean areAllLogsEncryptedWithKeyId(String keyId, SolrCore
core, SegmentInfos segmentInfos)
throws IOException {
EncryptionUpdateLog updateLog = getEncryptionUpdateLog(core);
return updateLog == null || updateLog.areAllLogsEncryptedWithKeyId(keyId,
segmentInfos.getUserData());
}
- private EncryptionUpdateLog getEncryptionUpdateLog(SolrCore core) {
+ private static EncryptionUpdateLog getEncryptionUpdateLog(SolrCore core) {
UpdateHandler updateHandler = core.getUpdateHandler();
if (updateHandler == null) {
return null;
@@ -424,7 +425,7 @@ public class EncryptionRequestHandler extends
RequestHandlerBase {
return (EncryptionUpdateLog) updateHandler.getUpdateLog();
}
- private void commitEncryptionComplete(String keyId,
+ private static void commitEncryptionComplete(String keyId,
SegmentInfos segmentInfos,
SolrQueryRequest req) throws
IOException {
assert isCommitActiveKeyId(keyId, segmentInfos);
@@ -439,7 +440,7 @@ public class EncryptionRequestHandler extends
RequestHandlerBase {
req.getCore().getUpdateHandler().commit(commitCmd);
}
- private RequestStatus checkEncryptionOngoing(String keyId, SolrQueryRequest
req) {
+ private static RequestStatus checkEncryptionOngoing(String keyId,
SolrQueryRequest req) {
synchronized (pendingEncryptionLock) {
PendingKeyId pendingKeyId =
pendingEncryptions.get(req.getCore().getName());
if (pendingKeyId != null) {
@@ -557,7 +558,8 @@ public class EncryptionRequestHandler extends
RequestHandlerBase {
: new RequestStatus(collectionState.state,
collectionState.state.isSuccess());
}
- private TimeOut getTimeOut(SolrQueryRequest req) {
+ // Visible for testing.
+ TimeOut getTimeOut(SolrQueryRequest req) {
long timeAllowedMs = req.getParams().getLong(TIME_ALLOWED, 0);
return timeAllowedMs <= 0 ?
null
@@ -635,7 +637,7 @@ public class EncryptionRequestHandler extends
RequestHandlerBase {
return new ModifiableSolrParams().set(PARAM_KEY_ID, keyId == null ?
NO_KEY_ID : keyId);
}
- private State sendEncryptionRequestWithRetry(
+ private static State sendEncryptionRequestWithRetry(
Replica replica,
SolrQueryRequest req,
ModifiableSolrParams params,
@@ -656,7 +658,7 @@ public class EncryptionRequestHandler extends
RequestHandlerBase {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Failed
encryption request to replica " + replica.getName() + " for keyId " + keyId);
}
- private SimpleSolrResponse sendEncryptionRequest(
+ private static SimpleSolrResponse sendEncryptionRequest(
Replica replica,
SolrQueryRequest req,
ModifiableSolrParams params)
diff --git
a/encryption/src/test/java/org/apache/solr/encryption/EncryptionRequestHandlerTest.java
b/encryption/src/test/java/org/apache/solr/encryption/EncryptionRequestHandlerTest.java
index 32fac67..0dcf9bc 100644
---
a/encryption/src/test/java/org/apache/solr/encryption/EncryptionRequestHandlerTest.java
+++
b/encryption/src/test/java/org/apache/solr/encryption/EncryptionRequestHandlerTest.java
@@ -23,8 +23,14 @@ import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.cloud.MiniSolrCloudCluster;
import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.common.params.SolrParams;
import org.apache.solr.embedded.JettySolrRunner;
import org.apache.solr.encryption.crypto.AesCtrEncrypterFactory;
+import org.apache.solr.request.SolrQueryRequest;
+import org.apache.solr.request.SolrQueryRequestBase;
+import org.apache.solr.response.SolrQueryResponse;
+import org.apache.solr.util.TimeOut;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -36,6 +42,7 @@ import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import static
org.apache.solr.encryption.EncryptionDirectoryFactory.PROPERTY_INNER_ENCRYPTION_DIRECTORY_FACTORY;
+import static org.apache.solr.common.params.CommonParams.TIME_ALLOWED;
import static org.apache.solr.encryption.EncryptionRequestHandler.NO_KEY_ID;
import static
org.apache.solr.encryption.EncryptionRequestHandler.STATUS_SUCCESS;
import static org.apache.solr.encryption.EncryptionUtil.getKeyIdFromCommit;
@@ -298,6 +305,84 @@ public class EncryptionRequestHandlerTest extends
SolrCloudTestCase {
cluster.waitForNode(node, 30);
}
+ /**
+ * Test for getRequestKeyId method.
+ * The method should return the key id passed in the request parameters. If
the key id is not present,
+ * then it should throw an exception.
+ */
+ @Test
+ public void testGetRequestKeyId() throws Exception {
+ // Test valid key ID
+ ModifiableSolrParams params = new ModifiableSolrParams();
+ params.set(EncryptionRequestHandler.PARAM_KEY_ID, KEY_ID_1);
+ String keyId =
EncryptionRequestHandler.getRequestKeyId(createRequest(params), new
SolrQueryResponse());
+ assertEquals(KEY_ID_1, keyId);
+
+ // Test NO_KEY_ID returns null
+ params.set(EncryptionRequestHandler.PARAM_KEY_ID, NO_KEY_ID);
+ keyId = EncryptionRequestHandler.getRequestKeyId(createRequest(params),
new SolrQueryResponse());
+ assertNull(keyId);
+
+ // Test missing key ID parameter
+ params.remove(EncryptionRequestHandler.PARAM_KEY_ID);
+ SolrQueryResponse rsp1 = new SolrQueryResponse();
+ IOException e1 = assertThrows(
+ "Expected IOException for missing key ID parameter",
+ IOException.class,
+ () -> EncryptionRequestHandler.getRequestKeyId(createRequest(params),
rsp1));
+
assertTrue(e1.getMessage().contains(EncryptionRequestHandler.PARAM_KEY_ID));
+ assertEquals(EncryptionRequestHandler.STATUS_FAILURE,
rsp1.getValues().get(EncryptionRequestHandler.STATUS));
+
+ // Test empty key ID parameter
+ params.set(EncryptionRequestHandler.PARAM_KEY_ID, "");
+ SolrQueryResponse rsp2 = new SolrQueryResponse();
+ IOException e2 = assertThrows(
+ "Expected IOException for empty key ID parameter",
+ IOException.class,
+ () -> EncryptionRequestHandler.getRequestKeyId(createRequest(params),
rsp2));
+
assertTrue(e2.getMessage().contains(EncryptionRequestHandler.PARAM_KEY_ID));
+ assertEquals(EncryptionRequestHandler.STATUS_FAILURE,
rsp2.getValues().get(EncryptionRequestHandler.STATUS));
+ }
+
+ /**
+ * Test for getTimeOut method.
+ * This method parses the timeout parameters from requests.
+ */
+ @Test
+ public void testGetTimeOut() throws Exception {
+ try (EncryptionRequestHandler handler = new EncryptionRequestHandler()) {
+
+ // Test with no timeout parameter
+ ModifiableSolrParams params = new ModifiableSolrParams();
+ SolrQueryRequest req = createRequest(params);
+ TimeOut timeOut = handler.getTimeOut(req);
+ assertNull(timeOut);
+
+ // Test with zero timeout
+ params.set(TIME_ALLOWED, "0");
+ req = createRequest(params);
+ timeOut = handler.getTimeOut(req);
+ assertNull(timeOut);
+
+ // Test with negative timeout
+ params.set(TIME_ALLOWED, "-1000");
+ req = createRequest(params);
+ timeOut = handler.getTimeOut(req);
+ assertNull(timeOut);
+
+ // Test with valid timeout
+ params.set(TIME_ALLOWED, "5000");
+ req = createRequest(params);
+ timeOut = handler.getTimeOut(req);
+ assertNotNull(timeOut);
+ assertTrue(timeOut.timeLeft(TimeUnit.MILLISECONDS) > 0);
+ }
+ }
+
+ SolrQueryRequest createRequest(SolrParams params) {
+ return new SolrQueryRequestBase(null, params) {};
+ }
+
private static void clearMockValues() {
forceClearText = false;
soleKeyIdAllowed = null;