Skip to content

Commit

Permalink
Parse and sync OVAL definitions in bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
HoussemNasri committed Oct 8, 2024
1 parent b8eb8be commit 07266bb
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@
import com.suse.oval.OVALCachingFactory;
import com.suse.oval.OVALCleaner;
import com.suse.oval.OsFamily;
import com.suse.oval.OvalParser;
import com.suse.oval.ShallowSystemPackage;
import com.suse.oval.config.OVALConfigLoader;
import com.suse.oval.ovaldownloader.OVALDownloadResult;
import com.suse.oval.ovaldownloader.OVALDownloader;
import com.suse.oval.ovaltypes.OvalRootType;
import com.suse.oval.parser.OVALResources;
import com.suse.oval.parser.OvalParser;
import com.suse.oval.vulnerablepkgextractor.VulnerablePackage;

import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -410,9 +411,18 @@ private static void syncOVALForProduct(OVALProduct product, OVALDownloader ovalD
* Extracts OVAL metadata from the given {@code ovalFile}, clean it and save it to the database.
* */
private static void extractAndSaveOVALData(OVALProduct product, File ovalFile) {
OvalRootType ovalRoot = new OvalParser().parse(ovalFile);
OVALCleaner.cleanup(ovalRoot, product.getOsFamily(), product.getOsVersion());
OVALCachingFactory.savePlatformsVulnerablePackages(ovalRoot);
OvalParser ovalParser = new OvalParser();
OVALResources ovalResources = ovalParser.parseResources(ovalFile);
ovalParser.parseDefinitionsInBulk(ovalFile, (definitionsBulk) -> {
OvalRootType ovalRoot = new OvalRootType();
ovalRoot.setDefinitions(definitionsBulk);
ovalRoot.setTests(ovalResources.getTests());
ovalRoot.setObjects(ovalResources.getObjects());
ovalRoot.setStates(ovalResources.getStates());

OVALCleaner.cleanup(ovalRoot, product.getOsFamily(), product.getOsVersion());
OVALCachingFactory.savePlatformsVulnerablePackages(ovalRoot);
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
import com.suse.oval.OVALCachingFactory;
import com.suse.oval.OVALCleaner;
import com.suse.oval.OsFamily;
import com.suse.oval.OvalParser;
import com.suse.oval.ovaltypes.OvalRootType;
import com.suse.oval.parser.OvalParser;

import org.junit.jupiter.api.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static com.suse.manager.webui.utils.SparkApplicationHelper.withCsrfToken;
import static com.suse.manager.webui.utils.SparkApplicationHelper.withUser;
import static com.suse.manager.webui.utils.SparkApplicationHelper.withUserPreferences;

import static spark.Spark.get;
import static spark.Spark.post;

Expand All @@ -32,6 +31,7 @@
import com.redhat.rhn.manager.audit.PatchStatus;
import com.redhat.rhn.manager.audit.UnknownCVEIdentifierException;
import com.redhat.rhn.manager.rhnset.RhnSetDecl;

import com.suse.manager.webui.utils.gson.ResultJson;

import com.google.gson.Gson;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

package com.suse.oval.exceptions;

import com.suse.oval.parser.OvalParser;

/**
* A runtime exception thrown by {@link com.suse.oval.OvalParser} when it encounters errors while parsing an OVAL file.
* A runtime exception thrown by {@link OvalParser} when it encounters errors while parsing an OVAL file.
* */
public class OvalParserException extends RuntimeException {

Expand Down
7 changes: 0 additions & 7 deletions java/code/src/com/suse/oval/manager/OVALResourcesCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

package com.suse.oval.manager;

import com.suse.oval.parser.OVALResourcesResult;
import com.suse.oval.ovaltypes.ObjectType;
import com.suse.oval.ovaltypes.OvalRootType;
import com.suse.oval.ovaltypes.StateType;
Expand Down Expand Up @@ -43,12 +42,6 @@ public OVALResourcesCache(OvalRootType rootType) {
this.objectManager = new OvalObjectManager(rootType.getObjects());
}

public OVALResourcesCache(OVALResourcesResult resourcesResult) {
this.stateManager = new OvalStateManager(resourcesResult.getStates());
this.testManager = new OvalTestManager(resourcesResult.getTests());
this.objectManager = new OvalObjectManager(resourcesResult.getObjects());
}

/**
* Looks up an OVAL test with an id of {@code testId}
*
Expand Down
33 changes: 33 additions & 0 deletions java/code/src/com/suse/oval/parser/OVALDefinitionsBulkHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2023 SUSE LLC
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/

package com.suse.oval.parser;

import com.suse.oval.ovaltypes.DefinitionType;

import java.util.List;

/**
* A handler class to apply an operation (.e.g. write them to database), on every bulk/list of parsed OVAL definitions.
* */
@FunctionalInterface
public interface OVALDefinitionsBulkHandler {
/**
* Handles the given bulk/list of OVAL definitions.
*
* @param bulk list of OVAL definitions.
* */
void handle(List<DefinitionType> bulk);
}
53 changes: 53 additions & 0 deletions java/code/src/com/suse/oval/parser/OVALResources.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2023 SUSE LLC
*
* This software is licensed to you under the GNU General Public License,
* version 2 (GPLv2). There is NO WARRANTY for this software, express or
* implied, including the implied warranties of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
* along with this software; if not, see
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
*
* Red Hat trademarks are not licensed under GPLv2. No permission is
* granted to use or replicate Red Hat trademarks that are incorporated
* in this software or its documentation.
*/

package com.suse.oval.parser;

import com.suse.oval.ovaltypes.ObjectType;
import com.suse.oval.ovaltypes.StateType;
import com.suse.oval.ovaltypes.TestType;

import java.util.ArrayList;
import java.util.List;

public class OVALResources {
private List<ObjectType> objects = new ArrayList<>();
private List<StateType> states = new ArrayList<>();
private List<TestType> tests = new ArrayList<>();

public List<ObjectType> getObjects() {
return objects;
}

public List<StateType> getStates() {
return states;
}

public List<TestType> getTests() {
return tests;
}

public void setObjects(List<ObjectType> objectsIn) {
this.objects = objectsIn;
}

public void setStates(List<StateType> statesIn) {
this.states = statesIn;
}

public void setTests(List<TestType> testsIn) {
this.tests = testsIn;
}
}
Loading

0 comments on commit 07266bb

Please sign in to comment.