Java Code Examples for org.eclipse.jgit.transport.URIish#getHost()

The following examples show how to use org.eclipse.jgit.transport.URIish#getHost() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitUtils.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns whether or not the git repository URI is forbidden. If a scheme of the URI is matched, check if the scheme is a supported protocol. Otherwise,
 * match for a scp-like ssh URI: [user@]host.xz:path/to/repo.git/ and ensure the URI does not represent a local file path.
 * 
 * @param uri
 *            A git repository URI
 * @return a boolean of whether or not the git repository URI is forbidden.
 */
public static boolean isForbiddenGitUri(URIish uri) {
	String scheme = uri.getScheme();
	String host = uri.getHost();
	String path = uri.getPath();
	boolean isForbidden = false;

	if (scheme != null) {
		isForbidden = !uriSchemeWhitelist.contains(scheme);
	} else {
		// match for a scp-like ssh URI
		if (host != null) {
			isForbidden = host.length() == 1 || path == null;
		} else {
			isForbidden = true;
		}
	}

	return isForbidden;
}
 
Example 2
Source File: GitUtils.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
public static boolean isInGithub(String url) throws URISyntaxException {
	URIish uri = new URIish(url);
	String domain = uri.getHost();
	if(domain==null){
		return false;
	}
	if(domain.equals("github.com")){
		return true;
	}
	String known = PreferenceHelper.getString(KNOWN_GITHUB_HOSTS);
	if(known!=null){
		String[] knownHosts = known.split(",");
		for(String host : knownHosts){
			if(domain.equals(host)){
				return true;
			}
		}
	}

	return false;
}
 
Example 3
Source File: GitRepository.java    From CardinalPGM with MIT License 6 votes vote down vote up
private static String format(URIish uri) {
    StringBuilder r = new StringBuilder();

    if (uri.getScheme() != null) r.append(uri.getScheme()).append("://");

    if (uri.getHost() != null) {
        r.append(uri.getHost());
        if (uri.getScheme() != null && uri.getPort() > 0) r.append(':').append(uri.getPort());
    }

    if (uri.getPath() != null) {
        if (uri.getScheme() != null) {
            if (!uri.getPath().startsWith("/") && !uri.getPath().isEmpty()) r.append('/');
        } else if(uri.getHost() != null) r.append(':');

        if (uri.getScheme() != null) r.append(uri.getRawPath());
        else r.append(uri.getPath());
    }

    return r.toString();
}
 
Example 4
Source File: JGitSshSessionFactory.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized RemoteSession getSession (URIish uri, CredentialsProvider credentialsProvider, FS fs, int tms) throws TransportException {
    boolean agentUsed = false;
    String host = uri.getHost();
    CredentialItem.StringType identityFile = null;
    if (credentialsProvider != null) {
        identityFile = new JGitCredentialsProvider.IdentityFileItem("Identity file for " + host, false);
        if (credentialsProvider.isInteractive() && credentialsProvider.get(uri, identityFile) && identityFile.getValue() != null) {
            LOG.log(Level.FINE, "Identity file for {0}: {1}", new Object[] { host, identityFile.getValue() }); //NOI18N
            agentUsed = setupJSch(fs, host, identityFile, uri, true);
            LOG.log(Level.FINE, "Setting cert auth for {0}, agent={1}", new Object[] { host, agentUsed }); //NOI18N
        }
    }
    try {
        LOG.log(Level.FINE, "Trying to connect to {0}, agent={1}", new Object[] { host, agentUsed }); //NOI18N
        return super.getSession(uri, credentialsProvider, fs, tms);
    } catch (Exception ex) {
        // catch rather all exceptions. In case jsch-agent-proxy is broken again we should
        // at least fall back on key/pasphrase
        if (agentUsed) {
            LOG.log(ex instanceof TransportException ? Level.FINE : Level.INFO, null, ex);
            setupJSch(fs, host, identityFile, uri, false);
            LOG.log(Level.FINE, "Trying to connect to {0}, agent={1}", new Object[] { host, false }); //NOI18N
            return super.getSession(uri, credentialsProvider, fs, tms);
        } else {
            LOG.log(Level.FINE, "Connection failed: {0}", host); //NOI18N
            throw ex;
        }
    }
}
 
Example 5
Source File: GitPullRequestHandlerV1.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public static String[] parseSshGitUrl(String url) throws URISyntaxException{
		String user = "", project = "";
		URIish uriish = new URIish(url);
		String[] scp = uriish.getPath().replaceFirst("^/", "").split("/", -1);;
		if(scp.length==2)
		{
			user = scp[0];
			project = uriish.getHumanishName();
		}
		return new String[]{uriish.getHost(),user,project};
}
 
