Browse Source

Fix push with jgit pgm failing with "unauthorized"

Pushing with JGit commandline to e.g. Github failed with "unauthorized"
since HttpUrlConnection calls the configured authenticator implicitly.

The problem is that during a push two requests are sent to the server,
first a GET and then a POST (containing the pack data). The first GET
request sent anonymously is rejected with 401 (unauthorized). When an
Authenticator is installed the java.net classes will use the
Authenticator to ask the user for credentials and retry the request.
But this happens under the hood and JGit level code doesn't see that
this happens.

The next request is the POST but since JGit thinks the first GET request
went through anonymously it doesn't add authentication headers to the
POST request. This POST of course also fails with 401 but since this
request contains a lot of body-data streamed from JGit (the pack file!)
the java.net classes can't simply retry the request with authorization
headers. The whole process fails.

Fix this by using Apache httpclient which doesn't use Authenticator to
retrieve credentials. Instead initialize TransportCommand to use the
default credential provider if no other credentials provider was set
explicitly. org.eclipse.jgit.pgm.Main sets this default for the JGit
command line client.

Change-Id: Ic4e0f8b60d4bd6e69d91eae0c7e1b44cdf851b00
Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
tags/v4.2.0.201601211800-r
Matthias Sohn 8 years ago
parent
commit
2647d024af

+ 2
- 1
org.eclipse.jgit.http.apache/META-INF/MANIFEST.MF View File

@@ -7,7 +7,8 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-Localization: plugin
Bundle-Vendor: %Provider-Name
Bundle-ActivationPolicy: lazy
Import-Package: org.apache.http;version="[4.1.0,5.0.0)",
Import-Package: org.apache.commons.logging;version="[1.1.1,2.0.0)",
org.apache.http;version="[4.1.0,5.0.0)",
org.apache.http.client;version="[4.1.0,5.0.0)",
org.apache.http.client.methods;version="[4.1.0,5.0.0)",
org.apache.http.client.params;version="[4.1.0,5.0.0)",

+ 3
- 1
org.eclipse.jgit.pgm/META-INF/MANIFEST.MF View File

@@ -7,7 +7,8 @@ Bundle-Vendor: %provider_name
Bundle-ActivationPolicy: lazy
Bundle-Localization: plugin
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Import-Package: org.apache.commons.compress.archivers;version="[1.3,2.0)",
Import-Package: org.apache.commons.codec.binary;version="[1.6.0,2.0.0)",
org.apache.commons.compress.archivers;version="[1.3,2.0)",
org.apache.commons.compress.archivers.tar;version="[1.3,2.0)",
org.apache.commons.compress.archivers.zip;version="[1.3,2.0)",
org.eclipse.jgit.api;version="[4.2.0,4.3.0)",
@@ -31,6 +32,7 @@ Import-Package: org.apache.commons.compress.archivers;version="[1.3,2.0)",
org.eclipse.jgit.storage.file;version="[4.2.0,4.3.0)",
org.eclipse.jgit.storage.pack;version="[4.2.0,4.3.0)",
org.eclipse.jgit.transport;version="[4.2.0,4.3.0)",
org.eclipse.jgit.transport.http.apache;version="[4.2.0,4.3.0)",
org.eclipse.jgit.transport.resolver;version="[4.2.0,4.3.0)",
org.eclipse.jgit.treewalk;version="[4.2.0,4.3.0)",
org.eclipse.jgit.treewalk.filter;version="[4.2.0,4.3.0)",

+ 11
- 0
org.eclipse.jgit.pgm/pom.xml View File

@@ -94,6 +94,17 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit.http.apache</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>

+ 3
- 0
org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Main.java View File

@@ -62,6 +62,8 @@ import org.eclipse.jgit.lib.RepositoryBuilder;
import org.eclipse.jgit.pgm.internal.CLIText;
import org.eclipse.jgit.pgm.opt.CmdLineParser;
import org.eclipse.jgit.pgm.opt.SubcommandHandler;
import org.eclipse.jgit.transport.HttpTransport;
import org.eclipse.jgit.transport.http.apache.HttpClientConnectionFactory;
import org.eclipse.jgit.util.CachedAuthenticator;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
@@ -95,6 +97,7 @@ public class Main {
* arguments.
*/
public static void main(final String[] argv) {
HttpTransport.setConnectionFactory(new HttpClientConnectionFactory());
new Main().run(argv);
}


+ 1
- 0
org.eclipse.jgit/src/org/eclipse/jgit/api/TransportCommand.java View File

@@ -79,6 +79,7 @@ public abstract class TransportCommand<C extends GitCommand, T> extends
*/
protected TransportCommand(final Repository repo) {
super(repo);
setCredentialsProvider(CredentialsProvider.getDefault());
}

/**

Loading…
Cancel
Save