-
-
Notifications
You must be signed in to change notification settings - Fork 282
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Jira Server Bearer auth support (#497)
- Loading branch information
1 parent
fbfeb9e
commit 851f553
Showing
12 changed files
with
364 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package hudson.plugins.jira; | ||
|
||
import java.net.URI; | ||
|
||
import com.atlassian.jira.rest.client.auth.BasicHttpAuthenticationHandler; | ||
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials; | ||
|
||
import hudson.plugins.jira.JiraSite.ExtendedAsynchronousJiraRestClientFactory; | ||
import hudson.plugins.jira.auth.BearerHttpAuthenticationHandler; | ||
import hudson.plugins.jira.extension.ExtendedJiraRestClient; | ||
|
||
/** | ||
* Jira Session factory implementation | ||
* | ||
* @author Elia Bracci | ||
*/ | ||
public class JiraSessionFactory { | ||
|
||
/** | ||
* This method takes as parameters the JiraSite class, the jira URI and | ||
* credentials and returns a JiraSession with Basic authentication if | ||
* useBearerAuth is set to false, otherwise it returns a JiraSession with Bearer | ||
* authentication if useBearerAuth is set to true. | ||
* | ||
* @param jiraSite jiraSite class | ||
* @param uri jira uri | ||
* @param credentials Jenkins credentials | ||
* @return JiraSession instance | ||
*/ | ||
public static JiraSession create(JiraSite jiraSite, URI uri, | ||
StandardUsernamePasswordCredentials credentials) { | ||
ExtendedJiraRestClient jiraRestClient; | ||
JiraRestService jiraRestService; | ||
|
||
if (jiraSite.isUseBearerAuth()) { | ||
BearerHttpAuthenticationHandler bearerHttpAuthenticationHandler = new BearerHttpAuthenticationHandler( | ||
credentials.getPassword().getPlainText()); | ||
|
||
jiraRestClient = new ExtendedAsynchronousJiraRestClientFactory() | ||
.create( | ||
uri, | ||
bearerHttpAuthenticationHandler, | ||
jiraSite.getHttpClientOptions()); | ||
|
||
jiraRestService = new JiraRestService( | ||
uri, | ||
jiraRestClient, | ||
credentials.getPassword().getPlainText(), | ||
jiraSite.getReadTimeout()); | ||
} else { | ||
jiraRestClient = new ExtendedAsynchronousJiraRestClientFactory() | ||
.create( | ||
uri, | ||
new BasicHttpAuthenticationHandler( | ||
credentials.getUsername(), | ||
credentials.getPassword().getPlainText()), | ||
jiraSite.getHttpClientOptions()); | ||
|
||
jiraRestService = new JiraRestService( | ||
uri, | ||
jiraRestClient, | ||
credentials.getUsername(), | ||
credentials.getPassword().getPlainText(), | ||
jiraSite.getReadTimeout()); | ||
} | ||
|
||
return new JiraSession(jiraSite, jiraRestService); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/main/java/hudson/plugins/jira/auth/BearerHttpAuthenticationHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package hudson.plugins.jira.auth; | ||
|
||
import com.atlassian.jira.rest.client.api.AuthenticationHandler; | ||
import com.atlassian.httpclient.api.Request.Builder; | ||
|
||
/** | ||
* Authentication handler for bearer authentication | ||
* | ||
* @author Elia Bracci | ||
*/ | ||
public class BearerHttpAuthenticationHandler implements AuthenticationHandler { | ||
|
||
private static final String AUTHORIZATION_HEADER = "Authorization"; | ||
private final String token; | ||
|
||
/** | ||
* Bearer http authentication handler constructor | ||
* @param token pat or api token to use for bearer authentication | ||
*/ | ||
public BearerHttpAuthenticationHandler(final String token) { | ||
this.token = token; | ||
} | ||
|
||
|
||
@Override | ||
public void configure(Builder builder) { | ||
builder.setHeader(AUTHORIZATION_HEADER, "Bearer " + token); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/main/resources/hudson/plugins/jira/JiraSite/config.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
site.alternativeUrl=Jira alternative URL | ||
site.timeout=in seconds | ||
site.useBearerAuth=Note: Bearer authentication is only supported in Jira Server, for Jira Cloud leave this unchecked |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
|
||
import com.atlassian.jira.rest.client.api.domain.Component; | ||
import com.atlassian.jira.rest.client.api.domain.Issue; | ||
import com.atlassian.jira.rest.client.api.domain.IssueType; | ||
import com.atlassian.jira.rest.client.api.domain.Status; | ||
import com.atlassian.jira.rest.client.api.domain.Transition; | ||
import com.atlassian.jira.rest.client.api.domain.User; | ||
import hudson.plugins.jira.JiraRestService; | ||
import hudson.plugins.jira.JiraSite; | ||
import hudson.plugins.jira.auth.BearerHttpAuthenticationHandler; | ||
import hudson.plugins.jira.extension.ExtendedJiraRestClient; | ||
import hudson.plugins.jira.extension.ExtendedVersion; | ||
|
||
import java.net.URI; | ||
import java.net.URL; | ||
import java.util.List; | ||
|
||
import static hudson.plugins.jira.JiraSite.ExtendedAsynchronousJiraRestClientFactory; | ||
|
||
/** | ||
* Test bed to play with Jira. | ||
* | ||
* @author Elia Bracci | ||
*/ | ||
public class JiraTesterBearerAuth { | ||
public static void main(String[] args) throws Exception { | ||
|
||
final URI uri = new URL(JiraConfig.getUrl()).toURI(); | ||
final BearerHttpAuthenticationHandler handler = new BearerHttpAuthenticationHandler(JiraConfig.getToken()); | ||
final ExtendedJiraRestClient jiraRestClient = new ExtendedAsynchronousJiraRestClientFactory() | ||
.createWithAuthenticationHandler(uri, handler); | ||
|
||
final JiraRestService restService = new JiraRestService(uri, jiraRestClient, JiraConfig.getToken(), JiraSite.DEFAULT_TIMEOUT); | ||
|
||
final String projectKey = "TESTPROJECT"; | ||
final String issueId = "TESTPROJECT-425"; | ||
final Integer actionId = 21; | ||
|
||
final Issue issue = restService.getIssue(issueId); | ||
System.out.println("issue:" + issue); | ||
|
||
|
||
final List<Transition> availableActions = restService.getAvailableActions(issueId); | ||
for (Transition action : availableActions) { | ||
System.out.println("Action:" + action); | ||
} | ||
|
||
for (IssueType issueType : restService.getIssueTypes()) { | ||
System.out.println(" issue type: " + issueType); | ||
} | ||
|
||
// restService.addVersion("TESTPROJECT", "0.0.2"); | ||
|
||
final List<Component> components = restService.getComponents(projectKey); | ||
for (Component component : components) { | ||
System.out.println("component: " + component); | ||
} | ||
|
||
// BasicComponent backendComponent = null; | ||
// final Iterable<BasicComponent> components1 = Lists.newArrayList(backendComponent); | ||
// restService.createIssue("TESTPROJECT", "This is a test issue created using Jira jenkins plugin. Please ignore it.", "TESTUSER", components1, "test issue from Jira jenkins plugin"); | ||
|
||
final List<Issue> searchResults = restService.getIssuesFromJqlSearch("project = \"TESTPROJECT\"", 3); | ||
for (Issue searchResult : searchResults) { | ||
System.out.println("JQL search result: " + searchResult); | ||
} | ||
|
||
final List<String> projectsKeys = restService.getProjectsKeys(); | ||
for (String projectsKey : projectsKeys) { | ||
System.out.println("project key: " + projectsKey); | ||
} | ||
|
||
final List<Status> statuses = restService.getStatuses(); | ||
for (Status status : statuses) { | ||
System.out.println("status:" + status); | ||
} | ||
|
||
final User user = restService.getUser("TESTUSER"); | ||
System.out.println("user: " + user); | ||
|
||
final List<ExtendedVersion> versions = restService.getVersions(projectKey); | ||
for (ExtendedVersion version : versions) { | ||
System.out.println("version: " + version); | ||
} | ||
|
||
// Version releaseVersion = new Version(version.getSelf(), version.getId(), version.getName(), | ||
// version.getDescription(), version.isArchived(), true, new DateTime()); | ||
// System.out.println(" >>>> release version 0.0.2"); | ||
// restService.releaseVersion("TESTPROJECT", releaseVersion); | ||
|
||
// System.out.println(" >>> update issue TESTPROJECT-425"); | ||
// restService.updateIssue(issueId, Collections.singletonList(releaseVersion)); | ||
|
||
// final Issue updatedIssue = restService.progressWorkflowAction(issueId, actionId); | ||
// System.out.println("Updated issue:" + updatedIssue); | ||
|
||
|
||
|
||
for(int i=0;i<10;i++){ | ||
callUniq( restService ); | ||
} | ||
|
||
for(int i=0;i<10;i++){ | ||
callDuplicate( restService ); | ||
} | ||
|
||
} | ||
|
||
private static void callUniq(final JiraRestService restService) throws Exception { | ||
long start = System.currentTimeMillis(); | ||
List<Issue> issues = restService.getIssuesFromJqlSearch( "key in ('JENKINS-53320','JENKINS-51057')", Integer.MAX_VALUE ); | ||
long end = System.currentTimeMillis(); | ||
System.out.println( "time uniq " + (end -start) ); | ||
} | ||
|
||
private static void callDuplicate(final JiraRestService restService) throws Exception { | ||
long start = System.currentTimeMillis(); | ||
List<Issue> issues = restService.getIssuesFromJqlSearch( "key in ('JENKINS-53320','JENKINS-53320','JENKINS-53320','JENKINS-53320','JENKINS-53320','JENKINS-51057','JENKINS-51057','JENKINS-51057','JENKINS-51057','JENKINS-51057')", Integer.MAX_VALUE ); | ||
long end = System.currentTimeMillis(); | ||
System.out.println( "time duplicate " + (end -start) ); | ||
} | ||
|
||
} |
Oops, something went wrong.