Some more updates.

This commit is contained in:
Cyrus 2022-08-02 08:29:49 -04:00
parent fa7eef6857
commit 228a5e56bc
11 changed files with 388 additions and 33 deletions

View File

@ -0,0 +1,14 @@
package hirs.attestationca.repository;
import hirs.data.persist.Policy;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.UUID;
/**
* Setting up for new creation for CRUD operations.
*/
@Repository
public interface PolicyRepository extends JpaRepository<Policy, UUID> {
}

View File

@ -13,7 +13,7 @@ public interface CertificateService {
/** /**
* Saves the <code>Certificate</code> in the database. This creates a new * Saves the <code>Certificate</code> in the database. This creates a new
* database session and saves the device. * database session and saves the certificate.
* *
* @param certificate Certificate to save * @param certificate Certificate to save
* @return reference to saved certificate * @return reference to saved certificate

View File

@ -3,6 +3,10 @@ package hirs.attestationca.service;
import hirs.FilteredRecordsList; import hirs.FilteredRecordsList;
import hirs.attestationca.repository.CertificateRepository; import hirs.attestationca.repository.CertificateRepository;
import hirs.data.persist.certificate.Certificate; import hirs.data.persist.certificate.Certificate;
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.CriteriaModifier; import hirs.persist.CriteriaModifier;
import hirs.persist.DBManagerException; import hirs.persist.DBManagerException;
import hirs.persist.OrderedQuery; import hirs.persist.OrderedQuery;
@ -21,10 +25,11 @@ import java.util.UUID;
* support for the basic create, read, update, and delete methods. * support for the basic create, read, update, and delete methods.
*/ */
@Service @Service
public class CertificateServiceImpl implements DefaultService<Certificate>, public class CertificateServiceImpl extends DbServiceImpl<Certificate>
implements DefaultService<Certificate>,
CertificateService, OrderedQuery<Certificate> { CertificateService, OrderedQuery<Certificate> {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger(CertificateServiceImpl.class);
@Autowired @Autowired
private CertificateRepository certificateRepository; private CertificateRepository certificateRepository;
@ -49,6 +54,8 @@ public class CertificateServiceImpl implements DefaultService<Certificate>,
// run through things that aren't equal and update // run through things that aren't equal and update
getCertificateClass(dbCertificate); // need to coming
} }
certificateRepository.save(dbCertificate); certificateRepository.save(dbCertificate);
@ -62,6 +69,17 @@ public class CertificateServiceImpl implements DefaultService<Certificate>,
return this.certificateRepository.findAll(); return this.certificateRepository.findAll();
} }
@Override
public void updateElements(final List<Certificate> certificates) {
LOGGER.debug("Updating {} certificates...", certificates.size());
certificates.stream().forEach((certificate) -> {
if (certificate != null) {
this.updateCertificate(certificate, certificate.getId());
}
});
}
@Override @Override
public void deleteObjectById(final UUID uuid) { public void deleteObjectById(final UUID uuid) {
LOGGER.debug("Deleting certificate by id: {}", uuid); LOGGER.debug("Deleting certificate by id: {}", uuid);
@ -86,4 +104,24 @@ public class CertificateServiceImpl implements DefaultService<Certificate>,
throws DBManagerException { throws DBManagerException {
return null; return null;
} }
/**
* Gets the concrete certificate class type to query for.
*
* @param certificate the instance of the certificate to get type.
* @return the certificate class type
*/
private Class<? extends Certificate> getCertificateClass(final Certificate certificate) {
if (certificate instanceof PlatformCredential) {
return PlatformCredential.class;
} else if (certificate instanceof EndorsementCredential) {
return EndorsementCredential.class;
} else if (certificate instanceof CertificateAuthorityCredential) {
return CertificateAuthorityCredential.class;
} else if (certificate instanceof IssuedAttestationCertificate) {
return IssuedAttestationCertificate.class;
} else {
return null;
}
}
} }

View File

