From 6a2ddb89fabf1e61fc1eb47aca9356274a1ead39 Mon Sep 17 00:00:00 2001 From: Manan Goel Date: Tue, 20 Feb 2024 21:14:25 +0000 Subject: [PATCH 1/2] Added function to validate gitlab credentials --- .../gitlabbranchsource/GitLabSCMSource.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java index 5dff095e..3dc28549 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java @@ -13,14 +13,18 @@ import com.cloudbees.plugins.credentials.CredentialsMatchers; import com.cloudbees.plugins.credentials.CredentialsProvider; +import com.cloudbees.plugins.credentials.common.StandardCredentials; import com.cloudbees.plugins.credentials.common.StandardListBoxModel; import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials; +import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import hudson.Extension; import hudson.ExtensionList; import hudson.Util; +import hudson.util.FormValidation; import hudson.console.HyperlinkNote; import hudson.model.Action; import hudson.model.Item; @@ -32,6 +36,7 @@ import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabAvatar; import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabLink; import io.jenkins.plugins.gitlabserverconfig.credentials.PersonalAccessToken; +import io.jenkins.plugins.gitlabserverconfig.credentials.helpers.GitLabCredentialMatcher; import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer; import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServers; import java.io.IOException; @@ -836,6 +841,34 @@ public ListBoxModel doFillServerNameItems( return GitLabServers.get().getServerItems(); } + public FormValidation doCheckCredentialId(@AncestorInPath SCMSourceOwner context, @QueryParameter String serverName, @QueryParameter String value) throws IOException, InterruptedException { + if (context == null) { + if (Jenkins.get().hasPermission(Jenkins.ADMINISTER)) { + return FormValidation.ok(); + } + } else { + if (!context.hasPermission(Item.EXTENDED_READ) && !context.hasPermission(CredentialsProvider.USE_ITEM)) { + return FormValidation.ok(); + } + } + GitLabServer server = GitLabServers.get().findServer(serverName); + if (server == null) { + return FormValidation.ok(); + } + if (StringUtils.isBlank(value)) { + return FormValidation.warning("Credentials are recommended"); + } + if (CredentialsProvider.listCredentialsInItem( + StandardCredentials.class, + context, + context instanceof Queue.Task ? ((Queue.Task) context).getDefaultAuthentication2() : ACL.SYSTEM2, + URIRequirementBuilder.fromUri(serverName).build(), + GitLabServer.CREDENTIALS_MATCHER).isEmpty()) { + return FormValidation.error(Messages.GitLabSCMNavigator_selectedCredentialsMissing()); + } + return FormValidation.ok(); + } + public ListBoxModel doFillCredentialsIdItems( @AncestorInPath SCMSourceOwner context, @QueryParameter String serverName, From 2cd1268a37e7c554b840fd966fdd19d6e2866722 Mon Sep 17 00:00:00 2001 From: Manan Goel Date: Wed, 21 Feb 2024 01:07:38 +0000 Subject: [PATCH 2/2] Added warning message when no credentials are provided for organization folders --- .../GitLabSCMNavigator.java | 35 +++++++++++ .../gitlabbranchsource/GitLabSCMSource.java | 60 ++++++++++--------- 2 files changed, 67 insertions(+), 28 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMNavigator.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMNavigator.java index 398af196..59fd2906 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMNavigator.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMNavigator.java @@ -15,6 +15,7 @@ import com.cloudbees.plugins.credentials.common.StandardCredentials; import com.cloudbees.plugins.credentials.common.StandardListBoxModel; import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials; +import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder; import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.Extension; @@ -526,6 +527,40 @@ public ListBoxModel doFillServerNameItems( return GitLabServers.get().getServerItems(); } + public FormValidation doCheckCredentialsId( + @AncestorInPath SCMSourceOwner context, @QueryParameter String serverName, @QueryParameter String value) + throws IOException, InterruptedException { + if (context == null) { + if (Jenkins.get().hasPermission(Jenkins.ADMINISTER)) { + return FormValidation.ok(); + } + } else { + if (!context.hasPermission(Item.EXTENDED_READ) + && !context.hasPermission(CredentialsProvider.USE_ITEM)) { + return FormValidation.ok(); + } + } + GitLabServer server = GitLabServers.get().findServer(serverName); + if (server == null) { + return FormValidation.ok(); + } + if (StringUtils.isBlank(value)) { + return FormValidation.warning("Credentials are recommended"); + } + if (CredentialsProvider.listCredentialsInItem( + StandardCredentials.class, + context, + context instanceof Queue.Task + ? ((Queue.Task) context).getDefaultAuthentication2() + : ACL.SYSTEM2, + URIRequirementBuilder.fromUri(serverName).build(), + GitLabServer.CREDENTIALS_MATCHER) + .isEmpty()) { + return FormValidation.error(Messages.GitLabSCMNavigator_selectedCredentialsMissing()); + } + return FormValidation.ok(); + } + public ListBoxModel doFillCredentialsIdItems( @AncestorInPath SCMSourceOwner context, @QueryParameter String serverName, diff --git a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java index 3dc28549..7c28b445 100644 --- a/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java +++ b/src/main/java/io/jenkins/plugins/gitlabbranchsource/GitLabSCMSource.java @@ -17,14 +17,12 @@ import com.cloudbees.plugins.credentials.common.StandardListBoxModel; import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials; import com.cloudbees.plugins.credentials.domains.URIRequirementBuilder; - import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import hudson.Extension; import hudson.ExtensionList; import hudson.Util; -import hudson.util.FormValidation; import hudson.console.HyperlinkNote; import hudson.model.Action; import hudson.model.Item; @@ -32,11 +30,11 @@ import hudson.model.TaskListener; import hudson.scm.SCM; import hudson.security.ACL; +import hudson.util.FormValidation; import hudson.util.ListBoxModel; import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabAvatar; import io.jenkins.plugins.gitlabbranchsource.helpers.GitLabLink; import io.jenkins.plugins.gitlabserverconfig.credentials.PersonalAccessToken; -import io.jenkins.plugins.gitlabserverconfig.credentials.helpers.GitLabCredentialMatcher; import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServer; import io.jenkins.plugins.gitlabserverconfig.servers.GitLabServers; import java.io.IOException; @@ -841,32 +839,38 @@ public ListBoxModel doFillServerNameItems( return GitLabServers.get().getServerItems(); } - public FormValidation doCheckCredentialId(@AncestorInPath SCMSourceOwner context, @QueryParameter String serverName, @QueryParameter String value) throws IOException, InterruptedException { - if (context == null) { - if (Jenkins.get().hasPermission(Jenkins.ADMINISTER)) { - return FormValidation.ok(); + public FormValidation doCheckCredentialsId( + @AncestorInPath SCMSourceOwner context, @QueryParameter String serverName, @QueryParameter String value) + throws IOException, InterruptedException { + if (context == null) { + if (Jenkins.get().hasPermission(Jenkins.ADMINISTER)) { + return FormValidation.ok(); + } + } else { + if (!context.hasPermission(Item.EXTENDED_READ) + && !context.hasPermission(CredentialsProvider.USE_ITEM)) { + return FormValidation.ok(); } - } else { - if (!context.hasPermission(Item.EXTENDED_READ) && !context.hasPermission(CredentialsProvider.USE_ITEM)) { - return FormValidation.ok(); - } - } - GitLabServer server = GitLabServers.get().findServer(serverName); - if (server == null) { - return FormValidation.ok(); - } - if (StringUtils.isBlank(value)) { - return FormValidation.warning("Credentials are recommended"); - } - if (CredentialsProvider.listCredentialsInItem( - StandardCredentials.class, - context, - context instanceof Queue.Task ? ((Queue.Task) context).getDefaultAuthentication2() : ACL.SYSTEM2, - URIRequirementBuilder.fromUri(serverName).build(), - GitLabServer.CREDENTIALS_MATCHER).isEmpty()) { - return FormValidation.error(Messages.GitLabSCMNavigator_selectedCredentialsMissing()); - } - return FormValidation.ok(); + } + GitLabServer server = GitLabServers.get().findServer(serverName); + if (server == null) { + return FormValidation.ok(); + } + if (StringUtils.isBlank(value)) { + return FormValidation.warning("Credentials are recommended"); + } + if (CredentialsProvider.listCredentialsInItem( + StandardCredentials.class, + context, + context instanceof Queue.Task + ? ((Queue.Task) context).getDefaultAuthentication2() + : ACL.SYSTEM2, + URIRequirementBuilder.fromUri(serverName).build(), + GitLabServer.CREDENTIALS_MATCHER) + .isEmpty()) { + return FormValidation.error(Messages.GitLabSCMNavigator_selectedCredentialsMissing()); + } + return FormValidation.ok(); } public ListBoxModel doFillCredentialsIdItems(