Merge branch 'master' of https://repository.entgra.net/community/device-mgt-core into device-info-config

pull/204/head
shamalka 1 year ago
commit f286cb2306

@ -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";

@ -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(

@ -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<CertificateResponse> searchCertificate(String serialNumber) throws CertificateManagementDAOException;

@ -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 {

@ -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);

@ -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;
}
}

@ -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;
}
}

@ -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;
}
}

@ -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;
}
}

@ -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());

@ -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<CertificateResponse> getCertificates() throws CertificateManagementException;

@ -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);

@ -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")

Loading…
Cancel
Save