@ -0,0 +1,72 @@
package hirs.attestationca.service;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.StaleObjectStateException;
import org.hibernate.exception.LockAcquisitionException;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
/**
* @param <T> passed in type
* Generic database manager for managing objects in a database. This provides create, read, update,
* archive, and delete operations for managing objects in a database.
*
*/
@Service
public class DbServiceImpl<T> {
private static final Logger LOGGER = LogManager.getLogger(DbServiceImpl.class);
/**
* The default maximum number of retries to attempt a database transaction.
*/
public static final int DEFAULT_MAX_RETRY_ATTEMPTS = 10;
/*
* The default number of milliseconds to wait before retrying a database transaction.
*/
private static final long DEFAULT_RETRY_WAIT_TIME_MS = 3000;
// structure for retrying methods in the database
private RetryTemplate retryTemplate;
/**
* Creates a new <code>DBManager</code> that uses the default database. The
* default database is used to store all of the objects.
*
*/
public DbServiceImpl() {
setRetryTemplate(DEFAULT_MAX_RETRY_ATTEMPTS, DEFAULT_RETRY_WAIT_TIME_MS);
}
/**
* Set the parameters used to retry database transactions. The retry template will
* retry transactions that throw a LockAcquisitionException or StaleObjectStateException.
* @param maxTransactionRetryAttempts the maximum number of database transaction attempts
* @param retryWaitTimeMilliseconds the transaction retry wait time in milliseconds
*/
public final void setRetryTemplate(final int maxTransactionRetryAttempts,
final long retryWaitTimeMilliseconds) {
Map<Class<? extends Throwable>, Boolean> exceptionsToRetry = new HashMap<>();
exceptionsToRetry.put(LockAcquisitionException.class, true);
exceptionsToRetry.put(StaleObjectStateException.class, true);
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(
maxTransactionRetryAttempts,
exceptionsToRetry,
true,
false
);
FixedBackOffPolicy backoffPolicy = new FixedBackOffPolicy();
backoffPolicy.setBackOffPeriod(retryWaitTimeMilliseconds);
this.retryTemplate = new RetryTemplate();
this.retryTemplate.setRetryPolicy(retryPolicy);
this.retryTemplate.setBackOffPolicy(backoffPolicy);
}
}

View File

@ -19,6 +19,13 @@ public interface DefaultService<T> {
*/ */
List<T> getList(); List<T> getList();
/**
* All passed in objects of type T will either be updated.
* However if the element doesn't exist, it will be saved.
* @param elements list of objects to save
*/
void updateElements(List<T> elements);
/** /**
* Deletes the <code>T</code> from the database. This removes all * Deletes the <code>T</code> from the database. This removes all
* of the database entries that stored information with regards to the * of the database entries that stored information with regards to the

View File

@ -3,7 +3,6 @@ package hirs.attestationca.service;
import hirs.data.persist.Device; import hirs.data.persist.Device;
import hirs.persist.DeviceManagerException; import hirs.persist.DeviceManagerException;
import java.util.List;
import java.util.UUID; import java.util.UUID;
/** /**
@ -40,17 +39,4 @@ public interface DeviceService {
* @return a device object * @return a device object
*/ */
Device updateDevice(Device device, UUID deviceId) throws DeviceManagerException; Device updateDevice(Device device, UUID deviceId) throws DeviceManagerException;
/**
* Updates list of <code>Device</code>s. This updates the database entries
* to reflect the new values that should be set. Commonly used when
* deleting a DeviceGroup.
*
* @param deviceList
* list of devices that should be updated in single transaction
* @throws DeviceManagerException
* if device has not previously been saved or an error occurs
* while trying to save it to the database
*/
void updateDeviceList(List<Device> deviceList) throws DeviceManagerException;
} }

View File

