Add improvements to certificate component

location-fix
Navod Zoysa 1 year ago
parent 017c70fa08
commit dbc73d18ed

@ -311,20 +311,17 @@ public interface CertificateManagementAdminService {
@ApiParam( @ApiParam(
name = "serialNumber", name = "serialNumber",
value = "The serial number of the certificates", value = "The serial number of the certificates",
required = false, required = false)
defaultValue = "0")
@QueryParam("serialNumber") String serialNumber, @QueryParam("serialNumber") String serialNumber,
@ApiParam( @ApiParam(
name = "deviceIdentifier", name = "deviceIdentifier",
value = "The device identifier of the certificates", value = "The device identifier of the certificates",
required = false, required = false)
defaultValue = "0")
@QueryParam("deviceIdentifier") String deviceIdentifier, @QueryParam("deviceIdentifier") String deviceIdentifier,
@ApiParam( @ApiParam(
name = "username", name = "username",
value = "User name of the certificate added user" , value = "User name of the certificate added user",
required = false, required = false)
defaultValue = "0")
@QueryParam("username") String username, @QueryParam("username") String username,
@ApiParam( @ApiParam(
name = "If-Modified-Since", name = "If-Modified-Since",

@ -27,6 +27,7 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import io.entgra.device.mgt.core.device.mgt.common.CertificatePaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.CertificatePaginationRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import io.entgra.device.mgt.core.certificate.mgt.cert.admin.api.CertificateManagementAdminService; import io.entgra.device.mgt.core.certificate.mgt.cert.admin.api.CertificateManagementAdminService;
@ -138,20 +139,23 @@ public class CertificateManagementAdminServiceImpl implements CertificateManagem
*/ */
@GET @GET
public Response getAllCertificates( public Response getAllCertificates(
@QueryParam("serialNumber") String serialNumber, @QueryParam("deviceIdentifier") String deviceIdentifier, @QueryParam("serialNumber") String serialNumber,
@QueryParam("username") String username, @HeaderParam("If-Modified-Since") String ifModifiedSince, @QueryParam("offset") int offset, @QueryParam("deviceIdentifier") String deviceIdentifier,
@QueryParam("username") String username,
@HeaderParam("If-Modified-Since") String ifModifiedSince,
@QueryParam("offset") int offset,
@QueryParam("limit") int limit) { @QueryParam("limit") int limit) {
RequestValidationUtil.validatePaginationInfo(offset, limit); RequestValidationUtil.validatePaginationInfo(offset, limit);
CertificateManagementService certificateService = CertificateMgtAPIUtils.getCertificateManagementService(); CertificateManagementService certificateService = CertificateMgtAPIUtils.getCertificateManagementService();
CertificatePaginationRequest request = new CertificatePaginationRequest(offset, limit); CertificatePaginationRequest request = new CertificatePaginationRequest(offset, limit);
if (serialNumber != null && !serialNumber.isEmpty()) { if (StringUtils.isNotEmpty(serialNumber)) {
request.setSerialNumber(serialNumber); request.setSerialNumber(serialNumber);
} }
if (deviceIdentifier != null){ if (StringUtils.isNotEmpty(deviceIdentifier)){
request.setDeviceIdentifier(deviceIdentifier); request.setDeviceIdentifier(deviceIdentifier);
} }
if (username != null){ if (StringUtils.isNotEmpty(username)){
request.setUsername(username); request.setUsername(username);
} }
try { try {
@ -188,18 +192,19 @@ public class CertificateManagementAdminServiceImpl implements CertificateManagem
serialNumber + "' has been removed").build(); serialNumber + "' has been removed").build();
} }
} catch (CertificateManagementException e) { } catch (CertificateManagementException e) {
String msg = "Error occurred while converting PEM file to X509Certificate"; String msg = "Error occurred while removing certificate with the given " +
"serial number '" + serialNumber + "'";
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} }
} else { } else {
return Response.status(Response.Status.NOT_FOUND).entity( return Response.status(Response.Status.UNAUTHORIZED).entity(
"User not have to access delete certificate " + "User unauthorized to delete certificate with " +
"serial number '" + serialNumber + "'").build(); "serial number '" + serialNumber + "'").build();
} }
} catch (CertificateManagementException e) { } catch (CertificateManagementException e) {
String msg = "Error occurred while converting PEM file to X509Certificate"; String msg = "Error occurred while getting the metadata entry for certificate deletion.";
log.error(msg, e); log.error(msg, e);
return Response.serverError().entity( return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();

@ -56,6 +56,7 @@
<Import-Package> <Import-Package>
org.osgi.framework.*;version="${imp.package.version.osgi.framework}", org.osgi.framework.*;version="${imp.package.version.osgi.framework}",
org.osgi.service.*;version="${imp.package.version.osgi.service}", org.osgi.service.*;version="${imp.package.version.osgi.service}",
org.apache.commons.lang,
org.apache.commons.logging, org.apache.commons.logging,
org.apache.commons.collections.map, org.apache.commons.collections.map,
javax.security.auth.x500, javax.security.auth.x500,

@ -21,11 +21,11 @@ package io.entgra.device.mgt.core.certificate.mgt.core.dao.impl;
import io.entgra.device.mgt.core.device.mgt.common.CertificatePaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.CertificatePaginationRequest;
import io.entgra.device.mgt.core.certificate.mgt.core.dto.CertificateResponse; import io.entgra.device.mgt.core.certificate.mgt.core.dto.CertificateResponse;
import io.entgra.device.mgt.core.certificate.mgt.core.impl.CertificateGenerator; import io.entgra.device.mgt.core.certificate.mgt.core.impl.CertificateGenerator;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOException; import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOException;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOFactory; import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOFactory;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOUtil;
import io.entgra.device.mgt.core.certificate.mgt.core.service.PaginationResult; import io.entgra.device.mgt.core.certificate.mgt.core.service.PaginationResult;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -55,66 +55,58 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
String serialNumber = request.getSerialNumber(); String serialNumber = request.getSerialNumber();
String deviceIdentifier = request.getDeviceIdentifier(); String deviceIdentifier = request.getDeviceIdentifier();
String username = request.getUsername(); String username = request.getUsername();
PreparedStatement stmt = null;
ResultSet rs = null;
try { try {
Connection conn = this.getConnection(); Connection conn = this.getConnection();
StringBuilder queryBuilder = new StringBuilder("SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?"); String sql = "SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT " +
"FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ?";
if (serialNumber != null && !serialNumber.isEmpty()) { if (StringUtils.isNotEmpty(serialNumber)) {
queryBuilder.append(" AND SERIAL_NUMBER = ?"); sql += " AND SERIAL_NUMBER = ?";
} }
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) { if (StringUtils.isNotEmpty(deviceIdentifier)) {
queryBuilder.append(" AND DEVICE_IDENTIFIER = ?"); sql += " AND DEVICE_IDENTIFIER = ?";
} }
if (username != null && !username.isEmpty()) { if (StringUtils.isNotEmpty(username)) {
queryBuilder.append(" AND USERNAME LIKE ?"); sql += " AND USERNAME LIKE ?";
} }
String sql = queryBuilder.toString(); try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
int paramIdx = 2; int paramIdx = 2;
if (serialNumber != null && !serialNumber.isEmpty()) { if (StringUtils.isNotEmpty(serialNumber)) {
stmt.setString(paramIdx++, serialNumber); stmt.setString(paramIdx++, serialNumber);
} }
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) { if (StringUtils.isNotEmpty(deviceIdentifier)) {
stmt.setString(paramIdx++, deviceIdentifier); stmt.setString(paramIdx++, deviceIdentifier);
} }
if (username != null && !username.isEmpty()) { if (StringUtils.isNotEmpty(username)) {
stmt.setString(paramIdx, "%" + username + "%"); stmt.setString(paramIdx, "%" + username + "%");
} }
try (ResultSet rs = stmt.executeQuery()) {
rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT"); certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT");
} }
}
}
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificate count."; String errorMsg = "SQL error occurred while retrieving the certificate count.";
log.error(errorMsg, e); log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e); throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, rs);
} }
return certificateCount; return certificateCount;
} }
@Override @Override
public PaginationResult getAllCertificates(CertificatePaginationRequest request) throws CertificateManagementDAOException { public PaginationResult getAllCertificates(CertificatePaginationRequest request) throws CertificateManagementDAOException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
PreparedStatement stmt = null;
ResultSet resultSet = null;
CertificateResponse certificateResponse; CertificateResponse certificateResponse;
List<CertificateResponse> certificates = new ArrayList<>(); List<CertificateResponse> certificates = new ArrayList<>();
PaginationResult paginationResult; PaginationResult paginationResult;
@ -130,24 +122,25 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
String query = "SELECT * " + String query = "SELECT * " +
"FROM DM_DEVICE_CERTIFICATE " + "FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ? "; "WHERE TENANT_ID = ? ";
if (serialNumber != null && !serialNumber.isEmpty()) {
if (StringUtils.isNotEmpty(serialNumber)) {
query += "AND SERIAL_NUMBER = ? "; query += "AND SERIAL_NUMBER = ? ";
isCertificateSerialNumberProvided = true; isCertificateSerialNumberProvided = true;
} }
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) { if (StringUtils.isNotEmpty(deviceIdentifier)) {
query += "AND DEVICE_IDENTIFIER = ? "; query += "AND DEVICE_IDENTIFIER = ? ";
isCertificateDeviceIdentifierProvided = true; isCertificateDeviceIdentifierProvided = true;
} }
if (username != null && !username.isEmpty()) { if (StringUtils.isNotEmpty(username)) {
query += "AND USERNAME LIKE ? "; query += "AND USERNAME LIKE ? ";
isCertificateUsernameProvided = true; isCertificateUsernameProvided = true;
} }
query += "ORDER BY ID LIMIT ?,?"; query += "ORDER BY ID LIMIT ?,?";
stmt = conn.prepareStatement(query); try (PreparedStatement stmt = conn.prepareStatement(query)) {
int paramIdx = 1; int paramIdx = 1;
stmt.setInt(paramIdx++, tenantId); stmt.setInt(paramIdx++, tenantId);
if (isCertificateSerialNumberProvided) { if (isCertificateSerialNumberProvided) {
@ -161,7 +154,7 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
} }
stmt.setInt(paramIdx++, request.getStartIndex()); stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx++, request.getRowCount()); stmt.setInt(paramIdx++, request.getRowCount());
resultSet = stmt.executeQuery(); try (ResultSet resultSet = stmt.executeQuery()) {
while (resultSet.next()) { while (resultSet.next()) {
certificateResponse = new CertificateResponse(); certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE"); byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
@ -176,13 +169,12 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
paginationResult = new PaginationResult(); paginationResult = new PaginationResult();
paginationResult.setData(certificates); paginationResult.setData(certificates);
paginationResult.setRecordsTotal(this.getCertificateCount(request)); paginationResult.setRecordsTotal(this.getCertificateCount(request));
}
}
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates."; String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e); log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e); throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
} }
return paginationResult; return paginationResult;
} }

