Skip to content

Commit

Permalink
Added constraint formal-name and description to SARIF output
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Jun 21, 2024
1 parent aedb2c0 commit dd6890e
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion core/metaschema
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ protected T loadInternal(@NonNull URI resource, @NonNull Deque<URI> visitedResou

T retval = cache.get(resource);
if (retval == null) {
LOGGER.info("Loading module '{}'", resource);
LOGGER.info("Loading '{}'", resource);

try {
visitedResources.push(resource);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

package gov.nist.secauto.metaschema.modules.sarif;

import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine;
import gov.nist.secauto.metaschema.core.model.IResourceLocation;
import gov.nist.secauto.metaschema.core.model.constraint.ConstraintValidationFinding;
import gov.nist.secauto.metaschema.core.model.constraint.IConstraint;
Expand All @@ -46,6 +47,7 @@
import org.schemastore.json.sarif.x210.Location;
import org.schemastore.json.sarif.x210.LogicalLocation;
import org.schemastore.json.sarif.x210.Message;
import org.schemastore.json.sarif.x210.MultiformatMessageString;
import org.schemastore.json.sarif.x210.PhysicalLocation;
import org.schemastore.json.sarif.x210.Region;
import org.schemastore.json.sarif.x210.ReportingDescriptor;
Expand Down Expand Up @@ -252,10 +254,24 @@ public void write(@NonNull Path outputFile) throws IOException {
private ReportingDescriptor rule(RuleRecord rule) {
ReportingDescriptor retval = new ReportingDescriptor();
retval.setId(rule.getId());
String name = rule.getConstraint().getId();
IConstraint constraint = rule.getConstraint();
String name = constraint.getId();
if (name != null) {
retval.setName(name);
}

String formalName = constraint.getFormalName();
if (formalName != null) {
MultiformatMessageString text = new MultiformatMessageString();
text.setText(formalName);
retval.setShortDescription(text);
}
MarkupLine description = constraint.getDescription();
if (description != null) {
MultiformatMessageString text = new MultiformatMessageString();
text.setMarkdown(description.toMarkdown());
retval.setFullDescription(text);
}
return retval;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,6 @@ public ExitStatus execute() {
SarifValidationHandler sarifHandler = new SarifValidationHandler(source, version);
sarifHandler.addFindings(validationResult.getFindings());
sarifHandler.write(sarifFile);

LOGGER.error("The file '{}' is invalid.", source);
} catch (IOException ex) {
return ExitCode.PROCESSING_ERROR.exit().withThrowable(ex);
}
Expand All @@ -288,12 +286,15 @@ public ExitStatus execute() {
LoggingValidationHandler.instance().handleValidationResults(validationResult);
}

if (validationResult.isPassing() && !cmdLine.hasOption(CLIProcessor.QUIET_OPTION) && LOGGER.isInfoEnabled()) {
LOGGER.info("The file '{}' is valid.", source);
if (validationResult.isPassing()) {
if (LOGGER.isInfoEnabled()) {
LOGGER.info("The file '{}' is valid.", source);
}
} else if (LOGGER.isErrorEnabled()) {
LOGGER.error("The file '{}' is invalid.", source);
}

return (validationResult.isPassing() ? ExitCode.OK : ExitCode.FAIL).exit();
}

}
}

0 comments on commit dd6890e

Please sign in to comment.