diff --git a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/dao/CertificateDAO.java b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/dao/CertificateDAO.java index 068766f3b9..992a04cbb6 100644 --- a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/dao/CertificateDAO.java +++ b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/dao/CertificateDAO.java @@ -103,4 +103,7 @@ public interface CertificateDAO { List searchCertificate(String serialNumber) throws CertificateManagementDAOException; + List retrieveEmptyDeviceIdCerts() throws CertificateManagementDAOException; + + int updateDeviceIdentifier(CertificateResponse cert) throws CertificateManagementDAOException; } diff --git a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/dao/impl/AbstractCertificateDAOImpl.java b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/dao/impl/AbstractCertificateDAOImpl.java index 888b3619ca..a1bab2c29f 100644 --- a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/dao/impl/AbstractCertificateDAOImpl.java +++ b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/dao/impl/AbstractCertificateDAOImpl.java @@ -229,6 +229,60 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{ return certificates; } + @Override + public List retrieveEmptyDeviceIdCerts() throws CertificateManagementDAOException { + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + CertificateResponse certificateResponse = null; + List 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 getAllCertificates() throws CertificateManagementDAOException { PreparedStatement stmt = null; diff --git a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/dto/CertificateResponse.java b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/dto/CertificateResponse.java index 23f37a0efa..7409f90572 100644 --- a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/dto/CertificateResponse.java +++ b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/dto/CertificateResponse.java @@ -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; diff --git a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/internal/CertificateManagementServiceComponent.java b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/internal/CertificateManagementServiceComponent.java index 956bb4683c..9869b113b8 100644 --- a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/internal/CertificateManagementServiceComponent.java +++ b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/internal/CertificateManagementServiceComponent.java @@ -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"); } diff --git a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/service/CertificateManagementService.java b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/service/CertificateManagementService.java index 611295ba3f..ac8c6ac8fb 100644 --- a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/service/CertificateManagementService.java +++ b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/service/CertificateManagementService.java @@ -84,4 +84,5 @@ public interface CertificateManagementService { X509Certificate generateAlteredCertificateFromCSR(String csr) throws KeystoreException; + void updateCertificateDeviceIdentifiers() throws CertificateManagementException; } diff --git a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/service/CertificateManagementServiceImpl.java b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/service/CertificateManagementServiceImpl.java index 06cbedfb4d..b3660520b3 100644 --- a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/service/CertificateManagementServiceImpl.java +++ b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/service/CertificateManagementServiceImpl.java @@ -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 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); + } + } } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/common/Constants.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/common/Constants.java index 71eaf20a5f..68111a5159 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/common/Constants.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/common/Constants.java @@ -21,6 +21,7 @@ package io.entgra.device.mgt.core.device.mgt.core.common; public class Constants { public static final String SCHEME_SEPARATOR = "://"; public static final String COLON = ":"; + public static final String QUERY_WILDCARD = "%"; public static final String URI_QUERY_SEPARATOR = "?"; public static final String URI_SEPARATOR = "/"; public static final String BASIC_AUTH_HEADER_PREFIX = "Basic "; diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/ApplicationDAOImpl.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/ApplicationDAOImpl.java index fb3ad85549..30655aa1e4 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/ApplicationDAOImpl.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/dao/impl/ApplicationDAOImpl.java @@ -17,6 +17,7 @@ */ package io.entgra.device.mgt.core.device.mgt.core.dao.impl; +import io.entgra.device.mgt.core.device.mgt.core.common.Constants; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import io.entgra.device.mgt.core.device.mgt.common.PaginationRequest; @@ -298,9 +299,13 @@ public class ApplicationDAOImpl implements ApplicationDAO { String filter = request.getFilter(); if (filter != null) { sql = sql + "AND NAME LIKE ? "; + filter = Constants.QUERY_WILDCARD.concat(filter).concat(Constants.QUERY_WILDCARD); } + + boolean isLimitPresent = false; if (request != null && request.getRowCount() != -1) { sql = sql + "LIMIT ? OFFSET ?"; + isLimitPresent = true; } Connection conn = this.getConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { @@ -312,7 +317,7 @@ public class ApplicationDAOImpl implements ApplicationDAO { if (filter != null){ stmt.setString(paramIdx++, filter); } - if (request != null && request.getRowCount() != -1) { + if (isLimitPresent) { stmt.setInt(paramIdx++, request.getRowCount()); stmt.setInt(paramIdx, request.getStartIndex()); } diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index ce963d1437..b0de3dc590 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -463,6 +463,8 @@ CREATE TABLE IF NOT EXISTS DM_APPLICATION ( )ENGINE = InnoDB; CREATE INDEX IDX_DM_APPLICATION ON DM_APPLICATION(DEVICE_ID, ENROLMENT_ID, TENANT_ID); +CREATE INDEX DM_APPLICATION_NAME ON DM_APPLICATION(NAME); +CREATE INDEX DM_APPLICATION_NAME_PLATFORM_TID ON DM_APPLICATION(NAME, PLATFORM, TENANT_ID); -- END OF POLICY RELATED TABLES --