Skip to content

Commit

Permalink
STarted refactoring of commands. Not completely working.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Aug 29, 2021
1 parent fed1913 commit 5e1c80b
Show file tree
Hide file tree
Showing 10 changed files with 284 additions and 96 deletions.
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "OSCAL"]
[submodule "oscal"]
path = oscal
url = git@github.com:usnistgov/OSCAL.git
url = https://github.com/usnistgov/OSCAL.git
1 change: 0 additions & 1 deletion OSCAL
Submodule OSCAL deleted from d26e3b
1 change: 1 addition & 0 deletions oscal
Submodule oscal added at aba66b
70 changes: 24 additions & 46 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<parent>
<groupId>gov.nist.secauto</groupId>
<artifactId>oss-parent</artifactId>
<version>17</version>
<version>18-SNAPSHOT</version>
</parent>

<groupId>gov.nist.secauto.oscal</groupId>
<artifactId>liboscal-java</artifactId>
<version>0.8.0-SNAPSHOT</version>
<version>1.0.0rc1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>OSCAL Java Library</name>
Expand Down Expand Up @@ -101,8 +101,9 @@
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<dependency.metaschema-framework.version>0.4.0-SNAPSHOT</dependency.metaschema-framework.version>
<version.plugin.license>4.0.rc1</version.plugin.license>
<oscal.commit>master</oscal.commit>
<dependency.jackson.version>2.12.1</dependency.jackson.version>

<oscal-content.commit>master</oscal-content.commit>
</properties>

