Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct OSCAL version handling in resolved profile #21

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/gov/nist/secauto/oscal/lib/OscalUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import java.util.regex.Pattern;

public final class OscalUtils {
public static final String OSCAL_VERSION = "1.0.0";
public static final String OSCAL_VERSION = "1.0.4";
private static final Pattern INTERNAL_REFERENCE_FRAGMENT_PATTERN = Pattern.compile("^#(.+)$");

private OscalUtils() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,14 @@ public void resolve(@NotNull ResolutionData data) throws IOException, IllegalSta

Metadata metadata = new Metadata();
metadata.setTitle(profile.getMetadata().getTitle());
metadata.setVersion(profile.getMetadata().getVersion());
metadata.setOscalVersion(OscalUtils.OSCAL_VERSION);

if (profile.getMetadata().getVersion() != null) {
metadata.setVersion(profile.getMetadata().getVersion());
}

// metadata.setOscalVersion(OscalUtils.OSCAL_VERSION);
metadata.setOscalVersion(data.getProfile().getMetadata().getOscalVersion());
aj-stein-nist marked this conversation as resolved.
Show resolved Hide resolved

metadata.setLastModified(ZonedDateTime.now(ZoneOffset.UTC));

metadata.addProp(Property.builder("resolution-tool").value("libOSCAL-Java").build());
Expand All @@ -146,14 +152,6 @@ public void resolve(@NotNull ResolutionData data) throws IOException, IllegalSta

resolvedCatalog.setMetadata(metadata);

Version version;
if (profile.getMetadata().getVersion() != null) {
version = VersionUtil.parseVersion(profile.getMetadata().getVersion(), null, null);
} else {
version = Version.unknownVersion();
}
metadata.setOscalVersion(version.toString());

resolveImports(data);
handleMerge(data);
handleReferences(data);
Expand Down Expand Up @@ -259,34 +257,34 @@ private void resolveImport(@NotNull ProfileImport profileImport, @NotNull Resolu
URI poppedUri = ObjectUtils.notNull(importHistory.pop());
assert document.getDocumentUri().equals(poppedUri);

Version catalogVersion = VersionUtil.parseVersion(importedCatalog.getMetadata().getOscalVersion(), null, null);
Version importOscalVersion = VersionUtil.parseVersion(importedCatalog.getMetadata().getOscalVersion(), null, null);

Catalog resolvingCatalog = data.getCatalog();
Version resolvingCatalogVersion
= VersionUtil.parseVersion(resolvingCatalog.getMetadata().getOscalVersion(), null, null);
Catalog resolvedCatalog = data.getCatalog();
Version resolvedCatalogVersion
= VersionUtil.parseVersion(resolvedCatalog.getMetadata().getOscalVersion(), null, null);

if (catalogVersion.compareTo(resolvingCatalogVersion) > 0) {
resolvingCatalog.getMetadata().setOscalVersion(catalogVersion.toString());
if (importOscalVersion.compareTo(resolvedCatalogVersion) > 0) {
resolvedCatalog.getMetadata().setOscalVersion(importOscalVersion.toString());
}

for (Parameter param : CollectionUtil.listOrEmpty(importedCatalog.getParams())) {
resolvingCatalog.addParam(param);
resolvedCatalog.addParam(param);
}
for (Control control : CollectionUtil.listOrEmpty(importedCatalog.getControls())) {
resolvingCatalog.addControl(control);
resolvedCatalog.addControl(control);
}
for (CatalogGroup group : CollectionUtil.listOrEmpty(importedCatalog.getGroups())) {
resolvingCatalog.addGroup(group);
resolvedCatalog.addGroup(group);
}

// TODO: copy roles, parties, and locations with prop name:keep and any referenced
// TODO: handle resources properly
BackMatter backMatter = importedCatalog.getBackMatter();
if (backMatter != null && backMatter.getResources() != null) {
BackMatter resolvingBackMatter = resolvingCatalog.getBackMatter();
BackMatter resolvingBackMatter = resolvedCatalog.getBackMatter();
if (resolvingBackMatter == null) {
resolvingBackMatter = new BackMatter();
resolvingCatalog.setBackMatter(resolvingBackMatter);
resolvedCatalog.setBackMatter(resolvingBackMatter);
}

for (Resource resource : backMatter.getResources()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

package gov.nist.secauto.oscal.lib.profile.resolver;

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

import gov.nist.secauto.metaschema.binding.io.BindingException;
Expand Down Expand Up @@ -60,6 +62,7 @@
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.nio.file.Path;
import java.time.ZoneOffset;
import java.util.Stack;

Expand Down Expand Up @@ -98,6 +101,15 @@ public static ProfileResolver getProfileResolver() {
return profileResolver;
}

private Catalog resolveProfile(@NotNull Path profileFile)
throws FileNotFoundException, BindingException, IOException {
Profile profile = OscalBindingContext.instance().loadProfile(profileFile);
ProfileResolver.ResolutionData data
= new ProfileResolver.ResolutionData(profile, ObjectUtils.notNull(profileFile.toUri()), new Stack<>());
getProfileResolver().resolve(data);
return data.getCatalog();
}

private Catalog resolveProfile(@NotNull File profileFile)
throws FileNotFoundException, BindingException, IOException {
Profile profile = OscalBindingContext.instance().loadProfile(profileFile);
Expand Down Expand Up @@ -178,4 +190,14 @@ void testCircularLink() throws IllegalStateException, IOException, BindingExcept

MatcherAssert.assertThat(exceptionThrown.getCause(), CoreMatchers.instanceOf(ImportCycleException.class));
}


@Test
void testOscalVersion() throws IllegalStateException, IOException, BindingException {
Path profileFile = Path.of("src/test/resources/test-oscal-version-profile.xml");
Catalog catalog = resolveProfile(profileFile);
assertNotNull(catalog);
assertEquals("1.0.4", catalog.getMetadata().getOscalVersion());
}

}
20 changes: 20 additions & 0 deletions src/test/resources/test-oscal-version-profile.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml-model href="../../../oscal/src/specification/profile-resolution/example-checkup.sch" type="application/xml" schematypens="http://purl.oclc.org/dsdl/schematron"?>
<!-- Modified by conversion XSLT 2021-04-05T11:22:07.701-04:00 - RC2 OSCAL becomes RC3 OSCAL -->
<profile xmlns="http://csrc.nist.gov/ns/oscal/1.0"
uuid="cb1ec926-3441-458f-8cce-ea11308c9d37">
<metadata>
<title>Test Profile</title>
<last-modified>2020-05-30T14:39:35.84-04:00</last-modified>
<version>2.3.4</version>
<oscal-version>1.0.4</oscal-version>
</metadata>
<import href="../../../oscal/src/specifications/profile-resolution/profile-resolution-examples/catalogs/abc-simple_catalog.xml">
<include-controls with-child-controls="yes">
<with-id>a1</with-id>
<with-id>b1</with-id>
<with-id>c1</with-id>
<with-id>c3</with-id>
</include-controls>
</import>
</profile>