Example 6
Source File: GitTest.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
protected void assertRepositoryInfo(URIish uri, JSONObject result) throws JSONException {
	assertEquals(uri.toString(), result.getJSONObject("JsonData").get("Url"));
	if (uri.getUser() != null)
		assertEquals(uri.getUser(), result.getJSONObject("JsonData").get("User"));
	if (uri.getHost() != null)
		assertEquals(uri.getHost(), result.getJSONObject("JsonData").get("Host"));
	if (uri.getHumanishName() != null)
		assertEquals(uri.getHumanishName(), result.getJSONObject("JsonData").get("HumanishName"));
	if (uri.getPass() != null)
		assertEquals(uri.getPass(), result.getJSONObject("JsonData").get("Password"));
	if (uri.getPort() > 0)
		assertEquals(uri.getPort(), result.getJSONObject("JsonData").get("Port"));
	if (uri.getScheme() != null)
		assertEquals(uri.getScheme(), result.getJSONObject("JsonData").get("Scheme"));
}
 
Example 7
Source File: SshUriPropertyProcessor.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
protected static String getHostname(String uri) {
	try {
		URIish urIish = new URIish(uri);
		return urIish.getHost();
	}
	catch (URISyntaxException e) {
		return null;
	}
}
 
Example 8
Source File: TrileadSessionFactory.java    From git-client-plugin with MIT License 5 votes vote down vote up
/** {@inheritDoc} */
@Override
public RemoteSession getSession(URIish uri, CredentialsProvider credentialsProvider, FS fs, int tms) throws TransportException {
    try {
        int p = uri.getPort();
        if (p<0)    p = 22;
        Connection con = new Connection(uri.getHost(), p);
        con.setTCPNoDelay(true);
        con.connect();  // TODO: host key check

        boolean authenticated;
        if (credentialsProvider instanceof SmartCredentialsProvider) {
            final SmartCredentialsProvider smart = (SmartCredentialsProvider) credentialsProvider;
            StandardUsernameCredentialsCredentialItem
                    item = new StandardUsernameCredentialsCredentialItem("Credentials for " + uri, false);
            authenticated = smart.supports(item)
                    && smart.get(uri, item)
                    && SSHAuthenticator.newInstance(con, item.getValue(), uri.getUser())
                    .authenticate(smart.listener);
        } else if (credentialsProvider instanceof CredentialsProviderImpl) {
            CredentialsProviderImpl sshcp = (CredentialsProviderImpl) credentialsProvider;

            authenticated = SSHAuthenticator.newInstance(con, sshcp.cred).authenticate(sshcp.listener);
        } else {
            authenticated = false;
        }
        if (!authenticated && con.isAuthenticationComplete())
            throw new TransportException("Authentication failure");

        return wrap(con);
    } catch (UnsupportedCredentialItem | IOException | InterruptedException e) {
        throw new TransportException(uri,"Failed to connect",e);
    }
}
 
Example 9
Source File: GitSshSessionFactory.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public RemoteSession getSession(URIish uri, CredentialsProvider credentialsProvider, FS fs, int tms) throws TransportException {
	int port = uri.getPort();
	String user = uri.getUser();
	String pass = uri.getPass();
	if (credentialsProvider instanceof GitCredentialsProvider) {
		if (port <= 0)
			port = SSH_PORT;

		GitCredentialsProvider cp = (GitCredentialsProvider) credentialsProvider;
		if (user == null) {
			CredentialItem.Username u = new CredentialItem.Username();
			if (cp.supports(u) && cp.get(cp.getUri(), u)) {
				user = u.getValue();
			}
		}
		if (pass == null) {
			CredentialItem.Password p = new CredentialItem.Password();
			if (cp.supports(p) && cp.get(cp.getUri(), p)) {
				pass = new String(p.getValue());
			}
		}

		try {
			final SessionHandler session = new SessionHandler(user, uri.getHost(), port, cp.getKnownHosts(), cp.getPrivateKey(), cp.getPublicKey(),
					cp.getPassphrase());
			if (pass != null)
				session.setPassword(pass);
			if (!credentialsProvider.isInteractive()) {
				session.setUserInfo(new CredentialsProviderUserInfo(session.getSession(), credentialsProvider));
			}

			session.connect(tms);

			return new JschSession(session.getSession(), uri);
		} catch (JSchException e) {
			throw new TransportException(uri, e.getMessage(), e);
		}
	}
	return null;
}