Skip to content

Commit a504676

Browse files
author
Thomas Salzinger
committed
Track credential usage within BitbucketCredentials#lookupCredentials
The previous implementation actually missed out on a couple of usages, for example within `BitbucketSCMNavigator`.
1 parent d4babb5 commit a504676

File tree

2 files changed

+39
-48
lines changed

2 files changed

+39
-48
lines changed

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

+39-29
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package com.cloudbees.jenkins.plugins.bitbucket;
2525

2626
import com.cloudbees.jenkins.plugins.bitbucket.api.BitbucketAuthenticator;
27+
import com.cloudbees.plugins.credentials.Credentials;
2728
import com.cloudbees.plugins.credentials.CredentialsMatchers;
2829
import com.cloudbees.plugins.credentials.CredentialsProvider;
2930
import com.cloudbees.plugins.credentials.common.StandardCertificateCredentials;
@@ -32,6 +33,7 @@
3233
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
3334
import edu.umd.cs.findbugs.annotations.CheckForNull;
3435
import edu.umd.cs.findbugs.annotations.NonNull;
36+
import hudson.model.Item;
3537
import hudson.model.Queue;
3638
import hudson.model.queue.Tasks;
3739
import hudson.security.ACL;
@@ -53,26 +55,34 @@ private BitbucketCredentials() {
5355
throw new IllegalAccessError("Utility class");
5456
}
5557

