Further updates to reference digest controller and the persistence

configuration which now includes updated refactoring for entity manager
This commit is contained in:
Cyrus 2022-08-03 12:17:55 -04:00
parent 56b77ab360
commit 9221befdf0
13 changed files with 141 additions and 184 deletions

View File

@ -1,31 +1,25 @@
package hirs.attestationca.configuration;
import hirs.attestationca.AttestationCertificateAuthorityConfiguration;
import hirs.attestationca.servicemanager.DBCertificateManager;
import hirs.attestationca.servicemanager.DBDeviceManager;
import hirs.attestationca.service.CertificateServiceImpl;
import hirs.attestationca.service.DbServiceImpl;
import hirs.attestationca.service.DeviceServiceImpl;
import hirs.attestationca.service.PolicyServiceImpl;
import hirs.attestationca.service.ReferenceDigestValueServiceImpl;
import hirs.attestationca.service.ReferenceManifestServiceImpl;
import hirs.attestationca.servicemanager.DBManager;
import hirs.attestationca.servicemanager.DBPolicyManager;
import hirs.attestationca.servicemanager.DBPortalInfoManager;
import hirs.attestationca.servicemanager.DBReferenceEventManager;
import hirs.attestationca.servicemanager.DBReferenceManifestManager;
import hirs.attestationca.servicemanager.DBReportManager;
import hirs.attestationca.servicemanager.DBReportRequestStateManager;
import hirs.attestationca.servicemanager.DBReportSummaryManager;
import hirs.data.persist.SupplyChainValidationSummary;
import hirs.persist.CertificateManager;
import hirs.persist.CrudManager;
import hirs.persist.DeviceManager;
import hirs.persist.PolicyManager;
import hirs.persist.PortalInfoManager;
import hirs.persist.ReferenceEventManager;
import hirs.persist.ReferenceManifestManager;
import hirs.persist.ReportManager;
import hirs.persist.ReportRequestStateManager;
import hirs.persist.ReportSummaryManager;
import hirs.persist.service.CertificateService;
import hirs.persist.service.DeviceService;
import hirs.persist.service.PolicyService;
import hirs.persist.service.ReferenceDigestValueService;
import hirs.persist.service.ReferenceManifestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
@ -39,7 +33,7 @@ import javax.persistence.PersistenceContext;
* file, the default persistence file will be used instead.
*/
@Configuration
@Import(AttestationCertificateAuthorityConfiguration.class)
@EnableJpaRepositories("hirs.attestationca.service")
public class PersistenceConfiguration {
/**
@ -60,115 +54,78 @@ public class PersistenceConfiguration {
private int maxTransactionRetryAttempts;
/**
* Creates a {@link hirs.persist.PolicyManager} ready to use.
* Creates a {@link hirs.persist.service.PolicyService} ready to use.
*
* @return {@link hirs.persist.PolicyManager}
* @return {@link hirs.persist.service.PolicyService}
*/
@Bean
public PolicyManager policyManager() {
DBPolicyManager manager = new DBPolicyManager(entityManager);
setDbManagerRetrySettings(manager);
return manager;
public PolicyService policyService() {
PolicyServiceImpl serviceImpl = new PolicyServiceImpl(entityManager);
setDbServiceRetrySettings(serviceImpl);
return serviceImpl;
}
/**
* Creates a {@link hirs.persist.ReportManager} ready to use.
* Creates a {@link hirs.persist.service.DeviceService} ready to use.
*
* @return {@link hirs.persist.ReportManager}
* @return {@link hirs.persist.service.DeviceService}
*/
@Bean
public ReportManager reportManager() {
DBReportManager manager = new DBReportManager(entityManager);
setDbManagerRetrySettings(manager);
return manager;
public DeviceService deviceService() {
DeviceServiceImpl serviceImpl = new DeviceServiceImpl(entityManager);
setDbServiceRetrySettings(serviceImpl);
return serviceImpl;
}
/**
* Creates a {@link hirs.persist.DeviceManager} ready to use.
* Creates a {@link hirs.persist.service.CertificateService} ready to use.
*
* @return {@link hirs.persist.DeviceManager}
* @return {@link hirs.persist.service.CertificateService}
*/
@Bean
public DeviceManager deviceManager() {
DBDeviceManager manager = new DBDeviceManager(entityManager);
setDbManagerRetrySettings(manager);
return manager;
public CertificateService certificateService() {
CertificateServiceImpl serviceImpl = new CertificateServiceImpl(entityManager);
setDbServiceRetrySettings(serviceImpl);
return serviceImpl;
}
/**
* Creates a {@link hirs.persist.ReportSummaryManager} ready to use.
* Creates a {@link hirs.persist.service.ReferenceManifestService} ready to use.
*
* @return {@link hirs.persist.ReportSummaryManager}
* @return {@link hirs.persist.service.ReferenceManifestService}
*/
@Bean
public ReportSummaryManager reportSummaryManager() {
DBReportSummaryManager manager = new DBReportSummaryManager(entityManager);
setDbManagerRetrySettings(manager);
return manager;
public ReferenceManifestService referenceManifestService() {
ReferenceManifestServiceImpl serviceImpl
= new ReferenceManifestServiceImpl(entityManager);
setDbServiceRetrySettings(serviceImpl);
return serviceImpl;
}
/**
* Creates a {@link hirs.persist.CertificateManager} ready to use.
* Creates a {@link hirs.persist.service.ReferenceDigestValueService} ready to use.
*
* @return {@link hirs.persist.CertificateManager}
* @return {@link hirs.persist.service.ReferenceDigestValueService}
*/
@Bean
public CertificateManager certificateManager() {
DBCertificateManager manager = new DBCertificateManager(entityManager);
manager.setRetryTemplate(maxTransactionRetryAttempts, retryWaitTimeMilliseconds);
return manager;
public ReferenceDigestValueService referenceEventService() {
ReferenceDigestValueServiceImpl serviceImpl
= new ReferenceDigestValueServiceImpl(entityManager);
setDbServiceRetrySettings(serviceImpl);
return serviceImpl;
}
/**
* Creates a {@link hirs.persist.ReferenceManifestManager} ready to use.
*
* @return {@link hirs.persist.ReferenceManifestManager}
*/
@Bean
public ReferenceManifestManager referenceManifestManager() {
DBReferenceManifestManager manager
= new DBReferenceManifestManager(entityManager);
setDbManagerRetrySettings(manager);
return manager;
}
/**
* Creates a {@link hirs.persist.ReferenceEventManager} ready to use.
*
* @return {@link hirs.persist.ReferenceEventManager}
*/
@Bean
public ReferenceEventManager referenceEventManager() {
DBReferenceEventManager manager
= new DBReferenceEventManager(entityManager);
setDbManagerRetrySettings(manager);
return manager;
}
/**
* Creates a {@link hirs.persist.ReportRequestStateManager} ready to use.
*
* @return {@link hirs.persist.ReportRequestStateManager}
*/
@Bean
public ReportRequestStateManager reportRequestStateManager() {
DBReportRequestStateManager manager
= new DBReportRequestStateManager(entityManager);
setDbManagerRetrySettings(manager);
return manager;
}
/**
* Creates a {@link hirs.persist.PortalInfoManager} ready to use.
*
* @return {@link hirs.persist.PortalInfoManager}
*/
@Bean
public PortalInfoManager portalInfoManager() {
DBPortalInfoManager manager = new DBPortalInfoManager(entityManager);
setDbManagerRetrySettings(manager);
return manager;
}
// /**
// * Creates a {@link hirs.persist.PortalInfoManager} ready to use.
// *
// * @return {@link hirs.persist.PortalInfoManager}
// */
// @Bean
// public PortalInfoManager portalInfoManager() {
// DBPortalInfoManager manager = new DBPortalInfoManager(entityManager);
// setDbServiceRetrySettings(manager);
// return manager;
// }
/**
* Creates a {@link hirs.attestationca.servicemanager.DBManager}
@ -178,20 +135,20 @@ public class PersistenceConfiguration {
*/
@Bean
public CrudManager<SupplyChainValidationSummary> supplyChainValidationSummaryManager() {
DBManager<SupplyChainValidationSummary> manager
= new DBManager<SupplyChainValidationSummary>(
DbServiceImpl<SupplyChainValidationSummary> manager
= new DbServiceImpl<>(
SupplyChainValidationSummary.class,
entityManager
);
setDbManagerRetrySettings(manager);
setDbServiceRetrySettings(manager);
return manager;
}
/**
* Apply the spring-wired retry template settings to the db manager.
* @param dbManager the manager to apply the retry settings to
* @param dbServiceImpl the service to apply the retry settings to
*/
private void setDbManagerRetrySettings(final DBManager dbManager) {
dbManager.setRetryTemplate(maxTransactionRetryAttempts, retryWaitTimeMilliseconds);
private void setDbServiceRetrySettings(final DbServiceImpl dbServiceImpl) {
dbServiceImpl.setRetryTemplate(maxTransactionRetryAttempts, retryWaitTimeMilliseconds);
}
}

View File

@ -7,10 +7,11 @@ import hirs.data.persist.certificate.CertificateAuthorityCredential;
import hirs.data.persist.certificate.EndorsementCredential;
import hirs.data.persist.certificate.IssuedAttestationCertificate;
import hirs.data.persist.certificate.PlatformCredential;
import hirs.persist.service.CertificateService;
import hirs.persist.CertificateSelector;
import hirs.persist.CriteriaModifier;
import hirs.persist.DBManagerException;
import hirs.persist.OrderedQuery;
import hirs.persist.service.CertificateService;
import hirs.persist.service.DefaultService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -19,8 +20,11 @@ import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
/**
@ -37,6 +41,13 @@ public class CertificateServiceImpl extends DbServiceImpl<Certificate>
@Autowired
private CertificateRepository certificateRepository;
/**
* Default constructor.
* @param em entity manager for jpa hibernate events
*/
public CertificateServiceImpl(final EntityManager em) {
}
@Override
public Certificate saveCertificate(final Certificate certificate) {
LOGGER.debug("Saving certificate: {}", certificate);
@ -73,6 +84,12 @@ public class CertificateServiceImpl extends DbServiceImpl<Certificate>
return saveCertificate(dbCertificate);
}
@Override
public <T extends Certificate> Set<T> getCertificate(
final CertificateSelector certificateSelector) {
return new HashSet<>(0);
}
@Override
public List<Certificate> getList() {
LOGGER.debug("Getting all certificates...");

View File

@ -16,6 +16,7 @@ import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -33,6 +34,13 @@ public class DeviceServiceImpl extends DbServiceImpl<Device> implements DefaultS
@Autowired
private DeviceRepository deviceRepository;
/**
* Default constructor.
* @param em entity manager for jpa hibernate events
*/
public DeviceServiceImpl(final EntityManager em) {
}
@Override
public final Device saveDevice(final Device device) throws DeviceManagerException {
LOGGER.debug("Saving device: {}", device);

View File

@ -15,6 +15,7 @@ import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -32,6 +33,12 @@ public class PolicyServiceImpl extends DbServiceImpl<Policy> implements DefaultS
@Autowired
private PolicyRepository policyRepository;
/**
* Default Constructor.
*/
public PolicyServiceImpl(final EntityManager em) {
}
@Override
public List<Policy> getList() {
LOGGER.debug("Getting all policies...");

View File

@ -15,6 +15,7 @@ import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -37,8 +38,7 @@ public class ReferenceDigestValueServiceImpl extends DbServiceImpl<ReferenceDige
/**
* Default Constructor.
*/
public ReferenceDigestValueServiceImpl() {
super();
public ReferenceDigestValueServiceImpl(final EntityManager em) {
}
@Override

View File

@ -15,6 +15,7 @@ import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -33,6 +34,13 @@ public class ReferenceManifestServiceImpl extends DbServiceImpl<ReferenceManifes
@Autowired
private ReferenceManifestRepository referenceManifestRepository;
/**
* Default constructor.
* @param em entity manager for jpa hibernate events
*/
public ReferenceManifestServiceImpl(final EntityManager em) {
}
@Override
public ReferenceManifest saveRIM(final ReferenceManifest rim) {
LOGGER.debug("Saving reference manifest: {}", rim);

View File

@ -2,7 +2,7 @@ package hirs.attestationca.portal.datatables;
import hirs.FilteredRecordsList;
import hirs.persist.CriteriaModifier;
import hirs.persist.OrderedListQuerier;
import hirs.persist.OrderedQuery;
import org.hibernate.Criteria;
import org.springframework.util.CollectionUtils;
@ -31,7 +31,7 @@ public final class OrderedListQueryDataTableAdapter<T> {
* @return the filtered record list
*/
public static <T> FilteredRecordsList<T> getOrderedList(final Class<T> clazz,
final OrderedListQuerier<T> dbManager,
final OrderedQuery<T> dbManager,
final DataTableInput dataTableInput,
final String orderColumnName) {
@ -54,8 +54,10 @@ public final class OrderedListQueryDataTableAdapter<T> {
* @param <T> the parameter type
* @return the filtered record list
*/
public static <T> FilteredRecordsList<T> getOrderedList(final Class<T> clazz,
final OrderedListQuerier<T> dbManager, final DataTableInput dataTableInput,
public static <T> FilteredRecordsList<T> getOrderedList(
final Class<T> clazz,
final OrderedQuery<T> dbManager,
final DataTableInput dataTableInput,
final String orderColumnName,
final CriteriaModifier criteriaModifier) {

View File

@ -8,14 +8,13 @@ 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.attestationca.service.ReferenceDigestValueServiceImpl;
import hirs.attestationca.servicemanager.DBReferenceManifestManager;
import hirs.data.persist.ReferenceDigestValue;
import hirs.data.persist.SupportReferenceManifest;
import hirs.data.persist.certificate.Certificate;
import hirs.persist.CriteriaModifier;
import hirs.persist.DBManagerException;
import hirs.attestationca.servicemanager.DBReferenceEventManager;
import hirs.attestationca.servicemanager.DBReferenceManifestManager;
import hirs.persist.ReferenceEventManager;
import hirs.persist.ReferenceManifestManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@ -47,7 +46,7 @@ public class RimDatabasePageController
private final BiosDateValidator biosValidator;
private final ReferenceManifestManager referenceManifestManager;
private final ReferenceEventManager referenceEventManager;
private final ReferenceDigestValueServiceImpl referenceDigestValueService;
private static final Logger LOGGER
= LogManager.getLogger(RimDatabasePageController.class);
@ -97,15 +96,15 @@ public class RimDatabasePageController
* Constructor providing the Page's display and routing specification.
*
* @param referenceManifestManager the ReferenceManifestManager object
* @param referenceEventManager the referenceEventManager object
* @param referenceDigestValueService the referenceDigestValueService object
*/
@Autowired
public RimDatabasePageController(
final DBReferenceManifestManager referenceManifestManager,
final DBReferenceEventManager referenceEventManager) {
final ReferenceDigestValueServiceImpl referenceDigestValueService) {
super(Page.RIM_DATABASE);
this.referenceManifestManager = referenceManifestManager;
this.referenceEventManager = referenceEventManager;
this.referenceDigestValueService = referenceDigestValueService;
this.biosValidator = new BiosDateValidator(BIOS_RELEASE_DATE_FORMAT);
}
@ -156,7 +155,7 @@ public class RimDatabasePageController
FilteredRecordsList<ReferenceDigestValue> referenceDigestValues =
OrderedListQueryDataTableAdapter.getOrderedList(
ReferenceDigestValue.class,
referenceEventManager,
referenceDigestValueService,
input, orderColumnName, criteriaModifier);
SupportReferenceManifest support;
@ -168,7 +167,7 @@ public class RimDatabasePageController
if (support != null) {
rdv.setBaseRimId(support.getAssociatedRim());
try {
referenceEventManager.updateEvent(rdv);
referenceDigestValueService.updateDigestValue(rdv, rdv.getId());
} catch (DBManagerException e) {
LOGGER.error("Failed to update TPM Event with Base RIM ID");
LOGGER.error(rdv);

View File

@ -13,10 +13,10 @@ import hirs.data.persist.certificate.Certificate;
import hirs.data.persist.certificate.PlatformCredential;
import hirs.data.persist.certificate.attributes.ComponentIdentifier;
import hirs.data.persist.certificate.attributes.V2.ComponentIdentifierV2;
import hirs.persist.CertificateManager;
import hirs.persist.CriteriaModifier;
import hirs.persist.CrudManager;
import hirs.persist.DeviceManager;
import hirs.persist.service.CertificateService;
import org.apache.logging.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
@ -60,7 +60,7 @@ public class ValidationReportsPageController extends PageController<NoPageParams
@Autowired
private final CrudManager<SupplyChainValidationSummary> supplyChainValidatorSummaryManager;
@Autowired
private final CertificateManager certificateManager;
private final CertificateService certificateService;
@Autowired
private final DeviceManager deviceManager;
@ -76,17 +76,17 @@ public class ValidationReportsPageController extends PageController<NoPageParams
/**
* Constructor providing the Page's display and routing specification.
* @param supplyChainValidatorSummaryManager the manager
* @param certificateManager the certificate manager
* @param certificateService the certificate service
* @param deviceManager the device manager
*/
@Autowired
public ValidationReportsPageController(
final CrudManager<SupplyChainValidationSummary> supplyChainValidatorSummaryManager,
final CertificateManager certificateManager,
final CertificateService certificateService,
final DeviceManager deviceManager) {
super(VALIDATION_REPORTS);
this.supplyChainValidatorSummaryManager = supplyChainValidatorSummaryManager;
this.certificateManager = certificateManager;
this.certificateService = certificateService;
this.deviceManager = deviceManager;
}
@ -266,7 +266,7 @@ public class ValidationReportsPageController extends PageController<NoPageParams
&& (createTimes.get(i).isBefore(endDate)
|| createTimes.get(i).isEqual(endDate))) {
UUID deviceId = deviceManager.getDevice(deviceNames[i]).getId();
PlatformCredential pc = PlatformCredential.select(certificateManager)
PlatformCredential pc = PlatformCredential.select(certificateService)
.byDeviceId(deviceId).getCertificate();
if (jsonVersion) {
jsonReportData.add(assembleJsonContent(pc, parseComponents(pc),
@ -381,7 +381,7 @@ public class ValidationReportsPageController extends PageController<NoPageParams
componentFailureString.append(pc.getComponentFailures());
// get all the certificates associated with the platform serial
List<PlatformCredential> chainCertificates = PlatformCredential
.select(certificateManager)
.select(certificateService)
.byBoardSerialNumber(pc.getPlatformSerial())
.getCertificates().stream().collect(Collectors.toList());
// combine all components in each certificate

View File

@ -1,51 +0,0 @@
package hirs;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
import java.util.Date;
/**
* Serializes <code>DeviceGroup</code> data for Json to process.
*/
public class DeviceGroupSerializer extends JsonSerializer<DeviceGroup> {
@Override
public void serialize(final DeviceGroup value, final JsonGenerator gen,
final SerializerProvider serializers) throws IOException {
gen.writeStartObject();
if (value.getId() != null) {
gen.writeStringField("id", value.getId().toString());
} else {
gen.writeNullField("id");
}
gen.writeNumberField("createTime", value.getCreateTime().getTime());
Date archivedTime = value.getArchivedTime();
if (archivedTime != null) {
gen.writeNumberField("archivedTime", archivedTime.getTime());
} else {
gen.writeNullField("archivedTime");
}
gen.writeStringField("archivedDescription", value.getArchivedDescription());
gen.writeStringField("name", value.getName());
gen.writeStringField("description", value.getDescription());
gen.writeNumberField("periodicReportDelayThreshold",
value.getPeriodicReportDelayThreshold());
gen.writeBooleanField("enablePeriodicReportDelayAlert",
value.isEnablePeriodicReportDelayAlert());
gen.writeNumberField("onDemandReportDelayThreshold",
value.getOnDemandReportDelayThreshold());
gen.writeBooleanField("enableOnDemandReportDelayAlert",
value.isEnableOnDemandReportDelayAlert());
gen.writeBooleanField("waitForAppraisalCompletionEnabled",
value.isWaitForAppraisalCompletionEnabled());
gen.writeObjectField("scheduledJobInfo", value.getScheduledJobInfo());
gen.writeNumberField("numberOfDevices", value.getNumberOfDevices());
gen.writeNumberField("numberOfTrustedDevices", value.getNumberOfTrustedDevices());
gen.writeStringField("healthStatus", value.getHealthStatus().toString());
gen.writeBooleanField("archived", value.isArchived());
gen.writeEndObject();
}
}

View File

@ -3,7 +3,6 @@ package hirs.data.persist;
import hirs.data.persist.enums.HealthStatus;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import hirs.DeviceGroupSerializer;
import hirs.foss.XMLCleaner;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

View File

@ -9,7 +9,7 @@ import java.util.List;
* Interface defining database CRUD operations (Create, Read, Update, Delete).
* @param <T> the object type, T.
*/
public interface CrudManager<T> extends OrderedListQuerier<T> {
public interface CrudManager<T> extends OrderedQuery<T> {
/**
*

View File

@ -1,7 +1,9 @@
package hirs.persist.service;
import hirs.data.persist.certificate.Certificate;
import hirs.persist.CertificateSelector;
import java.util.Set;
import java.util.UUID;
/**
@ -29,4 +31,13 @@ public interface CertificateService {
* @return a Certificate object
*/
Certificate updateCertificate(Certificate certificate, UUID uuid);
/**
* Retrieve Certificates according to the given {@link CertificateSelector}.
*
* @param <T> the type of certificate that will be retrieved
* @param certificateSelector a {@link CertificateSelector} to use for querying
* @return a Set of matching Certificates, which may be empty
*/
<T extends Certificate> Set<T> getCertificate(CertificateSelector certificateSelector);
}