Working through some inheritance issues for Policy. Archiving also is

on the list of things that need to be reworked.
This commit is contained in:
Cyrus 2022-08-04 10:02:34 -04:00
parent 81aeaf85c0
commit d80af7bc9e
14 changed files with 128 additions and 39 deletions

View File

@ -11,4 +11,6 @@ import java.util.UUID;
*/
@Repository
public interface AppraiserRepository extends JpaRepository<Appraiser, UUID> {
Appraiser findByName(String name);
}

View File

@ -12,4 +12,6 @@ import java.util.UUID;
*/
@Repository
public interface DeviceRepository extends JpaRepository<Device, UUID> {
Device findByName(String name);
}

View File

@ -10,5 +10,6 @@ import java.util.UUID;
* Setting up for new creation for CRUD operations.
*/
@Repository
public interface PolicyRepository extends JpaRepository<Policy, UUID> {
public interface PolicyRepository<T extends Policy> extends JpaRepository<T, UUID> {
T save(T policy);
}

View File

@ -4,6 +4,7 @@ import hirs.data.persist.ReferenceDigestValue;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.UUID;
/**
@ -11,4 +12,8 @@ import java.util.UUID;
*/
@Repository
public interface ReferenceDigestValueRepository extends JpaRepository<ReferenceDigestValue, UUID> {
List<ReferenceDigestValue> findValuesByBaseRimId(UUID uuid);
List<ReferenceDigestValue> findValuesBySupportRimId(UUID uuid);
}

View File

@ -13,7 +13,6 @@ import org.springframework.retry.RetryContext;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import java.util.List;
import java.util.UUID;
/**
@ -82,13 +81,8 @@ public class AppraiserServiceImpl extends DbServiceImpl<Appraiser>
@Override
public Appraiser doWithRetry(final RetryContext context)
throws DBManagerException {
List<Appraiser> appraiserList = appraiserRepository.findAll();
for (Appraiser appraiser : appraiserList) {
if (appraiser.getName().equals(name)) {
return appraiser;
}
}
return null; }
return appraiserRepository.findByName(name);
}
});
}

View File

@ -1,9 +1,12 @@
package hirs.attestationca.service;
import hirs.data.persist.ArchivableEntity;
import hirs.persist.DBManagerException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.StaleObjectStateException;
import org.hibernate.exception.LockAcquisitionException;
import org.jadira.usertype.spi.repository.JpaBaseRepository;
import org.springframework.retry.RetryListener;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
@ -106,4 +109,32 @@ public class DbServiceImpl<T> {
public final EntityManager getEm() {
return em;
}
/**
* Archives the named object and updates it in the database.
*
* @param name name of the object to archive
* @return true if the object was successfully found and archived, false if the object was not
* found
* @throws hirs.persist.DBManagerException if the object is not an instance of <code>ArchivableEntity</code>
*/
public final boolean archive(final String name) throws DBManagerException {
LOGGER.debug("archiving object: {}", name);
if (name == null) {
LOGGER.debug("null name argument");
return false;
}
T target = get(name);
if (target == null) {
return false;
}
if (!(target instanceof ArchivableEntity)) {
throw new DBManagerException("unable to archive non-archivable object");
}
((ArchivableEntity) target).archive();
update(target);
return true;
}
}

View File

