private CpsScript parseScript() throws IOException { // classloader hierarchy. See doc/classloader.md trusted = new CpsGroovyShellFactory(this).forTrusted().build(); shell = new CpsGroovyShellFactory(this).withParent(trusted).build(); CpsScript s = (CpsScript) shell.reparse("WorkflowScript",script); for (Entry<String, String> e : loadedScripts.entrySet()) { shell.reparse(e.getKey(), e.getValue()); } s.execution = this; if (false) { System.out.println("scriptName="+s.getClass().getName()); System.out.println(Arrays.asList(s.getClass().getInterfaces())); System.out.println(Arrays.asList(s.getClass().getDeclaredFields())); System.out.println(Arrays.asList(s.getClass().getDeclaredMethods())); } return s; }
public CpsGroovyShell build() { ClassLoader parent = this.parent; if (parent==null) parent = makeClassLoader(); CpsGroovyShell shell = new CpsGroovyShell(parent, execution, makeConfig()); for (GroovyShellDecorator d : decorators) { d.configureShell(execution,shell); } return shell; } }
private CompilerConfiguration makeConfig() { CompilerConfiguration cc = sandbox ? GroovySandbox.createBaseCompilerConfiguration() : new CompilerConfiguration(); cc.addCompilationCustomizers(makeImportCustomizer()); cc.addCompilationCustomizers(makeCpsTransformer()); cc.setScriptBaseClass(CpsScript.class.getName()); for (GroovyShellDecorator d : decorators) { d.configureCompiler(execution,cc); } return cc; }
/** * Derives a new factory for creating trusted {@link CpsGroovyShell} */ public CpsGroovyShellFactory forTrusted() { List<GroovyShellDecorator> inner = new ArrayList<>(); for (GroovyShellDecorator d : decorators) { inner.add(d.forTrusted()); } return new CpsGroovyShellFactory(execution, false, parent, inner); }
public CpsGroovyShellFactory withParent(GroovyShell parent) { return withParent(parent.getClassLoader()); }
@RequirePOST public JSON doCheckScriptCompile(@AncestorInPath Item job, @QueryParameter String value) { if (!job.hasPermission(Job.CONFIGURE)) { return CpsFlowDefinitionValidator.CheckStatus.SUCCESS.asJSON(); } try { CpsGroovyShell trusted = new CpsGroovyShellFactory(null).forTrusted().build(); new CpsGroovyShellFactory(null).withParent(trusted).withSandbox(true).build().getClassLoader().parseClass(value); } catch (CompilationFailedException x) { return JSONArray.fromObject(CpsFlowDefinitionValidator.toCheckStatus(x).toArray()); } return CpsFlowDefinitionValidator.CheckStatus.SUCCESS.asJSON(); // Approval requirements are managed by regular stapler form validation (via doCheckScript) }