From 044995ed971f257dac016950ce8f3bb5bd83e681 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Tue, 1 Sep 2015 15:03:09 +0530 Subject: [PATCH 1/3] Adding the activate method when policies are added. --- .../policy/mgt/core/mgt/impl/MonitoringManagerImpl.java | 9 ++++----- .../policy/mgt/core/mgt/impl/PolicyManagerImpl.java | 4 +++- 2 files changed, 7 insertions(+), 6 deletions(-) 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 6b52b04e7a4..3a0dd3d78d8 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 @@ -59,7 +59,7 @@ import java.util.Map; public class MonitoringManagerImpl implements MonitoringManager { private PolicyDAO policyDAO; -// private DeviceDAO deviceDAO; + // private DeviceDAO deviceDAO; private DeviceTypeDAO deviceTypeDAO; private MonitoringDAO monitoringDAO; private ComplianceDecisionPoint complianceDecisionPoint; @@ -226,8 +226,6 @@ public class MonitoringManagerImpl implements MonitoringManager { @Override public void addMonitoringOperation(List devices) throws PolicyComplianceException { - ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); - //int tenantId = PolicyManagerUtil.getTenantId(); Map deviceIds = new HashMap<>(); List complianceDatas; @@ -327,10 +325,11 @@ public class MonitoringManagerImpl implements MonitoringManager { } } - // TODO : This should be uncommented, this is to mark the device as unreachable, But given the current implementation - // we are not able to do so. + // TODO : This should be uncommented, this is to mark the device as unreachable, But given the current + // implementation we are not able to do so. // if(!deviceToMarkUnreachable.isEmpty()) { +// ComplianceDecisionPoint decisionPoint = new ComplianceDecisionPointImpl(); // decisionPoint.setDevicesAsUnreachable(this.getDeviceIdentifiersFromDevices( // new ArrayList<>(deviceToMarkUnreachable.values()))); // } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 53c322236fb..ffc3373fbae 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -46,7 +46,6 @@ public class PolicyManagerImpl implements PolicyManager { private ProfileDAO profileDAO; private FeatureDAO featureDAO; private ProfileManager profileManager; - private PolicyCacheManager policyCacheManager; private static Log log = LogFactory.getLog(PolicyManagerImpl.class); public PolicyManagerImpl() { @@ -105,6 +104,9 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); } + if(policy.isActive()){ + policyDAO.activatePolicy(policy.getId()); + } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { From d5199dbd62c56d4f666401a86e6d18554eb570b8 Mon Sep 17 00:00:00 2001 From: Dilshan Edirisuriya Date: Tue, 1 Sep 2015 16:13:17 +0530 Subject: [PATCH 2/3] Fixing issues --- .../mgt/core/impl/CertificateGenerator.java | 18 ++++++- .../mgt/core/impl/KeyStoreReader.java | 51 ++++++++++++++++++- ...CertificateManagementServiceComponent.java | 3 +- 3 files changed, 68 insertions(+), 4 deletions(-) diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java index 34dfe941fa3..a1ddb3c20e8 100755 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/CertificateGenerator.java @@ -52,9 +52,9 @@ import org.jscep.transaction.Nonce; import org.jscep.transaction.TransactionId; import org.wso2.carbon.certificate.mgt.core.dto.CAStatus; import org.wso2.carbon.certificate.mgt.core.dto.SCEPResponse; -import org.wso2.carbon.certificate.mgt.core.util.ConfigurationUtil; import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; import org.wso2.carbon.certificate.mgt.core.util.CommonUtil; +import org.wso2.carbon.certificate.mgt.core.util.ConfigurationUtil; import javax.security.auth.x500.X500Principal; import java.io.ByteArrayInputStream; @@ -292,9 +292,21 @@ public class CertificateGenerator { Date validityBeginDate = commonUtil.getValidityStartDate(); Date validityEndDate = commonUtil.getValidityEndDate(); + X500Name certSubject = request.getSubject(); + + if (certSubject == null) { + certSubject = new X500Name(ConfigurationUtil.DEFAULT_PRINCIPAL); + } else { + org.bouncycastle.asn1.x500.RDN[] rdn = certSubject.getRDNs(); + + if (rdn == null || rdn.length == 0) { + certSubject = new X500Name(ConfigurationUtil.DEFAULT_PRINCIPAL); + } + } + X509v3CertificateBuilder certificateBuilder = new X509v3CertificateBuilder( new X500Name(issueSubject), BigInteger.valueOf(System.currentTimeMillis()), - validityBeginDate, validityEndDate, request.getSubject(), request.getSubjectPublicKeyInfo()); + validityBeginDate, validityEndDate, certSubject, request.getSubjectPublicKeyInfo()); ContentSigner sigGen; X509Certificate issuedCert; @@ -461,6 +473,8 @@ public class CertificateGenerator { KeyStoreReader keyStoreReader = new KeyStoreReader(); KeyStore keyStore = keyStoreReader.loadCertificateKeyStore(); keyStore.setCertificateEntry(certificate.getSerialNumber().toString(), certificate); + + keyStoreReader.saveCertificateKeyStore(keyStore); } catch (KeyStoreException e) { String errorMsg = "KeySKeyStoreException occurred when saving the generated certificate"; log.error(errorMsg, e); diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/KeyStoreReader.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/KeyStoreReader.java index 5c23eb7deae..f714a4746b2 100755 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/KeyStoreReader.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/impl/KeyStoreReader.java @@ -24,6 +24,7 @@ import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.security.KeyStore; @@ -62,7 +63,7 @@ public class KeyStoreReader { log.error(errorMsg, e); throw new KeystoreException(errorMsg, e); } catch (CertificateException e) { - String errorMsg = "Certificate expired when loading KeyStore"; + String errorMsg = "CertificateException when loading KeyStore"; log.error(errorMsg, e); throw new KeystoreException(errorMsg, e); } catch (IOException e) { @@ -82,11 +83,59 @@ public class KeyStoreReader { return keystore; } + private synchronized void saveKeyStore(KeyStore keyStore, String configEntryKeyStorePath, + String configEntryKeyStorePassword) throws KeystoreException { + + FileOutputStream outputStream = null; + + try { + outputStream = new FileOutputStream( + ConfigurationUtil.getConfigEntry(configEntryKeyStorePath)); + keyStore.store(outputStream, ConfigurationUtil.getConfigEntry(configEntryKeyStorePassword).toCharArray()); + outputStream.close(); + + } catch (KeyStoreException e) { + String errorMsg = "KeyStore issue occurred when loading KeyStore"; + log.error(errorMsg, e); + throw new KeystoreException(errorMsg, e); + } catch (FileNotFoundException e) { + String errorMsg = "KeyStore file not found when loading KeyStore"; + log.error(errorMsg, e); + throw new KeystoreException(errorMsg, e); + } catch (NoSuchAlgorithmException e) { + String errorMsg = "Algorithm not found when loading KeyStore"; + log.error(errorMsg, e); + throw new KeystoreException(errorMsg, e); + } catch (CertificateException e) { + String errorMsg = "CertificateException when loading KeyStore"; + log.error(errorMsg, e); + throw new KeystoreException(errorMsg, e); + } catch (IOException e) { + String errorMsg = "Input output issue occurred when loading KeyStore"; + log.error(errorMsg, e); + throw new KeystoreException(errorMsg, e); + } finally { + try { + if (outputStream != null) { + outputStream.close(); + } + } catch (IOException e) { + log.error("Error closing KeyStore output stream", e); + } + } + } + + KeyStore loadCertificateKeyStore() throws KeystoreException { return loadKeyStore(ConfigurationUtil.CERTIFICATE_KEYSTORE, ConfigurationUtil.PATH_CERTIFICATE_KEYSTORE, ConfigurationUtil.CERTIFICATE_KEYSTORE_PASSWORD); } + void saveCertificateKeyStore(KeyStore keyStore) throws KeystoreException { + saveKeyStore(keyStore, ConfigurationUtil.PATH_CERTIFICATE_KEYSTORE, + ConfigurationUtil.CERTIFICATE_KEYSTORE_PASSWORD); + } + public Certificate getCACertificate() throws KeystoreException { KeyStore keystore = loadCertificateKeyStore(); diff --git a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/internal/CertificateManagementServiceComponent.java b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/internal/CertificateManagementServiceComponent.java index 5996028f7d6..ccf2f776498 100644 --- a/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/internal/CertificateManagementServiceComponent.java +++ b/components/certificate-mgt/org.wso2.carbon.certificate.mgt.core/src/main/java/org/wso2/carbon/certificate/mgt/core/internal/CertificateManagementServiceComponent.java @@ -21,6 +21,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementService; import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementServiceImpl; /** @@ -38,7 +39,7 @@ public class CertificateManagementServiceComponent { } BundleContext bundleContext = componentContext.getBundleContext(); - bundleContext.registerService(CertificateManagementServiceImpl.class.getName(), + bundleContext.registerService(CertificateManagementService.class.getName(), CertificateManagementServiceImpl.getInstance(), null); if (log.isDebugEnabled()) { From b14461a5af45f7a28fffe094def32315f67f4825 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Tue, 1 Sep 2015 17:16:51 +0530 Subject: [PATCH 3/3] Fixing the datasource issue in the device magt core --- .../mgt/core/util/DeviceManagerUtil.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index ac64d714a2b..bf2ee59a393 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -23,6 +23,7 @@ import org.w3c.dom.Document; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; @@ -35,6 +36,7 @@ import javax.sql.DataSource; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import java.io.File; +import java.sql.SQLException; import java.util.HashMap; import java.util.Hashtable; import java.util.List; @@ -97,6 +99,7 @@ public final class DeviceManagerUtil { public static boolean registerDeviceType(String typeName) throws DeviceManagementException { boolean status; try { + DeviceManagementDAOFactory.beginTransaction(); DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); DeviceType deviceType = deviceTypeDAO.getDeviceType(typeName); if (deviceType == null) { @@ -104,10 +107,18 @@ public final class DeviceManagerUtil { dt.setName(typeName); deviceTypeDAO.addDeviceType(dt); } + DeviceManagementDAOFactory.commitTransaction(); status = true; } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error occurred while registering the device type '" + typeName + "'", e); + } catch (TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + throw new DeviceManagementException("SQL occurred while registering the device type '" + + typeName + "'", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); } return status; } @@ -120,6 +131,7 @@ public final class DeviceManagerUtil { */ public static boolean unregisterDeviceType(String typeName) throws DeviceManagementException { try { + DeviceManagementDAOFactory.beginTransaction(); DeviceTypeDAO deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); DeviceType deviceType = deviceTypeDAO.getDeviceType(typeName); if (deviceType != null) { @@ -127,10 +139,18 @@ public final class DeviceManagerUtil { dt.setName(typeName); deviceTypeDAO.removeDeviceType(typeName); } + DeviceManagementDAOFactory.commitTransaction(); return true; } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error occurred while registering the device type '" + typeName + "'", e); + } catch (TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + throw new DeviceManagementException("SQL occurred while registering the device type '" + + typeName + "'", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); } }