The delete method was missing, reintroduced it and tested

This commit is contained in:
Cyrus 2023-07-31 13:43:06 -04:00
parent 391a4691c5
commit bd8019cfc6

View File

@ -1,6 +1,7 @@
package hirs.attestationca.portal.page.controllers;
import hirs.attestationca.persist.CriteriaModifier;
import hirs.attestationca.persist.DBManagerException;
import hirs.attestationca.persist.DBServiceException;
import hirs.attestationca.persist.FilteredRecordsList;
import hirs.attestationca.persist.entity.manager.CACredentialRepository;
@ -50,6 +51,7 @@ import java.net.URISyntaxException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -300,6 +302,72 @@ public class CertificatePageController extends PageController<NoPageParams> {
return redirectTo(getCertificatePage(certificateType), new NoPageParams(), model, attr);
}
/**
* Archives (soft delete) the credential.
*
* @param certificateType String containing the certificate type
* @param id the UUID of the cert to delete
* @param attr RedirectAttributes used to forward data back to the original
* page.
* @return redirect to this page
* @throws URISyntaxException if malformed URI
*/
@RequestMapping(value = "/{certificateType}/delete", method = RequestMethod.POST)
public RedirectView delete(
@PathVariable("certificateType") final String certificateType,
@RequestParam final String id,
final RedirectAttributes attr) throws URISyntaxException {
log.info("Handling request to delete " + id);
Map<String, Object> model = new HashMap<>();
PageMessages messages = new PageMessages();
try {
UUID uuid = UUID.fromString(id);
Certificate certificate = getCertificateById(certificateType, 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;
messages.addError(notFoundMessage);
log.warn(notFoundMessage);
} else {
if (certificateType.equals(PLATFORMCREDENTIAL)) {
PlatformCredential platformCertificate = (PlatformCredential) certificate;
if (platformCertificate.isPlatformBase()) {
// only do this if the base is being deleted.
List<PlatformCredential> sharedCertificates = getCertificateByBoardSN(
certificateType,
platformCertificate.getPlatformSerial());
for (PlatformCredential pc : sharedCertificates) {
if (!pc.isPlatformBase()) {
pc.archive();
certificateRepository.delete(pc);
}
}
}
}
certificate.archive();
certificateRepository.delete(certificate);
String deleteCompletedMessage = "Certificate successfully deleted";
messages.addInfo(deleteCompletedMessage);
log.info(deleteCompletedMessage);
}
} 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);
}
model.put(MESSAGES_ATTRIBUTE, messages);
return redirectTo(getCertificatePage(certificateType), new NoPageParams(), model, attr);
}
/**
* Handles request to download the cert by writing it to the response stream
@ -615,14 +683,30 @@ public class CertificatePageController extends PageController<NoPageParams> {
private List<PlatformCredential> getCertificateByBoardSN(
final String certificateType,
final String serialNumber) {
List<PlatformCredential> associatedCertificates = new LinkedList<>();
if (serialNumber == null) {
return null;
if (serialNumber != null){
switch (certificateType) {
case PLATFORMCREDENTIAL:
associatedCertificates.addAll(this.certificateRepository
.byBoardSerialNumber(serialNumber));
default:
}
}
return associatedCertificates;
}
private Certificate getCertificateById(final String certificateType, final UUID uuid) {
switch (certificateType) {
case PLATFORMCREDENTIAL:
return this.certificateRepository.byBoardSerialNumber(serialNumber);
return this.platformCertificateRepository.getReferenceById(uuid);
case ENDORSEMENTCREDENTIAL:
return this.endorsementCredentialRepository.getReferenceById(uuid);
case ISSUEDCERTIFICATES:
return this.issuedCertificateRepository.getReferenceById(uuid);
case TRUSTCHAIN:
return this.caCredentialRepository.getReferenceById(uuid);
default:
return null;
}