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

Added support for custom git http user agent and header #110

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
12 changes: 8 additions & 4 deletions org.archicontribs.modelrepository/.classpath
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="lib" path="lib/org.eclipse.jgit_6.1.0.202203080745-r.jar" sourcepath="libsrc/org.eclipse.jgit.source_6.1.0.202203080745-r.jar"/>
<classpathentry exported="true" kind="lib" path="lib/org.eclipse.jgit.ssh.apache_6.1.0.202203080745-r.jar" sourcepath="libsrc/org.eclipse.jgit.ssh.apache.source_6.1.0.202203080745-r.jar"/>
<classpathentry exported="true" kind="lib" path="lib/org.apache.sshd.osgi_2.8.0.v20211227-1750.jar" sourcepath="libsrc/org.apache.sshd.osgi.source_2.8.0.v20211227-1750.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="/Users/phil/Documents/ws/AZRe/archi-dev/eclipse-ws/archi-modelrepository-plugin/org.archicontribs.modelrepository/lib/org.eclipse.jgit_6.1.0.202203080745-r.jar" sourcepath="libsrc/org.eclipse.jgit.source_6.1.0.202203080745-r.jar"/>
<classpathentry kind="lib" path="/Users/phil/Documents/ws/AZRe/archi-dev/eclipse-ws/archi-modelrepository-plugin/org.archicontribs.modelrepository/lib/org.eclipse.jgit.ssh.apache_6.1.0.202203080745-r.jar" sourcepath="libsrc/org.eclipse.jgit.ssh.apache.source_6.1.0.202203080745-r.jar"/>
<classpathentry kind="lib" path="/Users/phil/Documents/ws/AZRe/archi-dev/eclipse-ws/archi-modelrepository-plugin/org.archicontribs.modelrepository/lib/org.apache.sshd.osgi_2.8.0.v20211227-1750.jar" sourcepath="libsrc/org.apache.sshd.osgi.source_2.8.0.v20211227-1750.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jgit.transport.UserAgent;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

Expand All @@ -44,6 +45,9 @@ public class ModelRepositoryPlugin extends AbstractUIPlugin implements PropertyC
*/
public static ModelRepositoryPlugin INSTANCE;

public static String ENV_VAR_USERAGENT = "ARCHI_GIT_USERAGENT";
public static String ENV_VAR_ADDITIONALHEADER = "ARCHI_GIT_ADDITIONALHEADER";

public ModelRepositoryPlugin() {
INSTANCE = this;
}
Expand All @@ -52,7 +56,10 @@ public ModelRepositoryPlugin() {
public void start(BundleContext context) throws Exception {
super.start(context);
IEditorModelManager.INSTANCE.addPropertyChangeListener(this);

//override git useragent if specified in system property
if (System.getenv(ENV_VAR_USERAGENT) != null && !System.getenv(ENV_VAR_USERAGENT).isEmpty()) {
UserAgent.set(System.getenv(ENV_VAR_USERAGENT));
}
// Set this first
ProxyAuthenticator.init();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,39 @@

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.security.GeneralSecurityException;

import org.archicontribs.modelrepository.ModelRepositoryPlugin;
import org.archicontribs.modelrepository.grafico.GraficoUtils;
import org.archicontribs.modelrepository.grafico.IGraficoConstants;
import org.archicontribs.modelrepository.preferences.IPreferenceConstants;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;

import org.eclipse.jgit.api.TransportConfigCallback;
import org.eclipse.jgit.transport.SshSessionFactory;
import org.eclipse.jgit.transport.Transport;
import org.eclipse.jgit.transport.TransportHttp;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import org.eclipse.jgit.util.FS;
import org.eclipse.osgi.util.NLS;
import org.osgi.framework.Bundle;
import org.osgi.framework.FrameworkUtil;


/**
* Authenticator for SSH and HTTP
*
* @author Phillip Beauvoir
*/
public final class CredentialsAuthenticator {
private static final Bundle BUNDLE = FrameworkUtil.getBundle(CredentialsAuthenticator.class);
private static final ILog LOGGER = Platform.getLog(BUNDLE);

public interface SSHIdentityProvider {
File getIdentityFile();
Expand Down Expand Up @@ -92,11 +106,34 @@ public void configure(Transport transport) {
if(GraficoUtils.isSSH(repoURL)) {
transport.setCredentialsProvider(new SSHCredentialsProvider());
}
// HTTP
// HTTP
else if(npw != null) {
transport.setCredentialsProvider(new UsernamePasswordCredentialsProvider(npw.getUsername(), npw.getPassword()));
// check for environment variable to add additional http headers to git request
Map<String,String> envVariables = System.getenv();
String additionalHeader = envVariables.get(ModelRepositoryPlugin.ENV_VAR_ADDITIONALHEADER);
if (additionalHeader != null && transport instanceof TransportHttp) {
TransportHttp transportHttp = (TransportHttp) transport;
Map<String, String> headerMap = new HashMap<String, String>(1);
String headerKeyValue[] = additionalHeader.split(":");
if (headerKeyValue.length==2) {
headerMap.put(headerKeyValue[0], headerKeyValue[1]);
transportHttp.setAdditionalHeaders(headerMap);
log(">> added http headers:" + headerMap);
}
}
}
}

};
};

}

public static void log(String msg) {
log(msg, null);
}

public static void log(String msg, Exception e) {
LOGGER.log(new Status((e==null? Status.INFO:Status.ERROR), BUNDLE.getSymbolicName(), msg, e));
}
}