diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/repository/PolicyRepository.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/repository/PolicyRepository.java new file mode 100644 index 00000000..a5cf331c --- /dev/null +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/repository/PolicyRepository.java @@ -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 { +} diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/CertificateService.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/CertificateService.java index 57b987c7..8fa9611b 100644 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/CertificateService.java +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/CertificateService.java @@ -13,7 +13,7 @@ public interface CertificateService { /** * Saves the Certificate in the database. This creates a new - * database session and saves the device. + * database session and saves the certificate. * * @param certificate Certificate to save * @return reference to saved certificate diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/CertificateServiceImpl.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/CertificateServiceImpl.java index 43d34e8b..16a5bc93 100644 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/CertificateServiceImpl.java +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/CertificateServiceImpl.java @@ -3,6 +3,10 @@ package hirs.attestationca.service; import hirs.FilteredRecordsList; import hirs.attestationca.repository.CertificateRepository; 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.DBManagerException; import hirs.persist.OrderedQuery; @@ -21,10 +25,11 @@ import java.util.UUID; * support for the basic create, read, update, and delete methods. */ @Service -public class CertificateServiceImpl implements DefaultService, +public class CertificateServiceImpl extends DbServiceImpl + implements DefaultService, CertificateService, OrderedQuery { - private static final Logger LOGGER = LogManager.getLogger(); + private static final Logger LOGGER = LogManager.getLogger(CertificateServiceImpl.class); @Autowired private CertificateRepository certificateRepository; @@ -49,6 +54,8 @@ public class CertificateServiceImpl implements DefaultService, // run through things that aren't equal and update + getCertificateClass(dbCertificate); // need to coming + } certificateRepository.save(dbCertificate); @@ -62,6 +69,17 @@ public class CertificateServiceImpl implements DefaultService, return this.certificateRepository.findAll(); } + @Override + public void updateElements(final List certificates) { + LOGGER.debug("Updating {} certificates...", certificates.size()); + + certificates.stream().forEach((certificate) -> { + if (certificate != null) { + this.updateCertificate(certificate, certificate.getId()); + } + }); + } + @Override public void deleteObjectById(final UUID uuid) { LOGGER.debug("Deleting certificate by id: {}", uuid); @@ -86,4 +104,24 @@ public class CertificateServiceImpl implements DefaultService, throws DBManagerException { 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 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; + } + } } diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DbServiceImpl.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DbServiceImpl.java new file mode 100644 index 00000000..6e2bccb5 --- /dev/null +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DbServiceImpl.java @@ -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 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 { + 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 DBManager 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, 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); + } +} diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DefaultService.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DefaultService.java index c5151696..950033c4 100644 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DefaultService.java +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DefaultService.java @@ -19,6 +19,13 @@ public interface DefaultService { */ List 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 elements); + /** * Deletes the T from the database. This removes all * of the database entries that stored information with regards to the diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DeviceService.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DeviceService.java index 8f1b9420..496b56a6 100644 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DeviceService.java +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DeviceService.java @@ -3,7 +3,6 @@ package hirs.attestationca.service; import hirs.data.persist.Device; import hirs.persist.DeviceManagerException; -import java.util.List; import java.util.UUID; /** @@ -40,17 +39,4 @@ public interface DeviceService { * @return a device object */ Device updateDevice(Device device, UUID deviceId) throws DeviceManagerException; - - /** - * Updates list of Devices. 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 deviceList) throws DeviceManagerException; } diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DeviceServiceImpl.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DeviceServiceImpl.java index 911d1431..3af29b11 100644 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DeviceServiceImpl.java +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/DeviceServiceImpl.java @@ -22,10 +22,10 @@ import java.util.UUID; * support for the basic create, read, update, and delete methods. */ @Service -public class DeviceServiceImpl implements DefaultService, +public class DeviceServiceImpl extends DbServiceImpl implements DefaultService, DeviceService, OrderedQuery { - private static final Logger LOGGER = LogManager.getLogger(); + private static final Logger LOGGER = LogManager.getLogger(DeviceServiceImpl.class); @Autowired private DeviceRepository deviceRepository; @@ -50,6 +50,8 @@ public class DeviceServiceImpl implements DefaultService, // run through things that aren't equal and update + + } deviceRepository.save(dbDevice); @@ -58,23 +60,22 @@ public class DeviceServiceImpl implements DefaultService, } @Override - public final void updateDeviceList(final List deviceList) - throws DeviceManagerException { - LOGGER.debug("Updating {} devices...", deviceList.size()); + public final List getList() { + LOGGER.debug("Getting all devices..."); + return deviceRepository.findAll(); + } - deviceList.stream().forEach((device) -> { + @Override + public void updateElements(final List devices) { + LOGGER.debug("Updating {} devices...", devices.size()); + + devices.stream().forEach((device) -> { if (device != null) { this.updateDevice(device, device.getId()); } }); } - @Override - public final List getList() { - LOGGER.debug("Getting all devices..."); - return deviceRepository.findAll(); - } - @Override public final void deleteObjectById(final UUID uuid) throws DeviceManagerException { diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/PolicyService.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/PolicyService.java new file mode 100644 index 00000000..5be5a51f --- /dev/null +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/PolicyService.java @@ -0,0 +1,32 @@ +package hirs.attestationca.service; + +import hirs.data.persist.Policy; + +import java.util.UUID; + +/** + * A PolicyService manages Policys. A + * PolicyService is used to store and manage policies. It has + * support for the basic create, read, update, and delete methods. + */ +public interface PolicyService { + + /** + * Saves the Policy 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 Policy. 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); +} diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/PolicyServiceImpl.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/PolicyServiceImpl.java new file mode 100644 index 00000000..6d8bdf2c --- /dev/null +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/PolicyServiceImpl.java @@ -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 PolicyServiceImpl manages Policys. A + * PolicyServiceImpl 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 implements DefaultService, + PolicyService, OrderedQuery { + + private static final Logger LOGGER = LogManager.getLogger(PolicyServiceImpl.class); + @Autowired + private PolicyRepository policyRepository; + + @Override + public List getList() { + LOGGER.debug("Getting all policies..."); + return this.policyRepository.findAll(); + } + + @Override + public void updateElements(final List 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 clazz, final String columnToOrder, + final boolean ascending, final int firstResult, final int maxResults, + final String search, final Map searchableColumns) + throws DBManagerException { + return null; + } + + @Override + public FilteredRecordsList getOrderedList( + final Class clazz, final String columnToOrder, + final boolean ascending, final int firstResult, final int maxResults, + final String search, final Map searchableColumns, + final CriteriaModifier criteriaModifier) + throws DBManagerException { + return null; + } +} diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/ReferenceDigestValueServiceImpl.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/ReferenceDigestValueServiceImpl.java index 8da0c2df..1ec32bf1 100644 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/ReferenceDigestValueServiceImpl.java +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/ReferenceDigestValueServiceImpl.java @@ -1,20 +1,114 @@ package hirs.attestationca.service; +import hirs.FilteredRecordsList; 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.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 ReferenceDigestValueServiceImpl manages Digest Value Events. A * ReferenceDigestValueServiceImpl is used to store and manage digest events. It has * support for the basic create, read, update, and delete methods. */ @Service -public class ReferenceDigestValueServiceImpl { +public class ReferenceDigestValueServiceImpl extends DbServiceImpl + implements DefaultService, + ReferenceDigestValueService, OrderedQuery { - private static final Logger LOGGER = LogManager.getLogger(); + private static final Logger LOGGER = LogManager + .getLogger(ReferenceDigestValueServiceImpl.class); @Autowired private ReferenceDigestValueRepository referenceDigestValueRepository; + + /** + * Default Constructor. + */ + public ReferenceDigestValueServiceImpl() { + super(); + } + + @Override + public List getList() { + LOGGER.debug("Getting all reference digest value..."); + return this.referenceDigestValueRepository.findAll(); + } + + @Override + public void updateElements(final List 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 clazz, final String columnToOrder, + final boolean ascending, final int firstResult, final int maxResults, + final String search, final Map searchableColumns) + throws DBManagerException { + return null; + } + + @Override + public FilteredRecordsList getOrderedList( + final Class clazz, final String columnToOrder, + final boolean ascending, final int firstResult, final int maxResults, + final String search, final Map searchableColumns, + final CriteriaModifier criteriaModifier) + throws DBManagerException { + return null; + } } diff --git a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/ReferenceManifestServiceImpl.java b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/ReferenceManifestServiceImpl.java index 0a70d045..5a3b1b9d 100644 --- a/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/ReferenceManifestServiceImpl.java +++ b/HIRS_AttestationCA/src/main/java/hirs/attestationca/service/ReferenceManifestServiceImpl.java @@ -21,10 +21,11 @@ import java.util.UUID; * support for the basic create, read, update, and delete methods. */ @Service -public class ReferenceManifestServiceImpl implements DefaultService, +public class ReferenceManifestServiceImpl extends DbServiceImpl + implements DefaultService, ReferenceManifestService, OrderedQuery { - private static final Logger LOGGER = LogManager.getLogger(); + private static final Logger LOGGER = LogManager.getLogger(ReferenceManifestServiceImpl.class); @Autowired private ReferenceManifestRepository referenceManifestRepository; @@ -62,6 +63,17 @@ public class ReferenceManifestServiceImpl implements DefaultService referenceManifests) { + LOGGER.debug("Updating {} reference manifests...", referenceManifests.size()); + + referenceManifests.stream().forEach((rim) -> { + if (rim != null) { + this.updateReferenceManifest(rim, rim.getId()); + } + }); + } + @Override public void deleteObjectById(final UUID uuid) { LOGGER.debug("Deleting reference manifest by id: {}", uuid);