Skip to content
Permalink
Browse files
[FIXED JENKINS-21892] Update swarm client to send CSRF token
  • Loading branch information
sjsf committed Feb 21, 2014
1 parent c75e858 commit 5d97fa1679d07e04e1fe93ed2ead77cd06a3ef36
Showing 1 changed file with 36 additions and 0 deletions.
@@ -3,6 +3,7 @@
import hudson.remoting.Launcher;
import hudson.remoting.jnlp.Main;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.UsernamePasswordCredentials;
import org.apache.commons.httpclient.auth.AuthScope;
import org.apache.commons.httpclient.methods.GetMethod;
@@ -364,6 +365,22 @@ protected HttpClient createHttpClient(URL urlForAuth) {
return client;
}

private Crumb getCsrfCrumb(HttpClient client) throws IOException {
GetMethod httpGet = new GetMethod(target.url + "crumbIssuer/api/xml?xpath=" + URLEncoder.encode("concat(//crumbRequestField,\":\",//crumb)", "UTF-8"));
httpGet.setDoAuthentication(true);
int responseCode = client.executeMethod(httpGet);
if (responseCode != HttpStatus.SC_OK) {
System.out.println("Could not obtain CSRF crumb. Response code: " + responseCode);
return null;
}
String[] crumbResponse = httpGet.getResponseBodyAsString().split(":");
if (crumbResponse.length != 2) {
System.out.println("Unexpected CSRF crumb response: " + httpGet.getResponseBodyAsString());
return null;
}
return new Crumb(crumbResponse[0], crumbResponse[1]);
}

protected void createSwarmSlave() throws IOException, InterruptedException,
RetryException {

@@ -391,6 +408,12 @@ protected void createSwarmSlave() throws IOException, InterruptedException,
+ param("mode", mode.toUpperCase()));

post.setDoAuthentication(true);

Crumb csrfCrumb = getCsrfCrumb(client);
if (csrfCrumb != null) {
post.addRequestHeader(csrfCrumb.crumbRequestField, csrfCrumb.crumb);
}

int responseCode = client.executeMethod(post);
if (responseCode != 200) {
throw new RetryException(
@@ -467,4 +490,17 @@ public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}

private static class Crumb {

private final String crumb;
private final String crumbRequestField;

Crumb(String crumbRequestField, String crumb) {
this.crumbRequestField = crumbRequestField;
this.crumb = crumb;
}

}

}

0 comments on commit 5d97fa1

Please sign in to comment.