<dependencies>
Expand All @@ -114,18 +115,16 @@
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.0</version>
<scope>test</scope>
<version>${dependency.jackson.version}</version>
</dependency>
<!-- <dependency> -->
<!-- <groupId>org.junit.jupiter</groupId> -->
<!-- <artifactId>junit-jupiter-api</artifactId> -->
<!-- <scope>test</scope> -->
<!-- </dependency> -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand All @@ -147,7 +146,7 @@
<goal>wget</goal>
</goals>
<configuration>
<uri>https://raw.githubusercontent.com/usnistgov/oscal-content/${oscal.commit}/nist.gov/SP800-53/rev5/xml/NIST_SP-800-53_rev5_catalog.xml</uri>
<uri>https://raw.githubusercontent.com/usnistgov/oscal-content/${oscal-content.commit}/nist.gov/SP800-53/rev5/xml/NIST_SP-800-53_rev5_catalog.xml</uri>
<outputDirectory>${project.build.directory}/download/content</outputDirectory>
</configuration>
</execution>
Expand All @@ -158,7 +157,7 @@
<goal>wget</goal>
</goals>
<configuration>
<uri>https://raw.githubusercontent.com/usnistgov/oscal-content/${oscal.commit}/nist.gov/SP800-53/rev5/json/NIST_SP-800-53_rev5_catalog.json</uri>
<uri>https://raw.githubusercontent.com/usnistgov/oscal-content/${oscal-content.commit}/nist.gov/SP800-53/rev5/json/NIST_SP-800-53_rev5_catalog.json</uri>
<outputDirectory>${project.build.directory}/download/content</outputDirectory>
</configuration>
</execution>
Expand All @@ -169,7 +168,7 @@
<goal>wget</goal>
</goals>
<configuration>
<uri>https://raw.githubusercontent.com/usnistgov/oscal-content/${oscal.commit}/nist.gov/SP800-53/rev5/yaml/NIST_SP-800-53_rev5_catalog.yaml</uri>
<uri>https://raw.githubusercontent.com/usnistgov/oscal-content/${oscal-content.commit}/nist.gov/SP800-53/rev5/yaml/NIST_SP-800-53_rev5_catalog.yaml</uri>
<outputDirectory>${project.build.directory}/download/content</outputDirectory>
</configuration>
</execution>
Expand All @@ -186,7 +185,10 @@
<goal>generate-sources</goal>
</goals>
<configuration>
<metaschemaDir>${project.basedir}/OSCAL/src/metaschema</metaschemaDir>
<metaschemaDir>${project.basedir}/oscal/src/metaschema</metaschemaDir>
<configs>
<config>${project.basedir}/src/main/metaschema-bindings/oscal-metaschema-bindings.xml</config>
</configs>
<configs>
<config>src/main/metaschema-bindings/oscal-metaschema-bindings.xml</config>
</configs>
Expand All @@ -198,39 +200,15 @@
</executions>
</plugin>
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>${version.plugin.license}</version>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<licenseSets>
<licenseSet>
<header>license/nist/license.txt</header>
<includes>
<include>**/*.java</include>
</includes>
</licenseSet>
</licenseSets>
<failIfMissing>true</failIfMissing>
<sourceDirectories>
<sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
<sourceDirectory>${project.build.testSourceDirectory}</sourceDirectory>
</sourceDirectories>
</configuration>
<executions>
<execution>
<id>check-license</id>
<phase>verify</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>gov.nist.secauto</groupId>
<artifactId>oss-build-support</artifactId>
<version>17</version>
<scope>compile</scope>
</dependency>
</dependencies>
</plugin>

</plugins>
</build>
</project>
122 changes: 107 additions & 15 deletions src/main/java/gov/nist/secauto/oscal/java/OscalLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,27 @@
import com.fasterxml.jackson.core.format.DataFormatMatcher;

import gov.nist.secauto.metaschema.binding.BindingContext;
import gov.nist.secauto.metaschema.binding.BindingException;
import gov.nist.secauto.metaschema.binding.Format;
import gov.nist.secauto.metaschema.binding.io.BindingException;
import gov.nist.secauto.metaschema.binding.io.Configuration;
import gov.nist.secauto.metaschema.binding.io.Deserializer;
import gov.nist.secauto.metaschema.binding.io.Feature;
import gov.nist.secauto.metaschema.binding.io.Format;
import gov.nist.secauto.metaschema.binding.io.MutableConfiguration;
import gov.nist.secauto.metaschema.binding.io.json.JsonUtil;
import gov.nist.secauto.oscal.java.objects.Catalog;
import gov.nist.secauto.oscal.java.objects.ComponentDefinition;
import gov.nist.secauto.oscal.java.objects.Profile;
import gov.nist.secauto.oscal.java.objects.SystemSecurityPlan;
import gov.nist.secauto.oscal.java.AssessmentPlan;
import gov.nist.secauto.oscal.java.AssessmentResults;
import gov.nist.secauto.oscal.java.Catalog;
import gov.nist.secauto.oscal.java.ComponentDefinition;
import gov.nist.secauto.oscal.java.PlanOfActionAndMilestones;
import gov.nist.secauto.oscal.java.Profile;
import gov.nist.secauto.oscal.java.SystemSecurityPlan;

import org.codehaus.stax2.XMLEventReader2;
import org.codehaus.stax2.XMLInputFactory2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
Expand All @@ -62,18 +66,57 @@
public class OscalLoader {
private final BindingContext bindingContext;

/**
* Construct a new OSCAL loader instance.
*/
public OscalLoader() {
this(BindingContext.newInstance());
}

/**
* Construct a new OSCAL loader instance, using the provided {@link BindingContext}.
*
* @param bindingContext
* the Metaschema binding context to use to load Java types
*/
public OscalLoader(BindingContext bindingContext) {
this.bindingContext = bindingContext;
}

/**
* Get the configured Metaschema binding context to use to load Java types.
*
* @return the binding context
*/
protected BindingContext getBindingContext() {
return bindingContext;
}

public <CLASS> CLASS load(File file) throws FileNotFoundException, IOException, BindingException {
Class<?> clazz = detectModel(file);
@SuppressWarnings("unchecked")
CLASS retval = (CLASS) load(clazz, file);
return retval;
}

private Class<?> detectModel(File file) throws FileNotFoundException, IOException, BindingException {
DataFormatMatcher matcher = ContentUtil.detectFormat(new FileInputStream(file));

Class<?> retval;
switch (matcher.getMatchedFormatName()) {
case "XML":
retval = detectModelXml(file);
break;
case "JSON":
case "YAML":
retval = detectModelJson(matcher.createParserWithMatch());
break;
default:
throw new IOException("Unsupported format: " + matcher.getMatchedFormatName());
}
return retval;
}

