1

I'm trying to login remotely to a Jenkins server using Java.

I didn't found some documentation on how this should be securely done.

For my local server using the url: http://user:pass@server doesn't work .

Can anybody recommend me some documentation regarding this topic ?

John Smith
  • 777
  • 2
  • 14
  • 37

2 Answers2

1

Documention from jenkins wiki

Should work with your Basic Authentication type.

Java example with httpclient 4.3.x

import java.io.IOException;
import java.net.URI;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.AuthCache;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
import org.apache.http.impl.client.BasicAuthCache;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class JenkinsScraper {

    public String scrape(String urlString, String username, String password) throws ClientProtocolException, IOException {
        URI uri = URI.create(urlString);
        HttpHost host = new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme());
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(username, password));
        // Create AuthCache instance
        AuthCache authCache = new BasicAuthCache();
        // Generate BASIC scheme object and add it to the local auth cache
        BasicScheme basicAuth = new BasicScheme();
        authCache.put(host, basicAuth);
        CloseableHttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
        HttpGet httpGet = new HttpGet(uri);
        // Add AuthCache to the execution context
        HttpClientContext localContext = HttpClientContext.create();
        localContext.setAuthCache(authCache);

        HttpResponse response = httpClient.execute(host, httpGet, localContext);

        return EntityUtils.toString(response.getEntity());
    }

}
MrSimpleMind
  • 7,890
  • 3
  • 40
  • 45
  • My problem actually concerns with security, is it ok to send the password directly from user into UsernamePasswordCredentials without crypting ? – John Smith Jul 10 '15 at 09:30
  • Well if it is more a security question than programming, I would not recommend remote login using http and basic auth. I would prefer some kind of security in front, some vpn or ssh tunnel before reaching the host. – MrSimpleMind Jul 10 '15 at 09:36
  • Sending it over http will allow anyone in your network to catch the password, it is plain text and easy to grab. As said, I would not open my server for public or remote logon. But using a secure Vpn or ssh tunnel or... will make it more secure. – MrSimpleMind Jul 10 '15 at 09:44
  • Thank you for your response, I will investigate on that then ! – John Smith Jul 10 '15 at 09:45
1

The Http/Html way is the most cumbersome! I would use jenkins cli or remote api. If you still insist using Java with http then you need to use basic http-authentication and if you plan on trigger more steps inside jenkins use a proper Http/Html Java library like Java-Selenium or HttpUnit.

Best and simple solution for using http basic auth in Java I found here: Http Basic Authentication in Java using HttpClient?

Community
  • 1
  • 1
blacklabelops
  • 4,708
  • 5
  • 25
  • 42