SOLR-13921: Processing UpdateRequest with delegation token throws NullPointerException

Signed-off-by: Kevin Risden <krisden@apache.org>
This commit is contained in:
Istvan Farkas 2019-11-11 23:34:39 +01:00 committed by Kevin Risden
parent 30e55e2b6e
commit 21a54c4bc7
No known key found for this signature in database
GPG Key ID: 040FAE3292C5F73F
3 changed files with 66 additions and 2 deletions

View File

@ -161,6 +161,8 @@ Bug Fixes
* SOLR-13872: Fixed Backup failures - typically manifesting as NoSuchFileException - due to race conditions
in saving/reserving commit points (hossman)
* SOLR-13921: Processing UpdateRequest with delegation token throws NullPointerException (Istvan Farkas via Kevin Risden)
Other Changes
---------------------

View File

@ -25,10 +25,13 @@ import org.apache.solr.client.solrj.impl.LBHttpSolrClient;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.embedded.JettySolrRunner;
import org.apache.solr.client.solrj.request.AbstractUpdateRequest.ACTION;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.request.DelegationTokenRequest;
import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.client.solrj.response.DelegationTokenResponse;
import org.apache.solr.common.SolrException.ErrorCode;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.cloud.SolrZkClient;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.params.ModifiableSolrParams;
@ -163,7 +166,7 @@ public class TestSolrCloudWithDelegationTokens extends SolrTestCaseJ4 {
}
Thread.sleep(1000);
}
assertEquals("Did not receieve excepted status code", expectedStatusCode, lastStatusCode);
assertEquals("Did not receive expected status code", expectedStatusCode, lastStatusCode);
}
private SolrRequest getAdminRequest(final SolrParams params) {
@ -176,6 +179,16 @@ public class TestSolrCloudWithDelegationTokens extends SolrTestCaseJ4 {
}
};
}
private SolrRequest getUpdateRequest(boolean commit) {
UpdateRequest request = new UpdateRequest();
if (commit) {
request.setAction(ACTION.COMMIT, false, false);
}
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", "dummy_id");
request.add(doc);
return request;
}
private int getStatusCode(String token, final String user, final String op, HttpSolrClient client)
throws Exception {
@ -225,6 +238,16 @@ public class TestSolrCloudWithDelegationTokens extends SolrTestCaseJ4 {
}
}
private void doSolrRequest(HttpSolrClient client, SolrRequest request, String collectionName,
int expectedStatusCode) throws Exception {
try {
client.request(request, collectionName);
assertEquals(HttpStatus.SC_OK, expectedStatusCode);
} catch (HttpSolrClient.RemoteSolrException ex) {
assertEquals(expectedStatusCode, ex.code());
}
}
private void verifyTokenValid(String token) throws Exception {
// pass with token
doSolrRequest(token, HttpStatus.SC_OK, solrClientPrimary);
@ -415,4 +438,43 @@ public class TestSolrCloudWithDelegationTokens extends SolrTestCaseJ4 {
ssWToken.close();
}
}
/**
* Test HttpSolrServer's delegation token support for Update Requests
*/
@Test
public void testDelegationTokenSolrClientWithUpdateRequests() throws Exception {
String collectionName = "testDelegationTokensWithUpdate";
// Get token
String token = getDelegationToken(null, "bar", solrClientPrimary);
assertNotNull(token);
// Tests with update request.
// Before SOLR-13921, the request without commit will fail with a NullpointerException in DelegationTokenHttpSolrClient.createMethod
// due to a missing null check in the createMethod. (When requesting a commit, the setAction method will call setParams on the
// request so there is no NPE in the createMethod.)
final HttpSolrClient scUpdateWToken = new HttpSolrClient.Builder(solrClientPrimary.getBaseURL().toString())
.withKerberosDelegationToken(token)
.withResponseParser(solrClientPrimary.getParser())
.build();
// Create collection
CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(collectionName, 1, 1);
create.process(scUpdateWToken);
try {
// test update request with token via property and commit=true
SolrRequest request = getUpdateRequest(true);
doSolrRequest(scUpdateWToken, request, collectionName, HttpStatus.SC_OK);
// test update request with token via property and commit=false
request = getUpdateRequest(false);
doSolrRequest(scUpdateWToken, request, collectionName, HttpStatus.SC_OK);
} finally {
scUpdateWToken.close();
}
}
}

View File

@ -90,7 +90,7 @@ public class DelegationTokenHttpSolrClient extends HttpSolrClient {
@Override
protected HttpRequestBase createMethod(final SolrRequest request, String collection) throws IOException, SolrServerException {
SolrParams params = request.getParams();
if (params.getParams(DELEGATION_TOKEN_PARAM) != null) {
if (params != null && params.getParams(DELEGATION_TOKEN_PARAM) != null) {
throw new IllegalArgumentException(DELEGATION_TOKEN_PARAM + " parameter not supported");
}
return super.createMethod(request, collection);