Skip to content

Commit

Permalink
Allow file or URI arguments to metaschema-cli (#298)
Browse files Browse the repository at this point in the history
* Add UriUtils class for URI/Path translation

Currently, we have the metaschema-cli only support local file paths for
arguments for content inputs and outputs. This loader will support the
use of MetaschemaLoader.load() with URIs and unify local file path and
URL access (primarily https://, http://) with the same command line
tooling for similar remote and local access.

Co-Authored-By: David Waltermire <[email protected]>

* CLI commands allow files or URIs for #297

This PR adjusts the commands and supporting databind classes and ports
them to use URIs. The commands will use the new UriUtils class and the
toUri() function to use a remote URI supported as a URL and load the
model, schema, or target file. If a local file, it will convert the file
path to valid URI.

Integration tests have been added to validate this functionality works
with the update CLI commands.

* Specific error handling for different URI failures

Per discussion with @david-waltermire, update the exception handling and
associated error messages to identify if a bad domain for a remote URI
is used or a valid domain is used but a bad URL.

Co-authored-by: David Waltermire <[email protected]>

* Feedback: use buffered input stream for YAML URIs

* Feedback: remove infeasible UriUtilsTest cases

* Feedback: arg count check for AbstractValidateContentCommand

* Feedback: docs for UriUtils.toUri()

---------

Co-authored-by: David Waltermire <[email protected]>
  • Loading branch information
aj-stein-nist and david-waltermire authored Feb 6, 2024
1 parent 2846899 commit 9360d97
Show file tree
Hide file tree
Showing 11 changed files with 343 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import gov.nist.secauto.metaschema.core.resource.AbstractResourceResolver;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand All @@ -43,7 +44,7 @@ public IValidationResult validate(URI uri) throws IOException {
URI resourceUri = resolve(uri);
URL resource = resourceUri.toURL();

try (InputStream is = ObjectUtils.notNull(resource.openStream())) {
try (InputStream is = new BufferedInputStream(ObjectUtils.notNull(resource.openStream()))) {
return validate(is, resourceUri);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Portions of this software was developed by employees of the National Institute
* of Standards and Technology (NIST), an agency of the Federal Government and is
* being made available as a public service. Pursuant to title 17 United States
* Code Section 105, works of NIST employees are not subject to copyright
* protection in the United States. This software may be subject to foreign
* copyright. Permission in the United States and in foreign countries, to the
* extent that NIST may hold copyright, to use, copy, modify, create derivative
* works, and distribute this software and its documentation without fee is hereby
* granted on a non-exclusive basis, provided that this notice and disclaimer
* of warranty appears in all copies.
*
* THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
* EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
* THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
* INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
* SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
* SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
* INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
* OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
* CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
* PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/

package gov.nist.secauto.metaschema.core.util;

import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;

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

public final class UriUtils {

private UriUtils() {
// disable construction
}

/**
* Process a string to a local file path or remote location. If the location is
* convertible to a URI, return the {@link URI}. Normalize the resulting URI
* with the base URI, if provided.
*
* @param location
* a string defining a remote or local file-based location
* @param baseUri
* the base URI to use for URI normalization
* @throws URISyntaxException
* an error if the location string is not convertible to URI
* @return a new URI
*/
public static URI toUri(@NonNull String location, @NonNull URI baseUri) throws URISyntaxException {
URI asUri;
try {
asUri = new URI(location);
} catch (URISyntaxException ex) {
// the location is not a valid URI
try {
// try to parse the location as a local file path
Path path = Paths.get(location);
asUri = path.toUri();
} catch (InvalidPathException ex2) {
// not a local file path, so rethrow the original URI expection
throw ex;
}
}
return baseUri.resolve(asUri.normalize());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Portions of this software was developed by employees of the National Institute
* of Standards and Technology (NIST), an agency of the Federal Government and is
* being made available as a public service. Pursuant to title 17 United States
* Code Section 105, works of NIST employees are not subject to copyright
* protection in the United States. This software may be subject to foreign
* copyright. Permission in the United States and in foreign countries, to the
* extent that NIST may hold copyright, to use, copy, modify, create derivative
* works, and distribute this software and its documentation without fee is hereby
* granted on a non-exclusive basis, provided that this notice and disclaimer
* of warranty appears in all copies.
*
* THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
* EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
* THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
* INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
* SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT
* SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
* INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
* OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
* CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
* PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
* OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
*/

package gov.nist.secauto.metaschema.core.util;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Stream;

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

class UriUtilsTest {
private static final boolean VALID = true;
private static final boolean INVALID = false;

private static Stream<Arguments> provideValuesTestToUri() {
List<Arguments> values = new LinkedList<>() {
{
add(Arguments.of("http://example.org/valid", VALID));
add(Arguments.of("https://example.org/valid", VALID));
add(Arguments.of("http://example.org/valid", VALID));
add(Arguments.of("ftp://example.org/valid", VALID));
add(Arguments.of("ssh://example.org/valid", VALID));
add(Arguments.of("example.org/good", VALID));
add(Arguments.of("bad.txt", VALID));
add(Arguments.of("relative\\windows\\path\\resource.txt", VALID));
add(Arguments.of("C:\\absolute\\valid.txt", VALID));
add(Arguments.of("local/relative/path/is/invalid.txt", VALID));
add(Arguments.of("/absolute/local/path/is/invalid.txt", VALID));
add(Arguments.of("1;", VALID));
}
};
return values.stream();
}

@ParameterizedTest
@MethodSource("provideValuesTestToUri")
void testToUri(@NonNull String location, boolean expectedResult) {
boolean result = INVALID;
Path cwd = Paths.get("");
try {
URI uri = UriUtils.toUri(location, cwd.toAbsolutePath().toUri());
result = VALID;
System.out.println(String.format("%s -> %s", location, uri.toASCIIString()));
} catch (Exception ex) {
ex.printStackTrace();
}
assertEquals(result, expectedResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

import java.io.IOException;
import java.math.BigInteger;
import java.net.URI;
import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.util.List;
Expand Down Expand Up @@ -346,7 +347,7 @@ default IValidationResult validate(@NonNull INodeItem nodeItem) {
* if an error occurred when parsing the target as XML
*/
default IValidationResult validate(
@NonNull Path target,
@NonNull URI target,
@NonNull Format asFormat,
@NonNull IValidationSchemaProvider schemaProvider) throws IOException, SAXException {
IValidationResult retval;
Expand All @@ -362,7 +363,7 @@ default IValidationResult validate(
JSONObject json = YamlOperations.yamlToJson(YamlOperations.parseYaml(target));
assert json != null;
retval = new JsonSchemaContentValidator(schemaProvider.getJsonSchema())
.validate(json, ObjectUtils.notNull(target.toUri()));
.validate(json, ObjectUtils.notNull(target));
break;
default:
throw new UnsupportedOperationException("Unsupported format: " + asFormat.name());
Expand All @@ -385,7 +386,7 @@ default IValidationResult validate(
* @throws IOException
* if an error occurred while loading the document
*/
default IValidationResult validateWithConstraints(@NonNull Path target) throws IOException {
default IValidationResult validateWithConstraints(@NonNull URI target) throws IOException {
IBoundLoader loader = newBoundLoader();
loader.disableFeature(DeserializationFeature.DESERIALIZE_VALIDATE_CONSTRAINTS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

package gov.nist.secauto.metaschema.databind.io.yaml;

import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.yaml.snakeyaml.DumperOptions;
Expand All @@ -36,11 +38,9 @@
import org.yaml.snakeyaml.representer.Representer;
import org.yaml.snakeyaml.resolver.Resolver;

import java.io.BufferedReader;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.net.URI;
import java.util.Map;

import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -84,9 +84,9 @@ private YamlOperations() {
*/
@SuppressWarnings({ "unchecked", "null" })
@NonNull
public static Map<String, Object> parseYaml(Path target) throws IOException {
try (BufferedReader reader = Files.newBufferedReader(target.toAbsolutePath(), StandardCharsets.UTF_8)) {
return (Map<String, Object>) YAML_PARSER.load(reader);
public static Map<String, Object> parseYaml(URI target) throws IOException {
try (BufferedInputStream is = new BufferedInputStream(ObjectUtils.notNull(target.toURL().openStream()))) {
return (Map<String, Object>) YAML_PARSER.load(is);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ public IBindingContext getBindingContext() {
public <CLASS> CLASS newInstance() {
Class<?> clazz = getBoundClass();
try {
@SuppressWarnings("unchecked")
Constructor<CLASS> constructor
@SuppressWarnings("unchecked") Constructor<CLASS> constructor
= (Constructor<CLASS>) clazz.getDeclaredConstructor();
return ObjectUtils.notNull(constructor.newInstance());
} catch (NoSuchMethodException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.CustomCollectors;
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.IBindingContext.IValidationSchemaProvider;
import gov.nist.secauto.metaschema.databind.io.Format;
Expand All @@ -58,8 +59,9 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -91,7 +93,7 @@ public abstract class AbstractValidateContentCommand
private static final Option CONSTRAINTS_OPTION = ObjectUtils.notNull(
Option.builder("c")
.hasArg()
.argName("FILE")
.argName("URI")
.desc("additional constraint definitions")
.build());

Expand All @@ -116,38 +118,11 @@ public List<ExtraArgument> getExtraArguments() {
@SuppressWarnings("PMD.PreserveStackTrace") // intended
@Override
public void validateOptions(CallingContext callingContext, CommandLine cmdLine) throws InvalidArgumentException {
if (cmdLine.hasOption(CONSTRAINTS_OPTION)) {
String[] args = cmdLine.getOptionValues(CONSTRAINTS_OPTION);
for (String arg : args) {
Path constraint = Paths.get(arg);
if (!Files.exists(constraint)) {
throw new InvalidArgumentException(
"The provided external constraint file '" + constraint + "' does not exist.");
}
if (!Files.isRegularFile(constraint)) {
throw new InvalidArgumentException(
"The provided external constraint file '" + constraint + "' is not a file.");
}
if (!Files.isReadable(constraint)) {
throw new InvalidArgumentException(
"The provided external constraint file '" + constraint + "' is not readable.");
}
}
}

List<String> extraArgs = cmdLine.getArgList();
if (extraArgs.size() != 1) {
throw new InvalidArgumentException("The source to validate must be provided.");
}

Path source = Paths.get(extraArgs.get(0));
if (!Files.exists(source)) {
throw new InvalidArgumentException("The provided source file '" + source + "' does not exist.");
}
if (!Files.isReadable(source)) {
throw new InvalidArgumentException("The provided source file '" + source + "' is not readable.");
}

if (cmdLine.hasOption(AS_OPTION)) {
try {
String toFormatText = cmdLine.getOptionValue(AS_OPTION);
Expand Down Expand Up @@ -182,6 +157,7 @@ protected abstract IBindingContext getBindingContext(@NonNull Set<IConstraintSet
@SuppressWarnings("PMD.OnlyOneReturn") // readability
@Override
public ExitStatus execute() {
URI cwd = Paths.get("").toAbsolutePath().toUri();
CommandLine cmdLine = getCommandLine();

Set<IConstraintSet> constraintSets;
Expand All @@ -190,11 +166,10 @@ public ExitStatus execute() {
constraintSets = new LinkedHashSet<>();
String[] args = cmdLine.getOptionValues(CONSTRAINTS_OPTION);
for (String arg : args) {
Path constraintPath = Paths.get(arg);
assert constraintPath != null;
try {
constraintSets.add(constraintLoader.load(constraintPath));
} catch (IOException | MetaschemaException ex) {
URI constraintUri = ObjectUtils.requireNonNull(UriUtils.toUri(arg, cwd));
constraintSets.add(constraintLoader.load(constraintUri));
} catch (IOException | MetaschemaException | URISyntaxException ex) {
return ExitCode.IO_ERROR.exitMessage("Unable to load constraint set '" + arg + "'.").withThrowable(ex);
}
}
Expand All @@ -213,8 +188,16 @@ public ExitStatus execute() {
IBoundLoader loader = bindingContext.newBoundLoader();

List<String> extraArgs = cmdLine.getArgList();
@SuppressWarnings("null") Path source = resolvePathAgainstCWD(Paths.get(extraArgs.get(0)));
assert source != null;
// @SuppressWarnings("null")
String sourceName = extraArgs.get(0);
URI source;

try {
source = UriUtils.toUri(sourceName, cwd);
} catch (URISyntaxException ex) {
return ExitCode.IO_ERROR.exitMessage("Cannot load source '%s' as it is not a valid file or URI.")
.withThrowable(ex);
}

Format asFormat;
if (cmdLine.hasOption(AS_OPTION)) {
Expand Down Expand Up @@ -256,6 +239,12 @@ public ExitStatus execute() {
IValidationResult validationResult;
try {
validationResult = bindingContext.validate(source, asFormat, this);
} catch (FileNotFoundException ex) {
return ExitCode.IO_ERROR.exitMessage(String.format("Resource not found at '%s'", source)).withThrowable(ex);

} catch (UnknownHostException ex) {
return ExitCode.IO_ERROR.exitMessage(String.format("Unknown host for '%s'.", source)).withThrowable(ex);

} catch (IOException | SAXException ex) {
return ExitCode.PROCESSING_ERROR.exit().withThrowable(ex);
}
Expand Down
Loading

0 comments on commit 9360d97

Please sign in to comment.