jenkinsci / java-client-api

A Jenkins API client for Java
MIT License
901 stars 470 forks source link

Sending POST http request for Job.build(Map<String, String> params, boolean crumbFlag) with crumbFlag=true raised java.lang.IllegalArgumentException: Name may not be null #433

Open mw32 opened 4 years ago

mw32 commented 4 years ago

Hi, I am quiet new to Jenkins client API. I'm trying to use the Job.build method to kick off a parameterized job with crumb flag set to true. However, it threw this exception:

java.lang.IllegalArgumentException: Name may not be null
    at org.apache.http.util.Args.notNull(Args.java:54)
    at org.apache.http.message.BasicHeader.<init>(BasicHeader.java:61)
    at com.offbytwo.jenkins.client.JenkinsHttpClient.post(JenkinsHttpClient.java:233)
    at com.offbytwo.jenkins.model.Job.build(Job.java:151)

Inspecting further in JenkinsHttpClient.java, it looks like the issue is with crumb issuer path:

Crumb crumb = getQuietly("/crumbIssuer", Crumb.class);

Shouldn't this be crumbIssuer/api/xml or crumbIssuer/api/json??

I can't turn off CSRF in this Jenkins instance, so that's not an option. How can I workaround or fix this issue?

kumarshorav11 commented 4 years ago

@mw32 Could you post your code that you are using to build the job. I have unchecked the CSRF option in Jenkins server /configureSecurity. I am using below code to create a job by passing config.xml -

HttpClientBuilder builder = HttpClientBuilder.create();
        JenkinsHttpClient client = new JenkinsHttpClient(uri, builder, "XXX", "XXX");
        JenkinsServer jenkins = new JenkinsServer(client);
        String sourceXML = readFile("src/main/resources/config.xml");
        System.out.println(String.format("Installed Jenkins Version >> %s", jenkins.getVersion().getLiteralVersion()));//works and gives correct result
        jenkins.createJob("test-nov1", sourceXML);

I am using createJob() Method to create job in Jenkins but getting 403 error even though passing correct username and password or token. Moreover, what I found is that, when I hit through user name and password/token, it is logged in as anonymous user than actual user that is passed as parameter. Hence throwing 403 error as unauthorized. Please help me out if you have some thing on this.

Thanks

mw32 commented 4 years ago

@kumarshorav11 - It's a very simple build call:

def runBuild(jobName: String) = {
  val server: JenkinsServer = new JenkinsServer(jenkinsURI, jenkinsConfig.user, jenkinsConfig.password)
    val details = server.getJobs.get(jobName).details
    val queue = details.build(true)
    queue
  }

This seems to be OK as far as credentials go, but the problem is as stated above, it looks like it's attempting to get the crumb from the wrong crumbIssuer path.

From what I can tell, your code above is going to call post_xml in JenkinsHttpClient (vs mine using post). Not sure why you were getting a different error though. U sure the password is correct (same as what you entered in GUI)?

kumarshorav11 commented 4 years ago

@mw32 Thank U so much for your quick answer. Yes, my password is correct as per WebUI. I am using same password that I have used for jenkins web ui to log in.