Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SOLR-16859 Missing Proxy support for Http2SolrClient #1779

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions solr/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ Bug Fixes

* PR#1826: Allow looking up Solr Package repo when that URL references a raw repository.json hosted on Github when the file is JSON but the mimetype used is text/plain. (Eric Pugh)

* SOLR-16859: Missing Proxy support for Http2SolrClient (Alex Deparvu)

Dependency Upgrades
---------------------
(No changes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@
import org.apache.solr.common.util.Utils;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpClientTransport;
import org.eclipse.jetty.client.HttpProxy;
import org.eclipse.jetty.client.Origin.Address;
import org.eclipse.jetty.client.Origin.Protocol;
import org.eclipse.jetty.client.ProtocolHandlers;
import org.eclipse.jetty.client.ProxyConfiguration;
import org.eclipse.jetty.client.Socks4Proxy;
import org.eclipse.jetty.client.api.AuthenticationStore;
import org.eclipse.jetty.client.api.Request;
import org.eclipse.jetty.client.api.Response;
Expand Down Expand Up @@ -281,6 +286,8 @@ private HttpClient createHttpClient(Builder builder) {
if (builder.connectionTimeoutMillis != null)
httpClient.setConnectTimeout(builder.connectionTimeoutMillis);

setupProxy(builder, httpClient);

try {
httpClient.start();
} catch (Exception e) {
Expand All @@ -291,6 +298,29 @@ private HttpClient createHttpClient(Builder builder) {
return httpClient;
}

private void setupProxy(Builder builder, HttpClient httpClient) {
if (builder.proxyHost == null) {
return;
}
Address address = new Address(builder.proxyHost, builder.proxyPort);

final ProxyConfiguration.Proxy proxy;
if (builder.proxyIsSocks4) {
proxy = new Socks4Proxy(address, builder.proxyIsSecure);
} else {
final Protocol protocol;
if (builder.useHttp1_1) {
protocol = HttpClientTransportOverHTTP.HTTP11;
} else {
// see HttpClientTransportOverHTTP2#newOrigin
String protocolName = builder.proxyIsSecure ? "h2" : "h2c";
protocol = new Protocol(List.of(protocolName), false);
}
proxy = new HttpProxy(address, builder.proxyIsSecure, protocol);
}
httpClient.getProxyConfiguration().addProxy(proxy);
}

@Override
public void close() {
// we wait for async requests, so far devs don't want to give sugar for this
Expand Down Expand Up @@ -1018,6 +1048,10 @@ public static class Builder {
protected ResponseParser responseParser;
private Set<String> urlParamNames;
private CookieStore cookieStore = getDefaultCookieStore();
private String proxyHost;
private int proxyPort;
private boolean proxyIsSocks4;
private boolean proxyIsSecure;

public Builder() {}

Expand Down Expand Up @@ -1241,6 +1275,24 @@ public Builder withCookieStore(CookieStore cookieStore) {
this.cookieStore = cookieStore;
return this;
}

/**
* Setup a proxy
*
* @param host The proxy host
* @param port The proxy port
* @param isSocks4 If true creates an SOCKS 4 proxy, otherwise creates an HTTP proxy
* @param isSecure If true enables the secure flag on the proxy
* @return this Builder
*/
public Builder withProxyConfiguration(
String host, int port, boolean isSocks4, boolean isSecure) {
this.proxyHost = host;
this.proxyPort = port;
this.proxyIsSocks4 = isSocks4;
this.proxyIsSecure = isSecure;
return this;
}
}

public Set<String> getUrlParamNames() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* 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.
*/
package org.apache.solr.client.solrj.impl;

import com.carrotsearch.randomizedtesting.RandomizedTest;
import java.nio.file.Path;
import java.util.Properties;
import org.apache.solr.SolrJettyTestBase;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.embedded.JettyConfig;
import org.apache.solr.embedded.JettySolrRunner;
import org.junit.BeforeClass;
import org.junit.Test;

public class Http2SolrClientProxyTest extends SolrJettyTestBase {

// TODO add SSL test

@BeforeClass
public static void beforeTest() throws Exception {
RandomizedTest.assumeFalse(sslConfig.isSSLMode());

JettyConfig jettyConfig =
JettyConfig.builder().withSSLConfig(sslConfig.buildServerSSLConfig()).build();
createAndStartJettyWithProxy(legacyExampleCollection1SolrHome(), new Properties(), jettyConfig);
}

public static JettySolrRunner createAndStartJettyWithProxy(
String solrHome, Properties nodeProperties, JettyConfig jettyConfig) throws Exception {

initCore(null, null, solrHome);

Path coresDir = createTempDir().resolve("cores");

Properties props = new Properties();
props.setProperty("name", DEFAULT_TEST_CORENAME);
props.setProperty("configSet", "collection1");
props.setProperty("config", "${solrconfig:solrconfig.xml}");
props.setProperty("schema", "${schema:schema.xml}");

writeCoreProperties(coresDir.resolve("core"), props, "RestTestBase");

Properties nodeProps = new Properties(nodeProperties);
nodeProps.setProperty("coreRootDirectory", coresDir.toString());
nodeProps.setProperty("configSetBaseDir", solrHome);

jetty = new JettySolrRunner(solrHome, nodeProps, jettyConfig, true);
jetty.start();
port = jetty.getLocalPort();
return jetty;
}

/** Setup a simple http proxy and verify a request works */
@Test
public void testProxy() throws Exception {
var proxy = jetty.getProxy();
assertNotNull(proxy);

String host = proxy.getUrl().getHost();
String url = "http://" + host + ":" + (proxy.getUrl().getPort() + 10) + "/solr";

var builder =
new Http2SolrClient.Builder(url)
.withProxyConfiguration(host, proxy.getListenPort(), false, false);

try (Http2SolrClient client = builder.build()) {
String id = "1234";
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", id);
client.add(DEFAULT_TEST_COLLECTION_NAME, doc);
client.commit(DEFAULT_TEST_COLLECTION_NAME);
assertEquals(
1,
client
.query(DEFAULT_TEST_COLLECTION_NAME, new SolrQuery("id:" + id))
.getResults()
.getNumFound());
}
}
}
Loading