Skip to content

Commit

Permalink
[SECURITY-3363]
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroslavafenkin committed Jun 18, 2024
1 parent 44cf5e4 commit ad359b3
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1029,7 +1029,9 @@ public SCM build(SCMHead head, SCMRevision revision) {
switch (type) {
case GIT:
default:
return new BitbucketGitSCMBuilder(this, head, revision, getCredentialsId())
BitbucketAuthenticator authenticator = authenticator();
return new BitbucketGitSCMBuilder(this, head, revision, null)
.withExtension(authenticator == null ? null : new GitClientAuthenticatorExtension(authenticator.getCredentialsForScm()))

Check warning on line 1034 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 1034 is only partially covered, one branch is missing
.withCloneLinks(primaryCloneLinks, mirrorCloneLinks)
.withTraits(traits)
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.cloudbees.jenkins.plugins.bitbucket;

import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import hudson.plugins.git.GitException;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.extensions.GitSCMExtension;
import org.jenkinsci.plugins.gitclient.GitClient;

public class GitClientAuthenticatorExtension extends GitSCMExtension {

private final StandardUsernameCredentials credentials;

public GitClientAuthenticatorExtension(StandardUsernameCredentials credentials) {
this.credentials = credentials;
}

@Override
public GitClient decorate(GitSCM scm, GitClient git) throws GitException {
if (credentials != null) {
git.setCredentials(credentials);
}

return git;

Check warning on line 23 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/GitClientAuthenticatorExtension.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 13-23 are not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketCloudEndpoint;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import jenkins.authentication.tokens.api.AuthenticationTokenContext;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequest;
Expand Down Expand Up @@ -107,6 +108,16 @@ public void configureRequest(HttpRequest request) {
// override to configure HttpRequest
}


/**
* Provides credentials that can be used for authenticated interactions with SCM.
*
* @return credentials to be passed to {@link org.jenkinsci.plugins.gitclient.GitClient#setCredentials(StandardUsernameCredentials)}
*/
public StandardUsernameCredentials getCredentialsForScm() {
return null;

Check warning on line 118 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/BitbucketAuthenticator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 118 is not covered by tests
}

/**
* Add authentication token to clone link if
* authentication method requires it
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
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.impl.UsernamePasswordCredentialsImpl;
import hudson.util.Secret;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpRequest;
import org.jenkinsci.plugins.plaincredentials.StringCredentials;
Expand Down Expand Up @@ -31,4 +35,10 @@ public BitbucketAccessTokenAuthenticator(StringCredentials credentials) {
public void configureRequest(HttpRequest request) {
request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token.getPlainText());
}

@Override
public StandardUsernameCredentials getCredentialsForScm() {
return new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL, null, null, StringUtils.EMPTY, token.getPlainText());

Check warning on line 42 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/credentials/BitbucketAccessTokenAuthenticator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 41-42 are not covered by tests
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.cloudbees.jenkins.plugins.bitbucket.api.credentials;

import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketHref;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import java.net.URI;
import java.net.URISyntaxException;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpRequest;
import org.scribe.model.OAuthConfig;
import org.scribe.model.OAuthConstants;
Expand Down Expand Up @@ -38,27 +39,8 @@ public void configureRequest(HttpRequest request) {
}

@Override
public BitbucketHref addAuthToken(BitbucketHref bitbucketHref) {
String link = bitbucketHref.getHref();
if (!link.startsWith("http")) {
return bitbucketHref;
}
try {
URI uri = new URI(link);
String userInfo = "x-token-auth:{" + token.getToken() + "}";
String newLink = new URI(
uri.getScheme(),
userInfo,
uri.getHost(),
uri.getPort(),
uri.getPath(),
uri.getQuery(),
uri.getFragment()
).toString();
return new BitbucketHref(bitbucketHref.getName(), newLink);
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
public StandardUsernameCredentials getCredentialsForScm() {
return new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL, null, null, StringUtils.EMPTY, token.getToken());

Check warning on line 44 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/credentials/BitbucketOAuthAuthenticator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 43-44 are not covered by tests
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
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 java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.http.HttpHost;
Expand Down Expand Up @@ -74,4 +77,10 @@ public void configureContext(HttpClientContext context, HttpHost host) {
context.setCredentialsProvider(credentialsProvider);
context.setAuthCache(authCache);
}

@Override
public StandardUsernameCredentials getCredentialsForScm() {
return new UsernamePasswordCredentialsImpl(
CredentialsScope.GLOBAL, null, null, httpCredentials.getUserName(), httpCredentials.getPassword());

Check warning on line 84 in src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/credentials/BitbucketUsernamePasswordAuthenticator.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 83-84 are not covered by tests
}
}

0 comments on commit ad359b3

Please sign in to comment.