From 3856a254491d13e4c30f2b275bf333036563faf6 Mon Sep 17 00:00:00 2001 From: charitha Date: Sun, 21 Oct 2018 14:30:06 +0530 Subject: [PATCH] Fixed authentication issue in deploying execution plans as non admin user --- .../GeoLocationProviderServiceImpl.java | 19 +++++++++++++++++-- .../GeoLocationProviderServiceTest.java | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java index c5bcec41c2..ccb0317a38 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java @@ -51,6 +51,7 @@ import org.wso2.carbon.identity.jwt.client.extension.service.JWTClientManagerSer import org.wso2.carbon.registry.api.Registry; import org.wso2.carbon.registry.api.RegistryException; import org.wso2.carbon.registry.api.Resource; +import org.wso2.carbon.stratos.common.util.ClaimsMgtUtil; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; @@ -428,6 +429,9 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic } catch (JWTClientException e) { throw new GeoLocationBasedServiceException( "JWT token creation failed while " + action + " geo alert '" + alertType, e); + } catch (Exception e) { + throw new GeoLocationBasedServiceException( + "Error occurred while " + action + " geo alert '" + alertType, e); } finally { cleanup(eventprocessorStub); } @@ -541,6 +545,9 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic throw new GeoLocationBasedServiceException( "JWT token creation failed while " + action + " geo alert '" + alertType + "' for " + identifier.getType() + " device with id:" + identifier.getId(), e); + } catch (Exception e) { + throw new GeoLocationBasedServiceException( + "Error occurred while " + action + " geo alert '" + alertType, e); } finally { cleanup(eventprocessorStub); } @@ -646,6 +653,9 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic executionPlanName + " for " + identifier.getType() + " device with id:" + identifier.getId(), e ); + } catch (Exception e) { + throw new GeoLocationBasedServiceException( + "Error occurred while removing geo alert '" + alertType, e); } finally { cleanup(eventprocessorStub); } @@ -672,6 +682,9 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic "JWT token creation failed while removing geo alert '" + alertType + "': " + executionPlanName, e ); + } catch (Exception e) { + throw new GeoLocationBasedServiceException( + "Error occurred while removing geo alert '" + alertType, e); } finally { cleanup(eventprocessorStub); } @@ -702,14 +715,16 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic } } - protected EventProcessorAdminServiceStub getEventProcessorAdminServiceStub() throws JWTClientException { + protected EventProcessorAdminServiceStub getEventProcessorAdminServiceStub() throws Exception { //send alert to event-processing String eventProcessorAdminServiceWSUrl = Utils.replaceSystemProperty(GeoServices.DAS_URL) + "/services/EventProcessorAdminService"; //Getting the tenant Domain + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(); - String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + String username = ClaimsMgtUtil.getAdminUserNameFromTenantId(DeviceManagementDataHolder.getInstance().getRealmService(), + tenantId); String tenantAdminUser = username + "@" + tenantDomain; try { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceTest.java index 1e0916d079..649ecf4244 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceTest.java @@ -143,7 +143,7 @@ public class GeoLocationProviderServiceTest { Assert.assertEquals(geoFenceNode.getStationaryTime(), SAMPLE_STATIONARY_TIME); } - private void initMocks() throws JWTClientException, RemoteException { + private void initMocks() throws Exception { EventProcessorAdminServiceStub mockEventProcessorAdminServiceStub = Mockito.mock(EventProcessorAdminServiceStub.class); geoLocationProviderServiceImpl = Mockito.mock(GeoLocationProviderServiceImpl.class, Mockito.CALLS_REAL_METHODS); mockExecutionPlanConfigurationDto[0] = Mockito.mock(ExecutionPlanConfigurationDto.class);