24
24
package com .cloudbees .jenkins .plugins .bitbucket ;
25
25
26
26
import com .cloudbees .jenkins .plugins .bitbucket .api .BitbucketAuthenticator ;
27
+ import com .cloudbees .plugins .credentials .Credentials ;
27
28
import com .cloudbees .plugins .credentials .CredentialsMatchers ;
28
29
import com .cloudbees .plugins .credentials .CredentialsProvider ;
29
30
import com .cloudbees .plugins .credentials .common .StandardCertificateCredentials ;
32
33
import com .cloudbees .plugins .credentials .domains .URIRequirementBuilder ;
33
34
import edu .umd .cs .findbugs .annotations .CheckForNull ;
34
35
import edu .umd .cs .findbugs .annotations .NonNull ;
36
+ import hudson .model .Item ;
35
37
import hudson .model .Queue ;
36
38
import hudson .model .queue .Tasks ;
37
39
import hudson .security .ACL ;
@@ -53,26 +55,34 @@ private BitbucketCredentials() {
53
55
throw new IllegalAccessError ("Utility class" );
54
56
}
55
57
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
+ */
56
62
@ CheckForNull
57
63
static <T extends StandardCredentials > T lookupCredentials (@ CheckForNull String serverUrl ,
58
64
@ CheckForNull SCMSourceOwner context ,
59
65
@ CheckForNull String id ,
60
66
@ NonNull Class <T > type ) {
61
67
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
+ )
75
81
);
82
+
83
+ CredentialsProvider .track (context , credentials );
84
+
85
+ return credentials ;
76
86
}
77
87
return null ;
78
88
}
@@ -87,13 +97,13 @@ static ListBoxModel fillCredentialsIdItems(
87
97
return result ;
88
98
}
89
99
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 ))
97
107
);
98
108
return result ;
99
109
}
@@ -106,15 +116,15 @@ static FormValidation checkCredentialsId(
106
116
AccessControlled contextToCheck = context == null ? Jenkins .get () : context ;
107
117
contextToCheck .checkPermission (CredentialsProvider .VIEW );
108
118
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
+ )
118
128
) != null ) {
119
129
return FormValidation .warning ("A certificate was selected. You will likely need to configure Checkout over SSH." );
120
130
}
0 commit comments