From 2d3f2b94dad5d4d7f55c90afc191fa9ed415f3d9 Mon Sep 17 00:00:00 2001 From: nishan Date: Fri, 7 Jul 2023 18:53:21 +0530 Subject: [PATCH] Show device identifier and changeed to delete certificate using Id --- ...CertificateManagementAdminServiceImpl.java | 12 +++---- .../admin/api/util/RequestValidationUtil.java | 8 +++++ .../mgt/core/dao/CertificateDAO.java | 4 +-- .../dao/impl/AbstractCertificateDAOImpl.java | 12 ++++--- .../dao/impl/GenericCertificateDAOImpl.java | 27 +++++++++++++-- .../dao/impl/OracleCertificateDAOImpl.java | 28 +++++++++++++-- .../impl/PostgreSQLCertificateDAOImpl.java | 28 +++++++++++++-- .../dao/impl/SQLServerCertificateDAOImpl.java | 28 +++++++++++++-- .../mgt/core/dto/CertificateResponse.java | 34 +++++++++++++++---- .../mgt/core/impl/CertificateGenerator.java | 2 +- .../service/CertificateManagementService.java | 2 +- .../CertificateManagementServiceImpl.java | 8 ++--- ...CertificateManagementServiceImplTests.java | 2 +- 13 files changed, 161 insertions(+), 34 deletions(-) diff --git a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.cert.admin.api/src/main/java/io/entgra/device/mgt/core/certificate/mgt/cert/admin/api/impl/CertificateManagementAdminServiceImpl.java b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.cert.admin.api/src/main/java/io/entgra/device/mgt/core/certificate/mgt/cert/admin/api/impl/CertificateManagementAdminServiceImpl.java index 0adb7ee6017..c64df487be9 100644 --- a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.cert.admin.api/src/main/java/io/entgra/device/mgt/core/certificate/mgt/cert/admin/api/impl/CertificateManagementAdminServiceImpl.java +++ b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.cert.admin.api/src/main/java/io/entgra/device/mgt/core/certificate/mgt/cert/admin/api/impl/CertificateManagementAdminServiceImpl.java @@ -151,20 +151,20 @@ public class CertificateManagementAdminServiceImpl implements CertificateManagem } @DELETE - public Response removeCertificate(@QueryParam("serialNumber") String serialNumber) { - RequestValidationUtil.validateSerialNumber(serialNumber); + public Response removeCertificate(@QueryParam("certificateId") String certificateId) { + RequestValidationUtil.validateCertificateId(certificateId); CertificateManagementService certificateService = CertificateMgtAPIUtils.getCertificateManagementService(); try { - boolean status = certificateService.removeCertificate(serialNumber); + boolean status = certificateService.removeCertificate(certificateId); if (!status) { return Response.status(Response.Status.NOT_FOUND).entity( "No certificate is found with the given " + - "serial number '" + serialNumber + "'").build(); + "certificate id '" + certificateId + "'").build(); } else { return Response.status(Response.Status.OK).entity( - "Certificate that carries the serial number '" + - serialNumber + "' has been removed").build(); + "Certificate that carries the certificate id '" + + certificateId + "' has been removed").build(); } } catch (CertificateManagementException e) { String msg = "Error occurred while converting PEM file to X509Certificate"; diff --git a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.cert.admin.api/src/main/java/io/entgra/device/mgt/core/certificate/mgt/cert/admin/api/util/RequestValidationUtil.java b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.cert.admin.api/src/main/java/io/entgra/device/mgt/core/certificate/mgt/cert/admin/api/util/RequestValidationUtil.java index 43f4d1ab48e..6263ac9e462 100644 --- a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.cert.admin.api/src/main/java/io/entgra/device/mgt/core/certificate/mgt/cert/admin/api/util/RequestValidationUtil.java +++ b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.cert.admin.api/src/main/java/io/entgra/device/mgt/core/certificate/mgt/cert/admin/api/util/RequestValidationUtil.java @@ -30,6 +30,14 @@ public class RequestValidationUtil { } } + public static void validateCertificateId(String certificateId) { + if (certificateId == null || certificateId.isEmpty()) { + throw new InputValidationException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage( + "Certificate Id cannot be null or empty").build()); + } + } + public static void validatePaginationInfo(int offset, int limit) { if (offset < 0) { throw new InputValidationException( 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 779251761e0..a3200621d16 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 @@ -95,10 +95,10 @@ public interface CertificateDAO { /** * Delete a certificate identified by a serial number() * - * @param serialNumber serial number + * @param certificateId number * @return whether the certificate was removed or not. */ - boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException; + boolean removeCertificate(String certificateId) throws CertificateManagementDAOException; List searchCertificate(String serialNumber) 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 d2b5be4062f..8e87ffff380 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 @@ -237,7 +237,7 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{ int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { Connection conn = this.getConnection(); - String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID, USERNAME" + String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, ID, DEVICE_IDENTIFIER, TENANT_ID, USERNAME" + " FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -247,6 +247,8 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{ certificateResponse = new CertificateResponse(); byte[] certificateBytes = resultSet.getBytes("CERTIFICATE"); certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER")); + certificateResponse.setCertificateId(resultSet.getString("ID")); + certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER")); certificateResponse.setTenantId(resultSet.getInt("TENANT_ID")); certificateResponse.setUsername(resultSet.getString("USERNAME")); CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse); @@ -263,7 +265,7 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{ } @Override - public boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException { + public boolean removeCertificate(String certificateId) throws CertificateManagementDAOException { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -271,15 +273,15 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{ try { conn = this.getConnection(); String query = - "DELETE FROM DM_DEVICE_CERTIFICATE WHERE SERIAL_NUMBER = ?" + + "DELETE FROM DM_DEVICE_CERTIFICATE WHERE ID = ?" + " AND TENANT_ID = ? "; stmt = conn.prepareStatement(query); - stmt.setString(1, serialNumber); + stmt.setString(1, certificateId); stmt.setInt(2, tenantId); return stmt.executeUpdate() > 0; } catch (SQLException e) { - String msg = "Unable to get the read the certificate with serial" + serialNumber; + String msg = "Unable to get the read the certificate with certificate id" + certificateId; log.error(msg, e); throw new CertificateManagementDAOException(msg, e); } finally { 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/GenericCertificateDAOImpl.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/GenericCertificateDAOImpl.java index 99bf38c73f8..f3cba82857f 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/GenericCertificateDAOImpl.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/GenericCertificateDAOImpl.java @@ -47,6 +47,27 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl { private Connection getConnection() throws SQLException { return CertificateManagementDAOFactory.getConnection(); } + private int getCertificateCount(int tenantId) throws CertificateManagementDAOException, SQLException { + int certificateCount = 0; + try { + Connection conn = this.getConnection(); + String sql = + "SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT"); + } + } + } + } catch (SQLException e) { + String errorMsg = "SQL error occurred while retrieving the certificates."; + log.error(errorMsg, e); + throw new CertificateManagementDAOException(errorMsg, e); + } + return certificateCount; + } @Override public PaginationResult getAllCertificates(int rowNum, int limit) throws CertificateManagementDAOException { @@ -58,7 +79,7 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { Connection conn = this.getConnection(); - String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID, USERNAME FROM " + String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, ID, DEVICE_IDENTIFIER, TENANT_ID, USERNAME FROM " + "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC LIMIT ?,?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -71,6 +92,8 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl { certificateResponse = new CertificateResponse(); byte[] certificateBytes = resultSet.getBytes("CERTIFICATE"); certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER")); + certificateResponse.setCertificateId(resultSet.getString("ID")); + certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER")); certificateResponse.setTenantId(resultSet.getInt("TENANT_ID")); certificateResponse.setUsername(resultSet.getString("USERNAME")); CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse); @@ -79,7 +102,7 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl { } paginationResult = new PaginationResult(); paginationResult.setData(certificates); - paginationResult.setRecordsTotal(resultCount); + paginationResult.setRecordsTotal(this.getCertificateCount(tenantId)); } catch (SQLException e) { String errorMsg = "SQL error occurred while retrieving the certificates."; log.error(errorMsg, e); 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/OracleCertificateDAOImpl.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/OracleCertificateDAOImpl.java index 2b2e9345701..f326f9cf97f 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/OracleCertificateDAOImpl.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/OracleCertificateDAOImpl.java @@ -53,7 +53,7 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { Connection conn = this.getConnection(); - String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID, USERNAME FROM " + String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, ID, DEVICE_IDENTIFIER, TENANT_ID, USERNAME FROM " + "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -66,6 +66,8 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl { certificateResponse = new CertificateResponse(); byte[] certificateBytes = resultSet.getBytes("CERTIFICATE"); certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER")); + certificateResponse.setCertificateId(resultSet.getString("ID")); + certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER")); certificateResponse.setTenantId(resultSet.getInt("TENANT_ID")); certificateResponse.setUsername(resultSet.getString("USERNAME")); CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse); @@ -74,7 +76,7 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl { } paginationResult = new PaginationResult(); paginationResult.setData(certificates); - paginationResult.setRecordsTotal(resultCount); + paginationResult.setRecordsTotal(this.getCertificateCount(tenantId)); } catch (SQLException e) { String errorMsg = "SQL error occurred while retrieving the certificates."; log.error(errorMsg, e); @@ -88,4 +90,26 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl { private Connection getConnection() throws SQLException { return CertificateManagementDAOFactory.getConnection(); } + + private int getCertificateCount(int tenantId) throws CertificateManagementDAOException, SQLException { + int certificateCount = 0; + try { + Connection conn = this.getConnection(); + String sql = + "SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT"); + } + } + } + } catch (SQLException e) { + String errorMsg = "SQL error occurred while retrieving the certificates."; + log.error(errorMsg, e); + throw new CertificateManagementDAOException(errorMsg, e); + } + return certificateCount; + } } 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/PostgreSQLCertificateDAOImpl.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/PostgreSQLCertificateDAOImpl.java index 6e8ea007032..7d187cfa3d6 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/PostgreSQLCertificateDAOImpl.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/PostgreSQLCertificateDAOImpl.java @@ -53,7 +53,7 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { Connection conn = this.getConnection(); - String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID, USERNAME FROM " + String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, ID, DEVICE_IDENTIFIER, TENANT_ID, USERNAME FROM " + "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC LIMIT ? OFFSET ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -66,6 +66,8 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl { certificateResponse = new CertificateResponse(); byte[] certificateBytes = resultSet.getBytes("CERTIFICATE"); certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER")); + certificateResponse.setCertificateId(resultSet.getString("ID")); + certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER")); certificateResponse.setTenantId(resultSet.getInt("TENANT_ID")); certificateResponse.setUsername(resultSet.getString("USERNAME")); CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse); @@ -74,7 +76,7 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl { } paginationResult = new PaginationResult(); paginationResult.setData(certificates); - paginationResult.setRecordsTotal(resultCount); + paginationResult.setRecordsTotal(this.getCertificateCount(tenantId)); } catch (SQLException e) { String errorMsg = "SQL error occurred while retrieving the certificates."; log.error(errorMsg, e); @@ -88,4 +90,26 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl { private Connection getConnection() throws SQLException { return CertificateManagementDAOFactory.getConnection(); } + + private int getCertificateCount(int tenantId) throws CertificateManagementDAOException, SQLException { + int certificateCount = 0; + try { + Connection conn = this.getConnection(); + String sql = + "SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT"); + } + } + } + } catch (SQLException e) { + String errorMsg = "SQL error occurred while retrieving the certificates."; + log.error(errorMsg, e); + throw new CertificateManagementDAOException(errorMsg, e); + } + return certificateCount; + } } 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/SQLServerCertificateDAOImpl.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/SQLServerCertificateDAOImpl.java index de8d911c29e..6615ab4c125 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/SQLServerCertificateDAOImpl.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/SQLServerCertificateDAOImpl.java @@ -53,7 +53,7 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { Connection conn = this.getConnection(); - String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID, USERNAME FROM " + String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, ID, DEVICE_IDENTIFIER, TENANT_ID, USERNAME FROM " + "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; stmt = conn.prepareStatement(sql); stmt.setInt(1, tenantId); @@ -66,6 +66,8 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl { certificateResponse = new CertificateResponse(); byte[] certificateBytes = resultSet.getBytes("CERTIFICATE"); certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER")); + certificateResponse.setCertificateId(resultSet.getString("ID")); + certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER")); certificateResponse.setTenantId(resultSet.getInt("TENANT_ID")); certificateResponse.setUsername(resultSet.getString("USERNAME")); CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse); @@ -74,7 +76,7 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl { } paginationResult = new PaginationResult(); paginationResult.setData(certificates); - paginationResult.setRecordsTotal(resultCount); + paginationResult.setRecordsTotal(this.getCertificateCount(tenantId)); } catch (SQLException e) { String errorMsg = "SQL error occurred while retrieving the certificates."; log.error(errorMsg, e); @@ -88,4 +90,26 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl { private Connection getConnection() throws SQLException { return CertificateManagementDAOFactory.getConnection(); } + + private int getCertificateCount(int tenantId) throws CertificateManagementDAOException, SQLException { + int certificateCount = 0; + try { + Connection conn = this.getConnection(); + String sql = + "SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?"; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT"); + } + } + } + } catch (SQLException e) { + String errorMsg = "SQL error occurred while retrieving the certificates."; + log.error(errorMsg, e); + throw new CertificateManagementDAOException(errorMsg, e); + } + return certificateCount; + } } 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 44b0df51936..23f37a0efad 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 @@ -32,6 +32,12 @@ public class CertificateResponse { @ApiModelProperty(name = "serialNumber", value = "It is the unique ID that is used to identify a certificate", required = true) String serialNumber; + @ApiModelProperty(name = "deviceIdentifier", value = "It is use to identify a certificate list", required = true) + String deviceIdentifier; + + @ApiModelProperty(name = "certificateId", value = "It is the unique ID that is used to identify a certificate", required = true) + String certificateId; + @ApiModelProperty(name = "tenantId", value = "The ID of the tenant who adds the certificate", required = true) int tenantId; @@ -44,8 +50,8 @@ public class CertificateResponse { @ApiModelProperty(name = "notBefore", value = "The date from when the certificate is valid", required = true) long notBefore; - @ApiModelProperty(name = "certificateserial", value = "The serial number of the certificate", required = true) - BigInteger certificateserial; + @ApiModelProperty(name = "certificateSerial", value = "The serial number of the certificate", required = true) + BigInteger certificateSerial; @ApiModelProperty(name = "issuer", value = "The identity of the authority that signs the SSL certificate", required = true) String issuer; @@ -83,12 +89,12 @@ public class CertificateResponse { this.notBefore = notBefore; } - public BigInteger getCertificateserial() { - return certificateserial; + public BigInteger getCertificateSerial() { + return certificateSerial; } - public void setCertificateserial(BigInteger certificateserial) { - this.certificateserial = certificateserial; + public void setCertificateSerial(BigInteger certificateSerial) { + this.certificateSerial = certificateSerial; } public String getIssuer() { @@ -146,4 +152,20 @@ public class CertificateResponse { public void setTenantId(int tenantId) { this.tenantId = tenantId; } + + public String getDeviceIdentifier() { + return deviceIdentifier; + } + + public void setDeviceIdentifier(String deviceIdentifier) { + this.deviceIdentifier = deviceIdentifier; + } + + public String getCertificateId() { + return certificateId; + } + + public void setCertificateId(String certificateId) { + this.certificateId = certificateId; + } } 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/impl/CertificateGenerator.java b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/impl/CertificateGenerator.java index 328c2fcbfb1..f90762897d7 100755 --- a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/impl/CertificateGenerator.java +++ b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/main/java/io/entgra/device/mgt/core/certificate/mgt/core/impl/CertificateGenerator.java @@ -130,7 +130,7 @@ public class CertificateGenerator { X509Certificate certificate = (X509Certificate) x509Certificate; certificateResponse.setNotAfter(certificate.getNotAfter().getTime()); certificateResponse.setNotBefore(certificate.getNotBefore().getTime()); - certificateResponse.setCertificateserial(certificate.getSerialNumber()); + certificateResponse.setCertificateSerial(certificate.getSerialNumber()); certificateResponse.setIssuer(certificate.getIssuerDN().getName()); certificateResponse.setSubject(certificate.getSubjectDN().getName()); certificateResponse.setCertificateVersion(certificate.getVersion()); 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 3eb94d21f1f..61d333413e5 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 @@ -73,7 +73,7 @@ public interface CertificateManagementService { PaginationResult getAllCertificates(int rowNum, int limit) throws CertificateManagementException; - boolean removeCertificate(String serialNumber) throws CertificateManagementException; + boolean removeCertificate(String certificateId) throws CertificateManagementException; List getCertificates() 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 95ec72a111b..bcd696f21e4 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 @@ -174,20 +174,20 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe } @Override - public boolean removeCertificate(String serialNumber) throws CertificateManagementException { + public boolean removeCertificate(String certificateId) throws CertificateManagementException { try { CertificateManagementDAOFactory.beginTransaction(); CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO(); - boolean status = certificateDAO.removeCertificate(serialNumber); + boolean status = certificateDAO.removeCertificate(certificateId); CertificateManagementDAOFactory.commitTransaction(); return status; } catch (TransactionManagementException e) { - String msg = "Error occurred while removing certificate carrying serial number '" + serialNumber + "'"; + String msg = "Error occurred while removing certificate carrying certificate id '" + certificateId + "'"; log.error(msg, e); throw new CertificateManagementException(msg, e); } catch (CertificateManagementDAOException e) { CertificateManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while removing the certificate carrying serial number '" + serialNumber + + String msg = "Error occurred while removing the certificate carrying certificate id '" + certificateId + "' from the certificate repository"; log.error(msg, e); throw new CertificateManagementException(msg, e); diff --git a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/test/java/io/entgra/device/mgt/core/certificate/mgt/core/impl/CertificateManagementServiceImplTests.java b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/test/java/io/entgra/device/mgt/core/certificate/mgt/core/impl/CertificateManagementServiceImplTests.java index 81ff3db86ff..ff6ad09e889 100644 --- a/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/test/java/io/entgra/device/mgt/core/certificate/mgt/core/impl/CertificateManagementServiceImplTests.java +++ b/components/certificate-mgt/io.entgra.device.mgt.core.certificate.mgt.core/src/test/java/io/entgra/device/mgt/core/certificate/mgt/core/impl/CertificateManagementServiceImplTests.java @@ -210,7 +210,7 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC X509Certificate x509Certificate = managementService.generateX509Certificate(); CertificateResponse certificateResponse = managementService.retrieveCertificate(x509Certificate.getSerialNumber().toString()); Assert.assertNotNull(certificateResponse); - Assert.assertEquals(x509Certificate.getSerialNumber(), certificateResponse.getCertificateserial()); + Assert.assertEquals(x509Certificate.getSerialNumber(), certificateResponse.getCertificateSerial()); } @Test(description = "This test case tests the retrieval of Certificates from keystore in desired pagination")