@Override public void apply(ImportTask task, ImportData data) throws Exception { File executable = getExecutable(); CommandLine cmd = new CommandLine(executable); for (String option : options) { cmd.addArgument(option, false); } execute(cmd, getScriptsFolder().dir()); }
@Test public void testPostScriptDisableAbsolutePath() { // check if sh is there Assume.assumeTrue(new File("/bin/sh").exists()); try { new PostScriptTransform("/bin/sh", Collections.emptyList()); fail("Should have failed disallowing usage of absoute path"); } catch (Exception e) { assertThat( e.getMessage(), allOf(containsString("/bin/sh"), containsString("not " + "found"))); } }
public PostScriptTransform(String name, List<String> options) { super(options); this.name = name; // force validation since there is no setter for name getExecutable(); }
private File getExecutable() { Resource scripts = getScriptsFolder(); Resource resource = scripts.get(name); if (resource.getType() != Resource.Type.RESOURCE) { throw new ValidationException( "Script named '" + name + "' was not found in " + "$GEOSERVER_DATA_DIR/importer/scripts"); } File executable = resource.file(); if (!executable.canExecute()) { throw new ValidationException( "Found file named '" + name + "' in " + "$GEOSERVER_DATA_DIR/importer/scripts, but it's not executable"); } return executable; }
@Test public void testPostScriptDisableTraversal() throws Exception { // prepare the scripts folder, but leave it empty File scripts = getDataDirectory().findOrCreateDir("importer", "scripts"); FileUtils.deleteQuietly(scripts); assertTrue(scripts.mkdirs()); try { new PostScriptTransform("../wfs.xml", Collections.emptyList()); fail("Should have thrown an exception"); } catch (Exception e) { assertThat(e.getMessage(), containsString("invalid ..")); } }
@Test public void testRunNonExistingScript() throws Exception { // prepare the scripts folder, but leave it empty File scripts = getDataDirectory().findOrCreateDir("importer", "scripts"); FileUtils.deleteQuietly(scripts); assertTrue(scripts.mkdirs()); try { new PostScriptTransform("i_am_not_there.sh", Collections.emptyList()); fail("Should have thrown an exception"); } catch (Exception e) { assertThat(e.getMessage(), containsString("i_am_not_there.sh")); } }
assertEquals("archsites", task.getLayer().getResource().getName()); TransformChain transformChain = task.getTransform(); transformChain.add(new PostScriptTransform("test.sh", Collections.emptyList())); importer.run(context);