@ -22,10 +22,10 @@ import java.util.UUID;
* support for the basic create, read, update, and delete methods. * support for the basic create, read, update, and delete methods.
*/ */
@Service @Service
public class DeviceServiceImpl implements DefaultService<Device>, public class DeviceServiceImpl extends DbServiceImpl<Device> implements DefaultService<Device>,
DeviceService, OrderedQuery<Device> { DeviceService, OrderedQuery<Device> {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger(DeviceServiceImpl.class);
@Autowired @Autowired
private DeviceRepository deviceRepository; private DeviceRepository deviceRepository;
@ -50,6 +50,8 @@ public class DeviceServiceImpl implements DefaultService<Device>,
// run through things that aren't equal and update // run through things that aren't equal and update
} }
deviceRepository.save(dbDevice); deviceRepository.save(dbDevice);
@ -58,23 +60,22 @@ public class DeviceServiceImpl implements DefaultService<Device>,
} }
@Override @Override
public final void updateDeviceList(final List<Device> deviceList) public final List<Device> getList() {
throws DeviceManagerException { LOGGER.debug("Getting all devices...");
LOGGER.debug("Updating {} devices...", deviceList.size()); return deviceRepository.findAll();
}
deviceList.stream().forEach((device) -> { @Override
public void updateElements(final List<Device> devices) {
LOGGER.debug("Updating {} devices...", devices.size());
devices.stream().forEach((device) -> {
if (device != null) { if (device != null) {
this.updateDevice(device, device.getId()); this.updateDevice(device, device.getId());
} }
}); });
} }
@Override
public final List<Device> getList() {
LOGGER.debug("Getting all devices...");
return deviceRepository.findAll();
}
@Override @Override
public final void deleteObjectById(final UUID uuid) public final void deleteObjectById(final UUID uuid)
throws DeviceManagerException { throws DeviceManagerException {

View File

@ -0,0 +1,32 @@
package hirs.attestationca.service;
import hirs.data.persist.Policy;
import java.util.UUID;
/**
* A <code>PolicyService</code> manages <code>Policy</code>s. A
* <code>PolicyService</code> is used to store and manage policies. It has
* support for the basic create, read, update, and delete methods.
*/
public interface PolicyService {
/**
* Saves the <code>Policy</code> in the database. This creates a new
* database session and saves the policy.
*
* @param policy Policy to save
* @return reference to saved policy
*/
Policy savePolicy(Policy policy);
/**
* Updates a <code>Policy</code>. This updates the database entries to
* reflect the new values that should be set.
*
* @param policy Policy object to save
* @param uuid UUID for the database object
* @return a Policy object
*/
Policy updatePolicy(Policy policy, UUID uuid);
}

View File

@ -0,0 +1,99 @@
package hirs.attestationca.service;
import hirs.FilteredRecordsList;
import hirs.attestationca.repository.PolicyRepository;
import hirs.data.persist.Policy;
import hirs.persist.CriteriaModifier;
import hirs.persist.DBManagerException;
import hirs.persist.OrderedQuery;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* A <code>PolicyServiceImpl</code> manages <code>Policy</code>s. A
* <code>PolicyServiceImpl</code> is used to store and manage policies. It has
* support for the basic create, read, update, and delete methods.
*/
@Service
public class PolicyServiceImpl extends DbServiceImpl<Policy> implements DefaultService<Policy>,
PolicyService, OrderedQuery<Policy> {
private static final Logger LOGGER = LogManager.getLogger(PolicyServiceImpl.class);
@Autowired
private PolicyRepository policyRepository;
@Override
public List<Policy> getList() {
LOGGER.debug("Getting all policies...");
return this.policyRepository.findAll();
}
@Override
public void updateElements(final List<Policy> policies) {
LOGGER.debug("Updating {} certificates...", policies.size());
policies.stream().forEach((policy) -> {
if (policy != null) {
this.updatePolicy(policy, policy.getId());
}
});
}
@Override
public void deleteObjectById(final UUID uuid) {
LOGGER.debug("Deleting policy by id: {}", uuid);
this.policyRepository.deleteById(uuid);
}
@Override
public Policy savePolicy(final Policy policy) {
LOGGER.debug("Saving policy: {}", policy);
return policyRepository.save(policy);
}
@Override
public Policy updatePolicy(final Policy policy, final UUID uuid) {
LOGGER.debug("Updating policy: {}", policy);
Policy dbPolicy;
if (uuid == null) {
LOGGER.debug("Policy not found: {}", policy);
dbPolicy = policy;
} else {
// will not return null, throws and exception
dbPolicy = policyRepository.getReferenceById(uuid);
// run through things that aren't equal and update
}
policyRepository.save(dbPolicy);
return dbPolicy;
}
@Override
public FilteredRecordsList getOrderedList(
final Class<Policy> clazz, final String columnToOrder,
final boolean ascending, final int firstResult, final int maxResults,
final String search, final Map<String, Boolean> searchableColumns)
throws DBManagerException {
return null;
}
@Override
public FilteredRecordsList<Policy> getOrderedList(
final Class<Policy> clazz, final String columnToOrder,
final boolean ascending, final int firstResult, final int maxResults,
final String search, final Map<String, Boolean> searchableColumns,
final CriteriaModifier criteriaModifier)
throws DBManagerException {
return null;
}
}

View File

@ -1,20 +1,114 @@
package hirs.attestationca.service; package hirs.attestationca.service;
import hirs.FilteredRecordsList;
import hirs.attestationca.repository.ReferenceDigestValueRepository; import hirs.attestationca.repository.ReferenceDigestValueRepository;
import hirs.data.persist.ReferenceDigestValue;
import hirs.persist.CriteriaModifier;
import hirs.persist.DBManagerException;
import hirs.persist.OrderedQuery;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/** /**
* A <code>ReferenceDigestValueServiceImpl</code> manages <code>Digest Value Event</code>s. A * A <code>ReferenceDigestValueServiceImpl</code> manages <code>Digest Value Event</code>s. A
* <code>ReferenceDigestValueServiceImpl</code> is used to store and manage digest events. It has * <code>ReferenceDigestValueServiceImpl</code> is used to store and manage digest events. It has
* support for the basic create, read, update, and delete methods. * support for the basic create, read, update, and delete methods.
*/ */
@Service @Service
public class ReferenceDigestValueServiceImpl { public class ReferenceDigestValueServiceImpl extends DbServiceImpl<ReferenceDigestValue>
implements DefaultService<ReferenceDigestValue>,
ReferenceDigestValueService, OrderedQuery<ReferenceDigestValue> {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager
.getLogger(ReferenceDigestValueServiceImpl.class);
@Autowired @Autowired
private ReferenceDigestValueRepository referenceDigestValueRepository; private ReferenceDigestValueRepository referenceDigestValueRepository;
/**
* Default Constructor.
*/
public ReferenceDigestValueServiceImpl() {
super();
}
@Override
public List<ReferenceDigestValue> getList() {
LOGGER.debug("Getting all reference digest value...");
return this.referenceDigestValueRepository.findAll();
}
@Override
public void updateElements(final List<ReferenceDigestValue> referenceDigestValues) {
LOGGER.debug("Updating {} reference digest values...", referenceDigestValues.size());
referenceDigestValues.stream().forEach((values) -> {
if (values != null) {
this.updateDigestValue(values, values.getId());
}
});
}
@Override
public void deleteObjectById(final UUID uuid) {
LOGGER.debug("Deleting reference digest values by id: {}", uuid);
this.referenceDigestValueRepository.deleteById(uuid);
}
@Override
public ReferenceDigestValue saveDigestValue(final ReferenceDigestValue digestValue) {
LOGGER.debug("Saving reference digest value: {}", digestValue);
return this.referenceDigestValueRepository.save(digestValue);
}
@Override
public ReferenceDigestValue updateDigestValue(
final ReferenceDigestValue digestValue, final UUID uuid) {
LOGGER.debug("Updating reference digest value: {}", digestValue);
ReferenceDigestValue dbDigestValue;
if (uuid == null) {
LOGGER.debug("Reference Digest Value not found: {}", digestValue);
dbDigestValue = digestValue;
} else {
// will not return null, throws and exception
dbDigestValue = this.referenceDigestValueRepository.getReferenceById(uuid);
// run through things that aren't equal and update
if (!dbDigestValue.getDigestValue().equals(digestValue.getDigestValue())) {
dbDigestValue.setDigestValue(digestValue.getDigestValue());
}
if (!dbDigestValue.getEventType().equals(digestValue.getEventType())) {
dbDigestValue.setEventType(digestValue.getEventType());
}
}
this.referenceDigestValueRepository.save(dbDigestValue);
return dbDigestValue;
}
@Override
public FilteredRecordsList getOrderedList(
final Class<ReferenceDigestValue> clazz, final String columnToOrder,
final boolean ascending, final int firstResult, final int maxResults,
final String search, final Map<String, Boolean> searchableColumns)
throws DBManagerException {
return null;
}
@Override
public FilteredRecordsList<ReferenceDigestValue> getOrderedList(
final Class<ReferenceDigestValue> clazz, final String columnToOrder,
final boolean ascending, final int firstResult, final int maxResults,
final String search, final Map<String, Boolean> searchableColumns,
final CriteriaModifier criteriaModifier)
throws DBManagerException {
return null;
}
} }

View File

@ -21,10 +21,11 @@ import java.util.UUID;
* support for the basic create, read, update, and delete methods. * support for the basic create, read, update, and delete methods.
*/ */
@Service @Service
public class ReferenceManifestServiceImpl implements DefaultService<ReferenceManifest>, public class ReferenceManifestServiceImpl extends DbServiceImpl<ReferenceManifest>
implements DefaultService<ReferenceManifest>,
ReferenceManifestService, OrderedQuery<ReferenceManifest> { ReferenceManifestService, OrderedQuery<ReferenceManifest> {
private static final Logger LOGGER = LogManager.getLogger(); private static final Logger LOGGER = LogManager.getLogger(ReferenceManifestServiceImpl.class);
@Autowired @Autowired
private ReferenceManifestRepository referenceManifestRepository; private ReferenceManifestRepository referenceManifestRepository;
@ -62,6 +63,17 @@ public class ReferenceManifestServiceImpl implements DefaultService<ReferenceMan
return this.referenceManifestRepository.findAll(); return this.referenceManifestRepository.findAll();
} }
@Override
public void updateElements(final List<ReferenceManifest> referenceManifests) {
LOGGER.debug("Updating {} reference manifests...", referenceManifests.size());
referenceManifests.stream().forEach((rim) -> {
if (rim != null) {
this.updateReferenceManifest(rim, rim.getId());
}
});
}
@Override @Override
public void deleteObjectById(final UUID uuid) { public void deleteObjectById(final UUID uuid) {
LOGGER.debug("Deleting reference manifest by id: {}", uuid); LOGGER.debug("Deleting reference manifest by id: {}", uuid);