From cc2eccae5d9a33b1d17e52a4f963adf94ba57e6a Mon Sep 17 00:00:00 2001 From: charitha Date: Thu, 26 Sep 2019 22:38:45 +0530 Subject: [PATCH] Fix configuration retrieval issue with non default admin credentials --- .../impl/DeviceManagementConfigServiceImpl.java | 17 ++++++++++------- .../mgt/core/DeviceManagementConstants.java | 2 -- .../device/mgt/core/util/DeviceManagerUtil.java | 6 +++--- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/service/impl/DeviceManagementConfigServiceImpl.java b/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/service/impl/DeviceManagementConfigServiceImpl.java index 93aba13627a..6fac3b8d8d6 100644 --- a/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/service/impl/DeviceManagementConfigServiceImpl.java +++ b/components/device-mgt/io.entgra.carbon.device.mgt.config.api/src/main/java/io/entgra/carbon/device/mgt/config/jaxrs/service/impl/DeviceManagementConfigServiceImpl.java @@ -35,6 +35,9 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.DeviceConfiguration; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; +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.config.keymanager.KeyManagerConfigurations; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.identity.jwt.client.extension.dto.AccessTokenInfo; @@ -163,13 +166,13 @@ public class DeviceManagementConfigServiceImpl implements DeviceManagementConfig private void setAccessTokenToDeviceConfigurations(DeviceConfiguration devicesConfiguration) throws DeviceManagementException { try { - AppRegistrationCredentials credentials = - DeviceManagerUtil.getApplicationRegistrationCredentials( - System.getProperty(DeviceManagementConstants - .ConfigurationManagement.IOT_GATEWAY_HOST), - System.getProperty(DeviceManagementConstants - .ConfigurationManagement.IOT_GATEWAY_HTTPS_PORT), - DeviceManagementConstants.ConfigurationManagement.ADMIN_CREDENTIALS); + DeviceManagementConfig deviceManagementConfig = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); + KeyManagerConfigurations kmConfig = deviceManagementConfig.getKeyManagerConfigurations(); + AppRegistrationCredentials credentials = DeviceManagerUtil.getApplicationRegistrationCredentials( + System.getProperty(DeviceManagementConstants.ConfigurationManagement.IOT_GATEWAY_HOST), + System.getProperty(DeviceManagementConstants.ConfigurationManagement.IOT_GATEWAY_HTTPS_PORT), + kmConfig.getAdminUsername(), + kmConfig.getAdminPassword()); AccessTokenInfo accessTokenForAdmin = DeviceManagerUtil.getAccessTokenForDeviceOwner( DeviceManagementConstants.ConfigurationManagement.SCOPES_FOR_TOKEN, credentials.getClient_id(), credentials.getClient_secret(), diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java index 7c7f265353a..9046c5c600b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java @@ -31,11 +31,9 @@ public final class DeviceManagementConstants { private ConfigurationManagement(){ throw new AssertionError(); } - public static final String ADMIN_CREDENTIALS = "admin:admin"; public static final String SCOPES_FOR_TOKEN = "perm:device:operations perm:device:publish-event"; public static final String IOT_GATEWAY_HOST = "iot.gateway.host"; public static final String IOT_GATEWAY_HTTPS_PORT = "iot.gateway.https.port"; - public static final String MQTT_ENDPOINTS = "mqttEndpoints"; public static final String APPLICATION_REGISTRATION_API_ENDPOINT = "/api-application-registration/register"; public static final String AUTHORIZATION_HEADER = "authorization"; 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 6ceb93ba464..1890d399cd9 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 @@ -606,7 +606,7 @@ public final class DeviceManagerUtil { */ @SuppressWarnings("PackageAccessibility") public static AppRegistrationCredentials getApplicationRegistrationCredentials(String host, String port, - String credentials) + String username, String password) throws ApplicationRegistrationException { if (host == null || port == null) { String msg = "Required gatewayHost or gatewayPort system property is null"; @@ -621,8 +621,8 @@ public final class DeviceManagerUtil { apiEndpoint.setHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()); apiEndpoint.setHeader(DeviceManagementConstants.ConfigurationManagement.AUTHORIZATION_HEADER, - DeviceManagementConstants.ConfigurationManagement.BASIC_AUTH.concat(" ") - .concat(getBase64EncodedCredentials(credentials))); + DeviceManagementConstants.ConfigurationManagement.BASIC_AUTH.concat(" ") + .concat(getBase64EncodedCredentials(username + ":" + password))); apiEndpoint.setEntity(constructApplicationRegistrationPayload()); HttpResponse response = client.execute(apiEndpoint); if (response != null) {