Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-41308

support Use Groovy Sandbox scripts in activeChoiceParams

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Minor Minor
    • job-dsl-plugin
    • None

      activeChoice supports placing scripts in the sandbox, which removes the requirement to have a admin approve the script.

      It would be great if job-dsl could support setting the sandbox.

      Currently its getting set to the default value, false.

          [JENKINS-41308] support Use Groovy Sandbox scripts in activeChoiceParams

          Anton Lundin created issue -

          Torben Knerr added a comment -

          Did anyone ever get this to work with the `configure` block?

          I lost a day of my life trying to modify the job xml in a `configure` block to enable the sandbox. Finally I got it so far:

          pipelineJob("foo") {
          
            parameters {
              activeChoiceParam('Param') {
                choiceType('SINGLE_SELECT')
                groovyScript {
                  script('return ["a","b","c"]')
                }
              }
            }
          
            // we need to wrap the active choice parameters' groovy script and fallbackScript
            // inside a secureScript wrapper with the sandbox enabled
            configure { proj ->
              // find all <script class="...GroovyScript"> nodes via depth-first search
              def scriptContainers = proj.'**'.findAll{ n ->
                n.class == Node && n.'@class' == 'org.biouno.unochoice.model.GroovyScript'
              }
              scriptContainers.each { s ->
                // wrap <script> inside a <secureScript>
                s.appendNode('secureScript', [plugin: 'script-security@1.25']).with {
                  appendNode('script', s.script.value)
                  appendNode('sandbox', 'true')
                  parent.remove(s.script)
                }
              }
            }
          }
          

          It seems to work (i.e. XML looks ok) on https://job-dsl.herokuapp.com/, but when used in Jenkins it still does not work. The generated config.xml ends up with both <script> and <secureScript> tags side-by-side, i.e. the removal of the script element via {{ parent.remove(s.script) }} does not work when this JobDSL runs in Jenkins.

          I had tried a variation with {{ replaceNode }} but this would not work in https://job-dsl.herokuapp.com ("java.lang.UnsupportedOperationException: Replacing the root node is not supported"). It did not throw this exception when run in Jenkins, but also not make any difference – still both elements there and the script needs approval.

          Help?!

          Torben Knerr added a comment - Did anyone ever get this to work with the `configure` block? I lost a day of my life trying to modify the job xml in a `configure` block to enable the sandbox. Finally I got it so far: pipelineJob( "foo" ) { parameters { activeChoiceParam( 'Param' ) { choiceType( 'SINGLE_SELECT' ) groovyScript { script( ' return [ "a" , "b" , "c" ]' ) } } } // we need to wrap the active choice parameters' groovy script and fallbackScript // inside a secureScript wrapper with the sandbox enabled configure { proj -> // find all <script class= "...GroovyScript" > nodes via depth-first search def scriptContainers = proj. '**' .findAll{ n -> n.class == Node && n. '@class' == 'org.biouno.unochoice.model.GroovyScript' } scriptContainers.each { s -> // wrap <script> inside a <secureScript> s.appendNode( 'secureScript' , [plugin: 'script-security@1.25' ]).with { appendNode( 'script' , s.script.value) appendNode( 'sandbox' , ' true ' ) parent.remove(s.script) } } } } It seems to work (i.e. XML looks ok) on https://job-dsl.herokuapp.com/ , but when used in Jenkins it still does not work. The generated config.xml ends up with both <script> and <secureScript> tags side-by-side, i.e. the removal of the script element via {{ parent.remove(s.script) }} does not work when this JobDSL runs in Jenkins. I had tried a variation with {{ replaceNode }} but this would not work in https://job-dsl.herokuapp.com ("java.lang.UnsupportedOperationException: Replacing the root node is not supported"). It did not throw this exception when run in Jenkins, but also not make any difference – still both elements there and the script needs approval. Help?!

          Use the Automatically Generated DSL:

          job('example') {
            parameters {
              choiceParameter {
                name('Param')
                script {
                  groovyScript {
                    script {
                      script('return ["a","b","c"]')
                      sandbox(true)
                    }
                    fallbackScript {
                      script('')
                      sandbox(false)
                    }
                  }
                }
                choiceType('SINGLE_SELECT')
                description('example param')
                randomName('param-4711')
                filterable(false)
              }
            } 
          }
          

          Daniel Spilker added a comment - Use the Automatically Generated DSL : job( 'example' ) { parameters { choiceParameter { name( 'Param' ) script { groovyScript { script { script( ' return [ "a" , "b" , "c" ]' ) sandbox( true ) } fallbackScript { script('') sandbox( false ) } } } choiceType( 'SINGLE_SELECT' ) description( 'example param' ) randomName( 'param-4711' ) filterable( false ) } } }
          Daniel Spilker made changes -
          Resolution New: Fixed [ 1 ]
          Status Original: Open [ 1 ] New: Resolved [ 5 ]
          Torben Knerr made changes -
          Attachment New: screenshot-1.png [ 35686 ]

          Torben Knerr added a comment -

          daspilker oh that is possible?

          It wasn't showing up on my local instances embedded API viewer, so I thought the automatically generated DSL would not be available. How can I tell if it is?

          Torben Knerr added a comment - daspilker oh that is possible? It wasn't showing up on my local instances embedded API viewer, so I thought the automatically generated DSL would not be available. How can I tell if it is?

          tknerr You must use the choiceParameter instead of activeChoiceParam. choiceParameter is provided by the Automatically Generated DSL, activeChoiceParam is the built-in DSL.

          Daniel Spilker added a comment - tknerr You must use the choiceParameter instead of activeChoiceParam . choiceParameter is provided by the Automatically Generated DSL, activeChoiceParam is the built-in DSL.
          Torben Knerr made changes -
          Attachment New: screenshot-2.png [ 35692 ]

          Torben Knerr added a comment -

          daspilker nevermind, I found it. Did not notice that the generated parameter is named differently ("choiceParameter"):

          Your example from above is working (in that it generates a valid config.xml at least) and gets me a big step further.

          Thanks a lot!

          Torben Knerr added a comment - daspilker nevermind, I found it. Did not notice that the generated parameter is named differently ("choiceParameter"): Your example from above is working (in that it generates a valid config.xml at least) and gets me a big step further. Thanks a lot!

          Dana Goyette added a comment -

          Using automatically generated DSL is a workaround, not a fix. We need real / direct support for enabling sandboxing.

          If I try to use automatically generated DSL, then "mvn test" in my dsl source fails, and the only examples of tests of automatically-generated DSL use gradle, not maven.

          Dana Goyette added a comment - Using automatically generated DSL is a workaround, not a fix. We need real / direct support for enabling sandboxing. If I try to use automatically generated DSL, then "mvn test" in my dsl source fails, and the only examples of tests of automatically-generated DSL use gradle, not maven.

            daspilker Daniel Spilker
            glance Anton Lundin
            Votes:
            3 Vote for this issue
            Watchers:
            11 Start watching this issue

              Created:
              Updated:
              Resolved: