Skip to content

Commit

Permalink
Fix null credentialsId passed to GitSCM when authentication is userna…
Browse files Browse the repository at this point in the history
…me/password or the StandardUsernameCredential provided by the BitbucketAuthenticator is null.
  • Loading branch information
nfalco79 committed Nov 24, 2024
1 parent 89ddddb commit 068d50e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.cloudbees.jenkins.plugins.bitbucket.server.client.repository.BitbucketServerRepository;
import com.cloudbees.plugins.credentials.CredentialsNameProvider;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.damnhandy.uri.template.UriTemplate;
import com.fasterxml.jackson.databind.util.StdDateFormat;
import edu.umd.cs.findbugs.annotations.CheckForNull;
Expand All @@ -66,6 +67,7 @@
import hudson.model.Items;
import hudson.model.TaskListener;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.scm.SCM;
import hudson.security.AccessControlled;
import hudson.util.FormFillFailure;
Expand Down Expand Up @@ -1000,12 +1002,32 @@ private BitbucketCommit findPRDestinationCommit(BitbucketPullRequest pr, TaskLis
public SCM build(SCMHead head, SCMRevision revision) {
initCloneLinks();

boolean sshAuth = traits.stream()
.anyMatch(SSHCheckoutTrait.class::isInstance);
String scmCredentialsId = credentialsId;

BitbucketAuthenticator authenticator = authenticator();
return new BitbucketGitSCMBuilder(this, head, revision, null)
.withExtension(new GitClientAuthenticatorExtension(authenticator == null || sshAuth ? null : authenticator.getCredentialsForSCM()))
GitSCMExtension scmExtension;
if (authenticator != null) {

Check warning on line 1009 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1009 is only partially covered, one branch is missing
boolean sshAuth = traits.stream()
.anyMatch(SSHCheckoutTrait.class::isInstance);
if (sshAuth) {
// trait will do the magic
scmCredentialsId = null;
scmExtension = new GitClientAuthenticatorExtension(null);
} else {
StandardUsernameCredentials scmCredentials = authenticator.getCredentialsForSCM();
// extension overrides the configured credentialsId with a custom StandardUsernameCredentials provided by the Authenticator
scmExtension = new GitClientAuthenticatorExtension(scmCredentials);
if (scmCredentials != null) {
// will be overridden by git extension
scmCredentialsId = null;
}
}
} else {

Check warning on line 1025 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 1010-1025 are not covered by tests
scmExtension = new GitClientAuthenticatorExtension(null);
}

return new BitbucketGitSCMBuilder(this, head, revision, scmCredentialsId)
.withExtension(scmExtension)
.withCloneLinks(primaryCloneLinks, mirrorCloneLinks)
.withTraits(traits)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,7 @@
package com.cloudbees.jenkins.plugins.bitbucket.api.credentials;

import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import hudson.model.Descriptor.FormException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpHost;
Expand Down Expand Up @@ -79,13 +75,4 @@ public void configureContext(HttpClientContext context, HttpHost host) {
context.setAuthCache(authCache);
}

@Override
public StandardUsernameCredentials getCredentialsForSCM() {
try {
return new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL, getId(), null, httpCredentials.getUserName(), httpCredentials.getPassword());
} catch (FormException e) {
throw new RuntimeException(e);
}
}
}

0 comments on commit 068d50e

Please sign in to comment.