From 668395510eaaaf01dbdb73803b58290818dde785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Sun, 13 Oct 2019 21:12:42 +0200 Subject: [PATCH 1/8] SOLR-13741 --- .../org/apache/solr/security/AuditEvent.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/security/AuditEvent.java b/solr/core/src/java/org/apache/solr/security/AuditEvent.java index f9c45be1a256..f7bf32e2f434 100644 --- a/solr/core/src/java/org/apache/solr/security/AuditEvent.java +++ b/solr/core/src/java/org/apache/solr/security/AuditEvent.java @@ -129,12 +129,15 @@ public AuditEvent(EventType eventType, Throwable exception, HttpServletRequest h this.solrPort = httpRequest.getLocalPort(); this.solrIp = httpRequest.getLocalAddr(); this.clientIp = httpRequest.getRemoteAddr(); - this.resource = httpRequest.getContextPath(); + this.resource = httpRequest.getPathInfo(); this.httpMethod = httpRequest.getMethod(); this.httpQueryString = httpRequest.getQueryString(); this.headers = getHeadersFromRequest(httpRequest); this.requestUrl = httpRequest.getRequestURL(); this.nodeName = MDC.get(ZkStateReader.NODE_NAME_PROP); + httpRequest.getParameterMap().forEach((k, v) -> { + this.solrParams.put(k, Arrays.asList(v)); + }); setRequestType(findRequestType()); @@ -459,14 +462,14 @@ private RequestType findRequestType() { } private static final List ADMIN_PATH_REGEXES = Arrays.asList( - "^/solr/admin/.*", - "^/api/(c|collections)/$", - "^/api/(c|collections)/[^/]+/config$", - "^/api/(c|collections)/[^/]+/schema$", - "^/api/(c|collections)/[^/]+/shards.*", - "^/api/cores.*$", - "^/api/node$", - "^/api/cluster$"); + "^/admin/.*", + "^/(____v2|api)/(c|collections)$", + "^/(____v2|api)/(c|collections)/[^/]+/config$", + "^/(____v2|api)/(c|collections)/[^/]+/schema$", + "^/(____v2|api)/(c|collections)/[^/]+/shards.*", + "^/(____v2|api)/cores.*$", + "^/(____v2|api)/node$", + "^/(____v2|api)/cluster$"); private static final List STREAMING_PATH_REGEXES = Collections.singletonList(".*/stream.*"); From 810e8717208340b4d176746f0678fe0b89cc46e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Sun, 13 Oct 2019 21:28:31 +0200 Subject: [PATCH 2/8] Prevent double audit logging and wrong error code response for 401 due to wrong pw --- solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java | 3 +++ .../org/apache/solr/security/AuditLoggerIntegrationTest.java | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java b/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java index 546a195e00e5..6b095a0c6631 100644 --- a/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java +++ b/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java @@ -478,9 +478,12 @@ Action authorize() throws IOException { for (Map.Entry e : headers.entrySet()) response.setHeader(e.getKey(), e.getValue()); } log.debug("USER_REQUIRED "+req.getHeader("Authorization")+" "+ req.getUserPrincipal()); + sendError(authResponse.statusCode, + "Authentication failed, Response code: " + authResponse.statusCode); if (shouldAudit(EventType.REJECTED)) { cores.getAuditLoggerPlugin().doAudit(new AuditEvent(EventType.REJECTED, req, context)); } + return RETURN; } if (!(authResponse.statusCode == HttpStatus.SC_ACCEPTED) && !(authResponse.statusCode == HttpStatus.SC_OK)) { log.info("USER_REQUIRED auth header {} context : {} ", req.getHeader("Authorization"), context); diff --git a/solr/core/src/test/org/apache/solr/security/AuditLoggerIntegrationTest.java b/solr/core/src/test/org/apache/solr/security/AuditLoggerIntegrationTest.java index 4e6fad28e300..ad2a7cdf17a5 100644 --- a/solr/core/src/test/org/apache/solr/security/AuditLoggerIntegrationTest.java +++ b/solr/core/src/test/org/apache/solr/security/AuditLoggerIntegrationTest.java @@ -188,7 +188,7 @@ public void auth() throws Exception { } catch (SolrException ex) { waitForAuditEventCallbacks(1); CallbackReceiver receiver = testHarness.get().receiver; - assertAuditEvent(receiver.popEvent(), UNAUTHORIZED, "/admin/collections", ADMIN, null,403); + assertAuditEvent(receiver.popEvent(), REJECTED, "/admin/collections", ADMIN, null, 401); } } From da56e313a75a731e9b761a5599a98ceac38c80f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Sun, 13 Oct 2019 21:30:01 +0200 Subject: [PATCH 3/8] CHANGES --- solr/CHANGES.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 9700c6b2d299..a61842a454f9 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -318,6 +318,8 @@ Bug Fixes state happening to change between checking if the current shard is active and later checking if there are any sub-shard leaders to forward the update to. (yonik) +* SOLR-13835: HttpSolrCall produces incorrect extra AuditEvent on AuthorizationResponse.PROMPT (janhoy, hossman) + Other Changes ---------------------- From 2ed9e08905d954a18e8e6ea0117470b701e6ba13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Sun, 13 Oct 2019 21:34:11 +0200 Subject: [PATCH 4/8] Precommit --- .../org/apache/solr/security/AuditLoggerIntegrationTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/solr/core/src/test/org/apache/solr/security/AuditLoggerIntegrationTest.java b/solr/core/src/test/org/apache/solr/security/AuditLoggerIntegrationTest.java index ad2a7cdf17a5..3faaa43240f4 100644 --- a/solr/core/src/test/org/apache/solr/security/AuditLoggerIntegrationTest.java +++ b/solr/core/src/test/org/apache/solr/security/AuditLoggerIntegrationTest.java @@ -60,7 +60,6 @@ import static org.apache.solr.security.AuditEvent.EventType.COMPLETED; import static org.apache.solr.security.AuditEvent.EventType.ERROR; import static org.apache.solr.security.AuditEvent.EventType.REJECTED; -import static org.apache.solr.security.AuditEvent.EventType.UNAUTHORIZED; import static org.apache.solr.security.AuditEvent.RequestType.ADMIN; import static org.apache.solr.security.AuditEvent.RequestType.SEARCH; From 5b7a7701c2bdcca382fcc2a5cf50a04371749218 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Sun, 13 Oct 2019 21:56:57 +0200 Subject: [PATCH 5/8] Fix precommit, parse using encoding safe method --- solr/core/src/java/org/apache/solr/security/AuditEvent.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/security/AuditEvent.java b/solr/core/src/java/org/apache/solr/security/AuditEvent.java index f7bf32e2f434..492384ee69ad 100644 --- a/solr/core/src/java/org/apache/solr/security/AuditEvent.java +++ b/solr/core/src/java/org/apache/solr/security/AuditEvent.java @@ -31,6 +31,7 @@ import org.apache.solr.common.SolrException; import org.apache.solr.common.cloud.ZkStateReader; +import org.apache.solr.servlet.SolrRequestParsers; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; @@ -135,8 +136,8 @@ public AuditEvent(EventType eventType, Throwable exception, HttpServletRequest h this.headers = getHeadersFromRequest(httpRequest); this.requestUrl = httpRequest.getRequestURL(); this.nodeName = MDC.get(ZkStateReader.NODE_NAME_PROP); - httpRequest.getParameterMap().forEach((k, v) -> { - this.solrParams.put(k, Arrays.asList(v)); + SolrRequestParsers.parseQueryString(httpQueryString).forEach(sp -> { + this.solrParams.put(sp.getKey(), Arrays.asList(sp.getValue())); }); setRequestType(findRequestType()); From e1fb3ab9390514bc8398f2cd0d228dfe119800b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Wed, 16 Oct 2019 13:48:25 +0200 Subject: [PATCH 6/8] Detecting ERROR response from AUTHZ and audit logging as EventType:ERROR --- .../org/apache/solr/servlet/HttpSolrCall.java | 27 +++++++++++++------ 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java b/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java index 91168d18b337..61e397b7458e 100644 --- a/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java +++ b/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java @@ -474,23 +474,34 @@ Action authorize() throws IOException { AuthorizationContext context = getAuthCtx(); log.debug("AuthorizationContext : {}", context); AuthorizationResponse authResponse = cores.getAuthorizationPlugin().authorize(context); - if (authResponse.statusCode == AuthorizationResponse.PROMPT.statusCode) { + int statusCode = authResponse.statusCode; + + if (statusCode == AuthorizationResponse.PROMPT.statusCode) { Map headers = (Map) getReq().getAttribute(AuthenticationPlugin.class.getName()); if (headers != null) { for (Map.Entry e : headers.entrySet()) response.setHeader(e.getKey(), e.getValue()); } log.debug("USER_REQUIRED "+req.getHeader("Authorization")+" "+ req.getUserPrincipal()); - sendError(authResponse.statusCode, - "Authentication failed, Response code: " + authResponse.statusCode); + sendError(statusCode, + "Authentication failed, Response code: " + statusCode); if (shouldAudit(EventType.REJECTED)) { cores.getAuditLoggerPlugin().doAudit(new AuditEvent(EventType.REJECTED, req, context)); } return RETURN; } - if (!(authResponse.statusCode == HttpStatus.SC_ACCEPTED) && !(authResponse.statusCode == HttpStatus.SC_OK)) { - log.info("USER_REQUIRED auth header {} context : {} ", req.getHeader("Authorization"), context); - sendError(authResponse.statusCode, - "Unauthorized request, Response code: " + authResponse.statusCode); + if (statusCode >= 500) { + log.warn("ERROR {} during authentication: {}", statusCode, authResponse.getMessage()); + sendError(statusCode, + "ERROR during authorization, Response code: " + statusCode); + if (shouldAudit(EventType.ERROR)) { + cores.getAuditLoggerPlugin().doAudit(new AuditEvent(EventType.ERROR, req, context)); + } + return RETURN; + } + if (!(statusCode == HttpStatus.SC_ACCEPTED) && !(statusCode == HttpStatus.SC_OK)) { + log.debug("UNAUTHORIZED auth header {} context : {}, msg: {}", req.getHeader("Authorization"), context, authResponse.getMessage()); + sendError(statusCode, + "Unauthorized request, Response code: " + statusCode); if (shouldAudit(EventType.UNAUTHORIZED)) { cores.getAuditLoggerPlugin().doAudit(new AuditEvent(EventType.UNAUTHORIZED, req, context)); } @@ -501,7 +512,7 @@ Action authorize() throws IOException { } return null; } - + /** * This method processes the request. */ From d544ec8a9af2bc36d39cb01be0234464623b69f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Wed, 16 Oct 2019 14:23:35 +0200 Subject: [PATCH 7/8] Explicitly handle 403, and treat all other responses as ERROR Fix fail msg --- .../org/apache/solr/servlet/HttpSolrCall.java | 18 +++++++++--------- .../security/AuditLoggerIntegrationTest.java | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java b/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java index 61e397b7458e..e7002f3750e0 100644 --- a/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java +++ b/solr/core/src/java/org/apache/solr/servlet/HttpSolrCall.java @@ -489,21 +489,21 @@ Action authorize() throws IOException { } return RETURN; } - if (statusCode >= 500) { - log.warn("ERROR {} during authentication: {}", statusCode, authResponse.getMessage()); + if (statusCode == AuthorizationResponse.FORBIDDEN.statusCode) { + log.debug("UNAUTHORIZED auth header {} context : {}, msg: {}", req.getHeader("Authorization"), context, authResponse.getMessage()); sendError(statusCode, - "ERROR during authorization, Response code: " + statusCode); - if (shouldAudit(EventType.ERROR)) { - cores.getAuditLoggerPlugin().doAudit(new AuditEvent(EventType.ERROR, req, context)); + "Unauthorized request, Response code: " + statusCode); + if (shouldAudit(EventType.UNAUTHORIZED)) { + cores.getAuditLoggerPlugin().doAudit(new AuditEvent(EventType.UNAUTHORIZED, req, context)); } return RETURN; } if (!(statusCode == HttpStatus.SC_ACCEPTED) && !(statusCode == HttpStatus.SC_OK)) { - log.debug("UNAUTHORIZED auth header {} context : {}, msg: {}", req.getHeader("Authorization"), context, authResponse.getMessage()); + log.warn("ERROR {} during authentication: {}", statusCode, authResponse.getMessage()); sendError(statusCode, - "Unauthorized request, Response code: " + statusCode); - if (shouldAudit(EventType.UNAUTHORIZED)) { - cores.getAuditLoggerPlugin().doAudit(new AuditEvent(EventType.UNAUTHORIZED, req, context)); + "ERROR during authorization, Response code: " + statusCode); + if (shouldAudit(EventType.ERROR)) { + cores.getAuditLoggerPlugin().doAudit(new AuditEvent(EventType.ERROR, req, context)); } return RETURN; } diff --git a/solr/core/src/test/org/apache/solr/security/AuditLoggerIntegrationTest.java b/solr/core/src/test/org/apache/solr/security/AuditLoggerIntegrationTest.java index 3faaa43240f4..28bbaa8affe9 100644 --- a/solr/core/src/test/org/apache/solr/security/AuditLoggerIntegrationTest.java +++ b/solr/core/src/test/org/apache/solr/security/AuditLoggerIntegrationTest.java @@ -183,7 +183,7 @@ public void auth() throws Exception { CollectionAdminRequest.Create createRequest = CollectionAdminRequest.createCollection("test", 1, 1); createRequest.setBasicAuthCredentials("solr", "wrongPW"); client.request(createRequest); - fail("Call should fail with 403"); + fail("Call should fail with 401"); } catch (SolrException ex) { waitForAuditEventCallbacks(1); CallbackReceiver receiver = testHarness.get().receiver; From 0c9f84cb2546b1af790c67eae814ba18d0d1d313 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Wed, 16 Oct 2019 15:41:24 +0200 Subject: [PATCH 8/8] Fix failing test --- .../org/apache/solr/security/BasicAuthIntegrationTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/solr/core/src/test/org/apache/solr/security/BasicAuthIntegrationTest.java b/solr/core/src/test/org/apache/solr/security/BasicAuthIntegrationTest.java index 39c5e1c92cc5..188ac5b71d5b 100644 --- a/solr/core/src/test/org/apache/solr/security/BasicAuthIntegrationTest.java +++ b/solr/core/src/test/org/apache/solr/security/BasicAuthIntegrationTest.java @@ -58,8 +58,9 @@ import org.apache.solr.common.params.ModifiableSolrParams; import org.apache.solr.common.params.SolrParams; import org.apache.solr.common.util.NamedList; -import org.apache.solr.common.util.Utils; import org.apache.solr.common.util.TimeSource; +import org.apache.solr.common.util.Utils; +import org.apache.solr.util.LogLevel; import org.apache.solr.util.SolrCLI; import org.apache.solr.util.TimeOut; import org.junit.After; @@ -96,6 +97,7 @@ public void tearDownCluster() throws Exception { @Test //commented 9-Aug-2018 @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // 21-May-2018 // commented out on: 17-Feb-2019 @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // annotated on: 24-Dec-2018 + @LogLevel("org.apache.solr.security=DEBUG") public void testBasicAuth() throws Exception { boolean isUseV2Api = random().nextBoolean(); String authcPrefix = "/admin/authentication"; @@ -232,7 +234,7 @@ public void testBasicAuth() throws Exception { HttpSolrClient.RemoteSolrException e = expectThrows(HttpSolrClient.RemoteSolrException.class, () -> { new UpdateRequest().deleteByQuery("*:*").process(aNewClient, COLLECTION); }); - assertTrue(e.getMessage().contains("Unauthorized request")); + assertTrue(e.getMessage(), e.getMessage().contains("Authentication failed")); } finally { aNewClient.close(); cluster.stopJettySolrRunner(aNewJetty);