From fbc7c0807c4634757d6a7f1e836d1d0f75ec69c8 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Wed, 30 Mar 2016 14:10:17 +0530 Subject: [PATCH 1/2] Skipping the validation of task operations --- .../operation/mgt/OperationManagerImpl.java | 9 +++++++ .../mgt/core/task/DeviceTaskManager.java | 8 ++++++ .../core/task/impl/DeviceTaskManagerImpl.java | 26 +++++++++++++++---- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index b6bcb4ce1c..265dd02f8c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -37,6 +37,8 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationManagementDAOF import org.wso2.carbon.device.mgt.core.operation.mgt.dao.OperationMappingDAO; import org.wso2.carbon.device.mgt.core.operation.mgt.dao.util.OperationDAOUtil; import org.wso2.carbon.device.mgt.core.operation.mgt.util.OperationCreateTimeComparator; +import org.wso2.carbon.device.mgt.core.task.DeviceTaskManager; +import org.wso2.carbon.device.mgt.core.task.impl.DeviceTaskManagerImpl; import java.sql.SQLException; import java.util.ArrayList; @@ -679,6 +681,13 @@ public class OperationManagerImpl implements OperationManager { } private boolean isAuthenticationSkippedOperation(Operation operation) { + + //This is to check weather operations are coming from the task related to retrieving device information. + DeviceTaskManager taskManager = new DeviceTaskManagerImpl(); + if (taskManager.isTaskOperation(operation.getCode())) { + return true; + } + boolean status; switch (operation.getCode()) { case DeviceManagementConstants.AuthorizationSkippedOperationCodes.POLICY_OPERATION_CODE : diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManager.java index cc8ff8515f..9e3306cbc1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/DeviceTaskManager.java @@ -68,4 +68,12 @@ public interface DeviceTaskManager { */ List getValidOperationNames() throws DeviceMgtTaskException; + /** + * This method will check wheather give operation is added by the task. + * @param opName - Operation name + * @return - true or false + * @throws DeviceMgtTaskException + */ + boolean isTaskOperation(String opName); + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java index 813caf7f3b..cc9e8ac40b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerImpl.java @@ -76,7 +76,7 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { @Override public String getTaskImplementedClazz() throws DeviceMgtTaskException { - return DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). + return DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). getDeviceManagementConfigRepository().getTaskConfiguration().getTaskClazz(); } @@ -97,7 +97,7 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { List devices = deviceManagementProviderService.getAllDevices(); List operations = this.getValidOperationNames(); - if(!devices.isEmpty()) { + if (!devices.isEmpty()) { for (String str : operations) { CommandOperation operation = new CommandOperation(); @@ -107,12 +107,12 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { deviceManagementProviderService.addOperation(operation, DeviceManagerUtil.convertDevices(devices)); } } else { - if(log.isDebugEnabled()){ + if (log.isDebugEnabled()) { log.debug("No devices are available to perform the operations."); } } } catch (DeviceManagementException e) { - throw new DeviceMgtTaskException("Error occurred while retrieving the device list.", e); + throw new DeviceMgtTaskException("Error occurred while retrieving the device list.", e); } catch (OperationManagementException e) { throw new DeviceMgtTaskException("Error occurred while adding the operations to devices", e); } @@ -142,10 +142,26 @@ public class DeviceTaskManagerImpl implements DeviceTaskManager { } } - if(log.isDebugEnabled()){ + if (log.isDebugEnabled()) { log.debug("Valid operation names are : " + Arrays.toString(opNames.toArray())); } return opNames; } + + @Override + public boolean isTaskOperation(String opName) { + try { + List taskOperations = this.getOperationList(); + for (TaskOperation taop : taskOperations) { + if (taop.getTaskName().equalsIgnoreCase(opName)) { + return true; + } + } + } catch (DeviceMgtTaskException e) { + // ignoring the error, no need to throw, If error occurs, return value will be false. + } + return false; + + } } From 41ba2f4f0a69413204559b942fef81c6dd12dd03 Mon Sep 17 00:00:00 2001 From: inosh-perera Date: Wed, 30 Mar 2016 19:08:42 +0530 Subject: [PATCH 2/2] adding database changes --- .../mgt/core/dao/CertificateDAO.java | 8 +++++ .../dao/impl/GenericCertificateDAOImpl.java | 33 +++++++++++++++++++ .../service/CertificateManagementService.java | 2 ++ .../CertificateManagementServiceImpl.java | 15 +++++++++ .../src/main/resources/dbscripts/cdm/h2.sql | 4 +-- .../main/resources/dbscripts/cdm/mssql.sql | 5 +-- .../main/resources/dbscripts/cdm/mysql.sql | 5 +-- .../main/resources/dbscripts/cdm/oracle.sql | 5 +-- .../resources/dbscripts/cdm/postgresql.sql | 5 +-- 9 files changed, 72 insertions(+), 10 deletions(-) diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateDAO.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateDAO.java index 53dc1a2b03..ca9310a97f 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateDAO.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/CertificateDAO.java @@ -18,6 +18,7 @@ package org.wso2.carbon.certificate.mgt.core.dao; +import org.wso2.carbon.certificate.mgt.core.dto.CertificateResponse; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; @@ -58,6 +59,13 @@ public interface CertificateDAO { */ PaginationResult getAllCertificates(PaginationRequest request) throws CertificateManagementDAOException; + /** + * Get all the certificates. + * @return List of certificates + * @throws CertificateManagementDAOException + */ + public List getAllCertificates() throws CertificateManagementDAOException; + /** * Delete a certificate identified by a serial number() * @param serialNumber serial number diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/impl/GenericCertificateDAOImpl.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/impl/GenericCertificateDAOImpl.java index a7fcc01c82..018d5bbbe8 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/impl/GenericCertificateDAOImpl.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/dao/impl/GenericCertificateDAOImpl.java @@ -157,6 +157,39 @@ public class GenericCertificateDAOImpl implements CertificateDAO { return paginationResult; } + @Override + public List getAllCertificates() throws CertificateManagementDAOException { + PreparedStatement stmt = null; + ResultSet resultSet = null; + CertificateResponse certificateResponse; + List certificates = new ArrayList<>(); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + Connection conn = this.getConnection(); + String sql = "SELECT CERTIFICATE, SERIAL_NUMBER, TENANT_ID FROM DM_DEVICE_CERTIFICATE WHERE TENANT_ID = ? " + + "ORDER BY ID DESC"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, tenantId); + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + certificateResponse = new CertificateResponse(); + byte [] certificateBytes = resultSet.getBytes("CERTIFICATE"); + certificateResponse.setSerialNumber(resultSet.getString("SERIAL_NUMBER")); + certificateResponse.setTenantId(resultSet.getInt("TENANT_ID")); + CertificateGenerator.extractCertificateDetails(certificateBytes, certificateResponse); + certificates.add(certificateResponse); + } + } catch (SQLException e) { + String errorMsg = "SQL error occurred while retrieving the certificates."; + log.error(errorMsg, e); + throw new CertificateManagementDAOException(errorMsg, e); + } finally { + OperationManagementDAOUtil.cleanupResources(stmt, resultSet); + } + return certificates; + } + @Override public boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException { Connection conn; diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java index 7fd89c9f6f..59a8e98b5a 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementService.java @@ -74,4 +74,6 @@ public interface CertificateManagementService { public PaginationResult getAllCertificates(PaginationRequest request) throws CertificateManagementDAOException; boolean removeCertificate(String serialNumber) throws CertificateManagementDAOException; + + public List getCertificates() throws CertificateManagementDAOException; } diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java index 80b865ebec..f4548337fa 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/service/CertificateManagementServiceImpl.java @@ -177,4 +177,19 @@ public class CertificateManagementServiceImpl implements CertificateManagementSe } } + @Override + public List getCertificates() throws CertificateManagementDAOException { + try { + CertificateManagementDAOFactory.openConnection(); + CertificateDAO certificateDAO = CertificateManagementDAOFactory.getCertificateDAO(); + return certificateDAO.getAllCertificates(); + } catch (SQLException e) { + String errorMsg = "Error when opening connection"; + log.error(errorMsg, e); + throw new CertificateManagementDAOException(errorMsg, e); + } finally { + CertificateManagementDAOFactory.closeConnection(); + } + } + } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index c4b369760e..3dfaa9e13b 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -217,7 +217,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY ( CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES ( ID INT(11) NOT NULL AUTO_INCREMENT, PROFILE_ID INT(11) NOT NULL, - FEATURE_CODE VARCHAR(30) NOT NULL, + FEATURE_CODE VARCHAR(100) NOT NULL, DEVICE_TYPE_ID INT NOT NULL, TENANT_ID INT(11) NOT NULL , CONTENT BLOB NULL DEFAULT NULL, @@ -349,7 +349,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES ( ID INT NOT NULL AUTO_INCREMENT, COMPLIANCE_STATUS_ID INT NOT NULL, TENANT_ID INT NOT NULL, - FEATURE_CODE VARCHAR(15) NOT NULL, + FEATURE_CODE VARCHAR(100) NOT NULL, STATUS INT NULL, PRIMARY KEY (ID), CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql index 91104f4641..bae7db4986 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -8,6 +8,7 @@ CREATE TABLE DM_DEVICE_CERTIFICATE ( ID INTEGER IDENTITY NOT NULL, SERIAL_NUMBER VARCHAR(500) DEFAULT NULL, CERTIFICATE VARBINARY(max) DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); @@ -196,7 +197,7 @@ CREATE TABLE DM_DEVICE_TYPE_POLICY ( CREATE TABLE DM_PROFILE_FEATURES ( ID INT NOT NULL IDENTITY, PROFILE_ID INT NOT NULL, - FEATURE_CODE VARCHAR(30) NOT NULL, + FEATURE_CODE VARCHAR(100) NOT NULL, DEVICE_TYPE_ID INT NOT NULL, TENANT_ID INT NOT NULL , CONTENT VARBINARY(max) NULL DEFAULT NULL, @@ -328,7 +329,7 @@ CREATE TABLE DM_POLICY_COMPLIANCE_FEATURES ( ID INT NOT NULL IDENTITY, COMPLIANCE_STATUS_ID INT NOT NULL, TENANT_ID INT NOT NULL, - FEATURE_CODE VARCHAR(15) NOT NULL, + FEATURE_CODE VARCHAR(100) NOT NULL, STATUS INT NULL, PRIMARY KEY (ID), CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql index 22494de244..b7431857ec 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -8,6 +8,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_CERTIFICATE ( ID INTEGER auto_increment NOT NULL, SERIAL_NUMBER VARCHAR(500) DEFAULT NULL, CERTIFICATE BLOB DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) )ENGINE = InnoDB; @@ -191,7 +192,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY ( CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES ( ID INT(11) NOT NULL AUTO_INCREMENT, PROFILE_ID INT(11) NOT NULL, - FEATURE_CODE VARCHAR(30) NOT NULL, + FEATURE_CODE VARCHAR(100) NOT NULL, DEVICE_TYPE_ID INT NOT NULL, TENANT_ID INT(11) NOT NULL , CONTENT BLOB NULL DEFAULT NULL, @@ -319,7 +320,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES ( ID INT NOT NULL AUTO_INCREMENT, COMPLIANCE_STATUS_ID INT NOT NULL, TENANT_ID INT NOT NULL, - FEATURE_CODE VARCHAR(15) NOT NULL, + FEATURE_CODE VARCHAR(100) NOT NULL, STATUS INT NULL, PRIMARY KEY (ID), CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql index bb88c94ac6..f7a68e3ee1 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -22,6 +22,7 @@ CREATE TABLE DM_DEVICE_CERTIFICATE ( ID NUMBER(10) NOT NULL, SERIAL_NUMBER VARCHAR2(500) DEFAULT NULL, CERTIFICATE BLOB DEFAULT NULL, + TENANT_ID NUMBER(10) DEFAULT 0, PRIMARY KEY (ID) ) / @@ -308,7 +309,7 @@ CREATE TABLE DM_DEVICE_TYPE_POLICY ( CREATE TABLE DM_PROFILE_FEATURES ( ID NUMBER(10) NOT NULL, PROFILE_ID NUMBER(10) NOT NULL, - FEATURE_CODE VARCHAR2(30) NOT NULL, + FEATURE_CODE VARCHAR2(100) NOT NULL, DEVICE_TYPE_ID NUMBER(10) NOT NULL, TENANT_ID NUMBER(10) NOT NULL , CONTENT BLOB DEFAULT NULL NULL, @@ -543,7 +544,7 @@ CREATE TABLE DM_POLICY_COMPLIANCE_FEATURES ( ID NUMBER(10) NOT NULL, COMPLIANCE_STATUS_ID NUMBER(10) NOT NULL, TENANT_ID NUMBER(10) NOT NULL, - FEATURE_CODE VARCHAR2(15) NOT NULL, + FEATURE_CODE VARCHAR2(100) NOT NULL, STATUS NUMBER(10) NULL, CONSTRAINT PK_COMPLIANCE_FEATURES_STATUS PRIMARY KEY (ID), CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 0364345d47..40e2a47540 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -7,6 +7,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_CERTIFICATE ( ID BIGSERIAL NOT NULL PRIMARY KEY, SERIAL_NUMBER VARCHAR(500) DEFAULT NULL, CERTIFICATE BYTEA DEFAULT NULL + TENANT_ID INTEGER DEFAULT 0, ); CREATE TABLE IF NOT EXISTS DM_DEVICE ( @@ -169,7 +170,7 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY ( CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES ( ID BIGSERIAL NOT NULL PRIMARY KEY, PROFILE_ID INTEGER NOT NULL, - FEATURE_CODE VARCHAR(30) NOT NULL, + FEATURE_CODE VARCHAR(100) NOT NULL, DEVICE_TYPE_ID INT NOT NULL, TENANT_ID INTEGER NOT NULL , CONTENT BYTEA NULL DEFAULT NULL, @@ -280,7 +281,7 @@ CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES ( ID BIGSERIAL NOT NULL PRIMARY KEY, COMPLIANCE_STATUS_ID INTEGER NOT NULL, TENANT_ID INTEGER NOT NULL, - FEATURE_CODE VARCHAR(15) NOT NULL, + FEATURE_CODE VARCHAR(100) NOT NULL, STATUS INTEGER NULL, CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS FOREIGN KEY (COMPLIANCE_STATUS_ID)