Proxy support for pull, push and mirror commands (#1773)

Apply proxy support for jGit by extracting the required functionality from the DefaultAdvancedHttpClient into its own class HttpURLConnectionFactory. This new class is now used by the DefaultAdvancedHttpClient and jGit.
The HttpURLConnection also fixes proxy server authentication, which was non functional in DefaultAdvancedHttpClient.
The proxy support for SVNKit is implemented by using the provided method of the BasicAuthenticationManager.
For mercurial the support is configured by writing the required settings to a temporary hgrc file.
This commit is contained in:
Sebastian Sdorra
2021-08-19 11:27:51 +02:00
committed by GitHub
parent a7bb67f36b
commit 7f9f4e566c
54 changed files with 2996 additions and 1098 deletions

View File

@@ -0,0 +1,33 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.eclipse.jgit.transport.http;
import java.net.HttpURLConnection;
public class WrappedHttpUrlConnection extends JDKHttpConnection {
public WrappedHttpUrlConnection(HttpURLConnection urlConnection) {
super(urlConnection);
}
}

View File

@@ -25,8 +25,8 @@
package sonia.scm.repository;
import org.eclipse.jgit.transport.HttpTransport;
import sonia.scm.web.ScmHttpConnectionFactory;
import sonia.scm.plugin.Extension;
import sonia.scm.web.ScmHttpConnectionFactory;
import javax.inject.Inject;
import javax.servlet.ServletContextEvent;
@@ -52,4 +52,5 @@ public class GitHttpTransportRegistration implements ServletContextListener {
public void contextDestroyed(ServletContextEvent servletContextEvent) {
// Nothing to destroy
}
}

View File

@@ -55,7 +55,6 @@ import sonia.scm.repository.api.MirrorCommandResult;
import sonia.scm.repository.api.MirrorCommandResult.ResultType;
import sonia.scm.repository.api.MirrorFilter;
import sonia.scm.repository.api.MirrorFilter.Result;
import sonia.scm.repository.api.Pkcs12ClientCertificateCredential;
import sonia.scm.repository.api.UsernamePasswordCredential;
import javax.inject.Inject;
@@ -389,15 +388,14 @@ public class GitMirrorCommand extends AbstractGitCommand implements MirrorComman
.setRefSpecs("refs/heads/*:refs/heads/*", "refs/tags/*:refs/tags/*")
.setForceUpdate(true)
.setRemoveDeletedRefs(true)
.setRemote(mirrorCommandRequest.getSourceUrl());
mirrorCommandRequest.getCredential(Pkcs12ClientCertificateCredential.class)
.ifPresent(c -> fetchCommand.setTransportConfigCallback(transport -> {
.setRemote(mirrorCommandRequest.getSourceUrl())
.setTransportConfigCallback(transport -> {
if (transport instanceof TransportHttp) {
TransportHttp transportHttp = (TransportHttp) transport;
transportHttp.setHttpConnectionFactory(mirrorHttpConnectionProvider.createHttpConnectionFactory(c, mirrorLog));
transportHttp.setHttpConnectionFactory(mirrorHttpConnectionProvider.createHttpConnectionFactory(mirrorCommandRequest, mirrorLog));
}
}));
});
mirrorCommandRequest.getCredential(UsernamePasswordCredential.class)
.ifPresent(c -> fetchCommand
.setCredentialsProvider(

View File

@@ -24,17 +24,17 @@
package sonia.scm.repository.spi;
import org.eclipse.jgit.transport.http.HttpConnectionFactory2;
import org.eclipse.jgit.transport.http.HttpConnectionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.net.HttpConnectionOptions;
import sonia.scm.net.HttpURLConnectionFactory;
import sonia.scm.repository.api.Pkcs12ClientCertificateCredential;
import sonia.scm.web.ScmHttpConnectionFactory;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.net.ssl.KeyManager;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManager;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
@@ -45,15 +45,22 @@ class MirrorHttpConnectionProvider {
private static final Logger LOG = LoggerFactory.getLogger(MirrorHttpConnectionProvider.class);
private final Provider<TrustManager> trustManagerProvider;
private final HttpURLConnectionFactory httpURLConnectionFactory;
@Inject
public MirrorHttpConnectionProvider(Provider<TrustManager> trustManagerProvider) {
this.trustManagerProvider = trustManagerProvider;
public MirrorHttpConnectionProvider(HttpURLConnectionFactory httpURLConnectionFactory) {
this.httpURLConnectionFactory = httpURLConnectionFactory;
}
public HttpConnectionFactory2 createHttpConnectionFactory(Pkcs12ClientCertificateCredential credential, List<String> log) {
return new ScmHttpConnectionFactory(trustManagerProvider, createKeyManagers(credential, log));
public HttpConnectionFactory createHttpConnectionFactory(MirrorCommandRequest mirrorCommandRequest, List<String> log) {
HttpConnectionOptions options = new HttpConnectionOptions();
mirrorCommandRequest.getCredential(Pkcs12ClientCertificateCredential.class)
.ifPresent(c -> options.withKeyManagers(createKeyManagers(c, log)));
mirrorCommandRequest.getProxyConfiguration()
.ifPresent(options::withProxyConfiguration);
return new ScmHttpConnectionFactory(httpURLConnectionFactory, options);
}
private KeyManager[] createKeyManagers(Pkcs12ClientCertificateCredential credential, List<String> log) {

View File

@@ -24,75 +24,40 @@
package sonia.scm.web;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.transport.http.HttpConnection;
import org.eclipse.jgit.transport.http.JDKHttpConnection;
import org.eclipse.jgit.transport.http.JDKHttpConnectionFactory;
import org.eclipse.jgit.transport.http.NoCheckX509TrustManager;
import org.eclipse.jgit.transport.http.HttpConnectionFactory;
import org.eclipse.jgit.transport.http.WrappedHttpUrlConnection;
import sonia.scm.net.HttpConnectionOptions;
import sonia.scm.net.HttpURLConnectionFactory;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.net.ssl.KeyManager;
import javax.net.ssl.TrustManager;
import java.security.GeneralSecurityException;
import java.text.MessageFormat;
import java.io.IOException;
import java.net.Proxy;
import java.net.URL;
public class ScmHttpConnectionFactory extends JDKHttpConnectionFactory {
public class ScmHttpConnectionFactory implements HttpConnectionFactory {
private final Provider<TrustManager> trustManagerProvider;
private final KeyManager[] keyManagers;
private final HttpURLConnectionFactory connectionFactory;
private final HttpConnectionOptions options;
@Inject
public ScmHttpConnectionFactory(Provider<TrustManager> trustManagerProvider) {
this(trustManagerProvider, null);
public ScmHttpConnectionFactory(HttpURLConnectionFactory connectionFactory) {
this(connectionFactory, new HttpConnectionOptions());
}
public ScmHttpConnectionFactory(Provider<TrustManager> trustManagerProvider, KeyManager[] keyManagers) {
this.trustManagerProvider = trustManagerProvider;
this.keyManagers = keyManagers;
public ScmHttpConnectionFactory(HttpURLConnectionFactory connectionFactory, HttpConnectionOptions options) {
this.connectionFactory = connectionFactory;
this.options = options;
}
@Override
public GitSession newSession() {
return new ScmConnectionSession(trustManagerProvider.get(), keyManagers);
public HttpConnection create(URL url) throws IOException {
return new WrappedHttpUrlConnection(connectionFactory.create(url, options));
}
private static class ScmConnectionSession implements GitSession {
private final TrustManager trustManager;
private final KeyManager[] keyManagers;
private ScmConnectionSession(TrustManager trustManager, KeyManager[] keyManagers) {
this.trustManager = trustManager;
this.keyManagers = keyManagers;
}
@Override
@SuppressWarnings("java:S5527")
public JDKHttpConnection configure(HttpConnection connection,
boolean sslVerify) throws GeneralSecurityException {
if (!(connection instanceof JDKHttpConnection)) {
throw new IllegalArgumentException(MessageFormat.format(
JGitText.get().httpWrongConnectionType,
JDKHttpConnection.class.getName(),
connection.getClass().getName()));
}
JDKHttpConnection conn = (JDKHttpConnection) connection;
String scheme = conn.getURL().getProtocol();
if ("https".equals(scheme) && sslVerify) { //$NON-NLS-1$
// sslVerify == true: use the JDK defaults
conn.configure(keyManagers, new TrustManager[]{trustManager}, null);
} else if ("https".equals(scheme)) {
conn.configure(keyManagers, new TrustManager[]{new NoCheckX509TrustManager()}, null);
conn.setHostnameVerifier((name, value) -> true);
}
return conn;
}
@Override
public void close() {
// Nothing
}
@Override
public HttpConnection create(URL url, Proxy proxy) throws IOException {
// we ignore proxy configuration of jgit, because we have our own
return create(url);
}
}

View File

@@ -0,0 +1,63 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.repository;
import org.eclipse.jgit.transport.HttpTransport;
import org.eclipse.jgit.transport.http.HttpConnectionFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.web.ScmHttpConnectionFactory;
import static org.assertj.core.api.Assertions.assertThat;
@ExtendWith(MockitoExtension.class)
class GitHttpTransportRegistrationTest {
@Mock
private ScmHttpConnectionFactory scmHttpConnectionFactory;
private HttpConnectionFactory capturedFactory;
@BeforeEach
void captureConnectionFactory() {
this.capturedFactory = HttpTransport.getConnectionFactory();
}
@AfterEach
void restoreConnectionFactory() {
HttpTransport.setConnectionFactory(capturedFactory);
}
@Test
void shouldSetHttpConnectionFactory() {
new GitHttpTransportRegistration(scmHttpConnectionFactory).contextInitialized(null);
assertThat(HttpTransport.getConnectionFactory()).isSameAs(scmHttpConnectionFactory);
}
}

View File

@@ -24,6 +24,7 @@
package sonia.scm.repository.spi;
import com.google.inject.util.Providers;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
@@ -37,6 +38,9 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import sonia.scm.api.v2.resources.GitRepositoryConfigStoreProvider;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.net.GlobalProxyConfiguration;
import sonia.scm.net.HttpURLConnectionFactory;
import sonia.scm.repository.GitChangesetConverterFactory;
import sonia.scm.repository.GitConfig;
import sonia.scm.repository.api.MirrorCommandResult;
@@ -47,6 +51,7 @@ import sonia.scm.repository.work.WorkdirProvider;
import sonia.scm.security.GPG;
import sonia.scm.store.InMemoryConfigurationStoreFactory;
import javax.net.ssl.TrustManager;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
@@ -70,7 +75,6 @@ public class GitMirrorCommandTest extends AbstractGitCommandTestBase {
public static final Consumer<MirrorCommandRequest> ACCEPT_ALL = r -> {
};
public static final Consumer<MirrorCommandRequest> REJECT_ALL = r -> r.setFilter(new DenyAllMirrorFilter());
private final MirrorHttpConnectionProvider mirrorHttpConnectionProvider = mock(MirrorHttpConnectionProvider.class);
private final GPG gpg = mock(GPG.class);
private final GitChangesetConverterFactory gitChangesetConverterFactory = new GitChangesetConverterFactory(gpg);
private final GitTagConverter gitTagConverter = new GitTagConverter(gpg);
@@ -78,6 +82,8 @@ public class GitMirrorCommandTest extends AbstractGitCommandTestBase {
private File clone;
private GitMirrorCommand command;
@Before
public void bendContextToNewRepository() throws IOException, GitAPIException {
clone = tempFolder.newFolder();
@@ -87,6 +93,14 @@ public class GitMirrorCommandTest extends AbstractGitCommandTestBase {
SimpleGitWorkingCopyFactory workingCopyFactory =
new SimpleGitWorkingCopyFactory(
new NoneCachingWorkingCopyPool(new WorkdirProvider(repositoryLocationResolver)), new SimpleMeterRegistry());
MirrorHttpConnectionProvider mirrorHttpConnectionProvider = new MirrorHttpConnectionProvider(
new HttpURLConnectionFactory(
new GlobalProxyConfiguration(new ScmConfiguration()),
Providers.of(mock(TrustManager.class))
)
);
command = new GitMirrorCommand(
emptyContext,
mirrorHttpConnectionProvider,

View File

@@ -0,0 +1,95 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.repository.spi;
import org.eclipse.jgit.transport.http.HttpConnection;
import org.eclipse.jgit.transport.http.HttpConnectionFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.net.HttpConnectionOptions;
import sonia.scm.net.HttpURLConnectionFactory;
import sonia.scm.net.ProxyConfiguration;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@ExtendWith(MockitoExtension.class)
class MirrorHttpConnectionProviderTest {
@Mock
private HttpURLConnectionFactory internalConnectionFactory;
@InjectMocks
private MirrorHttpConnectionProvider provider;
@Captor
private ArgumentCaptor<HttpConnectionOptions> captor;
@Test
void shouldNotConfigureProxy() throws IOException {
MirrorCommandRequest request = new MirrorCommandRequest();
HttpConnectionOptions value = create(request);
assertThat(value.getProxyConfiguration()).isEmpty();
}
@Test
void shouldConfigureProxy() throws IOException {
ProxyConfiguration proxy = mock(ProxyConfiguration.class);
MirrorCommandRequest request = new MirrorCommandRequest();
request.setProxyConfiguration(proxy);
HttpConnectionOptions value = create(request);
assertThat(value.getProxyConfiguration()).containsSame(proxy);
}
private HttpConnectionOptions create(MirrorCommandRequest request) throws IOException {
List<String> log = new ArrayList<>();
HttpConnectionFactory connectionFactory = provider.createHttpConnectionFactory(request, log);
assertThat(connectionFactory).isNotNull();
HttpConnection connection = connectionFactory.create(new URL("https://hitchhiker.com"));
assertThat(connection).isNotNull();
verify(internalConnectionFactory).create(any(), captor.capture());
return captor.getValue();
}
}

View File

@@ -0,0 +1,82 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.web;
import org.eclipse.jgit.transport.http.HttpConnection;
import org.eclipse.jgit.transport.http.JDKHttpConnection;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.net.HttpConnectionOptions;
import sonia.scm.net.HttpURLConnectionFactory;
import java.io.IOException;
import java.net.URL;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
@ExtendWith(MockitoExtension.class)
class ScmHttpConnectionFactoryTest {
@Mock
private HttpURLConnectionFactory internalConnectionFactory;
@Captor
private ArgumentCaptor<HttpConnectionOptions> connectionOptionsCaptor;
@Test
void shouldCreateConnection() throws IOException {
ScmHttpConnectionFactory connectionFactory = new ScmHttpConnectionFactory(internalConnectionFactory);
URL url = new URL("https://scm.hitchhiker.org");
HttpConnection httpConnection = connectionFactory.create(url, null);
assertThat(httpConnection)
.isNotNull()
.isInstanceOf(JDKHttpConnection.class);
verify(internalConnectionFactory).create(eq(url), connectionOptionsCaptor.capture());
assertThat(connectionOptionsCaptor.getValue()).isNotNull();
}
@Test
void shouldCreateConnectionWithOptions() throws IOException {
HttpConnectionOptions options = new HttpConnectionOptions();
ScmHttpConnectionFactory connectionFactory = new ScmHttpConnectionFactory(internalConnectionFactory, options);
URL url = new URL("https://scm.hitchhiker.org");
HttpConnection httpConnection = connectionFactory.create(url);
assertThat(httpConnection)
.isNotNull()
.isInstanceOf(JDKHttpConnection.class);
verify(internalConnectionFactory).create(url, options);
}
}