From 7d8df398e78676dcc71f41f31c12597e2bc0e432 Mon Sep 17 00:00:00 2001 From: chubtub <43381989+chubtub@users.noreply.github.com> Date: Wed, 6 Oct 2021 15:00:23 -0400 Subject: [PATCH] Controller class for returning TPM Event data to jsp --- .../controllers/TpmEventsPageController.java | 183 ++++++++++++++++++ .../main/webapp/WEB-INF/jsp/tpm-events.jsp | 19 +- .../hirs/persist/ReferenceDigestManager.java | 2 +- 3 files changed, 185 insertions(+), 19 deletions(-) create mode 100644 HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/TpmEventsPageController.java diff --git a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/TpmEventsPageController.java b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/TpmEventsPageController.java new file mode 100644 index 00000000..d5f1f174 --- /dev/null +++ b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/TpmEventsPageController.java @@ -0,0 +1,183 @@ + +package hirs.attestationca.portal.page.controllers; + +import hirs.FilteredRecordsList; +import hirs.attestationca.portal.datatables.DataTableInput; +import hirs.attestationca.portal.datatables.DataTableResponse; +import hirs.attestationca.portal.datatables.OrderedListQueryDataTableAdapter; +import hirs.attestationca.portal.page.Page; +import hirs.attestationca.portal.page.PageController; +import hirs.attestationca.portal.page.params.NoPageParams; +import hirs.data.persist.ReferenceDigestRecord; +import hirs.data.persist.ReferenceDigestValue; +import hirs.persist.DBReferenceDigestManager; +import hirs.persist.DBReferenceEventManager; +import hirs.persist.ReferenceDigestManager; +import hirs.persist.ReferenceEventManager; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; + +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; + +/** + * Controller for the TPM Events page. + */ +@Controller +@RequestMapping("/tpm-events") +public class TpmEventsPageController + extends PageController { + + private static final String BIOS_RELEASE_DATE_FORMAT = "yyyy-MM-dd"; + private static final String LOG_FILE_PATTERN = "([^\\s]+(\\.(?i)(rimpcr|rimel|bin|log))$)"; + + private final BiosDateValidator biosValidator; + private final ReferenceDigestManager referenceDigestManager; + private final ReferenceEventManager referenceEventManager; + private static final Logger LOGGER + = LogManager.getLogger(TpmEventsPageController.class); + + /** + * This class was created for the purposes of avoiding findbugs message: As + * the JavaDoc states, DateFormats are inherently unsafe for multi-threaded + * use. The detector has found a call to an instance of DateFormat that has + * been obtained via a static field. This looks suspicious. + *

+ * This class can have uses elsewhere but for now it will remain here. + */ + private static final class BiosDateValidator { + + private final String dateFormat; + + /** + * Default constructor that sets the format to parse against. + * + * @param dateFormat + */ + public BiosDateValidator(final String dateFormat) { + this.dateFormat = dateFormat; + } + + /** + * Validates a date by attempting to parse based on format provided. + * + * @param date string of the given date + * @return true if the format matches + */ + public boolean isValid(final String date) { + DateFormat validFormat = new SimpleDateFormat(this.dateFormat); + boolean result = true; + validFormat.setLenient(false); + + try { + validFormat.parse(date); + } catch (ParseException pEx) { + result = false; + } + + return result; + } + } + + /** + * Constructor providing the Page's display and routing specification. + * + * @param referenceDigestManager the ReferenceDigestManager object + * @param referenceEventManager the referenceEventManager object + */ + @Autowired + public TpmEventsPageController( + final DBReferenceDigestManager referenceDigestManager, + final DBReferenceEventManager referenceEventManager) { + super(Page.TPM_EVENTS); + this.referenceDigestManager = referenceDigestManager; + this.referenceEventManager = referenceEventManager; + this.biosValidator = new BiosDateValidator(BIOS_RELEASE_DATE_FORMAT); + } + + /** + * Returns the filePath for the view and the data model for the page. + * + * @param params The object to map url parameters into. + * @param model The data model for the request. Can contain data from + * redirect. + * @return the filePath for the view and data model for the page. + */ + @Override + public ModelAndView initPage(final NoPageParams params, + final Model model) { + return getBaseModelAndView(); + } + + /** + * Returns the list of TPM Events using the data table input for paging, ordering, + * and filtering. + * + * @param input the data tables input + * @return the data tables response, including the result set and paging + * information + */ + @ResponseBody + @RequestMapping(value = "/list", + produces = MediaType.APPLICATION_JSON_VALUE, + method = RequestMethod.GET) + public DataTableResponse getTableData( + final DataTableInput input) { + LOGGER.debug("Handling request for summary list: " + input); + + String orderColumnName = input.getOrderColumnName(); + LOGGER.debug("Ordering on column: " + orderColumnName); + + LOGGER.info("Querying with the following datatableinput: " + input.toString()); + FilteredRecordsList referenceDigestRecords + = OrderedListQueryDataTableAdapter.getOrderedList( + ReferenceDigestRecord.class, + referenceDigestManager, + input, orderColumnName); + FilteredRecordsList> + mappedRecordValues = mapRecordToValues(referenceDigestRecords); + + LOGGER.info("Returning list mapping: " + Arrays.toString(mappedRecordValues.toArray())); + return new DataTableResponse<>(referenceDigestRecords, input); + } + + /** + * This method returns a mapping of ReferenceDigestRecord to ReferenceDigestValue objects. + * + * @param records the list of ReferenceDigestRecords + * @return the collection of HashMap mappings + */ + private FilteredRecordsList> + mapRecordToValues(final FilteredRecordsList records) { + + LOGGER.info("Mapping RDRs and RDVs"); + FilteredRecordsList> filteredList = + new FilteredRecordsList<>(); + HashMap mappingRecordToValues = + new HashMap<>(); + for (ReferenceDigestRecord record : records) { + List values = referenceEventManager.getValuesByRecordId(record); + if (values != null && !values.isEmpty()) { + mappingRecordToValues.put(record, values.get(0)); + } else { + mappingRecordToValues.put(record, null); + } + filteredList.add(new HashMap<>(mappingRecordToValues)); + mappingRecordToValues.clear(); + } + + return filteredList; + } +} diff --git a/HIRS_AttestationCAPortal/src/main/webapp/WEB-INF/jsp/tpm-events.jsp b/HIRS_AttestationCAPortal/src/main/webapp/WEB-INF/jsp/tpm-events.jsp index 0436aa17..a523247a 100644 --- a/HIRS_AttestationCAPortal/src/main/webapp/WEB-INF/jsp/tpm-events.jsp +++ b/HIRS_AttestationCAPortal/src/main/webapp/WEB-INF/jsp/tpm-events.jsp @@ -22,11 +22,6 @@ Manufacturer Model - Type - Index - Digest - Details - Base RIM Support RIM @@ -39,19 +34,7 @@ var columns = [ {data: 'manufacturer'}, {data: 'model'}, - {data: 'supportRim'}, - { - data: 'id', - orderable: false, - searchable: false, - render: function(data, type, full, meta) { - // Set up a delete icon with link to handleDeleteRequest(). - // sets up a hidden input field containing the ID which is - // used as a parameter to the REST POST call to delete - var html = ''; - return html; - } - } + {data: 'supportRim'} ]; //Set data tables diff --git a/HIRS_Utils/src/main/java/hirs/persist/ReferenceDigestManager.java b/HIRS_Utils/src/main/java/hirs/persist/ReferenceDigestManager.java index 83b4d67a..952ddfbd 100644 --- a/HIRS_Utils/src/main/java/hirs/persist/ReferenceDigestManager.java +++ b/HIRS_Utils/src/main/java/hirs/persist/ReferenceDigestManager.java @@ -10,7 +10,7 @@ import java.util.UUID; * This class facilitates the persistence of {@link hirs.data.persist.ReferenceDigestRecord}s * including storage, retrieval, and deletion. */ -public interface ReferenceDigestManager { +public interface ReferenceDigestManager extends OrderedListQuerier{ /** * Persists a new Reference Digest.