@ -21,11 +21,11 @@ package io.entgra.device.mgt.core.certificate.mgt.core.dao.impl;
import io.entgra.device.mgt.core.device.mgt.common.CertificatePaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.CertificatePaginationRequest;
import io.entgra.device.mgt.core.certificate.mgt.core.dto.CertificateResponse; import io.entgra.device.mgt.core.certificate.mgt.core.dto.CertificateResponse;
import io.entgra.device.mgt.core.certificate.mgt.core.impl.CertificateGenerator; import io.entgra.device.mgt.core.certificate.mgt.core.impl.CertificateGenerator;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOException; import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOException;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOFactory; import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOFactory;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOUtil;
import io.entgra.device.mgt.core.certificate.mgt.core.service.PaginationResult; import io.entgra.device.mgt.core.certificate.mgt.core.service.PaginationResult;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -47,8 +47,6 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
@Override @Override
public PaginationResult getAllCertificates(CertificatePaginationRequest request) throws CertificateManagementDAOException { public PaginationResult getAllCertificates(CertificatePaginationRequest request) throws CertificateManagementDAOException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
PreparedStatement stmt = null;
ResultSet resultSet = null;
CertificateResponse certificateResponse; CertificateResponse certificateResponse;
List<CertificateResponse> certificates = new ArrayList<>(); List<CertificateResponse> certificates = new ArrayList<>();
PaginationResult paginationResult; PaginationResult paginationResult;
@ -64,24 +62,24 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
String query = "SELECT * " + String query = "SELECT * " +
"FROM DM_DEVICE_CERTIFICATE " + "FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ? "; "WHERE TENANT_ID = ? ";
if (serialNumber != null && !serialNumber.isEmpty()) { if (StringUtils.isNotEmpty(serialNumber)) {
query += "AND SERIAL_NUMBER = ? "; query += "AND SERIAL_NUMBER = ? ";
isCertificateSerialNumberProvided = true; isCertificateSerialNumberProvided = true;
} }
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) { if (StringUtils.isNotEmpty(deviceIdentifier)) {
query += "AND DEVICE_IDENTIFIER = ? "; query += "AND DEVICE_IDENTIFIER = ? ";
isCertificateDeviceIdentifierProvided = true; isCertificateDeviceIdentifierProvided = true;
} }
if (username != null && !username.isEmpty()) { if (StringUtils.isNotEmpty(username)) {
query += "AND USERNAME LIKE ? "; query += "AND USERNAME LIKE ? ";
isCertificateUsernameProvided = true; isCertificateUsernameProvided = true;
} }
query += "ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; query += "ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(query); try (PreparedStatement stmt = conn.prepareStatement(query)) {
int paramIdx = 1; int paramIdx = 1;
stmt.setInt(paramIdx++, tenantId); stmt.setInt(paramIdx++, tenantId);
if (isCertificateSerialNumberProvided) { if (isCertificateSerialNumberProvided) {
@ -95,7 +93,7 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
} }
stmt.setInt(paramIdx++, request.getStartIndex()); stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx++, request.getRowCount()); stmt.setInt(paramIdx++, request.getRowCount());
resultSet = stmt.executeQuery(); try (ResultSet resultSet = stmt.executeQuery()) {
while (resultSet.next()) { while (resultSet.next()) {
certificateResponse = new CertificateResponse(); certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE"); byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
@ -110,12 +108,12 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
paginationResult = new PaginationResult(); paginationResult = new PaginationResult();
paginationResult.setData(certificates); paginationResult.setData(certificates);
paginationResult.setRecordsTotal(this.getCertificateCount(request)); paginationResult.setRecordsTotal(this.getCertificateCount(request));
}
}
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates."; String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e); log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e); throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
} }
return paginationResult; return paginationResult;
} }
@ -130,56 +128,52 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
String serialNumber = request.getSerialNumber(); String serialNumber = request.getSerialNumber();
String deviceIdentifier = request.getDeviceIdentifier(); String deviceIdentifier = request.getDeviceIdentifier();
String username = request.getUsername(); String username = request.getUsername();
PreparedStatement stmt = null;
ResultSet rs = null;
try { try {
Connection conn = this.getConnection(); Connection conn = this.getConnection();
StringBuilder queryBuilder = new StringBuilder("SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?"); String sql = "SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT " +
"FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ?";
if (serialNumber != null && !serialNumber.isEmpty()) { if (StringUtils.isNotEmpty(serialNumber)) {
queryBuilder.append(" AND SERIAL_NUMBER = ?"); sql += " AND SERIAL_NUMBER = ?";
} }
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) { if (StringUtils.isNotEmpty(deviceIdentifier)) {
queryBuilder.append(" AND DEVICE_IDENTIFIER = ?"); sql += " AND DEVICE_IDENTIFIER = ?";
} }
if (username != null && !username.isEmpty()) { if (StringUtils.isNotEmpty(username)) {
queryBuilder.append(" AND USERNAME LIKE ?"); sql += " AND USERNAME LIKE ?";
} }
String sql = queryBuilder.toString(); try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
int paramIdx = 2; int paramIdx = 2;
if (serialNumber != null && !serialNumber.isEmpty()) { if (StringUtils.isNotEmpty(serialNumber)) {
stmt.setString(paramIdx++, serialNumber); stmt.setString(paramIdx++, serialNumber);
} }
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) { if (StringUtils.isNotEmpty(deviceIdentifier)) {
stmt.setString(paramIdx++, deviceIdentifier); stmt.setString(paramIdx++, deviceIdentifier);
} }
if (username != null && !username.isEmpty()) { if (StringUtils.isNotEmpty(username)) {
stmt.setString(paramIdx, "%" + username + "%"); stmt.setString(paramIdx, "%" + username + "%");
} }
rs = stmt.executeQuery(); try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) { if (rs.next()) {
certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT"); certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT");
} }
}
}
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificate count."; String errorMsg = "SQL error occurred while retrieving the certificate count.";
log.error(errorMsg, e); log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e); throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, rs);
} }
return certificateCount; return certificateCount;
} }

