Compare commits

Invalid templates have been ignored

1 invalid template(s) found pull_request_template.md: frontmatter must start with a separator line

..

10 Commits

@ -62,17 +62,18 @@ public class APIApplicationManagerExtensionDataHolder {
public void setRealmService(RealmService realmService) { public void setRealmService(RealmService realmService) {
this.realmService = realmService; this.realmService = realmService;
this.setTenantManager(realmService); setTenantManager(realmService != null ?
realmService.getTenantManager() : null);
} }
private void setTenantManager(RealmService realmService) { private void setTenantManager(TenantManager tenantManager) {
if (realmService == null) { this.tenantManager = tenantManager;
throw new IllegalStateException("Realm service is not initialized properly");
}
this.tenantManager = realmService.getTenantManager();
} }
public TenantManager getTenantManager() { public TenantManager getTenantManager() {
if (tenantManager == null) {
throw new IllegalStateException("Tenant manager is not initialized properly");
}
return tenantManager; return tenantManager;
} }

@ -75,17 +75,18 @@ public class APIPublisherDataHolder {
public void setRealmService(RealmService realmService) { public void setRealmService(RealmService realmService) {
this.realmService = realmService; this.realmService = realmService;
this.setTenantManager(realmService); setTenantManager(realmService != null ?
realmService.getTenantManager() : null);
} }
private void setTenantManager(RealmService realmService) { private void setTenantManager(TenantManager tenantManager) {
if (realmService == null) { this.tenantManager = tenantManager;
throw new IllegalStateException("Realm service is not initialized properly");
}
this.tenantManager = realmService.getTenantManager();
} }
public TenantManager getTenantManager() { public TenantManager getTenantManager() {
if (tenantManager == null) {
throw new IllegalStateException("Tenant manager is not initialized properly");
}
return tenantManager; return tenantManager;
} }

@ -151,20 +151,20 @@ public class CertificateManagementAdminServiceImpl implements CertificateManagem
} }
@DELETE @DELETE
public Response removeCertificate(@QueryParam("serialNumber") String serialNumber) { public Response removeCertificate(@QueryParam("certificateId") String certificateId) {
RequestValidationUtil.validateSerialNumber(serialNumber); RequestValidationUtil.validateCertificateId(certificateId);
CertificateManagementService certificateService = CertificateMgtAPIUtils.getCertificateManagementService(); CertificateManagementService certificateService = CertificateMgtAPIUtils.getCertificateManagementService();
try { try {
boolean status = certificateService.removeCertificate(serialNumber); boolean status = certificateService.removeCertificate(certificateId);
if (!status) { if (!status) {
return Response.status(Response.Status.NOT_FOUND).entity( return Response.status(Response.Status.NOT_FOUND).entity(
"No certificate is found with the given " + "No certificate is found with the given " +
"serial number '" + serialNumber + "'").build(); "certificate id '" + certificateId + "'").build();
} else { } else {
return Response.status(Response.Status.OK).entity( return Response.status(Response.Status.OK).entity(
"Certificate that carries the serial number '" + "Certificate that carries the certificate id '" +
serialNumber + "' has been removed").build(); certificateId + "' 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 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) { public static void validatePaginationInfo(int offset, int limit) {
if (offset < 0) { if (offset < 0) {
throw new InputValidationException( throw new InputValidationException(

@ -95,10 +95,10 @@ public interface CertificateDAO {
/** /**
* Delete a certificate identified by a serial number() * Delete a certificate identified by a serial number()
* *
* @param serialNumber serial number * @param certificateId number
* @return whether the certificate was removed or not. * @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; List<CertificateResponse> searchCertificate(String serialNumber) throws CertificateManagementDAOException;

@ -237,7 +237,7 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
Connection conn = this.getConnection(); 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"; + " FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
@ -247,6 +247,8 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{
certificateResponse = new CertificateResponse(); certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE"); byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER")); certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
certificateResponse.setCertificateId(resultSet.getString("ID"));
certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER"));
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID")); certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
certificateResponse.setUsername(resultSet.getString("USERNAME")); certificateResponse.setUsername(resultSet.getString("USERNAME"));
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse); CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
@ -263,7 +265,7 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{
} }
@Override @Override
public boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException { public boolean removeCertificate(String certificateId) throws CertificateManagementDAOException {
Connection conn; Connection conn;
PreparedStatement stmt = null; PreparedStatement stmt = null;
ResultSet resultSet = null; ResultSet resultSet = null;
@ -271,15 +273,15 @@ public abstract class AbstractCertificateDAOImpl implements CertificateDAO{
try { try {
conn = this.getConnection(); conn = this.getConnection();
String query = String query =
"DELETE FROM DM_DEVICE_CERTIFICATE WHERE SERIAL_NUMBER = ?" + "DELETE FROM DM_DEVICE_CERTIFICATE WHERE ID = ?" +
" AND TENANT_ID = ? "; " AND TENANT_ID = ? ";
stmt = conn.prepareStatement(query); stmt = conn.prepareStatement(query);
stmt.setString(1, serialNumber); stmt.setString(1, certificateId);
stmt.setInt(2, tenantId); stmt.setInt(2, tenantId);
return stmt.executeUpdate() > 0; return stmt.executeUpdate() > 0;
} catch (SQLException e) { } 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); log.error(msg, e);
throw new CertificateManagementDAOException(msg, e); throw new CertificateManagementDAOException(msg, e);
} finally { } finally {

@ -47,6 +47,27 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {
return CertificateManagementDAOFactory.getConnection(); 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 @Override
public PaginationResult getAllCertificates(int rowNum, int limit) throws CertificateManagementDAOException { public PaginationResult getAllCertificates(int rowNum, int limit) throws CertificateManagementDAOException {
@ -58,7 +79,7 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
Connection conn = this.getConnection(); 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 ?,?"; + "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC LIMIT ?,?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
@ -71,6 +92,8 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
certificateResponse = new CertificateResponse(); certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE"); byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER")); certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
certificateResponse.setCertificateId(resultSet.getString("ID"));
certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER"));
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID")); certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
certificateResponse.setUsername(resultSet.getString("USERNAME")); certificateResponse.setUsername(resultSet.getString("USERNAME"));
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse); CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
@ -79,7 +102,7 @@ public class GenericCertificateDAOImpl extends AbstractCertificateDAOImpl {
} }
paginationResult = new PaginationResult(); paginationResult = new PaginationResult();
paginationResult.setData(certificates); paginationResult.setData(certificates);
paginationResult.setRecordsTotal(resultCount); paginationResult.setRecordsTotal(this.getCertificateCount(tenantId));
} 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);

@ -53,7 +53,7 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
Connection conn = this.getConnection(); 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"; + "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
@ -66,6 +66,8 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
certificateResponse = new CertificateResponse(); certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE"); byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER")); certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
certificateResponse.setCertificateId(resultSet.getString("ID"));
certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER"));
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID")); certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
certificateResponse.setUsername(resultSet.getString("USERNAME")); certificateResponse.setUsername(resultSet.getString("USERNAME"));
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse); CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
@ -74,7 +76,7 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
} }
paginationResult = new PaginationResult(); paginationResult = new PaginationResult();
paginationResult.setData(certificates); paginationResult.setData(certificates);
paginationResult.setRecordsTotal(resultCount); paginationResult.setRecordsTotal(this.getCertificateCount(tenantId));
} 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);
@ -88,4 +90,26 @@ public class OracleCertificateDAOImpl extends AbstractCertificateDAOImpl {
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {
return CertificateManagementDAOFactory.getConnection(); 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(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
Connection conn = this.getConnection(); 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 ?"; + "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC LIMIT ? OFFSET ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
@ -66,6 +66,8 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
certificateResponse = new CertificateResponse(); certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE"); byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER")); certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
certificateResponse.setCertificateId(resultSet.getString("ID"));
certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER"));
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID")); certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
certificateResponse.setUsername(resultSet.getString("USERNAME")); certificateResponse.setUsername(resultSet.getString("USERNAME"));
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse); CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
@ -74,7 +76,7 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
} }
paginationResult = new PaginationResult(); paginationResult = new PaginationResult();
paginationResult.setData(certificates); paginationResult.setData(certificates);
paginationResult.setRecordsTotal(resultCount); paginationResult.setRecordsTotal(this.getCertificateCount(tenantId));
} 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);
@ -88,4 +90,26 @@ public class PostgreSQLCertificateDAOImpl extends AbstractCertificateDAOImpl {
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {
return CertificateManagementDAOFactory.getConnection(); 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(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
try { try {
Connection conn = this.getConnection(); 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"; + "DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? ORDER BY ID DESC OFFSET ? ROWS FETCH NEXT ? ROWS ONLY";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, tenantId); stmt.setInt(1, tenantId);
@ -66,6 +66,8 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
certificateResponse = new CertificateResponse(); certificateResponse = new CertificateResponse();
byte[] certificateBytes = resultSet.getBytes("CERTIFICATE"); byte[] certificateBytes = resultSet.getBytes("CERTIFICATE");
certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER")); certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER"));
certificateResponse.setCertificateId(resultSet.getString("ID"));
certificateResponse.setDeviceIdentifier(resultSet.getString("DEVICE_IDENTIFIER"));
certificateResponse.setTenantId(resultSet.getInt("TENANT_ID")); certificateResponse.setTenantId(resultSet.getInt("TENANT_ID"));
certificateResponse.setUsername(resultSet.getString("USERNAME")); certificateResponse.setUsername(resultSet.getString("USERNAME"));
CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse); CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse);
@ -74,7 +76,7 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
} }
paginationResult = new PaginationResult(); paginationResult = new PaginationResult();
paginationResult.setData(certificates); paginationResult.setData(certificates);
paginationResult.setRecordsTotal(resultCount); paginationResult.setRecordsTotal(this.getCertificateCount(tenantId));
} 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);
@ -88,4 +90,26 @@ public class SQLServerCertificateDAOImpl extends AbstractCertificateDAOImpl {
private Connection getConnection() throws SQLException { private Connection getConnection() throws SQLException {
return CertificateManagementDAOFactory.getConnection(); 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) @ApiModelProperty(name = "serialNumber", value = "It is the unique ID that is used to identify a certificate", required = true)
String serialNumber; 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) @ApiModelProperty(name = "tenantId", value = "The ID of the tenant who adds the certificate", required = true)
int tenantId; int tenantId;
@ -44,8 +50,8 @@ public class CertificateResponse {
@ApiModelProperty(name = "notBefore", value = "The date from when the certificate is valid", required = true) @ApiModelProperty(name = "notBefore", value = "The date from when the certificate is valid", required = true)
long notBefore; long notBefore;
@ApiModelProperty(name = "certificateserial", value = "The serial number of the certificate", required = true) @ApiModelProperty(name = "certificateSerial", value = "The serial number of the certificate", required = true)
BigInteger certificateserial; BigInteger certificateSerial;
@ApiModelProperty(name = "issuer", value = "The identity of the authority that signs the SSL certificate", required = true) @ApiModelProperty(name = "issuer", value = "The identity of the authority that signs the SSL certificate", required = true)
String issuer; String issuer;
@ -83,12 +89,12 @@ public class CertificateResponse {
this.notBefore = notBefore; this.notBefore = notBefore;
} }
public BigInteger getCertificateserial() { public BigInteger getCertificateSerial() {
return certificateserial; return certificateSerial;
} }
public void setCertificateserial(BigInteger certificateserial) { public void setCertificateSerial(BigInteger certificateSerial) {
this.certificateserial = certificateserial; this.certificateSerial = certificateSerial;
} }
public String getIssuer() { public String getIssuer() {
@ -146,4 +152,20 @@ public class CertificateResponse {
public void setTenantId(int tenantId) { public void setTenantId(int tenantId) {
this.tenantId = 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; X509Certificate certificate = (X509Certificate) x509Certificate;
certificateResponse.setNotAfter(certificate.getNotAfter().getTime()); certificateResponse.setNotAfter(certificate.getNotAfter().getTime());
certificateResponse.setNotBefore(certificate.getNotBefore().getTime()); certificateResponse.setNotBefore(certificate.getNotBefore().getTime());
certificateResponse.setCertificateserial(certificate.getSerialNumber()); certificateResponse.setCertificateSerial(certificate.getSerialNumber());
certificateResponse.setIssuer(certificate.getIssuerDN().getName()); certificateResponse.setIssuer(certificate.getIssuerDN().getName());
certificateResponse.setSubject(certificate.getSubjectDN().getName()); certificateResponse.setSubject(certificate.getSubjectDN().getName());
certificateResponse.setCertificateVersion(certificate.getVersion()); certificateResponse.setCertificateVersion(certificate.getVersion());

@ -73,7 +73,7 @@ public interface CertificateManagementService {
PaginationResult getAllCertificates(int rowNum, int limit) throws CertificateManagementException; 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; List<CertificateResponse> getCertificates() throws CertificateManagementException;

@ -174,20 +174,20 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe
} }
@Override @Override
public boolean removeCertificate(String serialNumber) throws CertificateManagementException { public boolean removeCertificate(String certificateId) throws CertificateManagementException {
try { try {
CertificateManagementDAOFactory.beginTransaction(); CertificateManagementDAOFactory.beginTransaction();
CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO(); CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO();
boolean status = certificateDAO.removeCertificate(serialNumber); boolean status = certificateDAO.removeCertificate(certificateId);
CertificateManagementDAOFactory.commitTransaction(); CertificateManagementDAOFactory.commitTransaction();
return status; return status;
} catch (TransactionManagementException e) { } 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); log.error(msg, e);
throw new CertificateManagementException(msg, e); throw new CertificateManagementException(msg, e);
} catch (CertificateManagementDAOException e) { } catch (CertificateManagementDAOException e) {
CertificateManagementDAOFactory.rollbackTransaction(); 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"; "' from the certificate repository";
log.error(msg, e); log.error(msg, e);
throw new CertificateManagementException(msg, e); throw new CertificateManagementException(msg, e);

@ -210,7 +210,7 @@ public class CertificateManagementServiceImplTests extends BaseDeviceManagementC
X509Certificate x509Certificate = managementService.generateX509Certificate(); X509Certificate x509Certificate = managementService.generateX509Certificate();
CertificateResponse certificateResponse = managementService.retrieveCertificate(x509Certificate.getSerialNumber().toString()); CertificateResponse certificateResponse = managementService.retrieveCertificate(x509Certificate.getSerialNumber().toString());
Assert.assertNotNull(certificateResponse); 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") @Test(description = "This test case tests the retrieval of Certificates from keystore in desired pagination")

@ -29,6 +29,7 @@ import io.entgra.device.mgt.core.device.mgt.common.group.mgt.RoleDoesNotExistExc
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.wso2.carbon.CarbonConstants; import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import io.entgra.device.mgt.core.device.mgt.common.Device; import io.entgra.device.mgt.core.device.mgt.common.Device;
import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier; import io.entgra.device.mgt.core.device.mgt.common.DeviceIdentifier;
@ -48,6 +49,8 @@ import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.RequestV
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils;
import io.entgra.device.mgt.core.policy.mgt.common.PolicyAdministratorPoint; import io.entgra.device.mgt.core.policy.mgt.common.PolicyAdministratorPoint;
import io.entgra.device.mgt.core.policy.mgt.common.PolicyManagementException; import io.entgra.device.mgt.core.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
import javax.ws.rs.DefaultValue; import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
@ -56,6 +59,7 @@ import javax.ws.rs.Path;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
public class GroupManagementServiceImpl implements GroupManagementService { public class GroupManagementServiceImpl implements GroupManagementService {
@ -109,8 +113,18 @@ public class GroupManagementServiceImpl implements GroupManagementService {
request.setGroupName(name); request.setGroupName(name);
request.setOwner(owner); request.setOwner(owner);
request.setDepth(depth); request.setDepth(depth);
PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService() int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
.getGroupsWithHierarchy(currentUser, request, requireGroupProps); UserRealm realmService = DeviceMgtAPIUtils.getRealmService().getTenantUserRealm(tenantId);
String[] roles = realmService.getUserStoreManager().getRoleListOfUser(currentUser);
boolean hasAdminRole = Arrays.asList(roles).contains(DEFAULT_ADMIN_ROLE);
PaginationResult deviceGroupsResult;
if (hasAdminRole) {
deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroupsWithHierarchy(null, request, requireGroupProps);
} else{
deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroupsWithHierarchy(currentUser, request, requireGroupProps);
}
DeviceGroupList deviceGroupList = new DeviceGroupList(); DeviceGroupList deviceGroupList = new DeviceGroupList();
deviceGroupList.setList(deviceGroupsResult.getData()); deviceGroupList.setList(deviceGroupsResult.getData());
deviceGroupList.setCount(deviceGroupsResult.getRecordsTotal()); deviceGroupList.setCount(deviceGroupsResult.getRecordsTotal());
@ -119,6 +133,10 @@ public class GroupManagementServiceImpl implements GroupManagementService {
String error = "Error occurred while retrieving groups with hierarchy."; String error = "Error occurred while retrieving groups with hierarchy.";
log.error(error, e); log.error(error, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build();
} catch (UserStoreException e) {
String msg = "Error occurred while getting user realm.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} }
} }

@ -31,7 +31,11 @@ import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.DeviceGroupList;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.admin.GroupManagementAdminService; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.api.admin.GroupManagementAdminService;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.RequestValidationUtil; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.RequestValidationUtil;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtAPIUtils;
import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.apache.commons.lang.StringUtils;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
import javax.ws.rs.DefaultValue; import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
@ -40,6 +44,7 @@ import javax.ws.rs.Path;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
public class GroupManagementAdminServiceImpl implements GroupManagementAdminService { public class GroupManagementAdminServiceImpl implements GroupManagementAdminService {
@ -94,13 +99,25 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ
@DefaultValue("5") @QueryParam("limit") int limit) { @DefaultValue("5") @QueryParam("limit") int limit) {
try { try {
RequestValidationUtil.validatePaginationParameters(offset, limit); RequestValidationUtil.validatePaginationParameters(offset, limit);
String currentUser = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
GroupPaginationRequest request = new GroupPaginationRequest(offset, limit); GroupPaginationRequest request = new GroupPaginationRequest(offset, limit);
request.setGroupName(name); request.setGroupName(name);
request.setOwner(owner); request.setOwner(owner);
request.setStatus(status); request.setStatus(status);
request.setDepth(depth); request.setDepth(depth);
PaginationResult deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService() int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
.getGroupsWithHierarchy(null, request, requireGroupProps); UserRealm realmService = DeviceMgtAPIUtils.getRealmService().getTenantUserRealm(tenantId);
String[] roles = realmService.getUserStoreManager().getRoleListOfUser(currentUser);
boolean isAdmin = DEFAULT_ADMIN_ROLE.equals(currentUser);
boolean hasAdminRole = Arrays.asList(roles).contains(DEFAULT_ADMIN_ROLE);
PaginationResult deviceGroupsResult;
if (StringUtils.isBlank(currentUser) || isAdmin || hasAdminRole) {
deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroupsWithHierarchy(null, request, requireGroupProps);
} else {
deviceGroupsResult = DeviceMgtAPIUtils.getGroupManagementProviderService()
.getGroupsWithHierarchy(currentUser, request, requireGroupProps);
}
DeviceGroupList deviceGroupList = new DeviceGroupList(); DeviceGroupList deviceGroupList = new DeviceGroupList();
deviceGroupList.setList(deviceGroupsResult.getData()); deviceGroupList.setList(deviceGroupsResult.getData());
deviceGroupList.setCount(deviceGroupsResult.getRecordsTotal()); deviceGroupList.setCount(deviceGroupsResult.getRecordsTotal());
@ -109,6 +126,10 @@ public class GroupManagementAdminServiceImpl implements GroupManagementAdminServ
String error = "Error occurred while retrieving groups with hierarchy."; String error = "Error occurred while retrieving groups with hierarchy.";
log.error(error, e); log.error(error, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(error).build();
} catch (UserStoreException e) {
String msg = "Error occurred while getting user realm.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
} }
} }

@ -480,6 +480,7 @@ public class OperationManagerImpl implements OperationManager {
int failAttempts = 0; int failAttempts = 0;
while (true) { while (true) {
try { try {
OperationManagementDAOFactory.beginTransaction();
operationMappingDAO.updateOperationMapping(operation.getId(), device.getEnrolmentInfo().getId(), operationMappingDAO.updateOperationMapping(operation.getId(), device.getEnrolmentInfo().getId(),
io.entgra.device.mgt.core.device.mgt.core.dto.operation.mgt.Operation.PushNotificationStatus.SCHEDULED); io.entgra.device.mgt.core.device.mgt.core.dto.operation.mgt.Operation.PushNotificationStatus.SCHEDULED);
OperationManagementDAOFactory.commitTransaction(); OperationManagementDAOFactory.commitTransaction();
@ -502,6 +503,11 @@ public class OperationManagerImpl implements OperationManager {
} catch (InterruptedException ignore) { } catch (InterruptedException ignore) {
break; break;
} }
} catch (TransactionManagementException ex) {
log.error("Error occurred while initiating the transaction", ex);
break;
} finally {
OperationManagementDAOFactory.closeConnection();
} }
} }
} catch (Exception e) { } catch (Exception e) {

@ -74,17 +74,18 @@ public class JWTClientExtensionDataHolder {
public void setRealmService(RealmService realmService) { public void setRealmService(RealmService realmService) {
this.realmService = realmService; this.realmService = realmService;
this.setTenantManager(realmService); setTenantManager(realmService != null ?
realmService.getTenantManager() : null);
} }
private void setTenantManager(RealmService realmService) { private void setTenantManager(TenantManager tenantManager) {
if (realmService == null) { this.tenantManager = tenantManager;
throw new IllegalStateException("Realm service is not initialized properly");
}
this.tenantManager = realmService.getTenantManager();
} }
public TenantManager getTenantManager() { public TenantManager getTenantManager() {
if (tenantManager == null) {
throw new IllegalStateException("Tenant manager is not initialized properly");
}
return tenantManager; return tenantManager;
} }
} }

Loading…
Cancel
Save