Skip to content

Commit

Permalink
Redirect DTD http request to the resource file (#850)
Browse files Browse the repository at this point in the history
* Redirect DTD http request to the resource file

* Add JobSpec unittests

* Remove Cuebot TCP 8080 port from Docker
  • Loading branch information
splhack authored Feb 4, 2021
1 parent 6d84794 commit a2a3642
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 6 deletions.
30 changes: 29 additions & 1 deletion cuebot/src/main/java/com/imageworks/spcue/service/JobSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
package com.imageworks.spcue.service;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashSet;
Expand All @@ -36,6 +38,9 @@
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.springframework.dao.EmptyResultDataAccessException;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.imageworks.spcue.BuildableDependency;
import com.imageworks.spcue.BuildableJob;
Expand Down Expand Up @@ -102,6 +107,8 @@ public class JobSpec {

public static final String DEFAULT_SERVICE = "default";

public static final String SPCUE_DTD_URL = "http://localhost:8080/spcue/dtd/";

private List<BuildableJob> jobs = new ArrayList<BuildableJob>();

private List<BuildableDependency> depends = new ArrayList<BuildableDependency>();
Expand Down Expand Up @@ -836,9 +843,30 @@ public void parse(File file) {
handleDependsTags();
}

private class DTDRedirector implements EntityResolver {
public InputSource resolveEntity(String publicId,
String systemId) throws SAXException, IOException {
if (systemId.startsWith(SPCUE_DTD_URL)) {
// Redirect to resource file.
try {
String filename = systemId.substring(SPCUE_DTD_URL.length());
InputStream dtd = getClass().getResourceAsStream("/public/dtd/" + filename);
return new InputSource(dtd);
} catch (Exception e) {
throw new SpecBuilderException("Failed to redirect DTD " + systemId + ", " + e);
}
} else {
// Use default resolver.
return null;
}
}
}

public void parse(String cjsl) {
try {
doc = new SAXBuilder(true).build(new StringReader(cjsl));
SAXBuilder builder = new SAXBuilder(true);
builder.setEntityResolver(new DTDRedirector());
doc = builder.build(new StringReader(cjsl));

} catch (Exception e) {
throw new SpecBuilderException("Failed to parse job spec XML, " + e);
Expand Down
5 changes: 1 addition & 4 deletions cuebot/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
server.contextPath=/spcue
server.port=8080

spring.main.allow-bean-definition-overriding=true
spring.mvc.static-path-pattern=/spcue/**
spring.main.web-application-type=none
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

/*
* Copyright Contributors to the OpenCue Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


package com.imageworks.spcue.test.service;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import javax.annotation.Resource;

import org.junit.Test;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.support.AnnotationConfigContextLoader;

import com.imageworks.spcue.SpecBuilderException;
import com.imageworks.spcue.config.TestAppConfig;
import com.imageworks.spcue.service.JobLauncher;
import com.imageworks.spcue.service.JobSpec;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;


@ContextConfiguration(classes=TestAppConfig.class, loader=AnnotationConfigContextLoader.class)
public class JobSpecTests extends AbstractTransactionalJUnit4SpringContextTests {

@Resource
JobLauncher jobLauncher;

private static String readJobSpec(String name)
{
String path = "src/test/resources/conf/jobspec/" + name;
byte[] encoded = null;

try {
encoded = Files.readAllBytes(Paths.get(path));
} catch (IOException e) {
fail("readJobSpec should succeed to read jobspec file");
}

return new String(encoded, StandardCharsets.UTF_8);
}

@Test
public void testParseSuccess() {
String xml = readJobSpec("jobspec_1_10.xml");
JobSpec spec = jobLauncher.parse(xml);
assertEquals(spec.getDoc().getDocType().getPublicID(),
"SPI Cue Specification Language");
assertEquals(spec.getDoc().getDocType().getSystemID(),
"http://localhost:8080/spcue/dtd/cjsl-1.10.dtd");
assertEquals(spec.getJobs().size(), 1);
assertEquals(spec.getJobs().get(0).detail.name, "testing-default-testuser_test");
}

@Test
public void testParseNonExistent() {
String xml = readJobSpec("jobspec_nonexistent_dtd.xml");
try {
jobLauncher.parse(xml);
fail("Expected exception");
} catch (SpecBuilderException e) {
assertEquals(e.getMessage(),
"Failed to parse job spec XML, java.net.MalformedURLException");
}
}
}
47 changes: 47 additions & 0 deletions cuebot/src/test/resources/conf/jobspec/jobspec_1_10.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0"?>
<!--
Copyright Contributors to the OpenCue Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->




<!DOCTYPE spec PUBLIC "SPI Cue Specification Language" "http://localhost:8080/spcue/dtd/cjsl-1.10.dtd">
<spec>
<facility>local</facility>
<show>testing</show>
<shot>default</shot>
<user>testuser</user>
<uid>9860</uid>

<job name="test">
<paused>False</paused>
<maxretries>2</maxretries>
<autoeat>False</autoeat>
<env/>
<layers>
<layer name="test" type="Render">
<cmd>echo hello</cmd>
<range>1</range>
<chunk>1</chunk>
<env/>
<services>
<service>shell</service>
</services>
</layer>
</layers>
</job>
<depends/>
</spec>
47 changes: 47 additions & 0 deletions cuebot/src/test/resources/conf/jobspec/jobspec_nonexistent_dtd.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0"?>
<!--
Copyright Contributors to the OpenCue Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->




<!DOCTYPE spec PUBLIC "SPI Cue Specification Language" "http://localhost:8080/spcue/dtd/cjsl-nonexistent.dtd">
<spec>
<facility>local</facility>
<show>testing</show>
<shot>default</shot>
<user>testuser</user>
<uid>9860</uid>

<job name="test">
<paused>False</paused>
<maxretries>2</maxretries>
<autoeat>False</autoeat>
<env/>
<layers>
<layer name="test" type="Render">
<cmd>echo hello</cmd>
<range>1</range>
<chunk>1</chunk>
<env/>
<services>
<service>shell</service>
</services>
</layer>
</layers>
</job>
<depends/>
</spec>
1 change: 0 additions & 1 deletion sandbox/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ services:
- db
ports:
- "8443:8443"
- "8080:8080"
depends_on:
- db
- flyway
Expand Down

0 comments on commit a2a3642

Please sign in to comment.