You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by no...@apache.org on 2017/03/02 12:41:14 UTC
lucene-solr:branch_6x: SOLR-9401: NPE in TestPKIAuthenticationPlugin.
tests would retry for timeout
Repository: lucene-solr
Updated Branches:
refs/heads/branch_6x a607a2c6c -> dea29d12f
SOLR-9401: NPE in TestPKIAuthenticationPlugin. tests would retry for timeout
Project: http://git-wip-us.apache.org/repos/asf/lucene-solr/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucene-solr/commit/dea29d12
Tree: http://git-wip-us.apache.org/repos/asf/lucene-solr/tree/dea29d12
Diff: http://git-wip-us.apache.org/repos/asf/lucene-solr/diff/dea29d12
Branch: refs/heads/branch_6x
Commit: dea29d12f7c320b19c65ba2fc32687d111e4a07d
Parents: a607a2c
Author: Noble Paul <no...@apache.org>
Authored: Thu Mar 2 20:50:06 2017 +1030
Committer: Noble Paul <no...@apache.org>
Committed: Thu Mar 2 23:06:35 2017 +1030
----------------------------------------------------------------------
.../security/TestPKIAuthenticationPlugin.java | 125 ++++++++++++-------
1 file changed, 81 insertions(+), 44 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/dea29d12/solr/core/src/test/org/apache/solr/security/TestPKIAuthenticationPlugin.java
----------------------------------------------------------------------
diff --git a/solr/core/src/test/org/apache/solr/security/TestPKIAuthenticationPlugin.java b/solr/core/src/test/org/apache/solr/security/TestPKIAuthenticationPlugin.java
index a5a279f..4eb0a80 100644
--- a/solr/core/src/test/org/apache/solr/security/TestPKIAuthenticationPlugin.java
+++ b/solr/core/src/test/org/apache/solr/security/TestPKIAuthenticationPlugin.java
@@ -38,10 +38,18 @@ import org.apache.solr.util.CryptoKeys;
import static org.mockito.Mockito.*;
public class TestPKIAuthenticationPlugin extends SolrTestCaseJ4 {
+ HttpServletRequest mockReq;
+ FilterChain filterChain;
+ final AtomicReference<ServletRequest> wrappedRequestByFilter = new AtomicReference<>();
+ final AtomicReference<Header> header = new AtomicReference<>();
+ AtomicReference<Principal> principal = new AtomicReference<>();
+ BasicHttpRequest request;
+
static class MockPKIAuthenticationPlugin extends PKIAuthenticationPlugin {
SolrRequestInfo solrRequestInfo;
+
Map<String, PublicKey> remoteKeys = new HashMap<>();
public MockPKIAuthenticationPlugin(CoreContainer cores, String node) {
@@ -70,7 +78,6 @@ public class TestPKIAuthenticationPlugin extends SolrTestCaseJ4 {
}
public void test() throws Exception {
- AtomicReference<Principal> principal = new AtomicReference<>();
String nodeName = "node_x_233";
final MockPKIAuthenticationPlugin mock = new MockPKIAuthenticationPlugin(null, nodeName);
@@ -85,66 +92,96 @@ public class TestPKIAuthenticationPlugin extends SolrTestCaseJ4 {
principal.set(new BasicUserPrincipal("solr"));
mock.solrRequestInfo = new SolrRequestInfo(localSolrQueryRequest, new SolrQueryResponse());
- BasicHttpRequest request = new BasicHttpRequest("GET", "http://localhost:56565");
+ request = new BasicHttpRequest("GET", "http://localhost:56565");
mock.setHeader(request);
- final AtomicReference<Header> header = new AtomicReference<>();
header.set(request.getFirstHeader(PKIAuthenticationPlugin.HEADER));
assertNotNull(header.get());
assertTrue(header.get().getValue().startsWith(nodeName));
- final AtomicReference<ServletRequest> wrappedRequestByFilter = new AtomicReference<>();
- HttpServletRequest mockReq = createMockRequest(header);
- FilterChain filterChain = (servletRequest, servletResponse) -> wrappedRequestByFilter.set(servletRequest);
- mock.doAuthenticate(mockReq, null, filterChain);
+ mockReq = createMockRequest(header);
+ filterChain = (servletRequest, servletResponse) -> wrappedRequestByFilter.set(servletRequest);
- assertNotNull(wrappedRequestByFilter.get());
- assertEquals("solr", ((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal().getName());
- //test 2
- principal.set(null); // no user
- header.set(null);
- wrappedRequestByFilter.set(null);//
- request = new BasicHttpRequest("GET", "http://localhost:56565");
- mock.setHeader(request);
- assertNull(request.getFirstHeader(PKIAuthenticationPlugin.HEADER));
- mock.doAuthenticate(mockReq, null, filterChain);
- assertNotNull(wrappedRequestByFilter.get());
- assertNull(((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal());
+ run("solr", () -> {
+ mock.doAuthenticate(mockReq, null, filterChain);
+ });
- //test 3 . No user request . Request originated from Solr
- mock.solrRequestInfo = null;
- header.set(null);
- wrappedRequestByFilter.set(null);
- request = new BasicHttpRequest("GET", "http://localhost:56565");
- mock.setHeader(request);
- header.set(request.getFirstHeader(PKIAuthenticationPlugin.HEADER));
- assertNotNull(header.get());
- assertTrue(header.get().getValue().startsWith(nodeName));
- mock.doAuthenticate(mockReq, null, filterChain);
- assertNotNull(wrappedRequestByFilter.get());
- assertEquals("$", ((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal().getName());
+ //test 2
+ run(null, () -> {
+ principal.set(null); // no user
+ header.set(null);
+ wrappedRequestByFilter.set(null);//
+ request = new BasicHttpRequest("GET", "http://localhost:56565");
+ mock.setHeader(request);
+ assertNull(request.getFirstHeader(PKIAuthenticationPlugin.HEADER));
+ mock.doAuthenticate(mockReq, null, filterChain);
+ });
+ //test 3 . No user request . Request originated from Solr
+ run("$", () -> {
+ mock.solrRequestInfo = null;
+ header.set(null);
+ wrappedRequestByFilter.set(null);
+ request = new BasicHttpRequest("GET", "http://localhost:56565");
+ mock.setHeader(request);
+ header.set(request.getFirstHeader(PKIAuthenticationPlugin.HEADER));
+ assertNotNull(header.get());
+ assertTrue(header.get().getValue().startsWith(nodeName));
+ mock.doAuthenticate(mockReq, null, filterChain);
+ });
- MockPKIAuthenticationPlugin mock1 = new MockPKIAuthenticationPlugin(null, nodeName) {
- int called = 0;
- @Override
- PublicKey getRemotePublicKey(String nodename) {
- try {
- return called == 0 ? new CryptoKeys.RSAKeyPair().getPublicKey() : correctKey;
- } finally {
- called++;
+ run("$", () -> {
+ mock.solrRequestInfo = null;
+ header.set(null);
+ wrappedRequestByFilter.set(null);
+ request = new BasicHttpRequest("GET", "http://localhost:56565");
+ mock.setHeader(request);
+ header.set(request.getFirstHeader(PKIAuthenticationPlugin.HEADER));
+ assertNotNull(header.get());
+ assertTrue(header.get().getValue().startsWith(nodeName));
+ MockPKIAuthenticationPlugin mock1 = new MockPKIAuthenticationPlugin(null, nodeName) {
+ int called = 0;
+
+ @Override
+ PublicKey getRemotePublicKey(String nodename) {
+ try {
+ return called == 0 ? new CryptoKeys.RSAKeyPair().getPublicKey() : correctKey;
+ } finally {
+ called++;
+ }
}
- }
- };
+ };
- mock1.doAuthenticate(mockReq, null,filterChain );
- assertNotNull(wrappedRequestByFilter.get());
- assertEquals("$", ((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal().getName());
+ mock1.doAuthenticate(mockReq, null, filterChain);
+ });
+ }
+
+ interface Runnable {
+ void run() throws Exception;
+ }
+ private void run(String expected, Runnable r) throws Exception {
+ int failures = 0;
+ for (; ; ) {
+ r.run();
+ if (expected == null) {
+ assertTrue(wrappedRequestByFilter.get() == null || ((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal() == null);
+ } else {
+ assertNotNull(wrappedRequestByFilter.get());
+ if (((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal() == null) {
+ //may be timed out
+ if (++failures < 3) continue;
+ else
+ fail("No principal obtained");
+ }
+ assertEquals(expected, ((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal().getName());
+ }
+ return;
+ }
}
private HttpServletRequest createMockRequest(final AtomicReference<Header> header) {