From 068d50e9e2f2690a6977026ef2e049e7efe9eac9 Mon Sep 17 00:00:00 2001 From: Nikolas Falco Date: Fri, 22 Nov 2024 18:51:07 +0100 Subject: [PATCH] Fix null credentialsId passed to GitSCM when authentication is username/password or the StandardUsernameCredential provided by the BitbucketAuthenticator is null. --- .../plugins/bitbucket/BitbucketSCMSource.java | 30 ++++++++++++++++--- ...itbucketUsernamePasswordAuthenticator.java | 13 -------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java index 02ccbad4c..e077ff4be 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java @@ -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; @@ -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; @@ -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) { + 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 { + scmExtension = new GitClientAuthenticatorExtension(null); + } + + return new BitbucketGitSCMBuilder(this, head, revision, scmCredentialsId) + .withExtension(scmExtension) .withCloneLinks(primaryCloneLinks, mirrorCloneLinks) .withTraits(traits) .build(); diff --git a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/credentials/BitbucketUsernamePasswordAuthenticator.java b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/credentials/BitbucketUsernamePasswordAuthenticator.java index 86d000a6c..fbdd55d1b 100644 --- a/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/credentials/BitbucketUsernamePasswordAuthenticator.java +++ b/src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/credentials/BitbucketUsernamePasswordAuthenticator.java @@ -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; @@ -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); - } - } }