diff --git a/pom.xml b/pom.xml index 032b092b..fcff7a53 100644 --- a/pom.xml +++ b/pom.xml @@ -44,7 +44,7 @@ io.jenkins.tools.bom bom-${jenkins.baseline}.x - 5750.vec44cb_c78352 + 5804.v80587a_38d937 pom import @@ -54,6 +54,11 @@ gitlab-api 6.2.0-111.vf174d6b_8c12e + + org.jenkins-ci.plugins + credentials + 1480.v2246fd131e83 + diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMBuilder.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMBuilder.java index 7067c2b2..7d1c259b 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMBuilder.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMBuilder.java @@ -6,7 +6,6 @@ import static org.apache.commons.lang3.StringUtils.defaultIfBlank; import com.cloudbees.jenkins.plugins.sshcredentials.SSHUserPrivateKey; -import com.cloudbees.plugins.credentials.CredentialsMatchers; import com.cloudbees.plugins.credentials.CredentialsProvider; import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials; import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder; @@ -119,17 +118,12 @@ public static UriTemplate checkoutUriTemplate( if (serverUri.getHost() != null) { builder.withHostname(serverUri.getHost()); } - StandardUsernameCredentials credentials = CredentialsMatchers.firstOrNull( - CredentialsProvider.lookupCredentials( - StandardUsernameCredentials.class, - context, - context instanceof Queue.Task - ? ((Queue.Task) context).getDefaultAuthentication() - : ACL.SYSTEM, - builder.build()), - CredentialsMatchers.allOf( - CredentialsMatchers.withId(credentialsId), - CredentialsMatchers.instanceOf(StandardUsernameCredentials.class))); + StandardUsernameCredentials credentials = CredentialsProvider.findCredentialByIdInItem( + credentialsId, + StandardUsernameCredentials.class, + context, + context instanceof Queue.Task t ? t.getDefaultAuthentication2() : ACL.SYSTEM2, + builder.build()); if (credentials instanceof SSHUserPrivateKey) { return UriTemplate.buildFromTemplate(sshRemote).build(); } diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabHelper.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabHelper.java index 984106d8..34a8d310 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabHelper.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/helpers/GitLabHelper.java @@ -1,10 +1,8 @@ package io.jenkins.plugins.gitlabbranchsource.helpers; -import static com.cloudbees.plugins.credentials.CredentialsMatchers.withId; -import static com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials; import static com.cloudbees.plugins.credentials.domains.URIRequirementBuilder.fromUri; -import com.cloudbees.plugins.credentials.CredentialsMatchers; +import com.cloudbees.plugins.credentials.CredentialsProvider; import com.cloudbees.plugins.credentials.common.StandardCredentials; import com.damnhandy.uri.template.UriTemplate; import com.damnhandy.uri.template.UriTemplateBuilder; @@ -12,7 +10,6 @@ import hudson.ProxyConfiguration; import hudson.model.Item; import hudson.model.ItemGroup; -import hudson.security.ACL; import hudson.security.AccessControlled; import io.jenkins.plugins.gitlabserverconfig.credentials.GroupAccessToken; import io.jenkins.plugins.gitlabserverconfig.credentials.PersonalAccessToken; @@ -168,26 +165,28 @@ public static String[] splitPath(String path) { public static StandardCredentials getCredential(String credentialsId, String serverName, AccessControlled context) { if (StringUtils.isNotBlank(credentialsId)) { - if (context instanceof ItemGroup) { - return CredentialsMatchers.firstOrNull( - lookupCredentials( - StandardCredentials.class, - (ItemGroup) context, - ACL.SYSTEM, - fromUri(StringUtils.defaultIfBlank( - getServerUrlFromName(serverName), GitLabServer.GITLAB_SERVER_URL)) - .build()), - CredentialsMatchers.allOf(withId(credentialsId), GitLabServer.CREDENTIALS_MATCHER)); - } else { - return CredentialsMatchers.firstOrNull( - lookupCredentials( - StandardCredentials.class, - (Item) context, - ACL.SYSTEM, - fromUri(StringUtils.defaultIfBlank( - getServerUrlFromName(serverName), GitLabServer.GITLAB_SERVER_URL)) - .build()), - CredentialsMatchers.allOf(withId(credentialsId), GitLabServer.CREDENTIALS_MATCHER)); + StandardCredentials c = null; + if (context instanceof ItemGroup g) { + c = CredentialsProvider.findCredentialByIdInItemGroup( + credentialsId, + StandardCredentials.class, + g, + null, + fromUri(StringUtils.defaultIfBlank( + getServerUrlFromName(serverName), GitLabServer.GITLAB_SERVER_URL)) + .build()); + } else if (context instanceof Item i) { + c = CredentialsProvider.findCredentialByIdInItem( + credentialsId, + StandardCredentials.class, + i, + null, + fromUri(StringUtils.defaultIfBlank( + getServerUrlFromName(serverName), GitLabServer.GITLAB_SERVER_URL)) + .build()); + } + if (c != null && GitLabServer.CREDENTIALS_MATCHER.matches(c)) { + return c; } } diff --git a/src/main/java/io/jenkins/plugins/gitlabserverconfig/servers/GitLabServer.java b/src/main/java/io/jenkins/plugins/gitlabserverconfig/servers/GitLabServer.java index 32b3b2ed..cd1780f0 100644 --- a/src/main/java/io/jenkins/plugins/gitlabserverconfig/servers/GitLabServer.java +++ b/src/main/java/io/jenkins/plugins/gitlabserverconfig/servers/GitLabServer.java @@ -1,7 +1,5 @@ package io.jenkins.plugins.gitlabserverconfig.servers; -import static com.cloudbees.plugins.credentials.CredentialsMatchers.withId; -import static com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials; import static com.cloudbees.plugins.credentials.domains.URIRequirementBuilder.fromUri; import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.getPrivateTokenAsPlainText; import static io.jenkins.plugins.gitlabbranchsource.helpers.GitLabHelper.getProxyConfig; @@ -14,7 +12,6 @@ import com.cloudbees.plugins.credentials.SystemCredentialsProvider; import com.cloudbees.plugins.credentials.common.StandardCredentials; import com.cloudbees.plugins.credentials.common.StandardListBoxModel; -import com.cloudbees.plugins.credentials.domains.DomainRequirement; import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.Extension; @@ -32,7 +29,6 @@ import java.net.MalformedURLException; import java.net.URL; import java.security.SecureRandom; -import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.logging.Level; @@ -281,14 +277,12 @@ public StandardCredentials getCredentials(AccessControlled context) { } return StringUtils.isBlank(credentialsId) ? null - : CredentialsMatchers.firstOrNull( - lookupCredentials( - StandardCredentials.class, - jenkins, - ACL.SYSTEM, - fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)) - .build()), - withId(credentialsId)); + : CredentialsProvider.findCredentialByIdInItemGroup( + credentialsId, + StandardCredentials.class, + null, + null, + fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)).build()); } /** @@ -337,38 +331,37 @@ public StringCredentials getWebhookSecretCredentials(AccessControlled context) { jenkins.checkPermission(CredentialsProvider.USE_OWN); return StringUtils.isBlank(webhookSecretCredentialsId) ? null - : CredentialsMatchers.firstOrNull( - lookupCredentials( - StringCredentials.class, - jenkins, - ACL.SYSTEM, - fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)) - .build()), - withId(webhookSecretCredentialsId)); + : CredentialsProvider.findCredentialByIdInItemGroup( + webhookSecretCredentialsId, + StringCredentials.class, + null, + null, + fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)) + .build()); } else { context.checkPermission(CredentialsProvider.USE_OWN); - if (context instanceof ItemGroup) { + if (context instanceof ItemGroup g) { return StringUtils.isBlank(webhookSecretCredentialsId) ? null - : CredentialsMatchers.firstOrNull( - lookupCredentials( - StringCredentials.class, - (ItemGroup) context, - ACL.SYSTEM, - fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)) - .build()), - withId(webhookSecretCredentialsId)); - } else { + : CredentialsProvider.findCredentialByIdInItemGroup( + webhookSecretCredentialsId, + StringCredentials.class, + g, + null, + fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)) + .build()); + } else if (context instanceof Item i) { return StringUtils.isBlank(webhookSecretCredentialsId) ? null - : CredentialsMatchers.firstOrNull( - lookupCredentials( - StringCredentials.class, - (Item) context, - ACL.SYSTEM, - fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)) - .build()), - withId(webhookSecretCredentialsId)); + : CredentialsProvider.findCredentialByIdInItem( + webhookSecretCredentialsId, + StringCredentials.class, + i, + null, + fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)) + .build()); + } else { + return null; } } } @@ -387,13 +380,10 @@ public Secret getSecretToken() { } private StringCredentials getWebhookSecretCredentials(String webhookSecretCredentialsId) { - Jenkins jenkins = Jenkins.get(); return StringUtils.isBlank(webhookSecretCredentialsId) ? null - : CredentialsMatchers.firstOrNull( - lookupCredentials( - StringCredentials.class, jenkins, ACL.SYSTEM, new ArrayList()), - withId(webhookSecretCredentialsId)); + : CredentialsProvider.findCredentialByIdInItemGroup( + webhookSecretCredentialsId, StringCredentials.class, null, null, null); } public String getSecretTokenAsPlainText() { @@ -668,14 +658,13 @@ private StandardCredentials getCredentials(String serverUrl, String credentialsI jenkins.checkPermission(Jenkins.MANAGE); return StringUtils.isBlank(credentialsId) ? null - : CredentialsMatchers.firstOrNull( - lookupCredentials( - StandardCredentials.class, - jenkins, - ACL.SYSTEM, - fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)) - .build()), - withId(credentialsId)); + : CredentialsProvider.findCredentialByIdInItemGroup( + credentialsId, + StandardCredentials.class, + null, + null, + fromUri(defaultIfBlank(serverUrl, GITLAB_SERVER_URL)) + .build()); } } }