From 7023537fcdd75ea0aaef088b8880946942fa113e Mon Sep 17 00:00:00 2001 From: Dilshan Edirisuriya Date: Mon, 16 Nov 2015 13:49:02 +0530 Subject: [PATCH 1/3] Setting owner in authentication info --- .../authenticator/CertificateAuthenticator.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/CertificateAuthenticator.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/CertificateAuthenticator.java index 88d695cf16..2dd530c16f 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/CertificateAuthenticator.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/CertificateAuthenticator.java @@ -7,6 +7,7 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.core.scep.SCEPException; import org.wso2.carbon.device.mgt.core.scep.SCEPManager; import org.wso2.carbon.device.mgt.core.scep.TenantedDeviceWrapper; @@ -68,6 +69,14 @@ public class CertificateAuthenticator implements WebappAuthenticator { TenantedDeviceWrapper tenantedDeviceWrapper = scepManager.getValidatedDevice(deviceIdentifier); authenticationInfo.setTenantDomain(tenantedDeviceWrapper.getTenantDomain()); authenticationInfo.setTenantId(tenantedDeviceWrapper.getTenantId()); + + if(tenantedDeviceWrapper.getDevice() != null && + tenantedDeviceWrapper.getDevice().getEnrolmentInfo() != null) { + + EnrolmentInfo enrolmentInfo = tenantedDeviceWrapper.getDevice().getEnrolmentInfo(); + authenticationInfo.setUsername(enrolmentInfo.getOwner()); + } + authenticationInfo.setStatus(Status.CONTINUE); } } From 813f9072d8a07ef70eb25a824368e72d7cc5faf7 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 16 Nov 2015 17:15:16 +0530 Subject: [PATCH 2/3] Fixing the database issue which causes the deviceids slelect did not work --- .../carbon/policy/mgt/core/dao/PolicyDAO.java | 2 +- .../mgt/core/dao/impl/PolicyDAOImpl.java | 7 ++-- .../core/mgt/impl/MonitoringManagerImpl.java | 34 +++++++++++-------- .../policy/mgt/core/MonitoringTestCase.java | 6 +--- .../src/test/resources/testng.xml | 2 +- 5 files changed, 26 insertions(+), 25 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index d1065e17a5..facb7528c9 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -140,7 +140,7 @@ public interface PolicyDAO { Policy getAppliedPolicy(int deviceId, int enrollmentId) throws PolicyManagerDAOException; - HashMap getAppliedPolicyIds(List deviceIds) throws PolicyManagerDAOException; + HashMap getAppliedPolicyIds() throws PolicyManagerDAOException; HashMap getAppliedPolicyIdsDeviceIds() throws PolicyManagerDAOException; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 33a56b6623..318505991b 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -1563,7 +1563,7 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public HashMap getAppliedPolicyIds(List deviceIds) throws PolicyManagerDAOException { + public HashMap getAppliedPolicyIds() throws PolicyManagerDAOException { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -1571,10 +1571,9 @@ public class PolicyDAOImpl implements PolicyDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; + String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?"; stmt = conn.prepareStatement(query); - stmt.setString(1, PolicyManagerUtil.makeString(deviceIds)); - stmt.setInt(2, tenantId); + stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index f6d14166a4..344de73897 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -62,8 +62,8 @@ public class MonitoringManagerImpl implements MonitoringManager { private static final Log log = LogFactory.getLog(MonitoringManagerImpl.class); private static final String OPERATION_MONITOR = "MONITOR"; - private static final String OPERATION_INFO = "DEVICE_INFO"; - private static final String OPERATION_APP_LIST = "APPLICATION_LIST"; + private static final String OPERATION_INFO = "DEVICE_INFO"; + private static final String OPERATION_APP_LIST = "APPLICATION_LIST"; public MonitoringManagerImpl() { this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO(); @@ -229,7 +229,7 @@ public class MonitoringManagerImpl implements MonitoringManager { //int tenantId = PolicyManagerUtil.getTenantId(); Map deviceIds = new HashMap<>(); List complianceDatas = new ArrayList<>(); - HashMap devicePolicyIdMap; + HashMap devicePolicyIdMap = null; try { PolicyManagementDAOFactory.openConnection(); @@ -247,7 +247,13 @@ public class MonitoringManagerImpl implements MonitoringManager { } List deviceIDs = new ArrayList<>(deviceIds.keySet()); - devicePolicyIdMap = policyDAO.getAppliedPolicyIds(deviceIDs); + HashMap temp = policyDAO.getAppliedPolicyIds(); + for (Integer id : deviceIDs) { + if (temp != null && temp.containsKey(id)) { + devicePolicyIdMap.put(id, temp.get(id)); + } + } + } catch (SQLException e) { throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e); } catch (MonitoringDAOException e) { @@ -380,19 +386,19 @@ public class MonitoringManagerImpl implements MonitoringManager { monitoringOperation.setEnabled(true); monitoringOperation.setType(Operation.Type.COMMAND); monitoringOperation.setCode(OPERATION_MONITOR); - CommandOperation infoOperation = new CommandOperation(); - infoOperation.setEnabled(true); - infoOperation.setType(Operation.Type.COMMAND); - infoOperation.setCode(OPERATION_INFO); - CommandOperation appListOperation = new CommandOperation(); - appListOperation.setEnabled(true); - appListOperation.setType(Operation.Type.COMMAND); - appListOperation.setCode(OPERATION_APP_LIST); +// CommandOperation infoOperation = new CommandOperation(); +// infoOperation.setEnabled(true); +// infoOperation.setType(Operation.Type.COMMAND); +// infoOperation.setCode(OPERATION_INFO); +// CommandOperation appListOperation = new CommandOperation(); +// appListOperation.setEnabled(true); +// appListOperation.setType(Operation.Type.COMMAND); +// appListOperation.setCode(OPERATION_APP_LIST); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); service.addOperation(monitoringOperation, deviceIdentifiers); - service.addOperation(infoOperation, deviceIdentifiers); - service.addOperation(appListOperation, deviceIdentifiers); +// service.addOperation(infoOperation, deviceIdentifiers); +// service.addOperation(appListOperation, deviceIdentifiers); } private List getDeviceIdentifiersFromDevices(List devices) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java index f1fdfed8e1..f0943c6f8c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java @@ -31,12 +31,8 @@ import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; -import org.wso2.carbon.ntask.common.TaskException; -import org.wso2.carbon.ntask.core.service.TaskService; -import org.wso2.carbon.ntask.core.service.impl.TaskServiceImpl; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; -import org.wso2.carbon.policy.mgt.common.PolicyMonitoringTaskException; import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; @@ -127,7 +123,7 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); List devices = service.getAllDevices(ANDROID); - monitoringManager.addMonitoringOperation(devices); + // monitoringManager.addMonitoringOperation(devices); log.debug("Compliance operations adding done."); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml index 24a1f0d80e..8d74befa5d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml @@ -22,7 +22,7 @@ - + From 0f146e83faf3a06413f20c567c54465baf743903 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 16 Nov 2015 17:17:15 +0530 Subject: [PATCH 3/3] Fixing the issue which cause policy not bieng applied to device which were enrolled before the adding the policy to the system --- .../policy/mgt/core/enforcement/DelegationTask.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java index ad9f58654c..2de040e451 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java @@ -32,7 +32,6 @@ import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -73,13 +72,13 @@ public class DelegationTask implements Task { throw new PolicyManagementException("Error occurred while taking the devices", e); } } - HashMap deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds(); +// HashMap deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds(); List toBeNotified = new ArrayList<>(); for (Device device : devices) { - if (deviceIdPolicy.containsKey(device.getId())) { - toBeNotified.add(device); - } +// if (deviceIdPolicy.containsKey(device.getId())) { + toBeNotified.add(device); +// } } if (!toBeNotified.isEmpty()) { PolicyEnforcementDelegator enforcementDelegator = new PolicyEnforcementDelegatorImpl(toBeNotified);