Skip to content

Commit ad359b3

Browse files
[SECURITY-3363]
1 parent 44cf5e4 commit ad359b3

File tree

6 files changed

+65
-26
lines changed

6 files changed

+65
-26
lines changed

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/BitbucketSCMSource.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,9 @@ public SCM build(SCMHead head, SCMRevision revision) {
10291029
switch (type) {
10301030
case GIT:
10311031
default:
1032-
return new BitbucketGitSCMBuilder(this, head, revision, getCredentialsId())
1032+
BitbucketAuthenticator authenticator = authenticator();
1033+
return new BitbucketGitSCMBuilder(this, head, revision, null)
1034+
.withExtension(authenticator == null ? null : new GitClientAuthenticatorExtension(authenticator.getCredentialsForScm()))
10331035
.withCloneLinks(primaryCloneLinks, mirrorCloneLinks)
10341036
.withTraits(traits)
10351037
.build();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.cloudbees.jenkins.plugins.bitbucket;
2+
3+
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
4+
import hudson.plugins.git.GitException;
5+
import hudson.plugins.git.GitSCM;
6+
import hudson.plugins.git.extensions.GitSCMExtension;
7+
import org.jenkinsci.plugins.gitclient.GitClient;
8+
9+
public class GitClientAuthenticatorExtension extends GitSCMExtension {
10+
11+
private final StandardUsernameCredentials credentials;
12+
13+
public GitClientAuthenticatorExtension(StandardUsernameCredentials credentials) {
14+
this.credentials = credentials;
15+
}
16+
17+
@Override
18+
public GitClient decorate(GitSCM scm, GitClient git) throws GitException {
19+
if (credentials != null) {
20+
git.setCredentials(credentials);
21+
}
22+
23+
return git;
24+
}
25+
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/BitbucketAuthenticator.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import com.cloudbees.jenkins.plugins.bitbucket.endpoints.BitbucketCloudEndpoint;
2828
import com.cloudbees.plugins.credentials.common.StandardCredentials;
29+
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
2930
import jenkins.authentication.tokens.api.AuthenticationTokenContext;
3031
import org.apache.http.HttpHost;
3132
import org.apache.http.HttpRequest;
@@ -107,6 +108,16 @@ public void configureRequest(HttpRequest request) {
107108
// override to configure HttpRequest
108109
}
109110

111+
112+
/**
113+
* Provides credentials that can be used for authenticated interactions with SCM.
114+
*
115+
* @return credentials to be passed to {@link org.jenkinsci.plugins.gitclient.GitClient#setCredentials(StandardUsernameCredentials)}
116+
*/
117+
public StandardUsernameCredentials getCredentialsForScm() {
118+
return null;
119+
}
120+
110121
/**
111122
* Add authentication token to clone link if
112123
* authentication method requires it

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/credentials/BitbucketAccessTokenAuthenticator.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package com.cloudbees.jenkins.plugins.bitbucket.api.credentials;
22

33
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
4+
import com.cloudbees.plugins.credentials.CredentialsScope;
5+
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
6+
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
47
import hudson.util.Secret;
8+
import org.apache.commons.lang.StringUtils;
59
import org.apache.http.HttpHeaders;
610
import org.apache.http.HttpRequest;
711
import org.jenkinsci.plugins.plaincredentials.StringCredentials;
@@ -31,4 +35,10 @@ public BitbucketAccessTokenAuthenticator(StringCredentials credentials) {
3135
public void configureRequest(HttpRequest request) {
3236
request.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token.getPlainText());
3337
}
38+
39+
@Override
40+
public StandardUsernameCredentials getCredentialsForScm() {
41+
return new UsernamePasswordCredentialsImpl(
42+
CredentialsScope.GLOBAL, null, null, StringUtils.EMPTY, token.getPlainText());
43+
}
3444
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/credentials/BitbucketOAuthAuthenticator.java

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.cloudbees.jenkins.plugins.bitbucket.api.credentials;
22

33
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
4-
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketHref;
4+
import com.cloudbees.plugins.credentials.CredentialsScope;
5+
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
56
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
6-
import java.net.URI;
7-
import java.net.URISyntaxException;
7+
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
8+
import org.apache.commons.lang.StringUtils;
89
import org.apache.http.HttpRequest;
910
import org.scribe.model.OAuthConfig;
1011
import org.scribe.model.OAuthConstants;
@@ -38,27 +39,8 @@ public void configureRequest(HttpRequest request) {
3839
}
3940

4041
@Override
41-
public BitbucketHref addAuthToken(BitbucketHref bitbucketHref) {
42-
String link = bitbucketHref.getHref();
43-
if (!link.startsWith("http")) {
44-
return bitbucketHref;
45-
}
46-
try {
47-
URI uri = new URI(link);
48-
String userInfo = "x-token-auth:{" + token.getToken() + "}";
49-
String newLink = new URI(
50-
uri.getScheme(),
51-
userInfo,
52-
uri.getHost(),
53-
uri.getPort(),
54-
uri.getPath(),
55-
uri.getQuery(),
56-
uri.getFragment()
57-
).toString();
58-
return new BitbucketHref(bitbucketHref.getName(), newLink);
59-
} catch (URISyntaxException e) {
60-
throw new RuntimeException(e);
61-
}
42+
public StandardUsernameCredentials getCredentialsForScm() {
43+
return new UsernamePasswordCredentialsImpl(
44+
CredentialsScope.GLOBAL, null, null, StringUtils.EMPTY, token.getToken());
6245
}
63-
6446
}

src/main/java/com/cloudbees/jenkins/plugins/bitbucket/api/credentials/BitbucketUsernamePasswordAuthenticator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
package com.cloudbees.jenkins.plugins.bitbucket.api.credentials;
2727

2828
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
29+
import com.cloudbees.plugins.credentials.CredentialsScope;
30+
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
2931
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
32+
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
3033
import java.util.logging.Level;
3134
import java.util.logging.Logger;
3235
import org.apache.http.HttpHost;
@@ -74,4 +77,10 @@ public void configureContext(HttpClientContext context, HttpHost host) {
7477
context.setCredentialsProvider(credentialsProvider);
7578
context.setAuthCache(authCache);
7679
}
80+
81+
@Override
82+
public StandardUsernameCredentials getCredentialsForScm() {
83+
return new UsernamePasswordCredentialsImpl(
84+
CredentialsScope.GLOBAL, null, null, httpCredentials.getUserName(), httpCredentials.getPassword());
85+
}
7786
}

0 commit comments

Comments
 (0)