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()) { 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(); } } 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 e5033c357c7..e1921f1aaca 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 @@ -420,11 +420,7 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setString(1, name); stmt.setInt(2, tenantId); resultSet = stmt.executeQuery(); - - if (resultSet.next()) { - //TODO: FIXME - exist = resultSet.getBoolean(1); - } + exist = resultSet.next(); } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while checking whether criterion (" + name + ") exists", e); 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 f9494684c39..e3a4d99efa9 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 be39b3c1daf..a9f1a10fd86 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 @@ -48,7 +48,6 @@ public class PolicyManagerImpl implements PolicyManager { private ProfileDAO profileDAO; private FeatureDAO featureDAO; private ProfileManager profileManager; - private PolicyCacheManager policyCacheManager; private DeviceDAO deviceDAO; private static Log log = LogFactory.getLog(PolicyManagerImpl.class); @@ -109,6 +108,9 @@ public class PolicyManagerImpl implements PolicyManager { policyDAO.addPolicyCriteriaProperties(policy.getPolicyCriterias()); } + if(policy.isActive()){ + policyDAO.activatePolicy(policy.getId()); + } PolicyManagementDAOFactory.commitTransaction(); } catch (PolicyManagerDAOException e) { @@ -715,20 +717,6 @@ public class PolicyManagerImpl implements PolicyManager { PolicyManagementDAOFactory.closeConnection(); } - try { - DeviceManagementDAOFactory.openConnection(); - for (int deviceId : deviceIds) { - //TODO FIX ME - deviceList.add(deviceDAO.getDevice(new DeviceIdentifier(Integer.toString(deviceId), ""), tenantId)); - } - } catch (SQLException e) { - throw new PolicyManagementException("Error occurred while opening a connection to the data source", e); - } catch (DeviceManagementDAOException e) { - throw new PolicyManagementException("Error occurred while retrieving device metadata", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - return deviceList; }