Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task/jenkins 42120 git creation credential changes #895

Merged
merged 40 commits into from
Mar 16, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
1c2e937
JENKINS-41397# validate git credential
Jan 25, 2017
0b207f7
Field specific error message
Jan 27, 2017
f029779
Merge remote-tracking branch 'origin/master' into task/JENKINS-41397
Jan 31, 2017
b0fb5dc
bug fixes
Jan 31, 2017
8dc82e1
credential validation is now 400 errors.
Jan 31, 2017
19d8621
Merge remote-tracking branch 'origin/master' into task/JENKINS-41397
Feb 14, 2017
718b731
Merge remote-tracking branch 'origin/master' into task/JENKINS-41397
Feb 14, 2017
6c11a94
[JENKINS-42120] fix a bug where some content from the FlowSteps (inac…
Mar 2, 2017
82821ab
[JENKINS-42120] rework Git creation flow to use modal for creating ne…
Mar 2, 2017
23d5978
Merge branch 'master' into task/JENKINS-42120-git-creation-credential…
Mar 6, 2017
954a68e
[JENKINS-42120] remove "System SSH" as an option
Mar 6, 2017
236b875
[JENKINS-42120] width of username / password inputs
Mar 6, 2017
8e9304b
[JENKINS-42120] remove obsolete code for credential type in "Connect"…
Mar 6, 2017
485d986
Merge branch 'task/JENKINS-41397' into task/JENKINS-42120-git-creatio…
Mar 6, 2017
830a0fb
[JENKINS-42120] add a step to Git creation flow that can display unha…
Mar 6, 2017
867f127
[JENKINS-42120] reworking Git creation to support different validatio…
Mar 6, 2017
9e9f778
[JENKINS-42120] fix bug where credentialId was not sent correctly; ad…
Mar 6, 2017
e852705
Merge remote-tracking branch 'origin/master' into task/JENKINS-41397
Mar 7, 2017
43bbde0
Use StandardCredentials in case of git.
Mar 7, 2017
699e1e8
List all credentials that user has access to.
Mar 7, 2017
eac6262
Merge branch 'task/JENKINS-41397' into task/JENKINS-42120-git-creatio…
Mar 8, 2017
3b49071
[JENKINS-42120] fix a ridiculous bug where credentials were being cre…
Mar 8, 2017
ada0ffd
[JENKINS-42120] add an API to remove all steps after the specified step
Mar 9, 2017
e0aad62
[JENKINS-42120] transform Git TransportException "authentication is r…
Mar 9, 2017
dae30b2
[JENKINS-42120] improve the error handling for Git creation, respondi…
Mar 9, 2017
f608ec8
[JENKINS-42120] delint
Mar 9, 2017
594b8a2
[JENKINS-42120] fix broken test
Mar 9, 2017
a8ceb12
Frogot to remove StandardCredentials.
vivek Mar 10, 2017
c8a986c
Merge branch 'task/JENKINS-41397' into task/JENKINS-42120-git-creatio…
Mar 10, 2017
393a791
[JENKINS-42120] use beta JDL with dropdown fixes and enhancements; re…
Mar 13, 2017
8614e64
[JENKINS-42120] fix a bug where selecting a credential would not allo…
Mar 13, 2017
9b95dde
[JENKINS-42120] adjust backend to transform the SSH "connection is no…
Mar 13, 2017
c7250b2
[JENKINS-42120] italicize the special "none" credential
Mar 14, 2017
3da646d
[JENKINS-42120] change Git creation UI to expose "System Default" opt…
Mar 15, 2017
fe568b1
[JENKINS-42120] use JDL that includes useful Dropdown fixes but inclu…
Mar 15, 2017
dfdb8fc
Merge branch 'master' into task/JENKINS-42120-git-creation-credential…
Mar 16, 2017
66ff520
release core-js 0.0.89
Mar 16, 2017
e606c7f
bump core-js version to 0.0.90-SNAPSHOT
Mar 16, 2017
b59388c
[JENKINS-42120] use latest JDL (and core-js, since we have to)
Mar 16, 2017
988d4d0
Merge branch 'master' into task/JENKINS-42120-git-creation-credential…
Mar 16, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
JENKINS-41397# validate git credential
  • Loading branch information
Vivek Pandey authored and Vivek Pandey committed Jan 27, 2017
commit 1c2e937312e732cd57acf84faaaed64f814f59fa
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ public BluePipeline create(Reachable parent) throws IOException {
.add(new ErrorMessage.Error("scmConfig.uri", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "uri is required")));
}

