Code cleanup

This commit is contained in:
anhefti 2023-03-30 12:13:22 +02:00
parent 340a61504d
commit 88a046379f
3 changed files with 55 additions and 8 deletions

View file

@ -22,29 +22,74 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.BulkActionSuppor
/** Concrete EntityDAO interface of Certificate entities */ /** Concrete EntityDAO interface of Certificate entities */
public interface CertificateDAO extends BulkActionSupportDAO<CertificateInfo> { public interface CertificateDAO extends BulkActionSupportDAO<CertificateInfo> {
/** Get the certificate with given alias for specified institution.
*
* @param institutionId Institution identifier
* @param alias the alias name of the certificate to get
* @return Result refer to the Certificate or to an error when happened. */
Result<Certificate> getCertificate(final Long institutionId, String alias); Result<Certificate> getCertificate(final Long institutionId, String alias);
/** Get all certificates of a given institution,
*
* @param institutionId Institution identifier
* @return Result to the Certificates or to an error when happend */
Result<Certificates> getCertificates(Long institutionId); Result<Certificates> getCertificates(Long institutionId);
/** Add a new uploaded certificate to the certificate store of the institution.
*
* @param institutionId Institution identifier
* @param alias the alias name of the institution
* @param certificate the certificate to add.
* @return Result refer to the generated CertificateInfo or to an error when happened */
Result<CertificateInfo> addCertificate( Result<CertificateInfo> addCertificate(
Long institutionId, Long institutionId,
String alias, String alias,
Certificate certificate); Certificate certificate);
/** Add a new uploaded certificate with private key to the certificate store of the institution.
*
* @param institutionId Institution identifier
* @param alias the alias name of the institution
* @param certificate the certificate to add.
* @param privateKey the private key of the certificate
* @return Result refer to the generated CertificateInfo or to an error when happened */
Result<CertificateInfo> addCertificate( Result<CertificateInfo> addCertificate(
Long institutionId, Long institutionId,
String alias, String alias,
Certificate certificate, Certificate certificate,
PrivateKey privateKey); PrivateKey privateKey);
/** Removes specified certificate from the certificate store of a given institution.
*
* @param institutionId The institution identifier
* @param alias the alias name of the certificate
* @return Result refer to the entity key of the removed certificate or to an error when happened */
Result<EntityKey> removeCertificate(Long institutionId, String alias); Result<EntityKey> removeCertificate(Long institutionId, String alias);
/** Get all alias names of all certificated that exists for a given institution.
*
* @param institutionId The institution identifier
* @return Result refer to the collection of all certificate alias names or to an error when happened */
Result<Collection<String>> getAllIdentityAlias(Long institutionId); Result<Collection<String>> getAllIdentityAlias(Long institutionId);
/** Get the certification information for a specific certificate from the the given Certificates.
*
* @param certificates The certificates bucket to get the info from
* @param alias the alias name of the certificate to get the info from
* @return Result refer to the certificate info or to an error when happened. */
Result<CertificateInfo> getDataFromCertificate(Certificates certificates, String alias); Result<CertificateInfo> getDataFromCertificate(Certificates certificates, String alias);
/** Get a collection of all alias names of all identity certificates for a given institution.
*
* @param institutionId The institution identifier
* @return Result refer to the collection of certificate alias or to an error when happened */
Result<Collection<String>> getIdentityAlias(Long institutionId); Result<Collection<String>> getIdentityAlias(Long institutionId);
String extractAlias(X509Certificate a, String alias); /** Get or extract the alias name of a given certificate. If there is not given a explicit alias name
* within the certificate, this will create one generic from the data that is available.
*
* @param certificate The X509Certificate to extract the alias name from
* @return the extracted alias */
String extractAlias(X509Certificate certificate);
} }

View file

@ -180,7 +180,7 @@ public class CertificateDAOImpl implements CertificateDAO {
final X509Certificate cert = certificate; final X509Certificate cert = certificate;
return new CertificateInfo( return new CertificateInfo(
extractAlias(cert, alias), StringUtils.isNotBlank(alias) ? alias : extractAlias(cert),
new DateTime(cert.getNotBefore()), new DateTime(cert.getNotBefore()),
new DateTime(cert.getNotAfter()), new DateTime(cert.getNotAfter()),
getTypes(certificates, cert)); getTypes(certificates, cert));
@ -224,10 +224,7 @@ public class CertificateDAOImpl implements CertificateDAO {
} }
@Override @Override
public String extractAlias(final X509Certificate certificate, final String alias) { public String extractAlias(final X509Certificate certificate) {
if (StringUtils.isNotBlank(alias)) {
return alias;
}
try { try {
final X500Name x500name = new JcaX509CertificateHolder(certificate).getSubject(); final X500Name x500name = new JcaX509CertificateHolder(certificate).getSubject();

View file

@ -41,6 +41,7 @@ import ch.ethz.seb.sebserver.webservice.servicelayer.dao.CertificateDAO;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.FilterMap; import ch.ethz.seb.sebserver.webservice.servicelayer.dao.FilterMap;
import ch.ethz.seb.sebserver.webservice.servicelayer.dao.SEBClientConfigDAO; import ch.ethz.seb.sebserver.webservice.servicelayer.dao.SEBClientConfigDAO;
import ch.ethz.seb.sebserver.webservice.servicelayer.institution.CertificateService; import ch.ethz.seb.sebserver.webservice.servicelayer.institution.CertificateService;
import io.micrometer.core.instrument.util.StringUtils;
@Lazy @Lazy
@Service @Service
@ -97,14 +98,18 @@ public class CertificateServiceImpl implements CertificateService {
return loadCertFromPEM(in) return loadCertFromPEM(in)
.flatMap(cert -> this.certificateDAO.addCertificate( .flatMap(cert -> this.certificateDAO.addCertificate(
institutionId, institutionId,
this.certificateDAO.extractAlias(cert, alias), StringUtils.isNotBlank(alias)
? alias
: this.certificateDAO.extractAlias(cert),
cert)); cert));
case PKCS12: case PKCS12:
return loadCertFromPKC(in, password) return loadCertFromPKC(in, password)
.flatMap(pair -> this.certificateDAO.addCertificate( .flatMap(pair -> this.certificateDAO.addCertificate(
institutionId, institutionId,
this.certificateDAO.extractAlias(pair.a, alias), StringUtils.isNotBlank(alias)
? alias
: this.certificateDAO.extractAlias(pair.a),
pair.a, pair.a,
pair.b)); pair.b));
default: default: