Skip to content
This repository was archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
adding remote authentication support
Browse files Browse the repository at this point in the history
- Remote authentication support via the API Token
- Help files for new fields
- Hitting the remote URL is now done via POST and not GET
  • Loading branch information
morficus committed Nov 30, 2013
1 parent 0e8d670 commit a23ade0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.Arrays;
import java.util.List;

import org.apache.commons.codec.binary.Base64;

/**
*
* @author Maurice W.
Expand Down Expand Up @@ -198,6 +200,7 @@ private String buildTriggerUrl() {
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException {

String triggerUrlString = this.buildTriggerUrl();
RemoteJenkinsServer remoteServer = this.findRemoteHost(this.getRemoteJenkinsName());

listener.getLogger().println("Triggering this job: " + this.getJob());
listener.getLogger().println("Using this remote Jenkins config: " + this.getRemoteJenkinsName());
Expand All @@ -212,9 +215,17 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
URL triggerUrl = new URL(triggerUrlString);
connection = (HttpURLConnection) triggerUrl.openConnection();


//if there is a username + apiToken defined for this remote host, then use it
String usernameTokenConcat = remoteServer.getUsername() + ":" + remoteServer.getApiToken();
if(usernameTokenConcat != null && !usernameTokenConcat.equals(":")) {

This comment has been minimized.

Copy link
@daniel-beck

daniel-beck Nov 30, 2013

Can this ever be null?

This comment has been minimized.

Copy link
@morficus

morficus Nov 30, 2013

Author Owner

Actually, no :-p It will always be a String

byte[] encodedAuthKey = Base64.encodeBase64(usernameTokenConcat.getBytes());
connection.setRequestProperty("Authorization", "Basic " + new String(encodedAuthKey));
}

connection.setDoInput(true);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestMethod("GET");
connection.setRequestMethod("POST");
// wait up to 5 seconds for the connection to be open
connection.setConnectTimeout(5000);
connection.connect();
Expand All @@ -230,8 +241,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
//StringBuffer response = new StringBuffer();

//while ((line = rd.readLine()) != null) {
//System.out.println(line); //JSONObject r = new

//System.out.println(line);
//}
//rd.close();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,19 @@ public class RemoteJenkinsServer extends AbstractDescribableImpl<RemoteJenkinsSe
private final URL address;
private final String displayName;
private final boolean hasBuildTokenRootSupport;
private final String username;
private final String apiToken;

@DataBoundConstructor
public RemoteJenkinsServer(String address, String displayName, boolean hasBuildTokenRootSupport) throws MalformedURLException {
public RemoteJenkinsServer(String address, String displayName, boolean hasBuildTokenRootSupport, String username, String apiToken) throws MalformedURLException {

this.address = new URL(address);
this.displayName = displayName;
this.displayName = displayName.trim();
this.hasBuildTokenRootSupport = hasBuildTokenRootSupport;

this.username = username.trim();
this.apiToken = apiToken.trim();


}

Expand All @@ -55,6 +61,14 @@ public URL getAddress() {
public boolean getHasBuildTokenRootSupport() {
return this.hasBuildTokenRootSupport;
}

public String getUsername() {
return this.username;
}

public String getApiToken() {
return this.apiToken;
}

@Override
public DescriptorImpl getDescriptor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
Remote Job Token
</div>
Security token which is defined on the job of the remote Jenkins host.
<br/>
If no job token is needed to trigger this job, then just leave it blank
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@
<f:checkbox />
</f:entry>

<f:advanced title="Add/Edit username &amp; token">
<f:entry title="Remote Username" field="username">
<f:textbox />
</f:entry>
<f:entry title="API Token" field="apiToken">
<f:password />
</f:entry>
</f:advanced>

<f:entry title="Remote address and port" field="address" >
<f:textbox />
</f:entry>
<f:validateButton title="Validate Address" method="validateAddress" with="address" />

</f:entry>
<f:validateButton title="Validate Address" method="validateAddress" with="address" />

<f:entry title="">
<div style="float: right">
<div style="float: left">
<f:repeatableDeleteButton />
</div>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
<div style="font-weight: bolder; text-decoration: underline">
API Token
</div>
This API token from the remote host that will be used for authentication.
<br/>
See the <a href="https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API" target="_blank">
Jenkins wiki
</a>
for more details.
</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div>
<div style="font-weight: bolder; text-decoration: underline">
Remote Username
</div>
The username that will be utilized when opening a connection to the remote host.
</div>

0 comments on commit a23ade0

Please sign in to comment.