From 4238f6ffa1445d2adf6eb994c8950eea1c928423 Mon Sep 17 00:00:00 2001 From: shamalka Date: Thu, 7 Dec 2023 23:00:40 +0530 Subject: [PATCH 1/2] Add sub tenant reserved user creation --- .../pom.xml | 11 ++- .../rest/api/APIApplicationServicesImpl.java | 85 ++++++++++++++++-- .../rest/api/constants/Constants.java | 2 + .../internal/PublisherRESTAPIDataHolder.java | 31 ++++++- .../publisher/APIPublisherServiceImpl.java | 25 +++--- .../pom.xml | 4 + .../core/internal/TenantCreateObserver.java | 86 ++++++++++++++++++- 7 files changed, 222 insertions(+), 22 deletions(-) diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/pom.xml b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/pom.xml index 60f7054d04..047fa62547 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/pom.xml +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/pom.xml @@ -84,6 +84,10 @@ okhttp compile + + org.wso2.carbon + org.wso2.carbon.user.api + @@ -121,7 +125,10 @@ org.wso2.carbon.apimgt.impl;version="${carbon.api.mgt.version.range}", org.wso2.carbon.apimgt.impl.utils;version="${carbon.api.mgt.version.range}", org.wso2.carbon.apimgt.impl.internal;version="${carbon.api.mgt.version.range}", - org.json + org.json, + org.wso2.carbon.user.api, + org.wso2.carbon.context;version="4.6", + org.wso2.carbon.utils.* @@ -155,4 +162,4 @@ - \ No newline at end of file + diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/APIApplicationServicesImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/APIApplicationServicesImpl.java index 05ecf1fd9e..70317dc32c 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/APIApplicationServicesImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/APIApplicationServicesImpl.java @@ -19,6 +19,7 @@ package io.entgra.device.mgt.core.apimgt.extension.rest.api; import com.google.gson.Gson; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.internal.PublisherRESTAPIDataHolder; import org.json.JSONObject; import io.entgra.device.mgt.core.apimgt.extension.rest.api.util.HttpsTrustManagerUtils; import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey; @@ -35,7 +36,17 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.apimgt.impl.APIManagerConfiguration; import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.user.api.UserRealm; +import org.wso2.carbon.user.api.UserStoreException; +import org.wso2.carbon.user.api.UserStoreManager; +import org.wso2.carbon.utils.multitenancy.MultitenantUtils; + import java.io.IOException; +import java.security.SecureRandom; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; public class APIApplicationServicesImpl implements APIApplicationServices { @@ -51,9 +62,27 @@ public class APIApplicationServicesImpl implements APIApplicationServices { public APIApplicationKey createAndRetrieveApplicationCredentials() throws APIServicesException { + String serverUser = null; + String serverPassword = null; + try { + UserRealm userRealm = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm(); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + UserStoreManager userStoreManager = userRealm.getUserStoreManager(); + + createUserIfNotExists(Constants.RESERVED_USER_NAME, Constants.RESERVED_USER_PASSWORD, userStoreManager); + + if(tenantDomain.equals("carbon.super")) { + serverUser = config.getFirstProperty(Constants.SERVER_USER); + serverPassword = config.getFirstProperty(Constants.SERVER_PASSWORD); + } else { + serverUser = Constants.RESERVED_USER_NAME + "@" + tenantDomain; + serverPassword = Constants.RESERVED_USER_PASSWORD; + } + } catch (UserStoreException e) { + throw new RuntimeException(e); + } + String applicationEndpoint = config.getFirstProperty(Constants.DCR_END_POINT); - String serverUser = config.getFirstProperty(Constants.SERVER_USER); - String serverPassword = config.getFirstProperty(Constants.SERVER_PASSWORD); JSONObject jsonObject = new JSONObject(); jsonObject.put("callbackUrl", Constants.EMPTY_STRING); @@ -69,8 +98,9 @@ public class APIApplicationServicesImpl implements APIApplicationServices { .post(requestBody) .build(); try { - Response response = client.newCall(request).execute(); - return gson.fromJson(response.body().string(), APIApplicationKey.class); + try (Response response = client.newCall(request).execute()) { + return gson.fromJson(response.body().string(), APIApplicationKey.class); + } } catch (IOException e) { msg = "Error occurred while processing the response"; log.error(msg, e); @@ -82,8 +112,16 @@ public class APIApplicationServicesImpl implements APIApplicationServices { public AccessTokenInfo generateAccessTokenFromRegisteredApplication(String consumerKey, String consumerSecret) throws APIServicesException { - String userName = config.getFirstProperty(Constants.SERVER_USER); - String userPassword = config.getFirstProperty(Constants.SERVER_PASSWORD); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + String userName = null; + String userPassword = null; + if(tenantDomain.equals("carbon.super")) { + userName = config.getFirstProperty(Constants.SERVER_USER); + userPassword = config.getFirstProperty(Constants.SERVER_PASSWORD); + } else { + userName = "shamalka@shamalka.com"; + userPassword = "admin"; + } JSONObject params = new JSONObject(); params.put(Constants.GRANT_TYPE_PARAM_NAME, Constants.PASSWORD_GRANT_TYPE); @@ -125,4 +163,39 @@ public class APIApplicationServicesImpl implements APIApplicationServices { throw new APIServicesException(e); } } + + private void createUserIfNotExists(String username, String password, UserStoreManager userStoreManager) { + + try { + if (!userStoreManager.isExistingUser(MultitenantUtils.getTenantAwareUsername(username))) { + String[] roles = {"admin"}; + userStoreManager.addUser(MultitenantUtils.getTenantAwareUsername(username), password, roles, null, ""); + +// userStoreManager.updateCredential(MultitenantUtils.getTenantAwareUsername(username), "reservedpwd", password); + } + } catch (UserStoreException e) { + String msg = "Error when trying to fetch tenant details"; + log.error(msg); + } + } + + private String generateInitialUserPassword() { + int passwordLength = 6; + //defining the pool of characters to be used for initial password generation + String lowerCaseCharset = "abcdefghijklmnopqrstuvwxyz"; + String upperCaseCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + String numericCharset = "0123456789"; + SecureRandom randomGenerator = new SecureRandom(); + String totalCharset = lowerCaseCharset + upperCaseCharset + numericCharset; + int totalCharsetLength = totalCharset.length(); + StringBuilder initialUserPassword = new StringBuilder(); + for (int i = 0; i < passwordLength; i++) { + initialUserPassword.append( + totalCharset.charAt(randomGenerator.nextInt(totalCharsetLength))); + } + if (log.isDebugEnabled()) { + log.debug("Initial user password is created for new user: " + initialUserPassword); + } + return initialUserPassword.toString(); + } } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/constants/Constants.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/constants/Constants.java index 5a577e3eb1..72c28f709f 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/constants/Constants.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/constants/Constants.java @@ -65,6 +65,8 @@ public final class Constants { public static final String SCOPE_API_ENDPOINT = "/api/am/publisher/v2/scopes/"; public static final String API_ENDPOINT = "/api/am/publisher/v2/apis/"; public static final String GET_ALL_APIS = "/api/am/publisher/v2/apis?limit=1000"; + public static final String RESERVED_USER_NAME = "test_reserved_user"; + public static final String RESERVED_USER_PASSWORD = "reserved_user"; } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java index 9a53757b0b..35c7f4c961 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java @@ -20,6 +20,8 @@ package io.entgra.device.mgt.core.apimgt.extension.rest.api.internal; import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices; import org.wso2.carbon.apimgt.impl.APIManagerConfigurationService; +import org.wso2.carbon.user.core.service.RealmService; +import org.wso2.carbon.user.core.tenant.TenantManager; public class PublisherRESTAPIDataHolder { @@ -28,10 +30,14 @@ public class PublisherRESTAPIDataHolder { private static PublisherRESTAPIDataHolder thisInstance = new PublisherRESTAPIDataHolder(); + private RealmService realmService; + + private TenantManager tenantManager; + private PublisherRESTAPIDataHolder() { } - static PublisherRESTAPIDataHolder getInstance() { + public static PublisherRESTAPIDataHolder getInstance() { return thisInstance; } @@ -54,4 +60,27 @@ public class PublisherRESTAPIDataHolder { return apiManagerConfigurationService; } + public RealmService getRealmService() { + if (realmService == null) { + throw new IllegalStateException("Realm service is not initialized properly"); + } + return realmService; + } + + public void setRealmService(RealmService realmService) { + this.realmService = realmService; + this.setTenantManager(realmService); + } + + public TenantManager getTenantManager() { + return tenantManager; + } + + private void setTenantManager(RealmService realmService) { + if (realmService == null) { + throw new IllegalStateException("Realm service is not initialized properly"); + } + this.tenantManager = realmService.getTenantManager(); + } + } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java index db7e58a32b..147deeb753 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/APIPublisherServiceImpl.java @@ -110,17 +110,9 @@ public class APIPublisherServiceImpl implements APIPublisherService { .getOSGiService(RealmService.class, null); APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); - APIApplicationKey apiApplicationKey; - AccessTokenInfo accessTokenInfo; - try { - apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials(); - accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication( - apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); - } catch (APIServicesException e) { - String errorMsg = "Error occurred while generating the API application"; - log.error(errorMsg, e); - throw new APIManagerPublisherException(e); - } + APIApplicationKey apiApplicationKey = null; + AccessTokenInfo accessTokenInfo = null; + try { boolean tenantFound = false; @@ -152,9 +144,20 @@ public class APIPublisherServiceImpl implements APIPublisherService { } if (tenantFound) { + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(apiConfig.getOwner()); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials(); + accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication( + apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); + } catch (APIServicesException e) { + String errorMsg = "Error occurred while generating the API application"; + log.error(errorMsg, e); + throw new APIManagerPublisherException(e); + } + try { apiConfig.setOwner(APIUtil.getTenantAdminUserName(tenantDomain)); apiConfig.setTenantDomain(tenantDomain); diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml index 6d298d39f7..274977294a 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/pom.xml @@ -374,6 +374,10 @@ 2.3.1.wso2v1 compile + + io.entgra.device.mgt.core + io.entgra.device.mgt.core.apimgt.extension.rest.api + diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/TenantCreateObserver.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/TenantCreateObserver.java index 9360428b56..47329ccf1a 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/TenantCreateObserver.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/TenantCreateObserver.java @@ -17,12 +17,24 @@ */ package io.entgra.device.mgt.core.device.mgt.core.internal; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServicesImpl; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServices; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.PublisherRESTAPIServicesImpl; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIInfo.Scope; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.AccessTokenInfo; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException; import org.apache.axis2.context.ConfigurationContext; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import io.entgra.device.mgt.core.device.mgt.core.DeviceManagementConstants; import io.entgra.device.mgt.core.device.mgt.core.DeviceManagementConstants.User; +import org.wso2.carbon.stratos.common.exception.TenantManagementClientException; +import org.wso2.carbon.tenant.mgt.exception.TenantManagementException; import org.wso2.carbon.user.api.AuthorizationManager; import org.wso2.carbon.user.api.Permission; import org.wso2.carbon.user.api.UserRealm; @@ -30,6 +42,10 @@ import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.user.api.UserStoreManager; import org.wso2.carbon.utils.AbstractAxis2ConfigurationContextObserver; import org.wso2.carbon.utils.multitenancy.MultitenantConstants; +import org.wso2.carbon.utils.multitenancy.MultitenantUtils; + +import java.security.SecureRandom; +import java.util.Stack; /** * Load configuration files to tenant's registry. @@ -37,6 +53,7 @@ import org.wso2.carbon.utils.multitenancy.MultitenantConstants; public class TenantCreateObserver extends AbstractAxis2ConfigurationContextObserver { private static final Log log = LogFactory.getLog(TenantCreateObserver.class); + /** * Create configuration context. * @@ -82,6 +99,29 @@ public class TenantCreateObserver extends AbstractAxis2ConfigurationContextObser userStoreManager.updateRoleListOfUser(tenantAdminName, null, new String[] {DeviceManagementConstants.User.DEFAULT_DEVICE_ADMIN, DeviceManagementConstants.User.DEFAULT_DEVICE_USER}); + +// String password = this.generateInitialUserPassword(); + +// createUserIfNotExists("test_reserved_user", password, userStoreManager); + + + PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServicesImpl(); + APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); + APIApplicationKey apiApplicationKey = null; + AccessTokenInfo accessTokenInfo = null; + try { + apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials(); + accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication( + apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); + } catch (APIServicesException e) { + String errorMsg = "Error occurred while generating the API application"; + log.error(errorMsg, e); + throw new TenantManagementException(errorMsg, e); + } + Scope[] scopes = publisherRESTAPIServices.getScopes(apiApplicationKey, accessTokenInfo); + + + if (log.isDebugEnabled()) { log.debug("Device management roles: " + User.DEFAULT_DEVICE_USER + ", " + User.DEFAULT_DEVICE_ADMIN + " created for the tenant:" + tenantDomain + "." @@ -90,8 +130,50 @@ public class TenantCreateObserver extends AbstractAxis2ConfigurationContextObser " is assigned to the role:" + User.DEFAULT_DEVICE_ADMIN + "." ); } - } catch (UserStoreException e) { + } catch (UserStoreException | TenantManagementException e) { log.error("Error occurred while creating roles for the tenant: " + tenantDomain + "."); + } catch (BadRequestException e) { + throw new RuntimeException(e); + } catch (UnexpectedResponseException e) { + throw new RuntimeException(e); + } catch (APIServicesException e) { + throw new RuntimeException(e); + } + } + + private void createUserIfNotExists(String username, String password, UserStoreManager userStoreManager) { + + try { + if (!userStoreManager.isExistingUser(MultitenantUtils.getTenantAwareUsername(username))) { + String[] roles = {"admin"}; + userStoreManager.addUser(MultitenantUtils.getTenantAwareUsername(username), password, roles, null, ""); + + userStoreManager.updateCredential(MultitenantUtils.getTenantAwareUsername(username), "reservedpwd", password); + } + } catch (UserStoreException e) { + String msg = "Error when trying to fetch tenant details"; + log.error(msg); + } + } + + private String generateInitialUserPassword() { + int passwordLength = 6; + //defining the pool of characters to be used for initial password generation + String lowerCaseCharset = "abcdefghijklmnopqrstuvwxyz"; + String upperCaseCharset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + String numericCharset = "0123456789"; + SecureRandom randomGenerator = new SecureRandom(); + String totalCharset = lowerCaseCharset + upperCaseCharset + numericCharset; + int totalCharsetLength = totalCharset.length(); + StringBuilder initialUserPassword = new StringBuilder(); + for (int i = 0; i < passwordLength; i++) { + initialUserPassword.append( + totalCharset.charAt(randomGenerator.nextInt(totalCharsetLength))); + } + if (log.isDebugEnabled()) { + log.debug("Initial user password is created for new user: " + initialUserPassword); } + return initialUserPassword.toString(); } -} \ No newline at end of file + +} -- 2.36.3 From 1accc5202c06c55d8f188e0e521a9a0047a3b69d Mon Sep 17 00:00:00 2001 From: shamalka Date: Tue, 19 Dec 2023 17:05:39 +0530 Subject: [PATCH 2/2] Add scope retrieval to tenantcreateobserver --- .../rest/api/APIApplicationServices.java | 1 + .../rest/api/APIApplicationServicesImpl.java | 92 +++++++++++++++++++ .../internal/PublisherRESTAPIDataHolder.java | 11 +++ .../core/internal/TenantCreateObserver.java | 66 ++++++++----- 4 files changed, 149 insertions(+), 21 deletions(-) diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/APIApplicationServices.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/APIApplicationServices.java index d628c4ce25..b47edc6c21 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/APIApplicationServices.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/APIApplicationServices.java @@ -25,6 +25,7 @@ import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIService public interface APIApplicationServices { APIApplicationKey createAndRetrieveApplicationCredentials() throws APIServicesException; + void createAndRetrieveApplicationCredentialsAndGenerateToken() throws APIServicesException; AccessTokenInfo generateAccessTokenFromRegisteredApplication(String clientId, String clientSecret) throws APIServicesException; diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/APIApplicationServicesImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/APIApplicationServicesImpl.java index 70317dc32c..96559823d3 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/APIApplicationServicesImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/APIApplicationServicesImpl.java @@ -19,6 +19,9 @@ package io.entgra.device.mgt.core.apimgt.extension.rest.api; import com.google.gson.Gson; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIInfo.Scope; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.BadRequestException; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException; import io.entgra.device.mgt.core.apimgt.extension.rest.api.internal.PublisherRESTAPIDataHolder; import org.json.JSONObject; import io.entgra.device.mgt.core.apimgt.extension.rest.api.util.HttpsTrustManagerUtils; @@ -62,9 +65,12 @@ public class APIApplicationServicesImpl implements APIApplicationServices { public APIApplicationKey createAndRetrieveApplicationCredentials() throws APIServicesException { + log.error("=====createAndRetrieveApplicationCredentials=====1"); + String serverUser = null; String serverPassword = null; try { + log.error("=====createAndRetrieveApplicationCredentials=====2"); UserRealm userRealm = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm(); String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); UserStoreManager userStoreManager = userRealm.getUserStoreManager(); @@ -72,9 +78,11 @@ public class APIApplicationServicesImpl implements APIApplicationServices { createUserIfNotExists(Constants.RESERVED_USER_NAME, Constants.RESERVED_USER_PASSWORD, userStoreManager); if(tenantDomain.equals("carbon.super")) { + log.error("=====createAndRetrieveApplicationCredentials=====3"); serverUser = config.getFirstProperty(Constants.SERVER_USER); serverPassword = config.getFirstProperty(Constants.SERVER_PASSWORD); } else { + log.error("=====createAndRetrieveApplicationCredentials=====4"); serverUser = Constants.RESERVED_USER_NAME + "@" + tenantDomain; serverPassword = Constants.RESERVED_USER_PASSWORD; } @@ -84,6 +92,8 @@ public class APIApplicationServicesImpl implements APIApplicationServices { String applicationEndpoint = config.getFirstProperty(Constants.DCR_END_POINT); + log.error("=====createAndRetrieveApplicationCredentials=====5"); + JSONObject jsonObject = new JSONObject(); jsonObject.put("callbackUrl", Constants.EMPTY_STRING); jsonObject.put("clientName", Constants.CLIENT_NAME); @@ -91,14 +101,21 @@ public class APIApplicationServicesImpl implements APIApplicationServices { jsonObject.put("owner", serverUser); jsonObject.put("saasApp", true); + log.error("=====createAndRetrieveApplicationCredentials=====6"); + RequestBody requestBody = RequestBody.Companion.create(jsonObject.toString(), JSON); Request request = new Request.Builder() .url(applicationEndpoint) .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Credentials.basic(serverUser, serverPassword)) .post(requestBody) .build(); + + log.error("=====createAndRetrieveApplicationCredentials=====7"); + try { + log.error("=====createAndRetrieveApplicationCredentials=====8"); try (Response response = client.newCall(request).execute()) { + log.error("=====createAndRetrieveApplicationCredentials=====9"); return gson.fromJson(response.body().string(), APIApplicationKey.class); } } catch (IOException e) { @@ -108,6 +125,81 @@ public class APIApplicationServicesImpl implements APIApplicationServices { } } + @Override + public void createAndRetrieveApplicationCredentialsAndGenerateToken() + throws APIServicesException { + + log.error("=====createAndRetrieveApplicationCredentials=====1"); + + String serverUser = null; + String serverPassword = null; + try { + log.error("=====createAndRetrieveApplicationCredentials=====2"); + UserRealm userRealm = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm(); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); + UserStoreManager userStoreManager = userRealm.getUserStoreManager(); + + createUserIfNotExists(Constants.RESERVED_USER_NAME, Constants.RESERVED_USER_PASSWORD, userStoreManager); + + if(tenantDomain.equals("carbon.super")) { + log.error("=====createAndRetrieveApplicationCredentials=====3"); + serverUser = config.getFirstProperty(Constants.SERVER_USER); + serverPassword = config.getFirstProperty(Constants.SERVER_PASSWORD); + } else { + log.error("=====createAndRetrieveApplicationCredentials=====4"); + serverUser = Constants.RESERVED_USER_NAME + "@" + tenantDomain; + serverPassword = Constants.RESERVED_USER_PASSWORD; + } + } catch (UserStoreException e) { + throw new RuntimeException(e); + } + + String applicationEndpoint = config.getFirstProperty(Constants.DCR_END_POINT); + + log.error("=====createAndRetrieveApplicationCredentials=====5"); + + JSONObject jsonObject = new JSONObject(); + jsonObject.put("callbackUrl", Constants.EMPTY_STRING); + jsonObject.put("clientName", Constants.CLIENT_NAME); + jsonObject.put("grantType", Constants.GRANT_TYPE); + jsonObject.put("owner", serverUser); + jsonObject.put("saasApp", true); + + log.error("=====createAndRetrieveApplicationCredentials=====6"); + + RequestBody requestBody = RequestBody.Companion.create(jsonObject.toString(), JSON); + Request request = new Request.Builder() + .url(applicationEndpoint) + .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Credentials.basic(serverUser, serverPassword)) + .post(requestBody) + .build(); + + log.error("=====createAndRetrieveApplicationCredentials=====7"); + + try { + log.error("=====createAndRetrieveApplicationCredentials=====8"); + try (Response response = client.newCall(request).execute()) { + log.error("=====createAndRetrieveApplicationCredentials=====9"); + APIApplicationKey apiApplicationKey = gson.fromJson(response.body().string(), APIApplicationKey.class); + AccessTokenInfo accessTokenInfo = generateAccessTokenFromRegisteredApplication( + apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); + + PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServicesImpl(); + + Scope[] scopes = publisherRESTAPIServices.getScopes(apiApplicationKey, accessTokenInfo); + + } catch (BadRequestException e) { + throw new RuntimeException(e); + } catch (UnexpectedResponseException e) { + throw new RuntimeException(e); + } + } catch (IOException e) { + msg = "Error occurred while processing the response"; + log.error(msg, e); + throw new APIServicesException(e); + } + } + @Override public AccessTokenInfo generateAccessTokenFromRegisteredApplication(String consumerKey, String consumerSecret) throws APIServicesException { diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java index 35c7f4c961..a1cce1ae62 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/internal/PublisherRESTAPIDataHolder.java @@ -83,4 +83,15 @@ public class PublisherRESTAPIDataHolder { this.tenantManager = realmService.getTenantManager(); } + public APIPublisherService getApiPublisherService() { + if (apiPublisherService == null) { + throw new IllegalStateException("APIPublisher service is not initialized properly"); + } + return apiPublisherService; + } + + public void setApiPublisherService(APIPublisherService apiPublisherService) { + this.apiPublisherService = apiPublisherService; + } + } diff --git a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/TenantCreateObserver.java b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/TenantCreateObserver.java index 47329ccf1a..395d9ceda6 100644 --- a/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/TenantCreateObserver.java +++ b/components/device-mgt/io.entgra.device.mgt.core.device.mgt.core/src/main/java/io/entgra/device/mgt/core/device/mgt/core/internal/TenantCreateObserver.java @@ -46,12 +46,16 @@ import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import java.security.SecureRandom; import java.util.Stack; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; /** * Load configuration files to tenant's registry. */ public class TenantCreateObserver extends AbstractAxis2ConfigurationContextObserver { private static final Log log = LogFactory.getLog(TenantCreateObserver.class); + private final ExecutorService executor = Executors.newSingleThreadExecutor(); + /** @@ -104,22 +108,26 @@ public class TenantCreateObserver extends AbstractAxis2ConfigurationContextObser // createUserIfNotExists("test_reserved_user", password, userStoreManager); + Thread thread = new Thread(new Runnable() { + @Override + public void run() { + try { + createApplication(tenantDomain); + } catch (TenantManagementException e) { + throw new RuntimeException(e); + } + } + }); + thread.start(); - PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServicesImpl(); - APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); - APIApplicationKey apiApplicationKey = null; - AccessTokenInfo accessTokenInfo = null; - try { - apiApplicationKey = apiApplicationServices.createAndRetrieveApplicationCredentials(); - accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication( - apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); - } catch (APIServicesException e) { - String errorMsg = "Error occurred while generating the API application"; - log.error(errorMsg, e); - throw new TenantManagementException(errorMsg, e); - } - Scope[] scopes = publisherRESTAPIServices.getScopes(apiApplicationKey, accessTokenInfo); +// executor.submit(() -> { +// try { +// createApplication(); +// } catch (TenantManagementException e) { +// throw new RuntimeException(e); +// } +// }); if (log.isDebugEnabled()) { @@ -130,17 +138,33 @@ public class TenantCreateObserver extends AbstractAxis2ConfigurationContextObser " is assigned to the role:" + User.DEFAULT_DEVICE_ADMIN + "." ); } - } catch (UserStoreException | TenantManagementException e) { + } catch (UserStoreException e) { log.error("Error occurred while creating roles for the tenant: " + tenantDomain + "."); - } catch (BadRequestException e) { - throw new RuntimeException(e); - } catch (UnexpectedResponseException e) { - throw new RuntimeException(e); - } catch (APIServicesException e) { - throw new RuntimeException(e); } } + + private void createApplication(String tenantDomain) throws TenantManagementException { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true); + + PublisherRESTAPIServices publisherRESTAPIServices = new PublisherRESTAPIServicesImpl(); + APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl(); + APIApplicationKey apiApplicationKey = null; + AccessTokenInfo accessTokenInfo = null; + try { + apiApplicationServices.createAndRetrieveApplicationCredentialsAndGenerateToken(); +// log.error("apiApplicationKey: " + apiApplicationKey.getClientId()); +// log.error("apiApplicationKey: " + apiApplicationKey.getClientSecret()); +// accessTokenInfo = apiApplicationServices.generateAccessTokenFromRegisteredApplication( +// apiApplicationKey.getClientId(), apiApplicationKey.getClientSecret()); + } catch (APIServicesException e) { + String errorMsg = "Error occurred while generating the API application"; + log.error(errorMsg, e); + throw new TenantManagementException(errorMsg, e); + } +// Scope[] scopes = publisherRESTAPIServices.getScopes(apiApplicationKey, accessTokenInfo); + } private void createUserIfNotExists(String username, String password, UserStoreManager userStoreManager) { try { -- 2.36.3