@ -21,11 +21,11 @@ package io.entgra.device.mgt.core.certificate.mgt.core.dao.impl;
import io.entgra.device.mgt.core.device.mgt.common.CertificatePaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.CertificatePaginationRequest;
import io.entgra.device.mgt.core.certificate.mgt.core.dto.CertificateResponse; import io.entgra.device.mgt.core.certificate.mgt.core.dto.CertificateResponse;
import io.entgra.device.mgt.core.certificate.mgt.core.impl.CertificateGenerator; import io.entgra.device.mgt.core.certificate.mgt.core.impl.CertificateGenerator;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOException; import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOException;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOFactory; import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOFactory;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOUtil;
import io.entgra.device.mgt.core.certificate.mgt.core.service.PaginationResult; import io.entgra.device.mgt.core.certificate.mgt.core.service.PaginationResult;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -47,8 +47,6 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
@Override @Override
public PaginationResult getAllCertificates(CertificatePaginationRequest request) throws CertificateManagementDAOException { public PaginationResult getAllCertificates(CertificatePaginationRequest request) throws CertificateManagementDAOException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
PreparedStatement stmt = null;
ResultSet resultSet = null;
CertificateResponse certificateResponse; CertificateResponse certificateResponse;
List<CertificateResponse> certificates = new ArrayList<>(); List<CertificateResponse> certificates = new ArrayList<>();
PaginationResult paginationResult; PaginationResult paginationResult;
@ -64,24 +62,24 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
String query = "SELECT * " + String query = "SELECT * " +
"FROM DM_DEVICE_CERTIFICATE " + "FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ? "; "WHERE TENANT_ID = ? ";
if (serialNumber != null && !serialNumber.isEmpty()) { if (StringUtils.isNotEmpty(serialNumber)) {
query += "AND SERIAL_NUMBER = ? "; query += "AND SERIAL_NUMBER = ? ";
isCertificateSerialNumberProvided = true; isCertificateSerialNumberProvided = true;
} }
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) { if (StringUtils.isNotEmpty(deviceIdentifier)) {
query += "AND DEVICE_IDENTIFIER = ? "; query += "AND DEVICE_IDENTIFIER = ? ";
isCertificateDeviceIdentifierProvided = true; isCertificateDeviceIdentifierProvided = true;
} }
if (username != null && !username.isEmpty()) { if (StringUtils.isNotEmpty(username)) {
query += "AND USERNAME LIKE ? "; query += "AND USERNAME LIKE ? ";
isCertificateUsernameProvided = true; isCertificateUsernameProvided = true;
} }
query += "ORDER BY ID LIMIT ? OFFSET ?"; query += "ORDER BY ID LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(query); try (PreparedStatement stmt = conn.prepareStatement(query)) {
int paramIdx = 1; int paramIdx = 1;
stmt.setInt(paramIdx++, tenantId); stmt.setInt(paramIdx++, tenantId);
if (isCertificateSerialNumberProvided) { if (isCertificateSerialNumberProvided) {
@ -95,7 +93,7 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
} }
stmt.setInt(paramIdx++, request.getStartIndex()); stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx++, request.getRowCount()); stmt.setInt(paramIdx++, request.getRowCount());
resultSet = stmt.executeQuery(); try (ResultSet resultSet = stmt.executeQuery()) {
while (resultSet.next()) { while (resultSet.next()) {
certificateResponse = new CertificateResponse(); certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE"); byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
@ -110,13 +108,12 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
paginationResult = new PaginationResult(); paginationResult = new PaginationResult();
paginationResult.setData(certificates); paginationResult.setData(certificates);
paginationResult.setRecordsTotal(this.getCertificateCount(request)); paginationResult.setRecordsTotal(this.getCertificateCount(request));
}
}
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates."; String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e); log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e); throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
} }
return paginationResult; return paginationResult;
} }
@ -131,56 +128,52 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
String serialNumber = request.getSerialNumber(); String serialNumber = request.getSerialNumber();
String deviceIdentifier = request.getDeviceIdentifier(); String deviceIdentifier = request.getDeviceIdentifier();
String username = request.getUsername(); String username = request.getUsername();
PreparedStatement stmt = null;
ResultSet rs = null;
try { try {
Connection conn = this.getConnection(); Connection conn = this.getConnection();
StringBuilder queryBuilder = new StringBuilder("SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?"); String sql = "SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT " +
"FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ?";
if (serialNumber != null && !serialNumber.isEmpty()) { if (StringUtils.isNotEmpty(serialNumber)) {
queryBuilder.append(" AND SERIAL_NUMBER = ?"); sql += " AND SERIAL_NUMBER = ?";
} }
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) { if (StringUtils.isNotEmpty(deviceIdentifier)) {
queryBuilder.append(" AND DEVICE_IDENTIFIER = ?"); sql += " AND DEVICE_IDENTIFIER = ?";
} }
if (username != null && !username.isEmpty()) { if (StringUtils.isNotEmpty(username)) {
queryBuilder.append(" AND USERNAME ILIKE ?"); sql += " AND USERNAME ILIKE ?";
} }
String sql = queryBuilder.toString(); try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
int paramIdx = 2; int paramIdx = 2;
if (serialNumber != null && !serialNumber.isEmpty()) { if (StringUtils.isNotEmpty(serialNumber)) {
stmt.setString(paramIdx++, serialNumber); stmt.setString(paramIdx++, serialNumber);
} }
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) { if (StringUtils.isNotEmpty(deviceIdentifier)) {
stmt.setString(paramIdx++, deviceIdentifier); stmt.setString(paramIdx++, deviceIdentifier);
} }
if (username != null && !username.isEmpty()) { if (StringUtils.isNotEmpty(username)) {
stmt.setString(paramIdx, "%" + username + "%"); stmt.setString(paramIdx, "%" + username + "%");
} }
rs = stmt.executeQuery(); try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) { if (rs.next()) {
certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT"); certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT");
} }
}
}
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificate count."; String errorMsg = "SQL error occurred while retrieving the certificate count.";
log.error(errorMsg, e); log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e); throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, rs);
} }
return certificateCount; return certificateCount;
} }

