Skip to content

Commit 04f287f

Browse files
authored
Use CredentialsProvider.findCredentialByIdInItem (#747)
1 parent 9781b1c commit 04f287f

4 files changed

Lines changed: 75 additions & 88 deletions

File tree

pom.xml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
<dependency>
4545
<groupId>io.jenkins.tools.bom</groupId>
4646
<artifactId>bom-${jenkins.baseline}.x</artifactId>
47-
<version>5750.vec44cb_c78352</version>
47+
<version>5804.v80587a_38d937</version>
4848
<type>pom</type>
4949
<scope>import</scope>
5050
</dependency>
@@ -54,6 +54,11 @@
5454
<artifactId>gitlab-api</artifactId>
5555
<version>6.2.0-111.vf174d6b_8c12e</version>
5656
</dependency>
57+
<dependency>
58+
<groupId>org.jenkins-ci.plugins</groupId>
59+
<artifactId>credentials</artifactId>
60+
<version>1480.v2246fd131e83</version>
61+
</dependency>
5762
</dependencies>
5863
</dependencyManagement>
5964

src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMBuilder.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import static org.apache.commons.lang3.StringUtils.defaultIfBlank;
77

88
import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey;
9-
import com.cloudbees.plugins.credentials.CredentialsMatchers;
109
import com.cloudbees.plugins.credentials.CredentialsProvider;
1110
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
1211
import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder;
@@ -119,17 +118,12 @@ public static UriTemplate checkoutUriTemplate(
119118
if (serverUri.getHost() != null) {
120119
builder.withHostname(serverUri.getHost());
121120
}
122-
StandardUsernameCredentials credentials = CredentialsMatchers.firstOrNull(
123-
CredentialsProvider.lookupCredentials(
124-
StandardUsernameCredentials.class,
125-
context,
126-
context instanceof Queue.Task
127-
? ((Queue.Task) context).getDefaultAuthentication()
128-
: ACL.SYSTEM,
129-
builder.build()),
130-
CredentialsMatchers.allOf(
131-
CredentialsMatchers.withId(credentialsId),
132-
CredentialsMatchers.instanceOf(StandardUsernameCredentials.class)));
121+
StandardUsernameCredentials credentials = CredentialsProvider.findCredentialByIdInItem(
122+
credentialsId,
123+
StandardUsernameCredentials.class,
124+
context,
125+
context instanceof Queue.Task t ? t.getDefaultAuthentication2() : ACL.SYSTEM2,
126+
builder.build());
133127
if (credentials instanceof SSHUserPrivateKey) {
134128
return UriTemplate.buildFromTemplate(sshRemote).build();
135129
}

src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabHelper.java

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package io.jenkins.plugins.gitlabbranchsource.helpers;
22

3-
import static com.cloudbees.plugins.credentials.CredentialsMatchers.withId;
4-
import static com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials;
53
import static com.cloudbees.plugins.credentials.domains.URIRequirementBuilder.fromUri;
64

7-
import com.cloudbees.plugins.credentials.CredentialsMatchers;
5+
import com.cloudbees.plugins.credentials.CredentialsProvider;
86
import com.cloudbees.plugins.credentials.common.StandardCredentials;
97
import com.damnhandy.uri.template.UriTemplate;
108
import com.damnhandy.uri.template.UriTemplateBuilder;
119
import com.damnhandy.uri.template.impl.Operator;
1210
import hudson.ProxyConfiguration;
1311
import hudson.model.Item;
1412
import hudson.model.ItemGroup;
15-
import hudson.security.ACL;
1613
import hudson.security.AccessControlled;
1714
import io.jenkins.plugins.gitlabserverconfig.credentials.GroupAccessToken;
1815
import io.jenkins.plugins.gitlabserverconfig.credentials.PersonalAccessToken;
@@ -168,26 +165,28 @@ public static String[] splitPath(String path) {
168165

169166
public static StandardCredentials getCredential(String credentialsId, String serverName, AccessControlled context) {
170167
if (StringUtils.isNotBlank(credentialsId)) {
171-
if (context instanceof ItemGroup) {
172-
return CredentialsMatchers.firstOrNull(
173-
lookupCredentials(
174-
StandardCredentials.class,
175-
(ItemGroup) context,
176-
ACL.SYSTEM,
177-
fromUri(StringUtils.defaultIfBlank(
178-
getServerUrlFromName(serverName), GitLabServer.GITLAB_SERVER_URL))
179-
.build()),
180-
CredentialsMatchers.allOf(withId(credentialsId), GitLabServer.CREDENTIALS_MATCHER));
181-
} else {
182-
return CredentialsMatchers.firstOrNull(
183-
lookupCredentials(
184-
StandardCredentials.class,
185-
(Item) context,
186-
ACL.SYSTEM,
187-
fromUri(StringUtils.defaultIfBlank(
188-
getServerUrlFromName(serverName), GitLabServer.GITLAB_SERVER_URL))
189-
.build()),
190-
CredentialsMatchers.allOf(withId(credentialsId), GitLabServer.CREDENTIALS_MATCHER));
168+
StandardCredentials c = null;
169+
if (context instanceof ItemGroup<?> g) {
170+
c = CredentialsProvider.findCredentialByIdInItemGroup(
171+
credentialsId,
172+
StandardCredentials.class,
173+
g,
174+
null,
175+
fromUri(StringUtils.defaultIfBlank(
176+
getServerUrlFromName(serverName), GitLabServer.GITLAB_SERVER_URL))
177+
.build());
178+
} else if (context instanceof Item i) {
179+
c = CredentialsProvider.findCredentialByIdInItem(
180+
credentialsId,
181+
StandardCredentials.class,
182+
i,
183+
null,
184+
fromUri(StringUtils.defaultIfBlank(
185+
getServerUrlFromName(serverName), GitLabServer.GITLAB_SERVER_URL))
186+
.build());
187+
}
188+
if (c != null && GitLabServer.CREDENTIALS_MATCHER.matches(c)) {
189+
return c;
191190
}
192191
}
193192

src/main/java/io/jenkins/plugins/gitlabserverconfig/servers/GitLabServer.java

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package io.jenkins.plugins.gitlabserverconfig.servers;
22

3-
import static com.cloudbees.plugins.credentials.CredentialsMatchers.withId;
4-
import static com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials;
53
import static com.cloudbees.plugins.credentials.domains.URIRequirementBuilder.fromUri;
64
import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.getPrivateTokenAsPlainText;
75
import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.getProxyConfig;
@@ -14,7 +12,6 @@
1412
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
1513
import com.cloudbees.plugins.credentials.common.StandardCredentials;
1614
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
17-
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
1815
import edu.umd.cs.findbugs.annotations.CheckForNull;
1916
import edu.umd.cs.findbugs.annotations.NonNull;
2017
import hudson.Extension;
@@ -32,7 +29,6 @@
3229
import java.net.MalformedURLException;
3330
import java.net.URL;
3431
import java.security.SecureRandom;
35-
import java.util.ArrayList;
3632
import java.util.Collections;
3733
import java.util.List;
3834
import java.util.logging.Level;
@@ -281,14 +277,12 @@ public StandardCredentials getCredentials(AccessControlled context) {
281277
}
282278
return StringUtils.isBlank(credentialsId)
283279
? null
284-
: CredentialsMatchers.firstOrNull(
285-
lookupCredentials(
286-
StandardCredentials.class,
287-
jenkins,
288-
ACL.SYSTEM,
289-
fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL))
290-
.build()),
291-
withId(credentialsId));
280+
: CredentialsProvider.findCredentialByIdInItemGroup(
281+
credentialsId,
282+
StandardCredentials.class,
283+
null,
284+
null,
285+
fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)).build());
292286
}
293287

