Skip to content

Commit

Permalink
Incremented minor version to 2.3.0. Integrated code changes from meta…
Browse files Browse the repository at this point in the history
…schema-java 2.0.0 and liboscal-java 5.0.0 changes.
  • Loading branch information
david-waltermire committed Oct 30, 2024
1 parent ef2fe6f commit 7558304
Show file tree
Hide file tree
Showing 18 changed files with 127 additions and 99 deletions.
22 changes: 17 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</parent>
<groupId>dev.metaschema.oscal</groupId>
<artifactId>oscal-cli-enhanced</artifactId>
<version>2.2.1-SNAPSHOT</version>
<version>2.3.0-SNAPSHOT</version>

<packaging>jar</packaging>

Expand Down Expand Up @@ -58,8 +58,8 @@

<properties>
<!-- metaschema dependencies -->
<dependency.metaschema-framework.version>1.2.0</dependency.metaschema-framework.version>
<dependency.liboscal-java.version>4.2.0</dependency.liboscal-java.version>
<dependency.metaschema-framework.version>2.0.0-SNAPSHOT</dependency.metaschema-framework.version>
<dependency.liboscal-java.version>5.0.0-SNAPSHOT</dependency.liboscal-java.version>

<!-- site configuration -->
<site.url>https://oscal-cli.metaschema.dev</site.url>
Expand Down Expand Up @@ -114,6 +114,11 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>${dependency.xmlbeans.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
Expand Down Expand Up @@ -184,7 +189,6 @@
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>${dependency.xmlbeans.version}</version>
</dependency>
<dependency>
<groupId>com.github.erosb</groupId>
Expand Down Expand Up @@ -287,6 +291,15 @@
<artifactId>maven-pmd-plugin</artifactId>
<executions>
<execution>
<id>pmd-analyze</id>
<goals>
<goal>pmd</goal>
</goals>
<configuration>
<format>net.sourceforge.pmd.renderers.SarifRenderer</format>
</configuration>
</execution>
<execution>
<id>pmd-verify</id>
<goals>
<goal>check</goal>
Expand All @@ -296,7 +309,6 @@
<!-- fail on error for all builds -->
<failOnViolation>true</failOnViolation>
<printFailingErrors>true</printFailingErrors>
<format>sarif</format>
</configuration>
</execution>
</executions>
Expand Down
21 changes: 9 additions & 12 deletions src/main/java/gov/nist/secauto/oscal/tools/cli/core/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import gov.nist.secauto.metaschema.cli.processor.ExitStatus;
import gov.nist.secauto.metaschema.core.MetaschemaJavaVersion;
import gov.nist.secauto.metaschema.core.model.MetaschemaVersion;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.IVersionInfo;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.oscal.lib.LibOscalVersion;
import gov.nist.secauto.oscal.lib.OscalVersion;
import gov.nist.secauto.oscal.tools.cli.core.commands.ConvertCommand;
Expand Down Expand Up @@ -43,17 +43,14 @@ public static void main(String[] args) {

@NonNull
public static ExitStatus runCli(String... args) {
@SuppressWarnings("serial") Map<String, IVersionInfo> versions = ObjectUtils.notNull(
new LinkedHashMap<>() {
{
put(CLIProcessor.COMMAND_VERSION, new OscalCliVersion());
put("https://github.com/usnistgov/liboscal-java", new LibOscalVersion());
put("https://github.com/usnistgov/OSCAL", new OscalVersion());
put("https://github.com/usnistgov/metaschema-java", new MetaschemaJavaVersion());
put("https://github.com/usnistgov/metaschema", new MetaschemaVersion());
}
});
CLIProcessor processor = new CLIProcessor("oscal-cli", versions);
@SuppressWarnings("PMD.UseConcurrentHashMap") Map<String, IVersionInfo> versions = new LinkedHashMap<>();
versions.put(CLIProcessor.COMMAND_VERSION, new OscalCliVersion());
versions.put("https://github.com/usnistgov/liboscal-java", new LibOscalVersion());
versions.put("https://github.com/usnistgov/OSCAL", new OscalVersion());
versions.put("https://github.com/usnistgov/metaschema-java", new MetaschemaJavaVersion());
versions.put("https://github.com/usnistgov/metaschema", new MetaschemaVersion());

CLIProcessor processor = new CLIProcessor("oscal-cli", CollectionUtil.unmodifiableMap(versions));
processor.addCommandHandler(new CatalogCommand());
processor.addCommandHandler(new ProfileCommand());
processor.addCommandHandler(new ComponentDefinitionCommand());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,43 @@
import gov.nist.secauto.metaschema.cli.commands.AbstractValidateContentCommand;
import gov.nist.secauto.metaschema.cli.processor.CLIProcessor.CallingContext;
import gov.nist.secauto.metaschema.cli.processor.command.ICommandExecutor;
import gov.nist.secauto.metaschema.core.model.IModule;
import gov.nist.secauto.metaschema.core.model.MetaschemaException;
import gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet;
import gov.nist.secauto.metaschema.core.model.xml.ExternalConstraintsModulePostProcessor;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.model.validation.JsonSchemaContentValidator;
import gov.nist.secauto.metaschema.core.model.validation.XmlSchemaContentValidator;
import gov.nist.secauto.metaschema.databind.IBindingContext;
import gov.nist.secauto.metaschema.databind.IBindingContext.ISchemaValidationProvider;
import gov.nist.secauto.oscal.lib.OscalBindingContext;
import gov.nist.secauto.oscal.lib.model.OscalCompleteModule;

import org.apache.commons.cli.CommandLine;
import org.json.JSONObject;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.net.URL;
import java.util.List;
import java.util.Set;

import javax.xml.transform.Source;

import edu.umd.cs.findbugs.annotations.NonNull;

public abstract class AbstractOscalValidationCommand
extends AbstractValidateContentCommand {

@NonNull
protected abstract List<Source> getOscalXmlSchemas() throws IOException;
protected abstract XmlSchemaContentValidator getOscalXmlSchemas() throws IOException, SAXException;

@NonNull
protected abstract JSONObject getOscalJsonSchema() throws IOException;
protected abstract JsonSchemaContentValidator getOscalJsonSchema() throws IOException;

@Override
public ICommandExecutor newExecutor(CallingContext callingContext, CommandLine commandLine) {
return new OscalCommandExecutor(callingContext, commandLine);
return new AbstractOscalValidationCommand.OscalCommandExecutor(callingContext, commandLine);
}

protected class OscalCommandExecutor
extends AbstractValidationCommandExecutor {
extends AbstractValidateContentCommand.AbstractValidationCommandExecutor
implements ISchemaValidationProvider {

protected OscalCommandExecutor(
@NonNull CallingContext callingContext,
Expand All @@ -51,28 +54,37 @@ protected OscalCommandExecutor(

@Override
protected IBindingContext getBindingContext(@NonNull Set<IConstraintSet> constraintSets) {
IBindingContext retval;
if (constraintSets.isEmpty()) {
retval = OscalBindingContext.instance();
} else {
ExternalConstraintsModulePostProcessor postProcessor
= new ExternalConstraintsModulePostProcessor(constraintSets);

retval = new OscalBindingContext(CollectionUtil.singletonList(postProcessor));
}
return retval;
return OscalBindingContext.builder()
.constraintSet(constraintSets)
.build();
}

@Override
@NonNull
public List<Source> getXmlSchemas(URL targetResource) throws IOException {
public XmlSchemaContentValidator getXmlSchemas(URL targetResource, IBindingContext bindingContext)
throws IOException, SAXException {
return getOscalXmlSchemas();
}

@Override
@NonNull
public JSONObject getJsonSchema(JSONObject json) throws IOException {
public JsonSchemaContentValidator getJsonSchema(JSONObject json, IBindingContext bindingContext)
throws IOException {
return getOscalJsonSchema();
}

@NonNull
protected ISchemaValidationProvider getSchemaValidationProvider(
@NonNull IModule module,
@NonNull CommandLine commandLine,
@NonNull IBindingContext bindingContext) {
return this;
}

@Override
protected IModule getModule(CommandLine commandLine, IBindingContext bindingContext)
throws IOException, MetaschemaException {
return bindingContext.registerModule(OscalCompleteModule.class);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
import gov.nist.secauto.metaschema.core.util.CustomCollectors;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.metaschema.databind.IBindingContext;
import gov.nist.secauto.metaschema.databind.io.DeserializationFeature;
import gov.nist.secauto.metaschema.databind.io.Format;
import gov.nist.secauto.metaschema.databind.io.IBoundLoader;
Expand Down Expand Up @@ -164,7 +165,8 @@ protected ExitStatus executeCommand(
List<String> extraArgs = cmdLine.getArgList();
Path source = resolvePathAgainstCWD(ObjectUtils.notNull(Paths.get(extraArgs.get(0))));

IBoundLoader loader = OscalBindingContext.instance().newBoundLoader();
IBindingContext bindingContext = OscalBindingContext.instance();
IBoundLoader loader = bindingContext.newBoundLoader();
loader.disableFeature(DeserializationFeature.DESERIALIZE_VALIDATE_CONSTRAINTS);

Format asFormat;
Expand Down Expand Up @@ -258,8 +260,7 @@ protected ExitStatus executeCommand(
// this is a profile
DynamicContext dynamicContext = new DynamicContext(document.getStaticContext());
dynamicContext.setDocumentLoader(loader);
ProfileResolver resolver = new ProfileResolver();
resolver.setDynamicContext(dynamicContext);
ProfileResolver resolver = new ProfileResolver(dynamicContext);

IDocumentNodeItem resolvedProfile;
try {
Expand All @@ -276,8 +277,7 @@ protected ExitStatus executeCommand(
// ((IBoundXdmNodeItem)resolvedProfile).validate(validator);
// validator.finalizeValidation();

ISerializer<Catalog> serializer
= OscalBindingContext.instance().newSerializer(toFormat, Catalog.class);
ISerializer<Catalog> serializer = bindingContext.newSerializer(toFormat, Catalog.class);
try {
if (destination == null) {
@SuppressWarnings({ "resource", "PMD.CloseResource" }) PrintStream stdOut = ObjectUtils.notNull(System.out);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ protected void handleConversion(URI source, Format toFormat, Writer writer, IBou
Class<? extends IBoundObject> boundClass;
IBoundObject object;
try (InputStream is = source.toURL().openStream()) {
assert is != null;
FormatDetector.Result formatResult = loader.detectFormat(is);
Format sourceformat = formatResult.getFormat();
try (InputStream fis = formatResult.getDataStream()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@
import gov.nist.secauto.metaschema.core.model.constraint.IAllowedValue;
import gov.nist.secauto.metaschema.core.model.constraint.IAllowedValuesConstraint;
import gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet;
import gov.nist.secauto.metaschema.core.model.xml.ExternalConstraintsModulePostProcessor;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.metaschema.core.util.UriUtils;
import gov.nist.secauto.metaschema.databind.IBindingContext;
import gov.nist.secauto.metaschema.databind.model.IBoundModule;
import gov.nist.secauto.metaschema.databind.model.metaschema.BindingConstraintLoader;
import gov.nist.secauto.oscal.lib.OscalBindingContext;
import gov.nist.secauto.oscal.lib.model.OscalCompleteModule;
import gov.nist.secauto.oscal.lib.model.util.AllowedValueCollectingNodeItemVisitor;
Expand Down Expand Up @@ -141,6 +139,7 @@ public ICommandExecutor newExecutor(CallingContext callingContext, CommandLine c
*/
@SuppressWarnings({
"PMD.OnlyOneReturn", // readability
"PMD.AvoidCatchingGenericException"
})
protected ExitStatus executeCommand(
@NonNull CallingContext callingContext,
Expand Down Expand Up @@ -181,7 +180,7 @@ protected ExitStatus executeCommand(

Set<IConstraintSet> constraintSets;
if (cmdLine.hasOption(CONSTRAINTS_OPTION)) {
IConstraintLoader constraintLoader = new BindingConstraintLoader(IBindingContext.instance());
IConstraintLoader constraintLoader = IBindingContext.getConstraintLoader();
constraintSets = new LinkedHashSet<>();
String[] args = cmdLine.getOptionValues(CONSTRAINTS_OPTION);
for (String arg : args) {
Expand All @@ -199,14 +198,9 @@ protected ExitStatus executeCommand(

IBindingContext bindingContext;
try {
if (constraintSets.isEmpty()) {
bindingContext = OscalBindingContext.instance();
} else {
ExternalConstraintsModulePostProcessor postProcessor
= new ExternalConstraintsModulePostProcessor(constraintSets);

bindingContext = new OscalBindingContext(CollectionUtil.singletonList(postProcessor));
}
bindingContext = OscalBindingContext.builder()
.constraintSet(constraintSets)
.build();
} catch (Exception ex) {
return ExitCode.PROCESSING_ERROR
.exitMessage("Unable to get binding context. " + ex.getMessage())
Expand Down Expand Up @@ -397,6 +391,6 @@ private static String metapath(@NonNull IDefinitionNodeItem<?, ?> item) {

private static String metapath(@NonNull String path) {
// remove position 1 predicates
return path.replaceAll("\\[1\\]", "");
return path.replace("[1]", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@

import gov.nist.secauto.metaschema.core.model.util.JsonUtil;
import gov.nist.secauto.metaschema.core.model.util.XmlUtil;
import gov.nist.secauto.metaschema.core.model.validation.JsonSchemaContentValidator;
import gov.nist.secauto.metaschema.core.model.validation.XmlSchemaContentValidator;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.oscal.lib.OscalBindingContext;

import org.json.JSONObject;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -31,20 +33,20 @@ public String getDescription() {

@Override
@NonNull
public List<Source> getOscalXmlSchemas() throws IOException {
public XmlSchemaContentValidator getOscalXmlSchemas() throws IOException, SAXException {
List<Source> retval = new LinkedList<>();
retval.add(
XmlUtil.getStreamSource(ObjectUtils.requireNonNull(
OscalBindingContext.class.getResource("/schema/xml/oscal-complete_schema.xsd"))));
return CollectionUtil.unmodifiableList(retval);
return new XmlSchemaContentValidator(CollectionUtil.unmodifiableList(retval));
}

@Override
@NonNull
public JSONObject getOscalJsonSchema() throws IOException {
public JsonSchemaContentValidator getOscalJsonSchema() throws IOException {
try (InputStream is = ObjectUtils.requireNonNull(
OscalBindingContext.class.getResourceAsStream("/schema/json/oscal-complete_schema.json"))) {
return JsonUtil.toJsonObject(is);
return new JsonSchemaContentValidator(JsonUtil.toJsonObject(is));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

import gov.nist.secauto.metaschema.core.model.util.JsonUtil;
import gov.nist.secauto.metaschema.core.model.util.XmlUtil;
import gov.nist.secauto.metaschema.core.model.validation.JsonSchemaContentValidator;
import gov.nist.secauto.metaschema.core.model.validation.XmlSchemaContentValidator;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.oscal.lib.OscalBindingContext;
import gov.nist.secauto.oscal.tools.cli.core.commands.oscal.AbstractDeprecatedOscalValidationSubcommand;

import org.json.JSONObject;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -29,19 +31,19 @@ public String getDescription() {
}

@Override
protected List<Source> getOscalXmlSchemas() throws IOException {
protected XmlSchemaContentValidator getOscalXmlSchemas() throws IOException, SAXException {
List<Source> retval = new LinkedList<>();
retval.add(
XmlUtil.getStreamSource(ObjectUtils.requireNonNull(
OscalBindingContext.class.getResource("/schema/xml/oscal-ap_schema.xsd"))));
return CollectionUtil.unmodifiableList(retval);
return new XmlSchemaContentValidator(CollectionUtil.unmodifiableList(retval));
}

@Override
protected JSONObject getOscalJsonSchema() throws IOException {
protected JsonSchemaContentValidator getOscalJsonSchema() throws IOException {
try (InputStream is = ObjectUtils.requireNonNull(
OscalBindingContext.class.getResourceAsStream("/schema/json/oscal-ap_schema.json"))) {
return JsonUtil.toJsonObject(is);
return new JsonSchemaContentValidator(JsonUtil.toJsonObject(is));
}
}
}
Loading

0 comments on commit 7558304

Please sign in to comment.