@ -21,11 +21,11 @@ package io.entgra.device.mgt.core.certificate.mgt.core.dao.impl;
import io.entgra.device.mgt.core.device.mgt.common.CertificatePaginationRequest; import io.entgra.device.mgt.core.device.mgt.common.CertificatePaginationRequest;
import io.entgra.device.mgt.core.certificate.mgt.core.dto.CertificateResponse; import io.entgra.device.mgt.core.certificate.mgt.core.dto.CertificateResponse;
import io.entgra.device.mgt.core.certificate.mgt.core.impl.CertificateGenerator; import io.entgra.device.mgt.core.certificate.mgt.core.impl.CertificateGenerator;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOException; import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOException;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOFactory; import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOFactory;
import io.entgra.device.mgt.core.certificate.mgt.core.dao.CertificateManagementDAOUtil;
import io.entgra.device.mgt.core.certificate.mgt.core.service.PaginationResult; import io.entgra.device.mgt.core.certificate.mgt.core.service.PaginationResult;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -47,8 +47,6 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
@Override @Override
public PaginationResult getAllCertificates(CertificatePaginationRequest request) throws CertificateManagementDAOException { public PaginationResult getAllCertificates(CertificatePaginationRequest request) throws CertificateManagementDAOException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
PreparedStatement stmt = null;
ResultSet resultSet = null;
CertificateResponse certificateResponse; CertificateResponse certificateResponse;
List<CertificateResponse> certificates = new ArrayList<>(); List<CertificateResponse> certificates = new ArrayList<>();
PaginationResult paginationResult; PaginationResult paginationResult;
@ -64,24 +62,24 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
String query = "SELECT * " + String query = "SELECT * " +
"FROM DM_DEVICE_CERTIFICATE " + "FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ? "; "WHERE TENANT_ID = ? ";
if (serialNumber != null && !serialNumber.isEmpty()) { if (StringUtils.isNotEmpty(serialNumber)) {
query += "AND SERIAL_NUMBER = ? "; query += "AND SERIAL_NUMBER = ? ";
isCertificateSerialNumberProvided = true; isCertificateSerialNumberProvided = true;
} }
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) { if (StringUtils.isNotEmpty(deviceIdentifier)) {
query += "AND DEVICE_IDENTIFIER = ? "; query += "AND DEVICE_IDENTIFIER = ? ";
isCertificateDeviceIdentifierProvided = true; isCertificateDeviceIdentifierProvided = true;
} }
if (username != null && !username.isEmpty()) { if (StringUtils.isNotEmpty(username)) {
query += "AND USERNAME LIKE ? "; query += "AND USERNAME LIKE ? ";
isCertificateUsernameProvided = true; isCertificateUsernameProvided = true;
} }
query += "ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY"; query += "ORDER BY ID OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(query); try (PreparedStatement stmt = conn.prepareStatement(query)) {
int paramIdx = 1; int paramIdx = 1;
stmt.setInt(paramIdx++, tenantId); stmt.setInt(paramIdx++, tenantId);
if (isCertificateSerialNumberProvided) { if (isCertificateSerialNumberProvided) {
@ -95,7 +93,7 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
} }
stmt.setInt(paramIdx++, request.getStartIndex()); stmt.setInt(paramIdx++, request.getStartIndex());
stmt.setInt(paramIdx++, request.getRowCount()); stmt.setInt(paramIdx++, request.getRowCount());
resultSet = stmt.executeQuery(); try (ResultSet resultSet = stmt.executeQuery()) {
while (resultSet.next()) { while (resultSet.next()) {
certificateResponse = new CertificateResponse(); certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE"); byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
@ -110,13 +108,12 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
paginationResult = new PaginationResult(); paginationResult = new PaginationResult();
paginationResult.setData(certificates); paginationResult.setData(certificates);
paginationResult.setRecordsTotal(this.getCertificateCount(request)); paginationResult.setRecordsTotal(this.getCertificateCount(request));
}
}
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificates."; String errorMsg = "SQL error occurred while retrieving the certificates.";
log.error(errorMsg, e); log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e); throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, resultSet);
} }
return paginationResult; return paginationResult;
} }
@ -131,56 +128,52 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
String serialNumber = request.getSerialNumber(); String serialNumber = request.getSerialNumber();
String deviceIdentifier = request.getDeviceIdentifier(); String deviceIdentifier = request.getDeviceIdentifier();
String username = request.getUsername(); String username = request.getUsername();
PreparedStatement stmt = null;
ResultSet rs = null;
try { try {
Connection conn = this.getConnection(); Connection conn = this.getConnection();
StringBuilder queryBuilder = new StringBuilder("SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ?"); String sql = "SELECT COUNT(*) AS DEVICE_CERTIFICATE_COUNT " +
"FROM DM_DEVICE_CERTIFICATE " +
"WHERE TENANT_ID = ?";
if (serialNumber != null && !serialNumber.isEmpty()) { if (StringUtils.isNotEmpty(serialNumber)) {
queryBuilder.append(" AND SERIAL_NUMBER = ?"); sql += " AND SERIAL_NUMBER = ?";
} }
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) { if (StringUtils.isNotEmpty(deviceIdentifier)) {
queryBuilder.append(" AND DEVICE_IDENTIFIER = ?"); sql += " AND DEVICE_IDENTIFIER = ?";
} }
if (username != null && !username.isEmpty()) { if (StringUtils.isNotEmpty(username)) {
queryBuilder.append(" AND USERNAME LIKE ?"); sql += " AND USERNAME LIKE ?";
} }
String sql = queryBuilder.toString(); try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
int paramIdx = 2; int paramIdx = 2;
if (serialNumber != null && !serialNumber.isEmpty()) { if (StringUtils.isNotEmpty(serialNumber)) {
stmt.setString(paramIdx++, serialNumber); stmt.setString(paramIdx++, serialNumber);
} }
if (deviceIdentifier != null && !deviceIdentifier.isEmpty()) { if (StringUtils.isNotEmpty(deviceIdentifier)) {
stmt.setString(paramIdx++, deviceIdentifier); stmt.setString(paramIdx++, deviceIdentifier);
} }
if (username != null && !username.isEmpty()) { if (StringUtils.isNotEmpty(username)) {
stmt.setString(paramIdx, "%" + username + "%"); stmt.setString(paramIdx, "%" + username + "%");
} }
rs = stmt.executeQuery(); try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) { if (rs.next()) {
certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT"); certificateCount = rs.getInt("DEVICE_CERTIFICATE_COUNT");
} }
}
}
} catch (SQLException e) { } catch (SQLException e) {
String errorMsg = "SQL error occurred while retrieving the certificate count."; String errorMsg = "SQL error occurred while retrieving the certificate count.";
log.error(errorMsg, e); log.error(errorMsg, e);
throw new CertificateManagementDAOException(errorMsg, e); throw new CertificateManagementDAOException(errorMsg, e);
} finally {
CertificateManagementDAOUtil.cleanupResources(stmt, rs);
} }
return certificateCount; return certificateCount;
} }