@ -41,6 +41,20 @@ public class DeviceServiceImpl extends DbServiceImpl<Device> implements DefaultS
public DeviceServiceImpl(final EntityManager em) {
}
@Override
public final Device getByName(final String name) {
LOGGER.debug("Find device by name: {}", name);
return getRetryTemplate().execute(new RetryCallback<Device,
DBManagerException>() {
@Override
public Device doWithRetry(final RetryContext context)
throws DBManagerException {
return deviceRepository.findByName(name);
}
});
}
@Override
public final Device saveDevice(final Device device) throws DeviceManagerException {
LOGGER.debug("Saving device: {}", device);

View File

@ -16,7 +16,6 @@ import org.springframework.retry.RetryContext;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -115,11 +114,31 @@ public class ReferenceDigestValueServiceImpl extends DbServiceImpl<ReferenceDige
}
@Override
public List<ReferenceDigestValue> getValuesByRimId(final UUID uuid) {
// this isn't right, it will look for the ids in the wrong column (CYRUYS)
// need to figure out repo search based on criteria associated with a specific column
public List<ReferenceDigestValue> getValuesByBaseRimId(final UUID uuid) {
LOGGER.debug("Find reference digest values base on: {}", uuid);
return new LinkedList<>(this.referenceDigestValueRepository.findAllById(uuid));
return getRetryTemplate().execute(new RetryCallback<List<ReferenceDigestValue>,
DBManagerException>() {
@Override
public List<ReferenceDigestValue> doWithRetry(final RetryContext context)
throws DBManagerException {
return referenceDigestValueRepository.findValuesByBaseRimId(uuid);
}
});
}
@Override
public List<ReferenceDigestValue> getValuesBySupportRimId(final UUID uuid) {
LOGGER.debug("Find reference digest values base on: {}", uuid);
return getRetryTemplate().execute(new RetryCallback<List<ReferenceDigestValue>,
DBManagerException>() {
@Override
public List<ReferenceDigestValue> doWithRetry(final RetryContext context)
throws DBManagerException {
return referenceDigestValueRepository.findValuesBySupportRimId(uuid);
}
});
}
@Override

View File

@ -6,10 +6,11 @@ import hirs.attestationca.portal.model.PolicyPageModel;
import hirs.attestationca.portal.page.PageController;
import hirs.attestationca.portal.page.PageMessages;
import hirs.attestationca.portal.page.params.NoPageParams;
import hirs.data.persist.policy.Policy;
import hirs.data.persist.policy.SupplyChainPolicy;
import hirs.persist.AppraiserManager;
import hirs.persist.PolicyManager;
import hirs.persist.PolicyManagerException;
import hirs.persist.service.AppraiserService;
import hirs.persist.service.PolicyService;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
@ -47,9 +48,9 @@ public class PolicyPageController extends PageController<NoPageParams> {
private static final String ENABLED_EXPIRES_PARAMETER_VALUE = "expires";
@Autowired
private PolicyManager policyManager;
private PolicyService policyService;
@Autowired
private AppraiserManager appraiserManager;
private AppraiserService appraiserService;
/**
* Model attribute name used by initPage for the initial data passed to the
@ -66,15 +67,15 @@ public class PolicyPageController extends PageController<NoPageParams> {
/**
* Constructor.
*
* @param policyManager the policy manager
* @param appraiserManager the appraiser manager
* @param policyService the policy service
* @param appraiserService the appraiser service
*/
@Autowired
public PolicyPageController(final PolicyManager policyManager,
final AppraiserManager appraiserManager) {
public PolicyPageController(final PolicyService policyService,
final AppraiserService appraiserService) {
super(POLICY);
this.policyManager = policyManager;
this.appraiserManager = appraiserManager;
this.policyService = policyService;
this.appraiserService = appraiserService;
}
/**
@ -933,9 +934,9 @@ public class PolicyPageController extends PageController<NoPageParams> {
* @return The default Supply Chain Policy
*/
private SupplyChainPolicy getDefaultPolicy() {
final Appraiser supplyChainAppraiser = appraiserManager.getAppraiser(
final Appraiser supplyChainAppraiser = appraiserService.getAppraiser(
SupplyChainAppraiser.NAME);
return (SupplyChainPolicy) policyManager.getDefaultPolicy(
return (SupplyChainPolicy) policyService.getDefaultPolicy(
supplyChainAppraiser);
}
@ -957,13 +958,12 @@ public class PolicyPageController extends PageController<NoPageParams> {
return policy;
}
private void savePolicyAndApplySuccessMessage(final PolicyPageModel ppModel,
final Map<String, Object> model,
final PageMessages messages,
final String successMessage,
private void savePolicyAndApplySuccessMessage(
final PolicyPageModel ppModel, final Map<String, Object> model,
final PageMessages messages, final String successMessage,
final SupplyChainPolicy policy) {
// save the policy to the DB
policyManager.updatePolicy(policy);
policyService.updatePolicy(policy);
// Log and set the success message
messages.addSuccess(successMessage);

View File

@ -15,8 +15,8 @@ import hirs.data.persist.certificate.attributes.ComponentIdentifier;
import hirs.data.persist.certificate.attributes.V2.ComponentIdentifierV2;
import hirs.persist.CriteriaModifier;
import hirs.persist.CrudManager;
import hirs.persist.DeviceManager;
import hirs.persist.service.CertificateService;
import hirs.persist.service.DeviceService;
import org.apache.logging.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
@ -62,7 +62,7 @@ public class ValidationReportsPageController extends PageController<NoPageParams
@Autowired
private final CertificateService certificateService;
@Autowired
private final DeviceManager deviceManager;
private final DeviceService deviceService;
private static String systemColumnHeaders = "Verified Manufacturer,"
+ "Model,SN,Verification Date,Device Status";
@ -77,17 +77,17 @@ public class ValidationReportsPageController extends PageController<NoPageParams
* Constructor providing the Page's display and routing specification.
* @param supplyChainValidatorSummaryManager the manager
* @param certificateService the certificate service
* @param deviceManager the device manager
* @param deviceService the device service
*/
@Autowired
public ValidationReportsPageController(
final CrudManager<SupplyChainValidationSummary> supplyChainValidatorSummaryManager,
final CertificateService certificateService,
final DeviceManager deviceManager) {
final DeviceService deviceService) {
super(VALIDATION_REPORTS);
this.supplyChainValidatorSummaryManager = supplyChainValidatorSummaryManager;
this.certificateService = certificateService;
this.deviceManager = deviceManager;
this.deviceService = deviceService;
}
/**
@ -265,7 +265,7 @@ public class ValidationReportsPageController extends PageController<NoPageParams
if ((createTimes.get(i).isAfter(startDate) || createTimes.get(i).isEqual(startDate))
&& (createTimes.get(i).isBefore(endDate)
|| createTimes.get(i).isEqual(endDate))) {
UUID deviceId = deviceManager.getDevice(deviceNames[i]).getId();
UUID deviceId = deviceService.getByName(deviceNames[i]).getId();
PlatformCredential pc = PlatformCredential.select(certificateService)
.byDeviceId(deviceId).getCertificate();
if (jsonVersion) {

View File

@ -1,5 +1,6 @@
package hirs.data.persist;
import lombok.Data;
import org.bouncycastle.util.Arrays;
import org.hibernate.annotations.Type;
@ -19,6 +20,7 @@ import java.util.UUID;
* Digest Value, Event Type, index, RIM Tagid
*/
@Entity
@Data
@Table(name = "ReferenceDigestValue")
@XmlRootElement(name = "ReferenceDigestValue")
@XmlAccessorType(XmlAccessType.FIELD)

View File

@ -7,6 +7,7 @@ import javax.persistence.AccessType;
import javax.persistence.Entity;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.MappedSuperclass;
import javax.persistence.Table;
/**
@ -20,6 +21,7 @@ import javax.persistence.Table;
@Table(name = "Policy")
@Inheritance(strategy = InheritanceType.JOINED)
@Access(AccessType.FIELD)
@MappedSuperclass
public abstract class Policy extends UserDefinedEntity {
/**

View File

@ -39,4 +39,13 @@ public interface DeviceService {
* @return a device object
*/
Device updateDevice(Device device, UUID deviceId) throws DeviceManagerException;
/**
* Simple accessor method using the repo to get a Device by the
* name column.
* @param name string instance of the name.
* @return instance of a device if found
*/
Device getByName(String name);
}

View File

@ -34,8 +34,16 @@ public interface ReferenceDigestValueService {
/**
* Persists a new Reference Digest value.
*
* @param uuid associated with the base rim or potentially support rim.
* @param uuid associated with the base rim .
* @return the persisted list of ReferenceDigestValue
*/
List<ReferenceDigestValue> getValuesByRimId(UUID uuid);
List<ReferenceDigestValue> getValuesByBaseRimId(UUID uuid);
/**
* Persists a new Reference Digest value.
*
* @param uuid associated with the support rim.
* @return the persisted list of ReferenceDigestValue
*/
List<ReferenceDigestValue> getValuesBySupportRimId(UUID uuid);
}