Apache Lucene open-source search software
SOLR-8373: KerberosPlugin: Using multiple nodes on same machine leads clients to fetch TGT for every request
Noble Paul0ac775685215
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txtindex 0c481ab4a07..eb179176aeb 100644--- a/solr/CHANGES.txt+++ b/solr/CHANGES.txt@@ -219,7 +219,11 @@ Bug Fixes* SOLR-6271: Fix ConjunctionSolrSpellChecker to not compare StringDistance by instance.(Igor Kostromin via James Dyer)-* SOLR-7304: Fix Spellcheck Collate to not invalidate range queries. (James Dyer)+* SOLR-7304: Fix Spellcheck Collate to not invalidate range queries. (James Dyer)++* SOLR-8373: KerberosPlugin: Using multiple nodes on same machine leads clients to+ fetch TGT for every request (Ishan Chattopadhyaya via noble)+Other Changes----------------------diff --git a/solr/core/src/java/org/apache/solr/core/CoreContainer.java b/solr/core/src/java/org/apache/solr/core/CoreContainer.javaindex c0165b3a5aa..259ed5882f3 100644--- a/solr/core/src/java/org/apache/solr/core/CoreContainer.java+++ b/solr/core/src/java/org/apache/solr/core/CoreContainer.java@@ -279,10 +279,14 @@ public class CoreContainer {SecurityPluginHolder<AuthenticationPlugin> old = authenticationPlugin;SecurityPluginHolder<AuthenticationPlugin> authenticationPlugin = null;- // Initialize the filter+ // Initialize the pluginif (pluginClassName != null) {authenticationPlugin = new SecurityPluginHolder<>(readVersion(authenticationConfig),- getResourceLoader().newInstance(pluginClassName, AuthenticationPlugin.class));+ getResourceLoader().newInstance(pluginClassName,+ AuthenticationPlugin.class,+ null,+ new Class[]{CoreContainer.class},+ new Object[]{this}));}if (authenticationPlugin != null) {authenticationPlugin.plugin.init(authenticationConfig);diff --git a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.javaindex 1ac44ef674d..ee70e95ae53 100644--- a/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java+++ b/solr/core/src/java/org/apache/solr/core/SolrResourceLoader.java@@ -600,7 +600,7 @@ public class SolrResourceLoader implements ResourceLoader,Closeablereturn obj;}-+public <T> T newInstance(String cName, Class<T> expectedType, String [] subPackages, Class[] params, Object[] args){Class<? extends T> clazz = findClass(cName, expectedType, subPackages);@@ -612,8 +612,19 @@ public class SolrResourceLoader implements ResourceLoader,CloseableT obj = null;try {- Constructor<? extends T> constructor = clazz.getConstructor(params);- obj = constructor.newInstance(args);+ Constructor<? extends T> constructor = null;+ try {+ constructor = clazz.getConstructor(params);+ obj = constructor.newInstance(args);+ } catch (NoSuchMethodException e) {+ //look for a zero arg constructor if the constructor args do not match+ try {+ constructor = clazz.getConstructor();+ obj = constructor.newInstance();+ } catch (NoSuchMethodException e1) {+ throw e;+ }+ }} catch (Error err) {log.error("Loading Class " + cName + " ("+clazz.getName() + ") triggered serious java error: "diff --git a/solr/core/src/java/org/apache/solr/security/KerberosPlugin.java b/solr/core/src/java/org/apache/solr/security/KerberosPlugin.javaindex 706dd6a9398..ca18b56bd6b 100644--- a/solr/core/src/java/org/apache/solr/security/KerberosPlugin.java+++ b/solr/core/src/java/org/apache/solr/security/KerberosPlugin.java@@ -29,8 +29,10 @@ import javax.servlet.descriptor.JspConfigDescriptor;import org.apache.commons.collections.iterators.IteratorEnumeration;import org.apache.solr.client.solrj.impl.HttpClientConfigurer;import org.apache.solr.client.solrj.impl.Krb5HttpClientConfigurer;+import org.apache.solr.cloud.ZkController;import org.apache.solr.common.SolrException;import org.apache.solr.common.SolrException.ErrorCode;+import org.apache.solr.core.CoreContainer;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -57,12 +59,18 @@ public class KerberosPlugin extends AuthenticationPlugin implements HttpClientInHttpClientConfigurer kerberosConfigurer = new Krb5HttpClientConfigurer();Filter kerberosFilter = new KerberosFilter();- final String NAME_RULES_PARAM = "solr.kerberos.name.rules";- final String COOKIE_DOMAIN_PARAM = "solr.kerberos.cookie.domain";- final String COOKIE_PATH_PARAM = "solr.kerberos.cookie.path";- final String PRINCIPAL_PARAM = "solr.kerberos.principal";- final String KEYTAB_PARAM = "solr.kerberos.keytab";- final String TOKEN_VALID_PARAM = "solr.kerberos.token.valid";+ public static final String NAME_RULES_PARAM = "solr.kerberos.name.rules";+ public static final String COOKIE_DOMAIN_PARAM = "solr.kerberos.cookie.domain";+ public static final String COOKIE_PATH_PARAM = "solr.kerberos.cookie.path";+ public static final String PRINCIPAL_PARAM = "solr.kerberos.principal";+ public static final String KEYTAB_PARAM = "solr.kerberos.keytab";+ public static final String TOKEN_VALID_PARAM = "solr.kerberos.token.valid";+ public static final String COOKIE_PORT_AWARE_PARAM = "solr.kerberos.cookie.portaware";+ private final CoreContainer coreContainer;++ public KerberosPlugin(CoreContainer coreContainer) {+ this.coreContainer = coreContainer;+ }@Overridepublic void init(Map<String, Object> pluginConfig) {@@ -71,11 +79,27 @@ public class KerberosPlugin extends AuthenticationPlugin implements HttpClientInparams.put("type", "kerberos");putParam(params, "kerberos.name.rules", NAME_RULES_PARAM, "DEFAULT");putParam(params, "token.valid", TOKEN_VALID_PARAM, "30");- putParam(params, "cookie.domain", COOKIE_DOMAIN_PARAM, null);putParam(params, "cookie.path", COOKIE_PATH_PARAM, "/");putParam(params, "kerberos.principal", PRINCIPAL_PARAM, null);putParam(params, "kerberos.keytab", KEYTAB_PARAM, null);+ // Special handling for the "cookie.domain" based on whether port should be+ // appended to the domain. Useful for situations where multiple solr nodes are+ // on the same host.+ String usePortStr = System.getProperty(COOKIE_PORT_AWARE_PARAM, null);+ boolean needPortAwareCookies = (usePortStr == null) ? false: Boolean.parseBoolean(usePortStr);++ if (!needPortAwareCookies || !coreContainer.isZooKeeperAware()) {+ putParam(params, "cookie.domain", COOKIE_DOMAIN_PARAM, null);+ } else { // we need port aware cookies and we are in SolrCloud mode.+ String host = System.getProperty(COOKIE_DOMAIN_PARAM, null);+ if (host==null) {+ throw new SolrException(ErrorCode.SERVER_ERROR, "Missing required parameter '"+COOKIE_DOMAIN_PARAM+"'.");+ }+ int port = coreContainer.getZkController().getHostPort();+ params.put("cookie.domain", host + ":" + port);+ }+log.info("Params: "+params);FilterConfig conf = new FilterConfig() {diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Krb5HttpClientConfigurer.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Krb5HttpClientConfigurer.javaindex d4c4f7c6131..9bfbc6c17c3 100644--- a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Krb5HttpClientConfigurer.java+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/Krb5HttpClientConfigurer.java@@ -40,6 +40,7 @@ import org.apache.http.impl.auth.SPNegoSchemeFactory;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.protocol.HttpContext;import org.apache.http.client.config.AuthSchemes;+import org.apache.http.client.params.ClientPNames;import org.apache.solr.common.params.SolrParams;import org.slf4j.Logger;import org.slf4j.LoggerFactory;@@ -93,6 +94,10 @@ public class Krb5HttpClientConfigurer extends HttpClientConfigurer {}};+ SolrPortAwareCookieSpecFactory cookieFactory = new SolrPortAwareCookieSpecFactory();+ httpClient.getCookieSpecs().register(cookieFactory.POLICY_NAME, cookieFactory);+ httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, cookieFactory.POLICY_NAME);+httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, useJaasCreds);httpClient.addRequestInterceptor(bufferedEntityInterceptor);diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrPortAwareCookieSpecFactory.java b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrPortAwareCookieSpecFactory.javanew file mode 100644index 00000000000..63c44054154--- /dev/null+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/impl/SolrPortAwareCookieSpecFactory.java@@ -0,0 +1,114 @@+package org.apache.solr.client.solrj.impl;++import java.util.Collection;++import org.apache.http.cookie.ClientCookie;+import org.apache.http.cookie.Cookie;+import org.apache.http.cookie.CookieOrigin;+import org.apache.http.cookie.CookieSpec;+import org.apache.http.cookie.CookieSpecFactory;+import org.apache.http.cookie.CookieSpecProvider;+import org.apache.http.cookie.MalformedCookieException;+import org.apache.http.cookie.params.CookieSpecPNames;+import org.apache.http.impl.cookie.NetscapeDomainHandler;+import org.apache.http.impl.cookie.NetscapeDraftSpec;+import org.apache.http.params.HttpParams;+import org.apache.http.protocol.HttpContext;++/*+ * 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.+ */++@SuppressWarnings("deprecation")+public class SolrPortAwareCookieSpecFactory implements CookieSpecFactory, CookieSpecProvider {+ public static final String POLICY_NAME = "solr-portaware";+ private final CookieSpec cookieSpec;++ public SolrPortAwareCookieSpecFactory(final String[] datepatterns) {+ super();+ this.cookieSpec = new PortAwareCookieSpec(datepatterns);+ }++ public SolrPortAwareCookieSpecFactory() {+ this(null);+ }++ @Override+ public CookieSpec newInstance(final HttpParams params) {+ if (params != null) {+ String[] patterns = null;+ final Collection<?> param = (Collection<?>) params.getParameter(+ CookieSpecPNames.DATE_PATTERNS);+ if (param != null) {+ patterns = new String[param.size()];+ patterns = param.toArray(patterns);+ }+ return new PortAwareCookieSpec(patterns);+ } else {+ return new PortAwareCookieSpec(null);+ }+ }++ @Override+ public CookieSpec create(final HttpContext context) {+ return this.cookieSpec;+ }++ public static class PortAwareCookieSpec extends NetscapeDraftSpec {+ public PortAwareCookieSpec(String patterns[]) {+ super(patterns);+ super.registerAttribHandler(ClientCookie.DOMAIN_ATTR, new PortAwareDomainHandler());+ }++ public PortAwareCookieSpec() {+ this(null);+ }+ }++ /**+ * A domain handler to validate and match cookies based on the domain and origin.+ * The domain is tested against host and port both, and if it doesn't match, it+ * delegates the handling to the base class' matching/validation logic.+ */+ public static class PortAwareDomainHandler extends NetscapeDomainHandler {++ public void validate(final Cookie cookie, final CookieOrigin origin)+ throws MalformedCookieException {+ if (origin != null && origin.getHost() != null && cookie != null) {+ String hostPort = origin.getHost() + ":" + origin.getPort();+ String domain = cookie.getDomain();++ if (hostPort.equals(domain)) {+ return;+ }+ }+ super.validate(cookie, origin);+ }++ @Override+ public boolean match(final Cookie cookie, final CookieOrigin origin) {+ if (origin != null && origin.getHost() != null && cookie != null) {+ String hostPort = origin.getHost() + ":" + origin.getPort();+ String domain = cookie.getDomain();+ if (hostPort.equals(domain)) {+ return true;+ }+ }+ return super.match(cookie, origin);+ }+ }+}+diff --git a/solr/solrj/src/test/org/apache/solr/client/solrj/impl/SolrPortAwareCookieSpecTest.java b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/SolrPortAwareCookieSpecTest.javanew file mode 100644index 00000000000..dbdee6671d5--- /dev/null+++ b/solr/solrj/src/test/org/apache/solr/client/solrj/impl/SolrPortAwareCookieSpecTest.java@@ -0,0 +1,197 @@+package org.apache.solr.client.solrj.impl;++/*+ * 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.+ */++import org.apache.http.cookie.CookieAttributeHandler;+import org.apache.http.cookie.CookieOrigin;+import org.apache.http.cookie.MalformedCookieException;+import org.apache.http.impl.cookie.BasicClientCookie;+import org.junit.Assert;+import org.junit.Test;++// Test cases imported from TestNetscapeCookieAttribHandlers of HttpClient project+public class SolrPortAwareCookieSpecTest {++ @Test+ public void testDomainHostPortValidate() throws Exception {+ final BasicClientCookie cookie = new BasicClientCookie("name", "value");+ final CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);+ final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();++ cookie.setDomain("somehost:80");+ h.validate(cookie, origin);++ cookie.setDomain("somehost:1234");+ try {+ h.validate(cookie, origin);+ Assert.fail("MalformedCookieException should have been thrown");+ } catch (final MalformedCookieException ex) {+ // expected+ }+ }++ @Test+ public void testDomainHostPortMatch() throws Exception {+ final BasicClientCookie cookie = new BasicClientCookie("name", "value");+ final CookieOrigin origin = new CookieOrigin("myhost", 80, "/", false);+ final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();++ cookie.setDomain("myhost");+ try {+ h.match(cookie, null);+ Assert.fail("IllegalArgumentException should have been thrown, since origin is null.");+ } catch (final IllegalArgumentException ex) {+ // expected+ }++ cookie.setDomain(null);+ Assert.assertFalse(h.match(cookie, origin));++ cookie.setDomain("otherhost");+ Assert.assertFalse(h.match(cookie, origin));++ cookie.setDomain("myhost");+ Assert.assertTrue(h.match(cookie, origin));++ cookie.setDomain("myhost:80");+ Assert.assertTrue(h.match(cookie, origin));++ cookie.setDomain("myhost:8080");+ Assert.assertFalse(h.match(cookie, origin));+ }++ @Test+ public void testDomainValidate1() throws Exception {+ final BasicClientCookie cookie = new BasicClientCookie("name", "value");+ final CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);+ final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();++ cookie.setDomain("somehost");+ h.validate(cookie, origin);++ cookie.setDomain("otherhost");+ try {+ h.validate(cookie, origin);+ Assert.fail("MalformedCookieException should have been thrown");+ } catch (final MalformedCookieException ex) {+ // expected+ }+ }++ @Test+ public void testDomainValidate2() throws Exception {+ final BasicClientCookie cookie = new BasicClientCookie("name", "value");+ final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);+ final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();++ cookie.setDomain(".somedomain.com");+ h.validate(cookie, origin);++ cookie.setDomain(".otherdomain.com");+ try {+ h.validate(cookie, origin);+ Assert.fail("MalformedCookieException should have been thrown");+ } catch (final MalformedCookieException ex) {+ // expected+ }+ cookie.setDomain("www.otherdomain.com");+ try {+ h.validate(cookie, origin);+ Assert.fail("MalformedCookieException should have been thrown");+ } catch (final MalformedCookieException ex) {+ // expected+ }+ }++ @Test+ public void testDomainValidate3() throws Exception {+ final BasicClientCookie cookie = new BasicClientCookie("name", "value");+ final CookieOrigin origin = new CookieOrigin("www.a.com", 80, "/", false);+ final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();++ cookie.setDomain(".a.com");+ h.validate(cookie, origin);++ cookie.setDomain(".com");+ try {+ h.validate(cookie, origin);+ Assert.fail("MalformedCookieException should have been thrown");+ } catch (final MalformedCookieException ex) {+ // expected+ }+ }++ @Test+ public void testDomainValidate4() throws Exception {+ final BasicClientCookie cookie = new BasicClientCookie("name", "value");+ final CookieOrigin origin = new CookieOrigin("www.a.b.c", 80, "/", false);+ final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();++ cookie.setDomain(".a.b.c");+ h.validate(cookie, origin);++ cookie.setDomain(".b.c");+ try {+ h.validate(cookie, origin);+ Assert.fail("MalformedCookieException should have been thrown");+ } catch (final MalformedCookieException ex) {+ // expected+ }+ }++ @Test+ public void testDomainMatch1() throws Exception {+ final BasicClientCookie cookie = new BasicClientCookie("name", "value");+ final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);+ final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();++ cookie.setDomain(null);+ Assert.assertFalse(h.match(cookie, origin));++ cookie.setDomain(".somedomain.com");+ Assert.assertTrue(h.match(cookie, origin));+ }++ @Test+ public void testDomainMatch2() throws Exception {+ final BasicClientCookie cookie = new BasicClientCookie("name", "value");+ final CookieOrigin origin = new CookieOrigin("www.whatever.somedomain.com", 80, "/", false);+ final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();++ cookie.setDomain(".somedomain.com");+ Assert.assertTrue(h.match(cookie, origin));+ }++ @Test+ public void testDomainInvalidInput() throws Exception {+ final CookieAttributeHandler h = new SolrPortAwareCookieSpecFactory.PortAwareDomainHandler();+ try {+ h.match(null, null);+ Assert.fail("IllegalArgumentException must have been thrown");+ } catch (final IllegalArgumentException ex) {+ // expected+ }+ try {+ h.match(new BasicClientCookie("name", "value"), null);+ Assert.fail("IllegalArgumentException must have been thrown");+ } catch (final IllegalArgumentException ex) {+ // expected+ }+ }++}