294288
/**
@@ -337,38 +331,37 @@ public StringCredentials getWebhookSecretCredentials(AccessControlled context) {
337331
jenkins.checkPermission(CredentialsProvider.USE_OWN);
338332
return StringUtils.isBlank(webhookSecretCredentialsId)
339333
? null
340-
: CredentialsMatchers.firstOrNull(
341-
lookupCredentials(
342-
StringCredentials.class,
343-
jenkins,
344-
ACL.SYSTEM,
345-
fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL))
346-
.build()),
347-
withId(webhookSecretCredentialsId));
334+
: CredentialsProvider.findCredentialByIdInItemGroup(
335+
webhookSecretCredentialsId,
336+
StringCredentials.class,
337+
null,
338+
null,
339+
fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL))
340+
.build());
348341
} else {
349342
context.checkPermission(CredentialsProvider.USE_OWN);
350-
if (context instanceof ItemGroup) {
343+
if (context instanceof ItemGroup<?> g) {
351344
return StringUtils.isBlank(webhookSecretCredentialsId)
352345
? null
353-
: CredentialsMatchers.firstOrNull(
354-
lookupCredentials(
355-
StringCredentials.class,
356-
(ItemGroup) context,
357-
ACL.SYSTEM,
358-
fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL))
359-
.build()),
360-
withId(webhookSecretCredentialsId));
361-
} else {
346+
: CredentialsProvider.findCredentialByIdInItemGroup(
347+
webhookSecretCredentialsId,
348+
StringCredentials.class,
349+
g,
350+
null,
351+
fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL))
352+
.build());
353+
} else if (context instanceof Item i) {
362354
return StringUtils.isBlank(webhookSecretCredentialsId)
363355
? null
364-
: CredentialsMatchers.firstOrNull(
365-
lookupCredentials(
366-
StringCredentials.class,
367-
(Item) context,
368-
ACL.SYSTEM,
369-
fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL))
370-
.build()),
371-
withId(webhookSecretCredentialsId));
356+
: CredentialsProvider.findCredentialByIdInItem(
357+
webhookSecretCredentialsId,
358+
StringCredentials.class,
359+
i,
360+
null,
361+
fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL))
362+
.build());
363+
} else {
364+
return null;
372365
}
373366
}
374367
}
@@ -387,13 +380,10 @@ public Secret getSecretToken() {
387380
}
388381

389382
private StringCredentials getWebhookSecretCredentials(String webhookSecretCredentialsId) {
390-
Jenkins jenkins = Jenkins.get();
391383
return StringUtils.isBlank(webhookSecretCredentialsId)
392384
? null
393-
: CredentialsMatchers.firstOrNull(
394-
lookupCredentials(
395-
StringCredentials.class, jenkins, ACL.SYSTEM, new ArrayList<DomainRequirement>()),
396-
withId(webhookSecretCredentialsId));
385+
: CredentialsProvider.findCredentialByIdInItemGroup(
386+
webhookSecretCredentialsId, StringCredentials.class, null, null, null);
397387
}
398388

399389
public String getSecretTokenAsPlainText() {
@@ -668,14 +658,13 @@ private StandardCredentials getCredentials(String serverUrl, String credentialsI
668658
jenkins.checkPermission(Jenkins.MANAGE);
669659
return StringUtils.isBlank(credentialsId)
670660
? null
671-
: CredentialsMatchers.firstOrNull(
672-
lookupCredentials(
673-
StandardCredentials.class,
674-
jenkins,
675-
ACL.SYSTEM,
676-
fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL))
677-
.build()),
678-
withId(credentialsId));
661+
: CredentialsProvider.findCredentialByIdInItemGroup(
662+
credentialsId,
663+
StandardCredentials.class,
664+
null,
665+
null,
666+
fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL))
667+
.build());
679668
}
680669
}
681670
}

0 commit comments

Comments
 (0)