58+
/**
59+
* Performs a lookup of credentials for the given context. Additionally, usage of the credentials is tracked for the
60+
* given {@link SCMSourceOwner} via {@link CredentialsProvider#track(Item, Credentials)}
61+
*/
5662
@CheckForNull
5763
static <T extends StandardCredentials> T lookupCredentials(@CheckForNull String serverUrl,
5864
@CheckForNull SCMSourceOwner context,
5965
@CheckForNull String id,
6066
@NonNull Class<T> type) {
6167
if (StringUtils.isNotBlank(id) && context != null) {
62-
return CredentialsMatchers.firstOrNull(
63-
CredentialsProvider.lookupCredentials(
64-
type,
65-
context,
66-
context instanceof Queue.Task
67-
? Tasks.getDefaultAuthenticationOf((Queue.Task) context)
68-
: ACL.SYSTEM,
69-
URIRequirementBuilder.fromUri(serverUrl).build()
70-
),
71-
CredentialsMatchers.allOf(
72-
CredentialsMatchers.withId(id),
73-
CredentialsMatchers.anyOf(CredentialsMatchers.instanceOf(type))
74-
)
68+
final T credentials = CredentialsMatchers.firstOrNull(
69+
CredentialsProvider.lookupCredentials(
70+
type,
71+
context,
72+
context instanceof Queue.Task
73+
? Tasks.getDefaultAuthenticationOf((Queue.Task) context)
74+
: ACL.SYSTEM,
75+
URIRequirementBuilder.fromUri(serverUrl).build()
76+
),
77+
CredentialsMatchers.allOf(
78+
CredentialsMatchers.withId(id),
79+
CredentialsMatchers.anyOf(CredentialsMatchers.instanceOf(type))
80+
)
7581
);
82+
83+
CredentialsProvider.track(context, credentials);
84+
85+
return credentials;
7686
}
7787
return null;
7888
}
@@ -87,13 +97,13 @@ static ListBoxModel fillCredentialsIdItems(
8797
return result;
8898
}
8999
result.includeMatchingAs(
90-
context instanceof Queue.Task
91-
? Tasks.getDefaultAuthenticationOf((Queue.Task) context)
92-
: ACL.SYSTEM,
93-
context,
94-
StandardCredentials.class,
95-
URIRequirementBuilder.fromUri(serverUrl).build(),
96-
AuthenticationTokens.matcher(BitbucketAuthenticator.authenticationContext(serverUrl))
100+
context instanceof Queue.Task
101+
? Tasks.getDefaultAuthenticationOf((Queue.Task) context)
102+
: ACL.SYSTEM,
103+
context,
104+
StandardCredentials.class,
105+
URIRequirementBuilder.fromUri(serverUrl).build(),
106+
AuthenticationTokens.matcher(BitbucketAuthenticator.authenticationContext(serverUrl))
97107
);
98108
return result;
99109
}
@@ -106,15 +116,15 @@ static FormValidation checkCredentialsId(
106116
AccessControlled contextToCheck = context == null ? Jenkins.get() : context;
107117
contextToCheck.checkPermission(CredentialsProvider.VIEW);
108118
if (CredentialsMatchers.firstOrNull(
109-
CredentialsProvider.lookupCredentials(
110-
StandardCertificateCredentials.class,
111-
context,
112-
context instanceof Queue.Task ? Tasks.getDefaultAuthenticationOf((Queue.Task) context) : ACL.SYSTEM,
113-
URIRequirementBuilder.fromUri(serverUrl).build()),
114-
CredentialsMatchers.allOf(
115-
CredentialsMatchers.withId(value),
116-
AuthenticationTokens.matcher(BitbucketAuthenticator.authenticationContext(serverUrl))
117-
)
119+
CredentialsProvider.lookupCredentials(
120+
StandardCertificateCredentials.class,
121+
context,
122+
context instanceof Queue.Task ? Tasks.getDefaultAuthenticationOf((Queue.Task) context) : ACL.SYSTEM,
123+
URIRequirementBuilder.fromUri(serverUrl).build()),
124+
CredentialsMatchers.allOf(
125+
CredentialsMatchers.withId(value),
126+
AuthenticationTokens.matcher(BitbucketAuthenticator.authenticationContext(serverUrl))
127+
)
118128
) != null) {
119129
return FormValidation.warning("A certificate was selected. You will likely need to configure Checkout over SSH.");
120130
}

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

-19
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
import com.cloudbees.jenkins.plugins.bitbucket.server.client.BitbucketServerAPIClient;
4949
import com.cloudbees.jenkins.plugins.bitbucket.server.client.repository.BitbucketServerRepository;
5050
import com.cloudbees.plugins.credentials.CredentialsNameProvider;
51-
import com.cloudbees.plugins.credentials.CredentialsProvider;
5251
import com.cloudbees.plugins.credentials.common.StandardCredentials;
5352
import com.damnhandy.uri.template.UriTemplate;
5453
import com.fasterxml.jackson.databind.util.StdDateFormat;
@@ -63,7 +62,6 @@
6362
import hudson.model.Action;
6463
import hudson.model.Actionable;
6564
import hudson.model.Item;
66-
import hudson.model.Run;
6765
import hudson.model.TaskListener;
6866
import hudson.plugins.git.GitSCM;
6967
import hudson.scm.SCM;
@@ -571,8 +569,6 @@ public void afterSave() {
571569
protected void retrieve(@CheckForNull SCMSourceCriteria criteria, @NonNull SCMHeadObserver observer,
572570
@CheckForNull SCMHeadEvent<?> event, @NonNull TaskListener listener)
573571
throws IOException, InterruptedException {
574-
575-
trackCredentialsUsage();
576572
try (BitbucketSCMSourceRequest request = new BitbucketSCMSourceContext(criteria, observer)
577573
.withTraits(traits)
578574
.newRequest(this, listener)) {
@@ -827,19 +823,9 @@ private void retrieveTags(final BitbucketSCMSourceRequest request) throws IOExce
827823
request.listener().getLogger().format("%n %d tags were processed%n", count);
828824
}
829825

830-
private void trackCredentialsUsage() {
831-
final SCMSourceOwner owner = getOwner();
832-
if (owner != null) {
833-
CredentialsProvider.track(owner, credentials());
834-
}
835-
}
836-
837826
@Override
838827
protected SCMRevision retrieve(SCMHead head, TaskListener listener) throws IOException, InterruptedException {
839828
final BitbucketApi bitbucket = buildBitbucketClient();
840-
841-
trackCredentialsUsage();
842-
843829
try {
844830
if (head instanceof PullRequestSCMHead) {
845831
PullRequestSCMHead h = (PullRequestSCMHead) head;
@@ -1239,11 +1225,6 @@ public static void setEventDelaySeconds(int eventDelaySeconds) {
12391225
BitbucketSCMSource.eventDelaySeconds = Math.min(300, Math.max(0, eventDelaySeconds));
12401226
}
12411227

1242-
public static BitbucketSCMSource findForRun(Run<?, ?> run) {
1243-
SCMSource s = SCMSource.SourceByItem.findSource(run.getParent());
1244-
return s instanceof BitbucketSCMSource ? (BitbucketSCMSource) s : null;
1245-
}
1246-
12471228
private void initCloneLinks() {
12481229
if (primaryCloneLinks == null) {
12491230
BitbucketApi bitbucket = buildBitbucketClient();

0 commit comments

Comments
 (0)