@ -17,8 +17,6 @@
*/ */
package io.entgra.device.mgt.core.certificate.mgt.core.service; package io.entgra.device.mgt.core.certificate.mgt.core.service;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
@ -38,6 +36,7 @@ import io.entgra.device.mgt.core.certificate.mgt.core.impl.KeyStoreReader;
import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException;
import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata; import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.bouncycastle.pkcs.PKCS10CertificationRequest; import org.bouncycastle.pkcs.PKCS10CertificationRequest;
@ -210,7 +209,7 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe
metadata = CertificateManagerUtil.getMetadataManagementService().retrieveMetadata(CertificateManagementConstants.CERTIFICATE_DELETE); metadata = CertificateManagerUtil.getMetadataManagementService().retrieveMetadata(CertificateManagementConstants.CERTIFICATE_DELETE);
if (metadata != null) { if (metadata != null) {
String metaValue = metadata.getMetaValue(); String metaValue = metadata.getMetaValue();
if (metaValue != null && !metaValue.isEmpty()) { if (StringUtils.isNotEmpty(metaValue)) {
JsonParser parser = new JsonParser(); JsonParser parser = new JsonParser();
JsonObject jsonObject = parser.parse(metaValue).getAsJsonObject(); JsonObject jsonObject = parser.parse(metaValue).getAsJsonObject();
return jsonObject.get(CertificateManagementConstants.IS_CERTIFICATE_DELETE_ENABLE).getAsBoolean(); return jsonObject.get(CertificateManagementConstants.IS_CERTIFICATE_DELETE_ENABLE).getAsBoolean();

Loading…
Cancel
Save