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 20b4833d562..d686ff51158 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 @@ -97,10 +97,7 @@ import java.security.cert.CertificateNotYetValidException; import java.security.cert.X509Certificate; import java.security.spec.InvalidKeySpecException; import java.security.spec.X509EncodedKeySpec; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Date; -import java.util.List; +import java.util.*; import java.util.concurrent.TimeUnit; public class CertificateGenerator { @@ -798,8 +795,16 @@ public class CertificateGenerator { BigInteger serialNumber = BigInteger.valueOf(System.currentTimeMillis()); - X500Name issuerName = new X500Name(certCA.getSubjectDN().getName()); - + //Reversing the order of components of the subject DN due to Nginx not verifying the client certificate + //generated by Java using this subject DN. + //Ref: https://stackoverflow.com/questions/33769978 & engineering mail SCEP implementation for Android + String[] dnParts = certCA.getSubjectDN().getName().split(","); + StringJoiner joiner = new StringJoiner(","); + for (int i = (dnParts.length - 1); i >= 0; i--) { + joiner.add(dnParts[i]); + } + String subjectDn = joiner.toString(); + X500Name issuerName = new X500Name(subjectDn); String commonName = certificationRequest.getSubject().getRDNs(BCStyle.CN)[0].getFirst() .getValue().toString(); X500Name subjectName = new X500Name("O=" + commonName + "O=AndroidDevice,CN=" + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index f36def494d7..0d24f1125c3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -86,11 +86,9 @@ import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientServic import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl; import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; -import org.wso2.carbon.device.mgt.core.util.DeviceMgtTenantMgtListener; import org.wso2.carbon.email.sender.core.service.EmailSenderService; import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.registry.core.service.RegistryService; -import org.wso2.carbon.stratos.common.listeners.TenantMgtListener; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.utils.Axis2ConfigurationContextObserver; import org.wso2.carbon.utils.ConfigurationContextService; @@ -271,9 +269,6 @@ public class DeviceManagementServiceComponent { componentContext.getBundleContext().registerService(PrivacyComplianceProvider.class.getName(), privacyComplianceProvider, null); - componentContext.getBundleContext() - .registerService(TenantMgtListener.class.getName(), new DeviceMgtTenantMgtListener(), null); - if (log.isDebugEnabled()) { log.debug("Device management core bundle has been successfully initialized"); } diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.common/pom.xml b/components/tenant-mgt/io.entgra.tenant.mgt.common/pom.xml new file mode 100644 index 00000000000..b69c2bb112d --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.common/pom.xml @@ -0,0 +1,66 @@ + + + + + tenant-mgt + org.wso2.carbon.devicemgt + 5.0.25-SNAPSHOT + ../pom.xml + + + 4.0.0 + io.entgra.tenant.mgt.common + Entgra IoT - Tenant Manager Common + Entgra IoT - Tenant Manager Common + bundle + + + + + org.apache.felix + maven-bundle-plugin + true + + + ${project.artifactId} + ${project.artifactId} + ${carbon.device.mgt.version} + Tenant Management Common Bundle + + org.apache.commons.logging, + org.wso2.carbon.stratos.common.beans + + io.entgra.tenant.mgt.common.* + + + + + + + + + org.wso2.carbon.commons + org.wso2.carbon.tenant.common + ${carbon.commons.version} + + + + \ No newline at end of file diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.java b/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.java new file mode 100644 index 00000000000..1470f07f63d --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/exception/TenantMgtException.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) All Rights Reserved. + * + * Entgra Pvt Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.tenant.mgt.common.exception; + +public class TenantMgtException extends Exception { + + private static final long serialVersionUID = 4304028531230841553L; + + public TenantMgtException(String msg, Throwable ex) { + super(msg, ex); + } + + public TenantMgtException(String msg) { + super(msg); + } +} diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/spi/TenantManagerService.java b/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/spi/TenantManagerService.java new file mode 100644 index 00000000000..472b1b7cb18 --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.common/src/main/java/io/entgra/tenant/mgt/common/spi/TenantManagerService.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) All Rights Reserved. + * + * Entgra Pvt Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.tenant.mgt.common.spi; + +import io.entgra.tenant.mgt.common.exception.TenantMgtException; +import org.wso2.carbon.stratos.common.beans.TenantInfoBean; + +public interface TenantManagerService { + void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException; + + void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException; +} \ No newline at end of file diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml b/components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml new file mode 100644 index 00000000000..a91e831ed5e --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/pom.xml @@ -0,0 +1,136 @@ + + + + + tenant-mgt + org.wso2.carbon.devicemgt + 5.0.25-SNAPSHOT + ../pom.xml + + + 4.0.0 + io.entgra.tenant.mgt.core + Entgra IoT - Tenant Manager Core + Entgra IoT - Tenant Manager Core + bundle + + + + + org.apache.felix + maven-scr-plugin + + + org.apache.felix + maven-bundle-plugin + true + + + ${project.artifactId} + ${project.artifactId} + ${carbon.device.mgt.version} + Tenant Management Core Bundle + io.entgra.tenant.mgt.core.internal + + org.osgi.framework.*;version="${imp.package.version.osgi.framework}", + org.osgi.service.*;version="${imp.package.version.osgi.service}", + org.apache.commons.logging, + io.entgra.application.mgt.common.*, + io.entgra.application.mgt.core.config, + org.wso2.carbon.device.mgt.core, + org.wso2.carbon.user.api, + org.wso2.carbon.registry.core.exceptions, + io.entgra.tenant.mgt.common.*, + org.wso2.carbon.stratos.common.beans, + org.wso2.carbon.stratos.common.exception, + org.wso2.carbon.stratos.common.listeners, + org.wso2.carbon.device.mgt.common.metadata.mgt, + org.wso2.carbon.device.mgt.common.exceptions, + org.wso2.carbon.device.mgt.common.permission.mgt, + org.wso2.carbon.device.mgt.common.roles.config, + org.wso2.carbon.device.mgt.core.metadata.mgt, + org.wso2.carbon.device.mgt.core.config, + org.wso2.carbon.user.core.service, + org.wso2.carbon.context + + + !io.entgra.tenant.mgt.core.internal, + io.entgra.tenant.mgt.core.* + + + + + + + + + + org.eclipse.osgi + org.eclipse.osgi + + + org.eclipse.osgi + org.eclipse.osgi.services + + + org.wso2.carbon.commons + org.wso2.carbon.tenant.common + ${carbon.commons.version} + + + org.wso2.carbon + org.wso2.carbon.registry.core + + + org.wso2.carbon + org.wso2.carbon.user.core + + + org.wso2.carbon + org.wso2.carbon.user.api + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.core + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.common + + + org.wso2.carbon.devicemgt + io.entgra.application.mgt.core + + + org.wso2.carbon.devicemgt + io.entgra.application.mgt.common + + + org.wso2.carbon.devicemgt + io.entgra.tenant.mgt.common + + + org.wso2.carbon + org.wso2.carbon.utils + + + + \ No newline at end of file diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/TenantManager.java b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/TenantManager.java new file mode 100644 index 00000000000..ab9b8b22900 --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/TenantManager.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) All Rights Reserved. + * + * Entgra Pvt Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.tenant.mgt.core; + +import io.entgra.tenant.mgt.common.exception.TenantMgtException; +import org.wso2.carbon.stratos.common.beans.TenantInfoBean; + +public interface TenantManager { + + /** + * Add default roles to a tenant described by the tenant info bean + * @param tenantInfoBean The info bean that provides tenant info + * @throws TenantMgtException Throws when error occurred while adding + * a role into user store or adding default white label theme to created tenant + */ + void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException; + + /** + * Add default application categories to a tenant described by the tenant info bean + * @param tenantInfoBean The info bean that provides tenant info + * @throws TenantMgtException Throws when error occurred while adding default application categories + */ + void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException; +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceMgtTenantMgtListener.java b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerImpl.java similarity index 56% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceMgtTenantMgtListener.java rename to components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerImpl.java index 79f58e82a17..4089b4bffaf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceMgtTenantMgtListener.java +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerImpl.java @@ -1,11 +1,12 @@ -/* Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. +/* + * Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) All Rights Reserved. * - * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, + * Entgra Pvt Ltd. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an @@ -14,23 +15,26 @@ * specific language governing permissions and limitations * under the License. */ - -package org.wso2.carbon.device.mgt.core.util; - +package io.entgra.tenant.mgt.core.impl; + +import io.entgra.application.mgt.common.exception.ApplicationManagementException; +import io.entgra.application.mgt.core.config.ConfigurationManager; +import io.entgra.application.mgt.common.services.ApplicationManager; +import io.entgra.tenant.mgt.core.TenantManager; +import io.entgra.tenant.mgt.common.exception.TenantMgtException; +import io.entgra.tenant.mgt.core.internal.TenantMgtDataHolder; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.common.exceptions.MetadataManagementException; -import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException; -import org.wso2.carbon.device.mgt.common.roles.config.Role; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionUtils; import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.stratos.common.beans.TenantInfoBean; -import org.wso2.carbon.stratos.common.listeners.TenantMgtListener; import org.wso2.carbon.user.api.Permission; +import org.wso2.carbon.device.mgt.common.exceptions.MetadataManagementException; +import org.wso2.carbon.device.mgt.common.permission.mgt.PermissionManagementException; +import org.wso2.carbon.device.mgt.common.roles.config.Role; import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreManager; @@ -39,97 +43,72 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -public class DeviceMgtTenantMgtListener implements TenantMgtListener { - private static final Log log = LogFactory.getLog(DeviceMgtTenantMgtListener.class); - private static final int EXEC_ORDER = 10; +public class TenantManagerImpl implements TenantManager { + private static final Log log = LogFactory.getLog(TenantManagerImpl.class); private static final String PERMISSION_ACTION = "ui.execute"; @Override - public void onTenantCreate(TenantInfoBean tenantInfoBean) { + public void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException { + initTenantFlow(tenantInfoBean); DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); if (config.getDefaultRoles().isEnabled()) { Map> roleMap = getValidRoleMap(config); try { - PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext() - .setTenantDomain(tenantInfoBean.getTenantDomain(), true); - UserStoreManager userStoreManager = DeviceManagementDataHolder.getInstance().getRealmService() + UserStoreManager userStoreManager = TenantMgtDataHolder.getInstance().getRealmService() .getTenantUserRealm(tenantInfoBean.getTenantId()).getUserStoreManager(); roleMap.forEach((key, value) -> { try { userStoreManager.addRole(key, null, value.toArray(new Permission[0])); } catch (UserStoreException e) { - log.error("Error occurred while adding default roles into user store.", e); + log.error("Error occurred while adding default roles into user store", e); } }); } catch (UserStoreException e) { - log.error("Error occurred while getting user store manager.", e); - } finally { - PrivilegedCarbonContext.endTenantFlow(); + String msg = "Error occurred while getting user store manager"; + log.error(msg, e); + throw new TenantMgtException(msg, e); } } try { - DeviceManagementDataHolder.getInstance().getWhiteLabelManagementService(). + TenantMgtDataHolder.getInstance().getWhiteLabelManagementService(). addDefaultWhiteLabelThemeIfNotExist(tenantInfoBean.getTenantId()); } catch (MetadataManagementException e) { - log.error("Error occurred while adding default white label theme to created tenant.", e); + String msg = "Error occurred while adding default white label theme to created tenant - id "+tenantInfoBean.getTenantId(); + log.error(msg, e); + throw new TenantMgtException(msg, e); + } finally { + endTenantFlow(); } } @Override - public void onTenantUpdate(TenantInfoBean tenantInfoBean) { - // doing nothing - } - - @Override - public void onTenantDelete(int i) { - // doing nothing - } - - @Override - public void onTenantRename(int i, String s, String s1) { - // doing nothing - } - - @Override - public void onTenantInitialActivation(int i) { - // doing nothing - } - - @Override - public void onTenantActivation(int i) { - // doing nothing - } - - @Override - public void onTenantDeactivation(int i) { - // doing nothing - } - - @Override - public void onSubscriptionPlanChange(int i, String s, String s1) { - // doing nothing + public void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException { + initTenantFlow(tenantInfoBean); + try { + ApplicationManager applicationManager = TenantMgtDataHolder.getInstance().getApplicationManager(); + applicationManager + .addApplicationCategories(ConfigurationManager.getInstance().getConfiguration().getAppCategories()); + } catch (ApplicationManagementException e) { + String msg = "Error occurred while getting default application categories"; + log.error(msg, e); + throw new TenantMgtException(msg, e); + } finally { + endTenantFlow(); + } } - @Override - public int getListenerOrder() { - return EXEC_ORDER; + private void initTenantFlow(TenantInfoBean tenantInfoBean) { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + privilegedCarbonContext.setTenantId(tenantInfoBean.getTenantId()); + privilegedCarbonContext.setTenantDomain(tenantInfoBean.getTenantDomain()); } - @Override - public void onPreDelete(int i) { - // doing nothing + private void endTenantFlow() { + PrivilegedCarbonContext.endTenantFlow(); } - /** - * Use the default roles defined in the cdm-config and evaluate the defined permissions. If permissions does not - * exist then exclude them and return role map which contains defined roles in the cdm-config and existing - * permission list as a roleMap - * @param config cdm-config - * @return {@link Map} key is role name and value is list of permissions which needs to be assigned to the role - * defined in the key. - */ private Map> getValidRoleMap(DeviceManagementConfig config) { Map> roleMap = new HashMap<>(); try { diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerServiceImpl.java b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerServiceImpl.java new file mode 100644 index 00000000000..e15d105fbf4 --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/impl/TenantManagerServiceImpl.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) All Rights Reserved. + * + * Entgra Pvt Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.tenant.mgt.core.impl; + +import io.entgra.tenant.mgt.common.spi.TenantManagerService; +import io.entgra.tenant.mgt.common.exception.TenantMgtException; +import io.entgra.tenant.mgt.core.internal.TenantMgtDataHolder; +import org.wso2.carbon.stratos.common.beans.TenantInfoBean; + +public class TenantManagerServiceImpl implements TenantManagerService { + + @Override + public void addDefaultRoles(TenantInfoBean tenantInfoBean) throws TenantMgtException { + TenantMgtDataHolder.getInstance().getTenantManager().addDefaultRoles(tenantInfoBean); + } + + @Override + public void addDefaultAppCategories(TenantInfoBean tenantInfoBean) throws TenantMgtException { + TenantMgtDataHolder.getInstance().getTenantManager().addDefaultAppCategories(tenantInfoBean); + } +} diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtDataHolder.java b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtDataHolder.java new file mode 100644 index 00000000000..d28862d4106 --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtDataHolder.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) All Rights Reserved. + * + * Entgra Pvt Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.tenant.mgt.core.internal; + +import io.entgra.application.mgt.common.services.ApplicationManager; +import io.entgra.tenant.mgt.core.TenantManager; +import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelManagementService; +import org.wso2.carbon.user.core.service.RealmService; + +public class TenantMgtDataHolder { + private static final TenantMgtDataHolder instance = new TenantMgtDataHolder(); + private TenantManager tenantManager; + + private ApplicationManager applicationManager; + + private WhiteLabelManagementService whiteLabelManagementService; + + private RealmService realmService; + + public RealmService getRealmService() { + return realmService; + } + + public void setRealmService(RealmService realmService) { + this.realmService = realmService; + } + + public ApplicationManager getApplicationManager() { + return applicationManager; + } + + public void setApplicationManager(ApplicationManager applicationManager) { + this.applicationManager = applicationManager; + } + + public WhiteLabelManagementService getWhiteLabelManagementService() { + return whiteLabelManagementService; + } + + public void setWhiteLabelManagementService(WhiteLabelManagementService whiteLabelManagementService) { + this.whiteLabelManagementService = whiteLabelManagementService; + } + + public TenantManager getTenantManager() { + return tenantManager; + } + + public void setTenantManager(TenantManager tenantManager) { + this.tenantManager = tenantManager; + } + + public static TenantMgtDataHolder getInstance() { + return instance; + } +} diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtServiceComponent.java b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtServiceComponent.java new file mode 100644 index 00000000000..cab03921c06 --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/internal/TenantMgtServiceComponent.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) All Rights Reserved. + * + * Entgra Pvt Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.tenant.mgt.core.internal; + +import io.entgra.application.mgt.common.services.ApplicationManager; +import io.entgra.tenant.mgt.common.spi.TenantManagerService; +import io.entgra.tenant.mgt.core.TenantManager; +import io.entgra.tenant.mgt.core.impl.TenantManagerImpl; +import io.entgra.tenant.mgt.core.impl.TenantManagerServiceImpl; +import io.entgra.tenant.mgt.core.listener.DeviceMgtTenantListener; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelManagementService; +import org.wso2.carbon.device.mgt.core.metadata.mgt.WhiteLabelManagementServiceImpl; +import org.wso2.carbon.stratos.common.listeners.TenantMgtListener; +import org.wso2.carbon.user.core.service.RealmService; + +/** + * @scr.component name="org.wso2.carbon.devicemgt.tenant.manager" immediate="true" + * @scr.reference name="org.wso2.carbon.application.mgt.service" + * interface="io.entgra.application.mgt.common.services.ApplicationManager" + * cardinality="1..1" + * policy="dynamic" + * bind="setApplicationManager" + * unbind="unsetApplicationManager" + * @scr.reference name="user.realmservice.default" + * interface="org.wso2.carbon.user.core.service.RealmService" + * cardinality="1..1" + * policy="dynamic" + * bind="setRealmService" + * unbind="unsetRealmService" + */ + +@SuppressWarnings("unused") +public class TenantMgtServiceComponent { + + private static final Log log = LogFactory.getLog(TenantManagerService.class); + + @SuppressWarnings("unused") + protected void activate(ComponentContext componentContext) { + try { + TenantManagerService tenantManagerService = new TenantManagerServiceImpl(); + componentContext.getBundleContext(). + registerService(TenantManagerServiceImpl.class.getName(), tenantManagerService, null); + TenantManager tenantManager = new TenantManagerImpl(); + TenantMgtDataHolder.getInstance().setTenantManager(tenantManager); + WhiteLabelManagementService whiteLabelManagementService = new WhiteLabelManagementServiceImpl(); + componentContext.getBundleContext().registerService(WhiteLabelManagementServiceImpl.class.getName(), + whiteLabelManagementService, null); + TenantMgtDataHolder.getInstance().setWhiteLabelManagementService(whiteLabelManagementService); + DeviceMgtTenantListener deviceMgtTenantListener = new DeviceMgtTenantListener(); + if(log.isDebugEnabled()) { + log.info("Tenant management listener is registering"); + } + componentContext.getBundleContext(). + registerService(TenantMgtListener.class.getName(), deviceMgtTenantListener, null); + if(log.isDebugEnabled()) { + log.info("Tenant management service activated"); + } + } catch (Throwable t) { + String msg = "Error occurred while activating tenant management service"; + log.error(msg, t); + } + } + + @SuppressWarnings("unused") + protected void deactivate(ComponentContext componentContext) { + // nothing to do + } + + protected void setApplicationManager(ApplicationManager applicationManager) { + if(log.isDebugEnabled()) { + log.info("Application manager service is binding"); + } + TenantMgtDataHolder.getInstance().setApplicationManager(applicationManager); + } + + protected void unsetApplicationManager(ApplicationManager applicationManager) { + if(log.isDebugEnabled()) { + log.info("Application manager service is unbinding"); + } + TenantMgtDataHolder.getInstance().setApplicationManager(null); + } + + protected void setRealmService(RealmService realmService) { + if(log.isDebugEnabled()) { + log.info("Realm Service service is binding"); + } + TenantMgtDataHolder.getInstance().setRealmService(realmService); + } + + protected void unsetRealmService(RealmService realmService) { + if(log.isDebugEnabled()) { + log.info("Realm Service service is unbinding"); + } + TenantMgtDataHolder.getInstance().setRealmService(null); + } +} diff --git a/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/listener/DeviceMgtTenantListener.java b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/listener/DeviceMgtTenantListener.java new file mode 100644 index 00000000000..a272585bf72 --- /dev/null +++ b/components/tenant-mgt/io.entgra.tenant.mgt.core/src/main/java/io/entgra/tenant/mgt/core/listener/DeviceMgtTenantListener.java @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2023, Entgra Pvt Ltd. (http://www.wso2.org) All Rights Reserved. + * + * Entgra Pvt Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package io.entgra.tenant.mgt.core.listener; + +import io.entgra.tenant.mgt.core.TenantManager; +import io.entgra.tenant.mgt.common.exception.TenantMgtException; +import io.entgra.tenant.mgt.core.internal.TenantMgtDataHolder; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.stratos.common.beans.TenantInfoBean; +import org.wso2.carbon.stratos.common.exception.StratosException; +import org.wso2.carbon.stratos.common.listeners.TenantMgtListener; + +public class DeviceMgtTenantListener implements TenantMgtListener { + + private static final Log log = LogFactory.getLog(DeviceMgtTenantListener.class); + public static final int LISTENER_EXECUTION_ORDER = 10; + + @Override + public void onTenantCreate(TenantInfoBean tenantInfoBean) { + // Any work to be performed after a tenant creation + TenantManager tenantManager = TenantMgtDataHolder.getInstance().getTenantManager(); + try { + tenantManager.addDefaultRoles(tenantInfoBean); + tenantManager.addDefaultAppCategories(tenantInfoBean); + } catch (TenantMgtException e) { + String msg = "Error occurred while executing tenant creation flow"; + log.error(msg, e); + } + } + + @Override + public void onTenantUpdate(TenantInfoBean tenantInfoBean) throws StratosException { + // Any work to be performed after a tenant information update happens + } + + @Override + public void onTenantDelete(int i) { + // Any work to be performed after a tenant deletion + } + + @Override + public void onTenantRename(int i, String s, String s1) throws StratosException { + // Any work to be performed after a tenant rename happens + } + + @Override + public void onTenantInitialActivation(int i) throws StratosException { + // Any work to be performed after a tenant's initial activation happens + } + + @Override + public void onTenantActivation(int i) throws StratosException { + // Any work to be performed after a tenant activation + } + + @Override + public void onTenantDeactivation(int i) throws StratosException { + // Any work to be performed after a tenant deactivation + } + + @Override + public void onSubscriptionPlanChange(int i, String s, String s1) throws StratosException { + // Any work to be performed after subscription plan change + } + + @Override + public int getListenerOrder() { + return LISTENER_EXECUTION_ORDER; + } + + @Override + public void onPreDelete(int i) throws StratosException { + // Any work to be performed before a tenant is deleted + } +} diff --git a/components/tenant-mgt/pom.xml b/components/tenant-mgt/pom.xml new file mode 100644 index 00000000000..7d13f340c8b --- /dev/null +++ b/components/tenant-mgt/pom.xml @@ -0,0 +1,40 @@ + + + + + carbon-devicemgt + org.wso2.carbon.devicemgt + 5.0.25-SNAPSHOT + ../../pom.xml + + + 4.0.0 + tenant-mgt + pom + Entgra IoT - Tenant Management Component + Entgra IoT - Tenant Management Component + + + io.entgra.tenant.mgt.core + io.entgra.tenant.mgt.common + + + \ No newline at end of file diff --git a/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/pom.xml b/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/pom.xml new file mode 100644 index 00000000000..87e3214a433 --- /dev/null +++ b/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/pom.xml @@ -0,0 +1,111 @@ + + + + + tenant-mgt-feature + org.wso2.carbon.devicemgt + 5.0.25-SNAPSHOT + ../pom.xml + + + 4.0.0 + io.entgra.tenant.mgt.server.feature + Entgra IoT - Tenant Management Server Feature + Entgra IoT - Tenant Management Server Feature + pom + + + + org.wso2.carbon.devicemgt + io.entgra.tenant.mgt.core + + + org.wso2.carbon.devicemgt + io.entgra.tenant.mgt.common + + + + + + + maven-resources-plugin + 2.6 + + + copy-resources + generate-resources + + copy-resources + + + src/main/resources + + + resources + + build.properties + p2.inf + + + + + + + + + org.wso2.maven + carbon-p2-plugin + ${carbon.p2.plugin.version} + + + p2-feature-generation + package + + p2-feature-gen + + + io.entgra.tenant.mgt.server + ../../etc/feature.properties + + + org.wso2.carbon.p2.category.type:server + + org.eclipse.equinox.p2.type.group:true + + + + + + + org.wso2.carbon.devicemgt:io.entgra.tenant.mgt.core:${carbon.device.mgt.version} + + + org.wso2.carbon.devicemgt:io.entgra.tenant.mgt.common:${carbon.device.mgt.version} + + + + + + + + + + \ No newline at end of file diff --git a/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/src/main/resources/build.properties b/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/src/main/resources/build.properties new file mode 100644 index 00000000000..920f5778887 --- /dev/null +++ b/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/src/main/resources/build.properties @@ -0,0 +1 @@ +custom=true \ No newline at end of file diff --git a/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/src/main/resources/p2.inf b/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/src/main/resources/p2.inf new file mode 100644 index 00000000000..7ab37b9d7d7 --- /dev/null +++ b/features/tenant-mgt/io.entgra.tenant.mgt.server.feature/src/main/resources/p2.inf @@ -0,0 +1 @@ +instructions.configure = \ \ No newline at end of file diff --git a/features/tenant-mgt/pom.xml b/features/tenant-mgt/pom.xml new file mode 100644 index 00000000000..44befb42404 --- /dev/null +++ b/features/tenant-mgt/pom.xml @@ -0,0 +1,36 @@ + + + + + + carbon-devicemgt + org.wso2.carbon.devicemgt + 5.0.25-SNAPSHOT + ../../pom.xml + + + 4.0.0 + tenant-mgt-feature + pom + Entgra IoT - Tenant Management Feature + + + io.entgra.tenant.mgt.server.feature + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2851e5865f0..7eee718846b 100644 --- a/pom.xml +++ b/pom.xml @@ -48,6 +48,7 @@ components/webapp-authenticator-framework components/logger components/task-mgt + components/tenant-mgt features/device-mgt features/apimgt-extensions features/application-mgt @@ -62,7 +63,7 @@ features/webapp-authenticator-framework features/logger features/task-mgt - + features/tenant-mgt @@ -370,6 +371,18 @@ + + + org.wso2.carbon.devicemgt + io.entgra.tenant.mgt.common + ${carbon.device.mgt.version} + + + org.wso2.carbon.devicemgt + io.entgra.tenant.mgt.core + ${carbon.device.mgt.version} + + org.wso2.carbon.devicemgt