From 917cfeeae19c0b2ccefa1ae759a52dc31feaabca Mon Sep 17 00:00:00 2001 From: Luis Toledo Date: Fri, 21 Oct 2022 16:22:21 -0300 Subject: [PATCH 1/4] check Artifact permissions in OptionProvider add tests --- pom.xml | 31 ++++ .../plugins/rundeck/OptionProvider.java | 32 +++- .../plugins/rundeck/OptionProviderSpec.groovy | 158 ++++++++++++++++++ 3 files changed, 220 insertions(+), 1 deletion(-) create mode 100644 src/test/groovy/jenkinsci/plugins/rundeck/OptionProviderSpec.groovy diff --git a/pom.xml b/pom.xml index 76a846a6..ed562f87 100644 --- a/pom.xml +++ b/pom.xml @@ -173,12 +173,43 @@ + + + cglib + cglib-nodep + 3.3.0 + test + + + + cglib + cglib + 3.3.0 + test + + + + + org.objenesis + objenesis + 3.3 + test + + org.spockframework spock-core 1.3-groovy-2.5 test + + + com.homeaway.devtools.jenkins + jenkins-spock + test + 2.1.5 + + com.google.code.gson gson diff --git a/src/main/java/org/jenkinsci/plugins/rundeck/OptionProvider.java b/src/main/java/org/jenkinsci/plugins/rundeck/OptionProvider.java index 68c6901f..6a442ed6 100644 --- a/src/main/java/org/jenkinsci/plugins/rundeck/OptionProvider.java +++ b/src/main/java/org/jenkinsci/plugins/rundeck/OptionProvider.java @@ -1,6 +1,5 @@ package org.jenkinsci.plugins.rundeck; -import hudson.model.AbstractProject; import hudson.model.Hudson; import hudson.model.Item; import hudson.model.ItemGroup; @@ -21,6 +20,8 @@ import org.kohsuke.stapler.StaplerRequest; import org.kohsuke.stapler.StaplerResponse; +import static hudson.model.Run.ARTIFACTS; + /** * Option provider for Rundeck - see http://rundeck.org/docs/manual/jobs.html#option-model-provider * @@ -60,6 +61,13 @@ public void doArtifact(StaplerRequest request, StaplerResponse response) throws return; } + try { + build.checkPermission(ARTIFACTS); + }catch (Exception e){ + response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); + return; + } + List - - com.homeaway.devtools.jenkins - jenkins-spock - test - 2.1.5 - - com.google.code.gson gson From 9a7f7364cc68622b0b6ec1daa77efaabc04a92b4 Mon Sep 17 00:00:00 2001 From: Luis Toledo Date: Fri, 21 Oct 2022 16:56:24 -0300 Subject: [PATCH 3/4] fix test --- .../plugins/rundeck/OptionProviderSpec.groovy | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/test/groovy/jenkinsci/plugins/rundeck/OptionProviderSpec.groovy b/src/test/groovy/jenkinsci/plugins/rundeck/OptionProviderSpec.groovy index 16ec8ac1..0a36114e 100644 --- a/src/test/groovy/jenkinsci/plugins/rundeck/OptionProviderSpec.groovy +++ b/src/test/groovy/jenkinsci/plugins/rundeck/OptionProviderSpec.groovy @@ -18,16 +18,26 @@ import static hudson.model.Run.ARTIFACTS class OptionProviderSpec extends Specification { + def originalHolder + + def setup() { + originalHolder = Jenkins.HOLDER + } + + def cleanup() { + Jenkins.HOLDER = originalHolder + } def "test option artifact without permissions"(){ given: final Hudson jenkins = Mock() - jenkins.getInstanceOrNull() >> jenkins jenkins.getInstance() >> jenkins + def originalHolder = Jenkins.HOLDER + Jenkins.HOLDER = new Jenkins.JenkinsHolder() { Jenkins getInstance() { return jenkins @@ -48,11 +58,13 @@ class OptionProviderSpec extends Specification { getParameter("project")>>"test" getParameter("build")>>"lastSuccessful" } - def result = optionProvider.doArtifact(request,response ) + + optionProvider.doArtifact(request,response ) then: 1 * response.sendError(HttpServletResponse.SC_BAD_REQUEST, {message-> message == "anonymous is missing the Run/Artifacts permission" }); + } def "test option artifact with permissions"(){ @@ -155,4 +167,5 @@ class OptionProviderSpec extends Specification { 1*writer.append({json-> json == "[]"}) } + } \ No newline at end of file From dd97ee27e4efa91bdee6ffdc11e4caa31b5cd796 Mon Sep 17 00:00:00 2001 From: Luis Toledo Date: Wed, 26 Oct 2022 10:22:42 -0300 Subject: [PATCH 4/4] refactoring --- .../plugins/rundeck/OptionProvider.java | 36 ++++++++----------- .../plugins/rundeck/OptionProviderSpec.groovy | 1 + 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/jenkinsci/plugins/rundeck/OptionProvider.java b/src/main/java/org/jenkinsci/plugins/rundeck/OptionProvider.java index 6a442ed6..1e1faba8 100644 --- a/src/main/java/org/jenkinsci/plugins/rundeck/OptionProvider.java +++ b/src/main/java/org/jenkinsci/plugins/rundeck/OptionProvider.java @@ -1,5 +1,6 @@ package org.jenkinsci.plugins.rundeck; +import hudson.Functions; import hudson.model.Hudson; import hudson.model.Item; import hudson.model.ItemGroup; @@ -62,7 +63,7 @@ public void doArtifact(StaplerRequest request, StaplerResponse response) throws } try { - build.checkPermission(ARTIFACTS); + this.checkArtifactPermissions(build); }catch (Exception e){ response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage()); return; @@ -155,12 +156,6 @@ public void doBuild(StaplerRequest request, StaplerResponse response) throws IOE List