//XXX: set credentialId to empty string if null or we get NPE later on
String credentialId = scmConfig.getCredentialId() == null ? "" : scmConfig.getCredentialId();

TopLevelItem item = create(Jenkins.getInstance(), getName(), MODE, MultiBranchProjectDescriptor.class);

if (item instanceof WorkflowMultiBranchProject) {
WorkflowMultiBranchProject project = (WorkflowMultiBranchProject) item;
GitUtils.validateCredentials(project, sourceUri, scmConfig.getCredentialId());

//XXX: set credentialId to empty string if null or we get NPE later on
String credentialId = scmConfig.getCredentialId() == null ? "" : scmConfig.getCredentialId();

project.getSourcesList().add(new BranchSource(new GitSCMSource(null, sourceUri, credentialId, "*", "", false)));
project.scheduleBuild(new Cause.UserIdCause());
return new MultiBranchPipelineImpl(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ private BranchSource getGitScmSource(MultiBranchProject mbp){

if(scmConfig != null){
sourceUri = scmConfig.getUri();
if(sourceUri != null) {
GitUtils.validateCredentials(mbp, sourceUri, scmConfig.getCredentialId());
}
credentialId = scmConfig.getCredentialId() == null ? "" : scmConfig.getCredentialId();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.jenkins.blueocean.blueocean_git_pipeline;

import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
import hudson.EnvVars;
import hudson.model.TaskListener;
import hudson.plugins.git.GitException;
import hudson.security.ACL;
import io.jenkins.blueocean.commons.ErrorMessage;
import io.jenkins.blueocean.commons.ServiceException;
import jenkins.scm.api.SCMSourceOwner;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.IOException;

/**
* @author Vivek Pandey
*/
class GitUtils {
/**
* Calls 'git ls-remote -h uri' to check if git uri or supplied credentials are valid
*
* @param owner SCM owner, such as MultiBranchProject
* @param uri git repo uri
* @param credentialId credential id to use when accessing git
*/
static void validateCredentials(@Nonnull SCMSourceOwner owner, @Nonnull String uri, @Nullable String credentialId){
StandardUsernameCredentials credentials = null;
if(credentialId != null) {
credentials = getCredentials(owner, uri, credentialId);
if (credentials == null) {
throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Git pipeline")
.add(new ErrorMessage.Error("scmConfig.credentialId",
ErrorMessage.Error.ErrorCodes.INVALID.toString(),
"Invalid credentialId: " + credentialId)));
}
}
Git git = new Git(TaskListener.NULL, new EnvVars());
try {
GitClient gitClient = git.getClient();
if(credentials != null) {
gitClient.addCredentials(uri, credentials);
}
gitClient.getRemoteReferences(uri,null, true,false);
} catch (IOException | InterruptedException e) {
throw new ServiceException.UnexpectedErrorException("Failed to create pipeline due to unexpected error: "+e.getMessage(), e);
} catch (GitException e){
throw new ServiceException.BadRequestExpception(e.getMessage(), e);
} catch (IllegalStateException e){
throw new ServiceException.ForbiddenException(e.getMessage(), e);
}
}

private static StandardUsernameCredentials getCredentials(SCMSourceOwner owner, String uri, String credentialId){
return CredentialsMatchers
.firstOrNull(
CredentialsProvider.lookupCredentials(StandardUsernameCredentials.class, owner,
ACL.SYSTEM, URIRequirementBuilder.fromUri(uri).build()),
CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialId),
GitClient.CREDENTIALS_MATCHER));
}
}
Loading