From c088a9bc9389e3df3ae3d90a19c1722a974ae255 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Wed, 1 Mar 2017 23:26:17 +0530 Subject: [PATCH 1/3] fixed api subscribe issue for device specific app creation --- .../ApiApplicationRegistrationService.java | 8 ------ ...ApiApplicationRegistrationServiceImpl.java | 27 ++++++++----------- .../api/util/RegistrationProfile.java | 11 -------- .../src/main/webapp/META-INF/permissions.xml | 9 +------ .../jaggeryapps/devicemgt/app/modules/init.js | 5 ++-- 5 files changed, 15 insertions(+), 45 deletions(-) diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationService.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationService.java index 5f122b593a0..3515a2e893f 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationService.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationService.java @@ -51,12 +51,4 @@ public interface ApiApplicationRegistrationService { @Path("register") Response register(RegistrationProfile registrationProfile); - /** - * This method is used to unregister an API application. - * @param applicationName name of the application that needs to be unregistered. - * @return the response status of request. - */ - @DELETE - @Path("unregister") - Response unregister(@QueryParam("applicationName") String applicationName); } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java index b2f46dc09ef..65392645ea1 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/ApiApplicationRegistrationServiceImpl.java @@ -18,6 +18,7 @@ package org.wso2.carbon.apimgt.application.extension.api; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.json.simple.JSONObject; @@ -95,6 +96,8 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi return Response.status(Response.Status.NOT_ACCEPTABLE).entity("APIs(Tags) are not allowed to this user." ).build(); } + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(PrivilegedCarbonContext. + getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration().getAdminUserName()); String username = APIUtil.getAuthenticatedUser(); APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService(); String validityPeriod; @@ -103,35 +106,27 @@ public class ApiApplicationRegistrationServiceImpl implements ApiApplicationRegi } else { validityPeriod = registrationProfile.getValidityPeriod(); } + + String applicationName = "devicetype_app_" + StringUtils.join(registrationProfile.getTags(), "_"); ApiApplicationKey apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys( - registrationProfile.getApplicationName(), registrationProfile.getTags(), + applicationName, registrationProfile.getTags(), ApiApplicationConstants.DEFAULT_TOKEN_TYPE, username, registrationProfile.isAllowedToAllDomains(), validityPeriod); return Response.status(Response.Status.CREATED).entity(apiApplicationKey.toString()).build(); } catch (APIManagerException e) { - String msg = "Error occurred while registering an application '" - + registrationProfile.getApplicationName() + "'"; + String msg = "Error occurred while registering an application with apis '" + + StringUtils.join(registrationProfile.getTags(), ",") + "'"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("false").build(); } catch (DeviceManagementException e) { String msg = "Failed to retrieve the device service"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); - } - } - - @Path("unregister") - @DELETE - public Response unregister(@QueryParam("applicationName") String applicationName) { - try { - String username = APIUtil.getAuthenticatedUser() + "@" + APIUtil.getTenantDomainOftheUser(); - APIManagementProviderService apiManagementProviderService = APIUtil.getAPIManagementProviderService(); - apiManagementProviderService.removeAPIApplication(applicationName, username); - return Response.status(Response.Status.ACCEPTED).build(); - } catch (APIManagerException e) { - String msg = "Error occurred while removing the application '" + applicationName; + } catch (UserStoreException e) { + String msg = "Failed to access user space."; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } } + } \ No newline at end of file diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/util/RegistrationProfile.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/util/RegistrationProfile.java index ef63946fd3b..2111753efec 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/util/RegistrationProfile.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/java/org/wso2/carbon/apimgt/application/extension/api/util/RegistrationProfile.java @@ -30,9 +30,6 @@ import javax.xml.bind.annotation.XmlRootElement; @JsonIgnoreProperties(ignoreUnknown = true) public class RegistrationProfile { - - @XmlElement(required = true) - private String applicationName; @XmlElement(required = true) private String tags[]; @XmlElement(required = true) @@ -40,14 +37,6 @@ public class RegistrationProfile { @XmlElement(required = false) private String validityPeriod; - public String getApplicationName() { - return applicationName; - } - - public void setApiApplicationName(String apiApplicationName) { - this.applicationName = apiApplicationName; - } - public String[] getTags() { return tags; } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/webapp/META-INF/permissions.xml b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/webapp/META-INF/permissions.xml index 9c41774ce57..2d907f170ec 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/webapp/META-INF/permissions.xml +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.application.extension.api/src/main/webapp/META-INF/permissions.xml @@ -37,16 +37,9 @@ Register application - /manage/api/subscribe + /device-mgt/device/api/subscribe /register POST application_user - - Delete application - /manage/api/subscribe - /unregister - DELETE - application_user - \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/init.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/init.js index 50cbba4dca1..457e4e1b08e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/init.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/init.js @@ -30,11 +30,12 @@ application.put("carbonServer", carbonServer); var permissions = { "/permission/admin/Login": ["ui.execute"], - "/permission/admin/manage/api/subscribe": ["ui.execute"] + "/permission/admin/device-mgt/device/api/subscribe": ["ui.execute"] }; var adminPermissions = { - "/permission/admin": ["ui.execute"] + "/permission/admin/device-mgt": ["ui.execute"], + "/permission/admin/manage/api": ["ui.execute"] }; //On Startup, admin user will get both roles: devicemgt-admin and devicemgt-user From 5d3aa49e06f24ea795aa87f04bfa611a7873dcc1 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Thu, 2 Mar 2017 00:31:34 +0530 Subject: [PATCH 2/3] added logger trace support for apim integration service --- .../client/OAuthRequestInterceptor.java | 13 ++++++-- .../client/publisher/PublisherClient.java | 9 ++++-- .../integration/client/store/StoreClient.java | 6 ++-- .../apimgt/integration/client/util/Utils.java | 32 +++++++++++++++++++ 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/OAuthRequestInterceptor.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/OAuthRequestInterceptor.java index e599a091540..1e43d386104 100755 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/OAuthRequestInterceptor.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/OAuthRequestInterceptor.java @@ -15,12 +15,15 @@ package org.wso2.carbon.apimgt.integration.client; import feign.Feign; +import feign.Logger; import feign.RequestInterceptor; import feign.RequestTemplate; import feign.auth.BasicAuthRequestInterceptor; import feign.gson.GsonDecoder; import feign.gson.GsonEncoder; import feign.jaxrs.JAXRSContract; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.apimgt.integration.client.configs.APIMConfigReader; import org.wso2.carbon.apimgt.integration.client.exception.APIMClientOAuthException; import org.wso2.carbon.apimgt.integration.client.internal.APIIntegrationClientDataHolder; @@ -47,10 +50,12 @@ public class OAuthRequestInterceptor implements RequestInterceptor { private static final String REQUIRED_SCOPE = "apim:api_create apim:api_view apim:api_publish apim:subscribe apim:tier_view apim:tier_manage " + "apim:subscription_view apim:subscription_block"; + private static final String APIM_SUBSCRIBE_SCOPE = "apim:subscribe"; private static final long DEFAULT_REFRESH_TIME_OFFSET_IN_MILLIS = 100000; private DCRClient dcrClient; private static OAuthApplication oAuthApplication; private static Map tenantUserTokenMap = new HashMap<>(); + private static final Log log = LogFactory.getLog(OAuthRequestInterceptor.class); /** * Creates an interceptor that authenticates all requests. @@ -58,8 +63,8 @@ public class OAuthRequestInterceptor implements RequestInterceptor { public OAuthRequestInterceptor() { String username = APIMConfigReader.getInstance().getConfig().getUsername(); String password = APIMConfigReader.getInstance().getConfig().getPassword(); - dcrClient = Feign.builder().client(Utils.getSSLClient()).requestInterceptor( - new BasicAuthRequestInterceptor(username, password)) + dcrClient = Feign.builder().client(Utils.getSSLClient()).logger(Utils.getLogger(log)).logLevel( + Logger.Level.FULL).requestInterceptor(new BasicAuthRequestInterceptor(username, password)) .contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder()) .target(DCRClient.class, Utils.replaceProperties( APIMConfigReader.getInstance().getConfig().getDcrEndpoint())); @@ -95,7 +100,9 @@ public class OAuthRequestInterceptor implements RequestInterceptor { REQUIRED_SCOPE); tenantBasedAccessTokenInfo.setExpiresIn( System.currentTimeMillis() + (tenantBasedAccessTokenInfo.getExpiresIn() * 1000)); - tenantUserTokenMap.put(username, tenantBasedAccessTokenInfo); + if (tenantBasedAccessTokenInfo.getScopes().contains(APIM_SUBSCRIBE_SCOPE)) { + tenantUserTokenMap.put(username, tenantBasedAccessTokenInfo); + } } if (tenantBasedAccessTokenInfo.getAccessToken() != null) { diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/publisher/PublisherClient.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/publisher/PublisherClient.java index 9585d20ae47..9eda3032096 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/publisher/PublisherClient.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/publisher/PublisherClient.java @@ -18,9 +18,11 @@ package org.wso2.carbon.apimgt.integration.client.publisher; import feign.Feign; +import feign.Logger; import feign.RequestInterceptor; import feign.gson.GsonDecoder; import feign.gson.GsonEncoder; +import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.apimgt.integration.client.configs.APIMConfigReader; import org.wso2.carbon.apimgt.integration.client.publisher.api.*; @@ -31,7 +33,7 @@ import org.wso2.carbon.core.util.Utils; */ public class PublisherClient { - private static final org.apache.commons.logging.Log log = LogFactory.getLog(PublisherClient.class); + private static final Log log = LogFactory.getLog(PublisherClient.class); private APIsApi api = null; private APIDocumentApi document = null; private ApplicationsApi application = null; @@ -46,8 +48,9 @@ public class PublisherClient { */ public PublisherClient(RequestInterceptor requestInterceptor) { Feign.Builder builder = Feign.builder().client( - org.wso2.carbon.apimgt.integration.client.util.Utils.getSSLClient()).requestInterceptor( - requestInterceptor).encoder(new GsonEncoder()).decoder(new GsonDecoder()); + org.wso2.carbon.apimgt.integration.client.util.Utils.getSSLClient()).logger( + org.wso2.carbon.apimgt.integration.client.util.Utils.getLogger(log)).logLevel(Logger.Level.FULL) + .requestInterceptor(requestInterceptor).encoder(new GsonEncoder()).decoder(new GsonDecoder()); String basePath = Utils.replaceSystemProperty(APIMConfigReader.getInstance().getConfig().getPublisherEndpoint()); api = builder.target(APIsApi.class, basePath); diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/store/StoreClient.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/store/StoreClient.java index 8bd83a729dd..47d9d3af9da 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/store/StoreClient.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/store/StoreClient.java @@ -18,6 +18,7 @@ package org.wso2.carbon.apimgt.integration.client.store; import feign.Feign; +import feign.Logger; import feign.RequestInterceptor; import feign.gson.GsonDecoder; import feign.gson.GsonEncoder; @@ -46,8 +47,9 @@ public class StoreClient { public StoreClient(RequestInterceptor requestInterceptor) { Feign.Builder builder = Feign.builder().client( - org.wso2.carbon.apimgt.integration.client.util.Utils.getSSLClient()).requestInterceptor( - requestInterceptor).encoder(new GsonEncoder()).decoder(new GsonDecoder()); + org.wso2.carbon.apimgt.integration.client.util.Utils.getSSLClient()).logger( + org.wso2.carbon.apimgt.integration.client.util.Utils.getLogger(log)).logLevel(Logger.Level.FULL) + .requestInterceptor(requestInterceptor).encoder(new GsonEncoder()).decoder(new GsonDecoder()); String basePath = Utils.replaceSystemProperty(APIMConfigReader.getInstance().getConfig().getStoreEndpoint()); apis = builder.target(ApisAPIApi.class, basePath); diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/util/Utils.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/util/Utils.java index 9ed2640f1db..da0b4233fe7 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/util/Utils.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.integration.client/src/main/java/org/wso2/carbon/apimgt/integration/client/util/Utils.java @@ -27,10 +27,15 @@ import javax.net.ssl.SSLSession; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; +import java.io.IOException; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.util.regex.Matcher; import java.util.regex.Pattern; +import feign.Logger; +import feign.Request; +import feign.Response; +import org.apache.commons.logging.Log; public class Utils { @@ -81,4 +86,31 @@ public class Utils { } } + + public static Logger getLogger(final Log log) { + return new Logger() { + @Override + protected void log(String configKey, String format, Object... args) { + if (log.isDebugEnabled()) { + log.debug(String.format(methodTag(configKey) + format, args)); + } + } + + @Override + protected void logRequest(String configKey, Level logLevel, Request request) { + if (log.isDebugEnabled()) { + super.logRequest(configKey, logLevel, request); + } + } + + @Override + protected Response logAndRebufferResponse(String configKey, Level logLevel, Response response, + long elapsedTime) throws IOException { + if (log.isDebugEnabled()) { + return super.logAndRebufferResponse(configKey, logLevel, response, elapsedTime); + } + return response; + } + }; + } } From d66e439f492ed2025a7d33ee877e1c35b28b5e72 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Thu, 2 Mar 2017 14:05:57 +0530 Subject: [PATCH 3/3] added carbon local ip as config --- .../main/resources/jaggeryapps/devicemgt/app/conf/app-conf.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/app-conf.json b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/app-conf.json index 4be20160d38..6171219bb9d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/app-conf.json +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/conf/app-conf.json @@ -30,7 +30,7 @@ "enabled": true, "issuer" : "devicemgt", "appName" : "devicemgt", - "identityProviderUrl" : "https://%iot.keymanager.host%:%iot.keymanager.https.port%/samlsso", + "identityProviderUrl" : "https://%carbon.local.ip%:%iot.keymanager.https.port%/samlsso", "acs": "https://%iot.manager.host%:%iot.manager.https.port%/devicemgt/uuf/sso/acs", "identityAlias": "wso2carbon", "responseSigningEnabled" : true,