Skip to content

Commit

Permalink
Merge pull request #122 from jenkinsci/credentials
Browse files Browse the repository at this point in the history
Work for fixing credentials issues
  • Loading branch information
DavidTanner committed Jun 26, 2015
2 parents 00a4533 + 1bbfe83 commit 19ca9ff
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 21 deletions.
9 changes: 7 additions & 2 deletions src/main/java/org/jenkinsci/plugins/ghprb/Ghprb.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.domains.DomainSpecification;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import com.cloudbees.plugins.credentials.domains.HostnamePortSpecification;
import com.cloudbees.plugins.credentials.domains.HostnameSpecification;
import com.cloudbees.plugins.credentials.domains.PathSpecification;
Expand All @@ -30,10 +31,10 @@
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.PredicateUtils;
import org.apache.commons.collections.functors.InstanceofPredicate;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.ghprb.extensions.GhprbExtension;
import org.jenkinsci.plugins.ghprb.extensions.GhprbExtensionDescriptor;
import org.jenkinsci.plugins.ghprb.extensions.GhprbProjectExtension;
import org.jenkinsci.plugins.gitclient.GitURIRequirementsBuilder;
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;
import org.kohsuke.github.GHCommitState;
import org.kohsuke.github.GHUser;
Expand Down Expand Up @@ -369,7 +370,7 @@ public static void addIfMissing(DescribableList<GhprbExtension, GhprbExtensionDe
public static StandardCredentials lookupCredentials(Item project, String credentialId, String uri) {
return (credentialId == null) ? null : CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(StandardCredentials.class, project, ACL.SYSTEM,
GitURIRequirementsBuilder.fromUri(uri).build()),
URIRequirementBuilder.fromUri(uri).build()),
CredentialsMatchers.withId(credentialId));
}

Expand Down Expand Up @@ -406,6 +407,10 @@ private static String createCredentials(String serverAPIUrl, StandardCredentials
}

specifications.add(new SchemeSpecification(serverUri.getScheme()));
String path = serverUri.getPath();
if (StringUtils.isEmpty(path)) {
path = "/";
}
specifications.add(new PathSpecification(serverUri.getPath(), null, false));

Domain domain = new Domain(serverUri.getHost(), "Auto generated credentials domain", specifications);
Expand Down
70 changes: 55 additions & 15 deletions src/main/java/org/jenkinsci/plugins/ghprb/GhprbGitHubAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import org.kohsuke.github.GHAuthorization;
import org.kohsuke.github.GHMyself;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.kohsuke.stapler.AncestorInPath;
Expand All @@ -35,6 +36,7 @@
import com.cloudbees.plugins.credentials.domains.PathRequirement;
import com.cloudbees.plugins.credentials.domains.SchemeRequirement;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import com.google.common.base.Joiner;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.plaincredentials.StringCredentials;
Expand Down Expand Up @@ -247,26 +249,64 @@ public FormValidation doCheckServerAPIUrl(@QueryParameter String value) {
}
return FormValidation.warning("GitHub API URI is \"https://api.github.com\". GitHub Enterprise API URL ends with \"/api/v3\"");
}

public FormValidation doCheckRepoAccess(
@QueryParameter("serverAPIUrl") final String serverAPIUrl,
@QueryParameter("credentialsId") final String credentialsId,
@QueryParameter("repo") final String repo) {
try {
GitHubBuilder builder = getBuilder(serverAPIUrl, credentialsId);
if (builder == null) {
return FormValidation.error("No credentials ID provided!!");
}
GitHub gh = builder.build();
GHRepository repository = gh.getRepository(repo);
StringBuilder sb = new StringBuilder();
sb.append("User has access to: ");
List<String> permissions = new ArrayList<String>(3);
if (repository.hasAdminAccess()) {
permissions.add("Admin");
}
if (repository.hasPushAccess()) {
permissions.add("Push");
}
if (repository.hasPullAccess()) {
permissions.add("Pull");
}
sb.append(Joiner.on(", ").join(permissions));

return FormValidation.ok(sb.toString());
} catch (Exception ex) {
return FormValidation.error("Unable to connect to GitHub API: " + ex);
}
}

private GitHubBuilder getBuilder(String serverAPIUrl, String credentialsId) {
GitHubBuilder builder = new GitHubBuilder()
.withEndpoint(serverAPIUrl)
.withConnector(new HttpConnectorWithJenkinsProxy());

StandardCredentials credentials = Ghprb.lookupCredentials(null, credentialsId, serverAPIUrl);
if (credentials instanceof StandardUsernamePasswordCredentials) {
StandardUsernamePasswordCredentials upCredentials = (StandardUsernamePasswordCredentials) credentials;
builder.withPassword(upCredentials.getUsername(), upCredentials.getPassword().getPlainText());

} else if (credentials instanceof StringCredentials) {
StringCredentials tokenCredentials = (StringCredentials) credentials;
builder.withOAuthToken(tokenCredentials.getSecret().getPlainText());
} else {
return null;
}
return builder;
}

public FormValidation doTestGithubAccess(
@QueryParameter("serverAPIUrl") final String serverAPIUrl,
@QueryParameter("credentialsId") final String credentialsId) {
try {

GitHubBuilder builder = new GitHubBuilder()
.withEndpoint(serverAPIUrl)
.withConnector(new HttpConnectorWithJenkinsProxy());

StandardCredentials credentials = Ghprb.lookupCredentials(null, credentialsId, serverAPIUrl);
if (credentials instanceof StandardUsernamePasswordCredentials) {
StandardUsernamePasswordCredentials upCredentials = (StandardUsernamePasswordCredentials) credentials;
builder.withPassword(upCredentials.getUsername(), upCredentials.getPassword().getPlainText());

} else if (credentials instanceof StringCredentials) {
StringCredentials tokenCredentials = (StringCredentials) credentials;
builder.withOAuthToken(tokenCredentials.getSecret().getPlainText());
} else {
return FormValidation.error("No credentials provided");
GitHubBuilder builder = getBuilder(serverAPIUrl, credentialsId);
if (builder == null) {
return FormValidation.error("No credentials ID provided!!");
}
GitHub gh = builder.build();
GHMyself me = gh.getMyself();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ f.entry(title:_("Credentials"), field:"credentialsId") {
}""" /* workaround for JENKINS-19124 */)
}

f.entry(title:_("Test Credentials")) {
f.validateButton(title:_("Connect to API"), progress:_("Connecting..."), with:"serverAPIUrl,credentialsId", method:"testGithubAccess")
f.advanced(title:_("Test Credentials")) {
f.entry(title:_("Test Credentials")) {
f.validateButton(title:_("Connect to API"), progress:_("Connecting..."), with:"serverAPIUrl,credentialsId", method:"testGithubAccess")
f.entry(title:_("Repository owner/name"), field:"repo") {
f.textbox()
}
f.validateButton(title:_("Check repo permissions"), progress:_("Connecting..."), with:"serverAPIUrl,credentialsId,repo", method:"checkRepoAccess")
}
}

f.advanced(title:_("Create API Token")) {
Expand All @@ -47,8 +53,6 @@ f.advanced(title:_("Auth ID")) {

f.entry {
div(align:"right") {

input (type:"button", value:_("Add Server"), class:"repeatable-add show-if-last")
input (type:"button", value:_("Delete Server"), class:"repeatable-delete")
}
}

0 comments on commit 19ca9ff

Please sign in to comment.