diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/service/CertificateService.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/service/CertificateService.java index 8e708eb9..03041b3e 100644 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/service/CertificateService.java +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/persist/service/CertificateService.java @@ -9,6 +9,7 @@ import hirs.attestationca.persist.entity.userdefined.certificate.PlatformCredent import hirs.attestationca.persist.entity.userdefined.certificate.attributes.ComponentIdentifier; import hirs.attestationca.persist.entity.userdefined.certificate.attributes.V2.ComponentIdentifierV2; import jakarta.persistence.EntityManager; +import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.TypedQuery; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; @@ -65,11 +66,11 @@ public class CertificateService { * @param generic entity class * @return page full of the generic certificates. */ - public Page findBySearchableColumnsAndArchiveFlag(Class entityClass, - List searchableColumns, - String searchText, - Boolean archiveFlag, - Pageable pageable) { + public Page findBySearchableColumnsAndArchiveFlag(Class entityClass, + List searchableColumns, + String searchText, + Boolean archiveFlag, + Pageable pageable) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery query = criteriaBuilder.createQuery(entityClass); Root certificate = query.from(entityClass); @@ -105,15 +106,17 @@ public class CertificateService { } /** - * @param uuid - * @return + * Attempts to find a certificate whose uuid matches the provided uuid. + * + * @param uuid certificate uuid + * @return certificate */ public Certificate findCertificate(UUID uuid) { return this.certificateRepository.getCertificate(uuid); } /** - * Stored the given certificate in the database. + * Stores the given certificate in the database. * * @param certificateType String containing the certificate type * @param fileName contain the name of the file of the certificate to @@ -238,17 +241,19 @@ public class CertificateService { * @param successMessages contains any success messages that will be displayed on the page * @param errorMessages contains any error messages that will be displayed on the page */ - public void deleteCertificate(UUID uuid, String certificateType, + public void deleteCertificate(final UUID uuid, + final String certificateType, final List successMessages, final List errorMessages) { - Certificate certificate = certificateRepository.getCertificate(uuid); + Certificate certificate = findCertificate(uuid); if (certificate == null) { // Use the term "record" here to avoid user confusion b/t cert and cred String notFoundMessage = "Unable to locate record with ID: " + uuid; errorMessages.add(notFoundMessage); log.warn(notFoundMessage); + throw new EntityNotFoundException(notFoundMessage); } else { if (certificateType.equals(PLATFORM_CREDENTIALS)) { PlatformCredential platformCertificate = (PlatformCredential) certificate; diff --git a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/datatables/Column.java b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/datatables/Column.java index 95f303a2..8119e0f2 100644 --- a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/datatables/Column.java +++ b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/datatables/Column.java @@ -27,7 +27,6 @@ public class Column { @NotBlank private String data; - /** * Column's name. * @@ -35,7 +34,7 @@ public class Column { */ @NotBlank private String name; - + /** * Flag to indicate if this column is searchable (true) or not (false). * diff --git a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/datatables/Search.java b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/datatables/Search.java index ae8b6d38..78618d70 100644 --- a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/datatables/Search.java +++ b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/datatables/Search.java @@ -22,9 +22,8 @@ public class Search { */ @NotNull private String value = ""; - /** - * true if the global filter should be treated as a regular expression for advanced searching, + * True if the global filter should be treated as a regular expression for advanced searching, * false otherwise. Note that normally server-side processing scripts will not perform regular * expression searching for performance reasons on large data sets, * but it is technically possible and at the discretion of your script. diff --git a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/PageMessages.java b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/PageMessages.java index 437070b7..3559ee00 100644 --- a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/PageMessages.java +++ b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/PageMessages.java @@ -1,43 +1,36 @@ package hirs.attestationca.portal.page; +import lombok.Getter; + import java.util.ArrayList; -import java.util.Collections; import java.util.List; /** * Encapsulates error, success, and informational messages to display on a page. */ +@Getter public class PageMessages { - private final List error = new ArrayList<>(); - private final List success = new ArrayList<>(); - private final List info = new ArrayList<>(); - - /** - * Returns the list of error messages. - * - * @return the list of error messages. - */ - public List getError() { - return Collections.unmodifiableList(error); - } + private final List errorMessages = new ArrayList<>(); + private final List successMessages = new ArrayList<>(); + private final List infoMessages = new ArrayList<>(); /** * Adds an error message. * * @param error the error message to add */ - public void addError(final String error) { - this.error.add(error); + public void addErrorMessage(final String error) { + this.errorMessages.add(error); } /** - * Returns the list of success messages. + * Adds multiple error messages. * - * @return the list of success messages. + * @param multipleErrors list of error messages */ - public List getSuccess() { - return Collections.unmodifiableList(success); + public void addErrorMessages(final List multipleErrors) { + this.errorMessages.addAll(multipleErrors); } /** @@ -45,17 +38,17 @@ public class PageMessages { * * @param success the success message to add */ - public void addSuccess(final String success) { - this.success.add(success); + public void addSuccessMessage(final String success) { + this.successMessages.add(success); } /** - * Returns the list of informational messages. + * Adds multiple success messages. * - * @return the list of informational messages. + * @param multipleSuccessMessages list of success messages to add */ - public List getInfo() { - return Collections.unmodifiableList(info); + public void addSuccessMessages(final List multipleSuccessMessages) { + this.successMessages.addAll(multipleSuccessMessages); } /** @@ -63,8 +56,16 @@ public class PageMessages { * * @param info the informational message to add */ - public void addInfo(final String info) { - this.info.add(info); + public void addInfoMessage(final String info) { + this.infoMessages.add(info); } + /** + * Adds multiple informational messages. + * + * @param multipleInfoMessages list of informational messages to add + */ + public void addInfoMessages(final List multipleInfoMessages) { + this.errorMessages.addAll(multipleInfoMessages); + } } diff --git a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/CertificateDetailsPageController.java b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/CertificateDetailsPageController.java index 0853d7df..7e29388d 100644 --- a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/CertificateDetailsPageController.java +++ b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/CertificateDetailsPageController.java @@ -77,12 +77,12 @@ public class CertificateDetailsPageController extends PageController { produces = MediaType.APPLICATION_JSON_VALUE) public DataTableResponse> getTableData( final DataTableInput input) { - log.debug("Handling request for device list"); + log.debug("Receiving request to for device list"); String orderColumnName = input.getOrderColumnName(); log.debug("Ordering on column: {}", orderColumnName); diff --git a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/EndorsementCredentialPageController.java b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/EndorsementCredentialPageController.java index df7ed854..ae326083 100644 --- a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/EndorsementCredentialPageController.java +++ b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/EndorsementCredentialPageController.java @@ -1,6 +1,5 @@ package hirs.attestationca.portal.page.controllers; -import hirs.attestationca.persist.DBManagerException; import hirs.attestationca.persist.FilteredRecordsList; import hirs.attestationca.persist.entity.manager.EndorsementCredentialRepository; import hirs.attestationca.persist.entity.userdefined.Certificate; @@ -13,6 +12,7 @@ import hirs.attestationca.portal.page.Page; import hirs.attestationca.portal.page.PageController; import hirs.attestationca.portal.page.PageMessages; import hirs.attestationca.portal.page.params.NoPageParams; +import jakarta.persistence.EntityNotFoundException; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; @@ -44,6 +44,9 @@ import java.util.UUID; import java.util.stream.Collectors; import java.util.zip.ZipOutputStream; +/** + * Controller for the Endorsement Key Credentials page. + */ @Log4j2 @Controller @RequestMapping("/HIRS_AttestationCAPortal/portal/certificate-request/endorsement-key-credentials") @@ -54,6 +57,12 @@ public class EndorsementCredentialPageController extends PageController getEndorsementCredentialsTableData( final DataTableInput input) { - - log.debug("Handling list request for endorsement credentials: {}", input); + log.info("Receiving request to display list of endorsement credentials"); + log.debug("Request received a datatable input object for the endorsement credentials page: {}", + input); // attempt to get the column property based on the order index. String orderColumnName = input.getOrderColumnName(); @@ -132,10 +142,10 @@ public class EndorsementCredentialPageController extends PageController model = new HashMap<>(); PageMessages messages = new PageMessages(); - List errorMessages = new ArrayList<>(); - List successMessages = new ArrayList<>(); - for (MultipartFile file : files) { + List errorMessages = new ArrayList<>(); + List successMessages = new ArrayList<>(); + //Parse endorsement credential EndorsementCredential parsedEndorsementCredential = parseEndorsementCredential(file, messages); @@ -240,6 +261,9 @@ public class EndorsementCredentialPageController extends PageController model = new HashMap<>(); PageMessages messages = new PageMessages(); + List successMessages = new ArrayList<>(); + List errorMessages = new ArrayList<>(); try { - List successMessages = new ArrayList<>(); - List errorMessages = new ArrayList<>(); - UUID uuid = UUID.fromString(id); this.certificateService.deleteCertificate(uuid, ENDORSEMENT_CREDENTIALS, successMessages, errorMessages); - } catch (IllegalArgumentException ex) { - String uuidError = "Failed to parse ID from: " + id; - messages.addError(uuidError); - log.error(uuidError, ex); - } catch (DBManagerException ex) { - String dbError = "Failed to archive cert: " + id; - messages.addError(dbError); - log.error(dbError, ex); + messages.addSuccessMessages(successMessages); + messages.addErrorMessages(errorMessages); + } catch (Exception exception) { + final String errorMessage = "An exception was thrown while attempting to delete" + + " endorsement credential"; + messages.addErrorMessage(errorMessage); + log.error(errorMessage, exception); } model.put(MESSAGES_ATTRIBUTE, messages); @@ -293,9 +315,10 @@ public class EndorsementCredentialPageController extends PageController findSearchableColumnsNames(List columns) { + private List findSearchableColumnsNames(final List columns) { // Retrieve all searchable columns and collect their names into a list of strings. return columns.stream().filter(Column::isSearchable).map(Column::getName) @@ -310,7 +333,8 @@ public class EndorsementCredentialPageController extends PageController getIDevIdCertificatesTableData( final DataTableInput input) { - log.debug("Handling list request for idevid certificates: {}", input); + log.info("Receiving request to display list of idevid certificates"); + log.debug("Request received a datatable input object for the idevid certificates page: {}", input); // attempt to get the column property based on the order index. String orderColumnName = input.getOrderColumnName(); @@ -126,15 +136,15 @@ public class IDevIdCertificatePageController extends PageController(records, input); } /** - * Handles request to download the IDevId certificate by writing it to the response stream + * Processes request to download the IDevId certificate by writing it to the response stream * for download. * - * @param id the UUID of the cert to download + * @param id the UUID of the idevid certificate to download * @param response the response object (needed to update the header with the * file name) * @throws IOException when writing to response output stream @@ -144,41 +154,48 @@ public class IDevIdCertificatePageController extends PageController model = new HashMap<>(); PageMessages messages = new PageMessages(); - List errorMessages = new ArrayList<>(); - List successMessages = new ArrayList<>(); - for (MultipartFile file : files) { + List errorMessages = new ArrayList<>(); + List successMessages = new ArrayList<>(); + //Parse IDevId Certificate IDevIDCertificate parsedIDevIDCertificate = parseIDevIDCertificate(file, messages); @@ -240,6 +259,9 @@ public class IDevIdCertificatePageController extends PageController model = new HashMap<>(); PageMessages messages = new PageMessages(); - try { - List successMessages = new ArrayList<>(); - List errorMessages = new ArrayList<>(); + List successMessages = new ArrayList<>(); + List errorMessages = new ArrayList<>(); + try { UUID uuid = UUID.fromString(id); this.certificateService.deleteCertificate(uuid, IDEVID_CERTIFICATE, successMessages, errorMessages); - } catch (IllegalArgumentException ex) { - String uuidError = "Failed to parse ID from idevid certificate: " + id; - messages.addError(uuidError); - log.error(uuidError, ex); - } catch (DBManagerException ex) { - String dbError = "Failed to archive idevid certificate: " + id; - messages.addError(dbError); - log.error(dbError, ex); + messages.addSuccessMessages(successMessages); + messages.addErrorMessages(errorMessages); + } catch (Exception exception) { + final String errorMessage = "An exception was thrown while attempting to delete" + + " the specified idevid certificate"; + messages.addErrorMessage(errorMessage); + log.error(errorMessage, exception); } model.put(MESSAGES_ATTRIBUTE, messages); @@ -293,9 +314,10 @@ public class IDevIdCertificatePageController extends PageController findSearchableColumnsNames(List columns) { + private List findSearchableColumnsNames(final List columns) { // Retrieve all searchable columns and collect their names into a list of strings. return columns.stream().filter(Column::isSearchable).map(Column::getName) @@ -309,8 +331,8 @@ public class IDevIdCertificatePageController extends PageController { private final IssuedCertificateRepository issuedCertificateRepository; private final CertificateService certificateService; + /** + * Constructor for the Issued Attestation Certificate page. + * + * @param issuedCertificateRepository issuedCertificateRepository + * @param certificateService certificateService + */ @Autowired public IssuedCertificateController( final IssuedCertificateRepository issuedCertificateRepository, @@ -67,7 +76,7 @@ public class IssuedCertificateController extends PageController { * @param params The object to map url parameters into. * @param model The data model for the request. Can contain data from * redirect. - * @return the path for the view and data model for the page. + * @return the path for the view and data model for the Issued Attestation Certificate page. */ @RequestMapping public ModelAndView initPage( @@ -76,15 +85,20 @@ public class IssuedCertificateController extends PageController { } /** - * @param input - * @return + * Processes request to retrieve the collection of issued attestation certificates + * that will be displayed on the issued certificates page. + * + * @param input data table input received from the front-end + * @return data table of issued certificates */ @ResponseBody @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE) public DataTableResponse getIssuedCertificatesTableData( final DataTableInput input) { - log.debug("Handling list request for issued certificates: {}", input); + log.info("Receiving request to display list of issued attestation certificates"); + log.debug("Request received a datatable input object for the issued attestation certificate page: " + + "{}", input); // attempt to get the column property based on the order index. String orderColumnName = input.getOrderColumnName(); @@ -121,12 +135,12 @@ public class IssuedCertificateController extends PageController { records.setRecordsFiltered(issuedCertificateRepository.findByArchiveFlag(false).size()); - log.debug("Returning the size of the list of issued certificates: {}", records.size()); + log.info("Returning the size of the list of issued certificates: {}", records.size()); return new DataTableResponse<>(records, input); } /** - * Handles request to download the issued attestation certificate by writing it to the response stream + * Processes request to download the issued attestation certificate by writing it to the response stream * for download. * * @param id the UUID of the issued attestation certificate to download @@ -139,42 +153,53 @@ public class IssuedCertificateController extends PageController { @RequestParam final String id, final HttpServletResponse response) throws IOException { - log.info("Handling request to download issued certificate id {}", id); + log.info("Receiving request to download issued certificate id {}", id); try { UUID uuid = UUID.fromString(id); Certificate certificate = this.certificateService.findCertificate(uuid); if (certificate == null) { - // Use the term "record" here to avoid user confusion b/t cert and cred - String notFoundMessage = "Unable to locate record with ID: " + uuid; - log.warn(notFoundMessage); - // send a 404 error when invalid certificate - response.sendError(HttpServletResponse.SC_NOT_FOUND); - } else if (certificate instanceof IssuedAttestationCertificate uploadedIssuedCertificate) { - String fileName = "filename=\"" + IssuedAttestationCertificate.class.getSimpleName() - + "_" - + uploadedIssuedCertificate.getSerialNumber() - + ".cer\""; - - // Set filename for download. - response.setHeader("Content-Disposition", "attachment;" + fileName); - response.setContentType("application/octet-stream"); - - // write cert to output stream - response.getOutputStream().write(certificate.getRawBytes()); + final String errorMessage = + "Unable to locate issued attestation certificate record with ID " + uuid; + log.warn(errorMessage); + throw new EntityNotFoundException(errorMessage); + } else if (!(certificate instanceof IssuedAttestationCertificate)) { + final String errorMessage = + "Unable to cast the found certificate to an issued attestation certificate " + + "object"; + log.warn(errorMessage); + throw new ClassCastException(errorMessage); } - } catch (IllegalArgumentException ex) { - String uuidError = "Failed to parse ID from: " + id; - log.error(uuidError, ex); - // send a 404 error when invalid certificate + + final IssuedAttestationCertificate issuedAttestationCertificate = + (IssuedAttestationCertificate) certificate; + + final String fileName = "filename=\"" + IssuedAttestationCertificate.class.getSimpleName() + + "_" + + issuedAttestationCertificate.getSerialNumber() + + ".cer\""; + + // Set filename for download. + response.setHeader("Content-Disposition", "attachment;" + fileName); + response.setContentType("application/octet-stream"); + + // write issued certificate to output stream + response.getOutputStream().write(certificate.getRawBytes()); + + } catch (Exception ex) { + log.error("An exception was thrown while attempting to download the" + + " specified issued attestation certificate", ex); + + // send a 404 error when an exception is thrown while attempting to download the + // specified issued attestation certificate response.sendError(HttpServletResponse.SC_NOT_FOUND); } } /** - * Handles request to download the issued attestation certificates by writing it to the response stream - * for download in bulk. + * Processes request to bulk download all the issued attestation certificates by writing it + * to the response stream for download in bulk. * * @param response the response object (needed to update the header with the * file name) @@ -183,7 +208,7 @@ public class IssuedCertificateController extends PageController { @GetMapping("/bulk-download") public void bulkDownloadIssuedCertificates(final HttpServletResponse response) throws IOException { - log.info("Handling request to download all issued certificates"); + log.info("Receiving request to download all issued certificates"); final String singleFileName = "Issued_Certificate"; final String fileName = "issued_certificates.zip"; @@ -196,14 +221,17 @@ public class IssuedCertificateController extends PageController { // write issued attestation certificates to output stream and bulk download them this.certificateService.bulkDownloadCertificates(zipOut, ISSUED_CERTIFICATES, singleFileName); } catch (Exception ex) { - log.error("Failed to bulk download issued certificates:", ex); - // send a 404 error when invalid certificate + log.error("An exception was thrown while attempting to bulk download all the" + + "issued attestation certificates", ex); + + // send a 404 error when an exception is thrown while attempting to download the + // issued attestation certificates response.sendError(HttpServletResponse.SC_NOT_FOUND); } } /** - * Archives (soft deletes) the issued attestation certificate. + * Processes request to archive/soft delete the provided issued attestation certificate. * * @param id the UUID of the issued attestation certificate to delete * @param attr RedirectAttributes used to forward data back to the original @@ -215,28 +243,27 @@ public class IssuedCertificateController extends PageController { public RedirectView deleteIssuedCertificate( @RequestParam final String id, final RedirectAttributes attr) throws URISyntaxException { - log.info("Handling request to delete issued attestation certificate id {}", id); + log.info("Receiving request to delete issued attestation certificate id {}", id); Map model = new HashMap<>(); PageMessages messages = new PageMessages(); - try { - List successMessages = new ArrayList<>(); - List errorMessages = new ArrayList<>(); + List successMessages = new ArrayList<>(); + List errorMessages = new ArrayList<>(); + try { UUID uuid = UUID.fromString(id); this.certificateService.deleteCertificate(uuid, ISSUED_CERTIFICATES, successMessages, errorMessages); - } catch (IllegalArgumentException ex) { - String uuidError = "Failed to parse ID from issued attestation certificate: " + id; - messages.addError(uuidError); - log.error(uuidError, ex); - } catch (DBManagerException ex) { - String dbError = "Failed to archive issued attestation certificate: " + id; - messages.addError(dbError); - log.error(dbError, ex); + messages.addSuccessMessages(successMessages); + messages.addErrorMessages(errorMessages); + } catch (Exception exception) { + final String errorMessage = "An exception was thrown while attempting to delete" + + " the specified issued attestation certificate"; + messages.addErrorMessage(errorMessage); + log.error(errorMessage, exception); } model.put(MESSAGES_ATTRIBUTE, messages); @@ -246,9 +273,10 @@ public class IssuedCertificateController extends PageController { /** * Helper method that returns a list of column names that are searchable. * + * @param columns columns * @return searchable column names */ - private List findSearchableColumnsNames(List columns) { + private List findSearchableColumnsNames(final List columns) { // Retrieve all searchable columns and collect their names into a list of strings. return columns.stream().filter(Column::isSearchable).map(Column::getName) diff --git a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/PlatformCredentialPageController.java b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/PlatformCredentialPageController.java index 85267c5f..58e01a28 100644 --- a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/PlatformCredentialPageController.java +++ b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/PlatformCredentialPageController.java @@ -1,6 +1,5 @@ package hirs.attestationca.portal.page.controllers; -import hirs.attestationca.persist.DBManagerException; import hirs.attestationca.persist.FilteredRecordsList; import hirs.attestationca.persist.entity.manager.EndorsementCredentialRepository; import hirs.attestationca.persist.entity.manager.PlatformCertificateRepository; @@ -15,6 +14,7 @@ import hirs.attestationca.portal.page.Page; import hirs.attestationca.portal.page.PageController; import hirs.attestationca.portal.page.PageMessages; import hirs.attestationca.portal.page.params.NoPageParams; +import jakarta.persistence.EntityNotFoundException; import jakarta.servlet.http.HttpServletResponse; import lombok.extern.log4j.Log4j2; import org.apache.commons.lang3.StringUtils; @@ -46,6 +46,9 @@ import java.util.UUID; import java.util.stream.Collectors; import java.util.zip.ZipOutputStream; +/** + * Controller for the Platform Credentials page. + */ @Log4j2 @Controller @RequestMapping("/HIRS_AttestationCAPortal/portal/certificate-request/platform-credentials") @@ -57,6 +60,13 @@ public class PlatformCredentialPageController extends PageController getPlatformCredentialsTableData( final DataTableInput input) { - - log.debug("Handling list request for platform credentials: {}", input); + log.info("Receiving request to display list of platform credentials"); + log.debug("Request received a datatable input object for the platform credentials page: {}", input); // attempt to get the column property based on the order index. String orderColumnName = input.getOrderColumnName(); @@ -151,12 +161,12 @@ public class PlatformCredentialPageController extends PageController(records, input); } /** - * Handles request to download the platform credential by writing it to the response stream + * Processes request to download the platform credential by writing it to the response stream * for download. * * @param id the UUID of the platform credential to download @@ -169,41 +179,50 @@ public class PlatformCredentialPageController extends PageController model = new HashMap<>(); PageMessages messages = new PageMessages(); - List errorMessages = new ArrayList<>(); - List successMessages = new ArrayList<>(); - for (MultipartFile file : files) { + List errorMessages = new ArrayList<>(); + List successMessages = new ArrayList<>(); + //Parse platform credential PlatformCredential parsedPlatformCredential = parsePlatformCredential(file, messages); @@ -264,6 +285,9 @@ public class PlatformCredentialPageController extends PageController model = new HashMap<>(); PageMessages messages = new PageMessages(); - try { - List successMessages = new ArrayList<>(); - List errorMessages = new ArrayList<>(); + List successMessages = new ArrayList<>(); + List errorMessages = new ArrayList<>(); + try { UUID uuid = UUID.fromString(id); this.certificateService.deleteCertificate(uuid, PLATFORM_CREDENTIALS, successMessages, errorMessages); - } catch (IllegalArgumentException ex) { - String uuidError = "Failed to parse platform credential ID from: " + id; - messages.addError(uuidError); - log.error(uuidError, ex); - } catch (DBManagerException ex) { - String dbError = "Failed to archive platform credential: " + id; - messages.addError(dbError); - log.error(dbError, ex); + messages.addSuccessMessages(successMessages); + messages.addErrorMessages(errorMessages); + } catch (Exception exception) { + final String errorMessage = "An exception was thrown while attempting to delete" + + " the specified platform credential"; + messages.addErrorMessage(errorMessage); + log.error(errorMessage, exception); } model.put(MESSAGES_ATTRIBUTE, messages); @@ -318,22 +340,24 @@ public class PlatformCredentialPageController extends PageController findSearchableColumnsNames(List columns) { + private List findSearchableColumnsNames(final List columns) { // Retrieve all searchable columns and collect their names into a list of strings. return columns.stream().filter(Column::isSearchable).map(Column::getName) .collect(Collectors.toList()); } /** - * Attempts to parse the provided file in order to create a PLatform Credential. + * Attempts to parse the provided file in order to create a Platform Credential. * * @param file file * @param messages page messages * @return platform credential */ - private PlatformCredential parsePlatformCredential(MultipartFile file, PageMessages messages) { + private PlatformCredential parsePlatformCredential(final MultipartFile file, + final PageMessages messages) { log.info("Received platform credential file of size: {}", file.getSize()); byte[] fileBytes; @@ -346,7 +370,7 @@ public class PlatformCredentialPageController extends PageController { final PolicyManagerException pmEx, final String message, final String error) { log.error(message, pmEx); - messages.addError(error); + messages.addErrorMessage(error); model.put(MESSAGES_ATTRIBUTE, messages); } private void handleUserError(final Map model, final PageMessages messages, final String errorMessage) { - messages.addError(errorMessage); + messages.addErrorMessage(errorMessage); model.put(MESSAGES_ATTRIBUTE, messages); } @@ -1003,7 +1003,7 @@ public class PolicyPageController extends PageController { policyRepository.saveAndFlush(settings); // Log and set the success message - messages.addSuccess(successMessage); + messages.addSuccessMessage(successMessage); log.debug("ACA Policy set to: " + ppModel.toString()); model.put(MESSAGES_ATTRIBUTE, messages); diff --git a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/ReferenceManifestDetailsPageController.java b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/ReferenceManifestDetailsPageController.java index 16d2e988..542b6069 100644 --- a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/ReferenceManifestDetailsPageController.java +++ b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/ReferenceManifestDetailsPageController.java @@ -595,7 +595,7 @@ public class ReferenceManifestDetailsPageController // Check if parameters were set if (params.getId() == null) { String typeError = "ID was not provided"; - messages.addError(typeError); + messages.addErrorMessage(typeError); log.debug(typeError); mav.addObject(MESSAGES_ATTRIBUTE, messages); } else { @@ -606,7 +606,7 @@ public class ReferenceManifestDetailsPageController caCertificateRepository)); } catch (IllegalArgumentException iaEx) { String uuidError = "Failed to parse ID from: " + params.getId(); - messages.addError(uuidError); + messages.addErrorMessage(uuidError); log.error(uuidError, iaEx); } catch (CertificateException cEx) { log.error(cEx); @@ -620,7 +620,7 @@ public class ReferenceManifestDetailsPageController if (data.isEmpty()) { String notFoundMessage = "Unable to find RIM with ID: " + params.getId(); - messages.addError(notFoundMessage); + messages.addErrorMessage(notFoundMessage); log.warn(notFoundMessage); mav.addObject(MESSAGES_ATTRIBUTE, messages); } else { diff --git a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/ReferenceManifestPageController.java b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/ReferenceManifestPageController.java index eca02cad..ec217f87 100644 --- a/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/ReferenceManifestPageController.java +++ b/HIRS_AttestationCAPortal/src/main/java/hirs/attestationca/portal/page/controllers/ReferenceManifestPageController.java @@ -183,7 +183,7 @@ public class ReferenceManifestPageController extends PageController { @@ -236,21 +236,21 @@ public class ReferenceManifestPageController extends PageController getTrustChainCertificatesTableData( final DataTableInput input) { - log.debug("Handling list request for trust chain certificates: {}", input); + log.info("Receiving request to display list of trust chain certificates"); + log.debug("Request received a datatable input object for the trust chain certificates page: {}", + input); // attempt to get the column property based on the order index. String orderColumnName = input.getOrderColumnName(); @@ -161,7 +177,7 @@ public class TrustChainCertificatePageController extends PageController(records, input); } @@ -179,41 +195,52 @@ public class TrustChainCertificatePageController extends PageController model = new HashMap<>(); PageMessages messages = new PageMessages(); - List errorMessages = new ArrayList<>(); - List successMessages = new ArrayList<>(); - for (MultipartFile file : files) { + List errorMessages = new ArrayList<>(); + List successMessages = new ArrayList<>(); + //Parse trust chain certificate CertificateAuthorityCredential parsedTrustChainCertificate = parseTrustChainCertificate(file, messages); @@ -294,10 +326,10 @@ public class TrustChainCertificatePageController extends PageController model = new HashMap<>(); PageMessages messages = new PageMessages(); - try { - List successMessages = new ArrayList<>(); - List errorMessages = new ArrayList<>(); + List successMessages = new ArrayList<>(); + List errorMessages = new ArrayList<>(); + try { UUID uuid = UUID.fromString(id); this.certificateService.deleteCertificate(uuid, TRUST_CHAIN, successMessages, errorMessages); - var a = successMessages; - - } catch (IllegalArgumentException ex) { - String uuidError = "Failed to parse ID from: " + id; - messages.addError(uuidError); - log.error(uuidError, ex); - } catch (DBManagerException ex) { - String dbError = "Failed to archive cert: " + id; - messages.addError(dbError); - log.error(dbError, ex); + messages.addSuccessMessages(successMessages); + messages.addErrorMessages(errorMessages); + } catch (Exception exception) { + final String errorMessage = "An exception was thrown while attempting to delete" + + " the specified trust chain certificate"; + messages.addErrorMessage(errorMessage); + log.error(errorMessage, exception); } - model.put(MESSAGES_ATTRIBUTE, messages); return redirectTo(Page.TRUST_CHAIN, new NoPageParams(), model, attr); } @@ -353,9 +381,10 @@ public class TrustChainCertificatePageController extends PageController findSearchableColumnsNames(List columns) { + private List findSearchableColumnsNames(final List columns) { // Retrieve all searchable columns and collect their names into a list of strings. return columns.stream().filter(Column::isSearchable).map(Column::getName) @@ -369,8 +398,8 @@ public class TrustChainCertificatePageController extends PageController successMessages = new ArrayList<>(); List errorMessages = new ArrayList<>(); + this.certificateService.storeCertificate( TRUST_CHAIN, file.getOriginalFilename(), @@ -405,6 +435,9 @@ public class TrustChainCertificatePageController extends PageController
    - +
  • @@ -15,7 +15,7 @@
  • - +
  • @@ -24,7 +24,7 @@
  • - +
  • diff --git a/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/CertificateDetailsPageControllerTest.java b/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/CertificateDetailsPageControllerTest.java index 57e290a5..71002e75 100644 --- a/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/CertificateDetailsPageControllerTest.java +++ b/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/CertificateDetailsPageControllerTest.java @@ -42,34 +42,51 @@ public class CertificateDetailsPageControllerTest extends PageControllerTest { // Random UUID for certificate search. private static final String ID = "046b6c7f-0b8a-43b9-b35d-6489e6daee91"; + private static final String TEST_CA_CERTIFICATE = "/certificates/fakestmtpmekint02.pem"; + private static final String TEST_ROOT_CA_CERTIFICATE = "/certificates/fakeCA.pem"; + private static final String ISSUED_CLIENT_CERT = "/certificates/sample_identity_cert.cer"; + private static final String TEST_ENDORSEMENT_CREDENTIAL = "/endorsement_credentials/tpmcert.pem"; + private static final String TEST_PLATFORM_CREDENTIAL = "/platform_credentials/Intel_pc.cer"; + private static final String TEST_PLATFORM_CREDENTIAL_2 = "/platform_credentials/basic_plat_cert_2-0.pem"; + private static final String TEST_PLATFORM_CREDENTIAL_2_PCI = "/platform_credentials/pciids_plat_cert_2-0.pem"; + // Base path for the page private final String pagePath; + // Repository manager to handle data access between device entity and data storage in db @Autowired private DeviceRepository deviceRepository; + // Repository manager to handle data access between certificate entity and data storage in db @Autowired private CertificateRepository certificateRepository; + private CertificateAuthorityCredential caCertificate; + private CertificateAuthorityCredential caRootCertificate; + private PlatformCredential platformCredential; + private PlatformCredential platformCredential2; + private PlatformCredential platformCertificatePCI; + private EndorsementCredential endorsementCredential; + private IssuedAttestationCertificate issuedCredential; /** @@ -171,7 +188,7 @@ public class CertificateDetailsPageControllerTest extends PageControllerTest { .param("id", ID) .param("type", "certificateauthority")) .andExpect(status().isOk()) - .andExpect(model().attribute(PageController.MESSAGES_ATTRIBUTE, hasProperty("error", + .andExpect(model().attribute(PageController.MESSAGES_ATTRIBUTE, hasProperty("errorMessages", hasItem("Unable to find certificate with ID: " + ID)))) .andReturn(); } @@ -189,7 +206,7 @@ public class CertificateDetailsPageControllerTest extends PageControllerTest { .param("id", ID) .param("type", "invalid")) .andExpect(status().isOk()) - .andExpect(model().attribute(PageController.MESSAGES_ATTRIBUTE, hasProperty("error", + .andExpect(model().attribute(PageController.MESSAGES_ATTRIBUTE, hasProperty("errorMessages", hasItem("Invalid certificate type: invalid")))) .andReturn(); } @@ -206,7 +223,7 @@ public class CertificateDetailsPageControllerTest extends PageControllerTest { .perform(MockMvcRequestBuilders.get(pagePath) .param("id", ID)) .andExpect(status().isOk()) - .andExpect(model().attribute(PageController.MESSAGES_ATTRIBUTE, hasProperty("error", + .andExpect(model().attribute(PageController.MESSAGES_ATTRIBUTE, hasProperty("errorMessages", hasItem("Type was not provided")))) .andReturn(); } diff --git a/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/EndorsementKeyCredentialsPageControllerTest.java b/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/EndorsementKeyCredentialsPageControllerTest.java index e7a843ea..3432dca4 100644 --- a/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/EndorsementKeyCredentialsPageControllerTest.java +++ b/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/EndorsementKeyCredentialsPageControllerTest.java @@ -99,8 +99,8 @@ public class EndorsementKeyCredentialsPageControllerTest extends PageControllerT FlashMap flashMap = result.getFlashMap(); PageMessages pageMessages = (PageMessages) flashMap.get("messages"); assertEquals("New certificate successfully uploaded (" + pathTokens[1] + "): ", - pageMessages.getSuccess().get(0)); - assertEquals(0, pageMessages.getError().size()); + pageMessages.getSuccessMessages().get(0)); + assertEquals(0, pageMessages.getErrorMessages().size()); // verify the cert was actually stored List records = @@ -147,8 +147,8 @@ public class EndorsementKeyCredentialsPageControllerTest extends PageControllerT // verify redirection messages FlashMap flashMap = result.getFlashMap(); PageMessages pageMessages = (PageMessages) flashMap.get("messages"); - assertEquals(1, pageMessages.getError().size()); - assertEquals(0, pageMessages.getSuccess().size()); + assertEquals(1, pageMessages.getErrorMessages().size()); + assertEquals(0, pageMessages.getSuccessMessages().size()); // verify the cert was not actually stored List records = diff --git a/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/PlatformCredentialsPageControllerTest.java b/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/PlatformCredentialsPageControllerTest.java index a48e0ea1..f3e69b67 100644 --- a/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/PlatformCredentialsPageControllerTest.java +++ b/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/PlatformCredentialsPageControllerTest.java @@ -111,8 +111,8 @@ public class PlatformCredentialsPageControllerTest extends PageControllerTest { // verify redirection messages FlashMap flashMap = result.getFlashMap(); PageMessages pageMessages = (PageMessages) flashMap.get("messages"); - assertEquals(1, pageMessages.getSuccess().size()); - assertEquals(0, pageMessages.getError().size()); + assertEquals(1, pageMessages.getSuccessMessages().size()); + assertEquals(0, pageMessages.getErrorMessages().size()); // verify the cert was actually stored List records = @@ -173,11 +173,11 @@ public class PlatformCredentialsPageControllerTest extends PageControllerTest { // verify redirection messages FlashMap flashMap = result.getFlashMap(); PageMessages pageMessages = (PageMessages) flashMap.get("messages"); - assertEquals(1, pageMessages.getSuccess().size()); - assertEquals(0, pageMessages.getError().size()); + assertEquals(1, pageMessages.getSuccessMessages().size()); + assertEquals(0, pageMessages.getErrorMessages().size()); assertEquals("Pre-existing certificate found and unarchived (" + pathTokens[1] + "): ", - pageMessages.getSuccess().get(0)); + pageMessages.getSuccessMessages().get(0)); // verify there is still only one cert in db List records = certificateRepository.findAll(); @@ -216,8 +216,8 @@ public class PlatformCredentialsPageControllerTest extends PageControllerTest { // verify redirection messages FlashMap flashMap = result.getFlashMap(); PageMessages pageMessages = (PageMessages) flashMap.get("messages"); - assertEquals(0, pageMessages.getSuccess().size()); - assertEquals(1, pageMessages.getError().size()); + assertEquals(0, pageMessages.getSuccessMessages().size()); + assertEquals(1, pageMessages.getErrorMessages().size()); // verify the cert was not actually stored List records = @@ -243,8 +243,8 @@ public class PlatformCredentialsPageControllerTest extends PageControllerTest { // verify redirection messages FlashMap flashMap = result.getFlashMap(); PageMessages pageMessages = (PageMessages) flashMap.get("messages"); - assertEquals(1, pageMessages.getError().size()); - assertEquals(0, pageMessages.getSuccess().size()); + assertEquals(1, pageMessages.getErrorMessages().size()); + assertEquals(0, pageMessages.getSuccessMessages().size()); // verify the cert was not actually stored List records = diff --git a/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/PolicyPageControllerTest.java b/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/PolicyPageControllerTest.java index 2033a3b9..c1b021d7 100644 --- a/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/PolicyPageControllerTest.java +++ b/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/PolicyPageControllerTest.java @@ -61,7 +61,6 @@ public class PolicyPageControllerTest extends PageControllerTest { */ @Test public void verifySpringInitialized() { - assertNotNull(policyRepository); assertNotNull(policy); } @@ -116,7 +115,7 @@ public class PolicyPageControllerTest extends PageControllerTest { .andExpect(status().is3xxRedirection()) // check the messages forwarded to the redirected page .andExpect(flash().attribute(PageController.MESSAGES_ATTRIBUTE, - hasProperty("success", + hasProperty("successMessages", hasItem("Endorsement credential validation enabled")))); policy = policyRepository.findByName("Default"); @@ -148,7 +147,7 @@ public class PolicyPageControllerTest extends PageControllerTest { .andExpect(status().is3xxRedirection()) // check the messages forwarded to the redirected page .andExpect(flash().attribute(PageController.MESSAGES_ATTRIBUTE, - hasProperty("success", + hasProperty("successMessages", hasItem("Endorsement credential validation disabled")))); policy = policyRepository.findByName("Default"); @@ -169,7 +168,7 @@ public class PolicyPageControllerTest extends PageControllerTest { .andExpect(status().is3xxRedirection()) // check the messages forwarded to the redirected page .andExpect(flash().attribute(PageController.MESSAGES_ATTRIBUTE, - hasProperty("error", + hasProperty("errorMessages", hasItem("To disable Endorsement Credential Validation, Platform Validation" + " must also be disabled.")))); @@ -203,7 +202,7 @@ public class PolicyPageControllerTest extends PageControllerTest { .andExpect(status().is3xxRedirection()) // check the messages forwarded to the redirected page .andExpect(flash().attribute(PageController.MESSAGES_ATTRIBUTE, - hasProperty("success", + hasProperty("successMessages", hasItem("Platform certificate validation enabled")))); policy = policyRepository.findByName("Default"); @@ -224,7 +223,7 @@ public class PolicyPageControllerTest extends PageControllerTest { .andExpect(status().is3xxRedirection()) // check the messages forwarded to the redirected page .andExpect(flash().attribute(PageController.MESSAGES_ATTRIBUTE, - hasProperty("error", + hasProperty("errorMessages", hasItem("Unable to change Platform Validation setting," + " invalid policy configuration.")))); @@ -258,7 +257,7 @@ public class PolicyPageControllerTest extends PageControllerTest { .andExpect(status().is3xxRedirection()) // check the messages forwarded to the redirected page .andExpect(flash().attribute(PageController.MESSAGES_ATTRIBUTE, - hasProperty("success", + hasProperty("successMessages", hasItem("Platform certificate validation disabled")))); policy = policyRepository.findByName("Default"); @@ -279,7 +278,7 @@ public class PolicyPageControllerTest extends PageControllerTest { .andExpect(status().is3xxRedirection()) // check the messages forwarded to the redirected page .andExpect(flash().attribute(PageController.MESSAGES_ATTRIBUTE, - hasProperty("error", + hasProperty("errorMessages", hasItem("Unable to change Platform Validation setting," + " invalid policy configuration.")))); @@ -313,7 +312,7 @@ public class PolicyPageControllerTest extends PageControllerTest { .andExpect(status().is3xxRedirection()) // check the messages forwarded to the redirected page .andExpect(flash().attribute(PageController.MESSAGES_ATTRIBUTE, - hasProperty("success", + hasProperty("successMessages", hasItem("Platform certificate attribute validation enabled")))); policy = policyRepository.findByName("Default"); @@ -334,7 +333,7 @@ public class PolicyPageControllerTest extends PageControllerTest { .andExpect(status().is3xxRedirection()) // check the messages forwarded to the redirected page .andExpect(flash().attribute(PageController.MESSAGES_ATTRIBUTE, - hasProperty("error", + hasProperty("errorMessages", hasItem("To enable Platform Attribute Validation," + " Platform Credential Validation must also be enabled.")))); @@ -367,7 +366,7 @@ public class PolicyPageControllerTest extends PageControllerTest { .andExpect(status().is3xxRedirection()) // check the messages forwarded to the redirected page .andExpect(flash().attribute(PageController.MESSAGES_ATTRIBUTE, - hasProperty("success", + hasProperty("successMessages", hasItem("Platform certificate attribute validation disabled")))); policy = policyRepository.findByName("Default"); diff --git a/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/TrustChainManagementPageControllerTest.java b/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/TrustChainManagementPageControllerTest.java index 9bd37a12..f8d24460 100644 --- a/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/TrustChainManagementPageControllerTest.java +++ b/HIRS_AttestationCAPortal/src/test/java/hirs/attestationca/portal/page/controllers/TrustChainManagementPageControllerTest.java @@ -183,9 +183,9 @@ public class TrustChainManagementPageControllerTest extends PageControllerTest { FlashMap flashMap = result.getFlashMap(); PageMessages pageMessages = (PageMessages) flashMap.get("messages"); assertEquals("New certificate successfully uploaded (" + pathTokens[1] + "): ", - pageMessages.getSuccess() + pageMessages.getSuccessMessages() .get(0)); - assertEquals(0, pageMessages.getError().size()); + assertEquals(0, pageMessages.getErrorMessages().size()); // verify the cert was actually stored List records = @@ -244,10 +244,10 @@ public class TrustChainManagementPageControllerTest extends PageControllerTest { // verify redirection messages FlashMap flashMap = result.getFlashMap(); PageMessages pageMessages = (PageMessages) flashMap.get("messages"); - assertEquals(1, pageMessages.getSuccess().size()); - assertEquals(0, pageMessages.getError().size()); + assertEquals(1, pageMessages.getSuccessMessages().size()); + assertEquals(0, pageMessages.getErrorMessages().size()); assertEquals("Pre-existing certificate found and unarchived (" + pathTokens[1] + "): ", - pageMessages.getSuccess().get(0)); + pageMessages.getSuccessMessages().get(0)); // verify the cert can be retrieved and that there is only 1 cert in db List records = certificateRepository.findAll(); @@ -279,8 +279,8 @@ public class TrustChainManagementPageControllerTest extends PageControllerTest { // verify redirection messages FlashMap flashMap = result.getFlashMap(); PageMessages pageMessages = (PageMessages) flashMap.get("messages"); - assertEquals(1, pageMessages.getError().size()); - assertEquals(0, pageMessages.getSuccess().size()); + assertEquals(1, pageMessages.getErrorMessages().size()); + assertEquals(0, pageMessages.getSuccessMessages().size()); // verify the cert was not actually stored List records = certificateRepository.findAll();