Add certificate device identifier update task

pull/341/head
Pahansith Gunathilake 10 months ago
parent 521d3e9623
commit ebf9e4f513

@ -103,4 +103,7 @@ public interface CertificateDAO {
List<CertificateResponse> searchCertificate(String serialNumber) throws CertificateManagementDAOException;
List<CertificateResponse> retrieveEmptyDeviceIdCerts() throws CertificateManagementDAOException;
int updateDeviceIdentifier(CertificateResponse cert) throws CertificateManagementDAOException;
}

@ -229,6 +229,60 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{
return certificates;
}
@Override
public List<CertificateResponse> retrieveEmptyDeviceIdCerts() throws CertificateManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet resultSet = null;
CertificateResponse certificateResponse = null;
List<CertificateResponse> certificates = new ArrayList<>();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try {
conn = this.getConnection();
String query =
"SELECT ID, CERTIFICATE FROM DM_DEVICE_CERTIFICATE "
+ "WHERE DEVICE_IDENTIFIER is NULL";
stmt = conn.prepareStatement(query);
resultSet = stmt.executeQuery();
while (resultSet.next()) {
certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
certificateResponse.setId(resultSet.getInt("ID"));
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
certificates.add(certificateResponse);
}
} catch (SQLException e) {
String errorMsg =
"Error while reading null device identifier certificates";
log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
}
return certificates;
}
@Override
public int updateDeviceIdentifier(CertificateResponse cert) throws CertificateManagementDAOException {
Connection conn;
PreparedStatement stmt = null;
try {
conn = this.getConnection();
stmt = conn.prepareStatement(
"UPDATE DM_DEVICE_CERTIFICATE SET DEVICE_IDENTIFIER = ? WHERE ID = ?");
stmt.setString(1, cert.getOrganization());
stmt.setInt(2, cert.getId());
return stmt.executeUpdate();
} catch (SQLException e) {
throw new CertificateManagementDAOException("Error occurred while updating device identifier "
+ cert.getOrganization() + " of certificate id " + cert.getId()
, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public List<CertificateResponse> getAllCertificates() throws CertificateManagementDAOException {
PreparedStatement stmt = null;

@ -26,6 +26,26 @@ import java.math.BigInteger;
@ApiModel(value = "CertificateResponse", description = "This class carries all information related to certificates")
public class CertificateResponse {
int id;
String organization;
public String getOrganization() {
return organization;
}
public void setOrganization(String organization) {
this.organization = organization;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@ApiModelProperty(name = "certificate", value = "The certificate in bytes", required = true)
byte[] certificate;

@ -77,6 +77,20 @@ public class CertificateManagementServiceComponent {
bundleContext.registerService(SCEPManager.class.getName(),
new SCEPManagerImpl(), null);
new Thread(new Runnable() {
@Override
public void run() {
log.info("=================Starting the certificate table device identifier updating worker===============");
CertificateManagementService certificateManagementService = CertificateManagementServiceImpl.getInstance();
try {
certificateManagementService.updateCertificateDeviceIdentifiers();
log.info("=================Completed the certificate table device identifier updating worker===============");
} catch (CertificateManagementException e) {
log.error("Failed while updating device identifiers of the certificates.");
}
}
}).start();
if (log.isDebugEnabled()) {
log.debug("Certificate management core bundle has been successfully initialized");
}

@ -84,4 +84,5 @@ public interface CertificateManagementService {
X509Certificate generateAlteredCertificateFromCSR(String csr) throws KeystoreException;
void updateCertificateDeviceIdentifiers() throws CertificateManagementException;
}

@ -272,4 +272,44 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe
return certificateGenerator.generateAlteredCertificateFromCSR(csr);
}
@Override
public void updateCertificateDeviceIdentifiers() throws CertificateManagementException {
try {
CertificateManagementDAOFactory.beginTransaction();
CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO();
List<CertificateResponse> certificateResponses = certificateDAO.retrieveEmptyDeviceIdCerts();
for (CertificateResponse certificateResponse : certificateResponses) {
String subject = certificateResponse.getSubject();
String deviceId = null;
log.info("Extracting deviceId from certificate with subject : " + subject);
if (subject.contains("O=")) {
String[] dnParts = subject.split(",");
for (int i = 0; i < dnParts.length; i++) {
if (dnParts[i].contains("O=")) {
String[] orgParts = dnParts[i].split("=");
if (orgParts[1].matches("^[a-zA-Z0-9]+$")) { //check if the O is alphanumeric
deviceId = orgParts[1];
} else {
log.info("Certificate is not representing an Android device. " +
"Marking device identifier as null");
}
}
}
}
certificateResponse.setOrganization(deviceId);
certificateDAO.updateDeviceIdentifier(certificateResponse);
}
CertificateManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {
String msg = "Failed while updating certificate device identifier";
log.error(msg, e);
throw new CertificateManagementException(e);
} catch (CertificateManagementDAOException e) {
CertificateManagementDAOFactory.rollbackTransaction();
String msg = "Failed while updating certificate device identifier";
log.error(msg, e);
throw new CertificateManagementException(e);
}
}
}

Loading…
Cancel
Save