private Class<?> detectModelXml(File file) throws BindingException {
Class<?> retval = null;
try {
Expand Down Expand Up @@ -110,6 +153,15 @@ private Class<?> detectModelXml(File file) throws BindingException {
case "component-definition":
retval = ComponentDefinition.class;
break;
case "assessment-plan":
retval = AssessmentPlan.class;
break;
case "assessment-results":
retval = AssessmentResults.class;
break;
case "plan-of-action-and-milestones":
retval = PlanOfActionAndMilestones.class;
break;
default:
throw new UnsupportedOperationException("Unrecognized element name: " + qname.toString());
}
Expand Down Expand Up @@ -145,8 +197,17 @@ private Class<?> detectModelJson(JsonParser parser) throws BindingException {
case "component-definition":
retval = ComponentDefinition.class;
break outer;
case "assessment-plan":
retval = AssessmentPlan.class;
break outer;
case "assessment-results":
retval = AssessmentResults.class;
break outer;
case "plan-of-action-and-milestones":
retval = PlanOfActionAndMilestones.class;
break outer;
case "$schema":
JsonUtil.skipValue(parser);
JsonUtil.skipNextValue(parser);
break;
default:
throw new UnsupportedOperationException("Unrecognized field name: " + name);
Expand All @@ -158,13 +219,31 @@ private Class<?> detectModelJson(JsonParser parser) throws BindingException {
return retval;
}

private Deserializer getDeserializer(Class<?> clazz, Format format, Configuration config)
private <CLASS> Deserializer<CLASS> getDeserializer(Class<CLASS> clazz, Format format, Configuration config)
throws BindingException {
Deserializer retval = getBindingContext().newDeserializer(format, clazz, config);
Deserializer<CLASS> retval = getBindingContext().newDeserializer(format, clazz, config);
return retval;
}

public <CLASS> CLASS load(Class<CLASS> clazz, File file) throws BindingException {
/**
* Load the specified data file as the specified Java class.
*
* @param <CLASS>
* the Java type to load data into
* @param clazz
* the class for the java type
* @param file
* the file to load
* @return the loaded instance data
* @throws BindingException
* if a binding error occurred while loading the data in the specified file
* @throws FileNotFoundException
* if the specified file does not exist
*/
public <CLASS> CLASS load(Class<CLASS> clazz, File file) throws BindingException, FileNotFoundException {
if (!file.exists()) {
throw new FileNotFoundException(file.getAbsolutePath());
}
try (InputStream is = new FileInputStream(file)) {

DataFormatMatcher matcher = ContentUtil.detectFormat(is);
Expand All @@ -191,7 +270,7 @@ public <CLASS> CLASS load(Class<CLASS> clazz, File file) throws BindingException
}

MutableConfiguration config = new MutableConfiguration().enableFeature(Feature.DESERIALIZE_ROOT);
Deserializer deserializer = getDeserializer(clazz, format, config);
Deserializer<CLASS> deserializer = getDeserializer(clazz, format, config);
CLASS retval = deserializer.deserialize(file);
return retval;
case INCONCLUSIVE:
Expand All @@ -205,19 +284,32 @@ public <CLASS> CLASS load(Class<CLASS> clazz, File file) throws BindingException
}
}

public Catalog loadCatalog(File file) throws BindingException {
public Catalog loadCatalog(File file) throws BindingException, FileNotFoundException {
return load(Catalog.class, file);
}

public Profile loadProfile(File file) throws BindingException {
public Profile loadProfile(File file) throws BindingException, FileNotFoundException {
return load(Profile.class, file);
}

public SystemSecurityPlan loadSystemSecurityPlan(File file) throws BindingException {
public SystemSecurityPlan loadSystemSecurityPlan(File file) throws BindingException, FileNotFoundException {
return load(SystemSecurityPlan.class, file);
}

public ComponentDefinition loadComponentDefinition(File file) throws BindingException {
public ComponentDefinition loadComponentDefinition(File file) throws BindingException, FileNotFoundException {
return load(ComponentDefinition.class, file);
}

public AssessmentPlan loadAssessmentPlan(File file) throws BindingException, FileNotFoundException {
return load(AssessmentPlan.class, file);
}

public AssessmentResults loadAssessmentResults(File file) throws BindingException, FileNotFoundException {
return load(AssessmentResults.class, file);
}

public PlanOfActionAndMilestones loadPlanOfActionAndMilestones(File file)
throws BindingException, FileNotFoundException {
return load(PlanOfActionAndMilestones.class, file);
}
}
Loading

0 comments on commit 5e1c80b

Please sign in to comment.