From af17e4fa8e971334c2c67446b2cd86c2a6ea0dc3 Mon Sep 17 00:00:00 2001 From: Dharmakeerthi Lasantha Date: Mon, 12 Jun 2023 16:27:38 +0530 Subject: [PATCH] Improve APIM application key generate method --- .../pom.xml | 7 ++ .../APIManagementProviderServiceImpl.java | 41 +++++++++++- ...ApplicationManagerExtensionDataHolder.java | 10 +++ ...ationManagerExtensionServiceComponent.java | 31 +++++++++ .../keymgt/extension/api/DCRRequest.java | 6 ++ .../pom.xml | 6 ++ .../extension/internal/KeyMgtDataHolder.java | 20 ++++++ .../internal/KeyMgtServiceComponent.java | 67 ++++++++++++++++++- 8 files changed, 184 insertions(+), 4 deletions(-) diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/pom.xml b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/pom.xml index 6ad08c770a..7434c58e54 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/pom.xml +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/pom.xml @@ -84,6 +84,12 @@ io.entgra.device.mgt.core.apimgt.extension.rest.api + + io.entgra.device.mgt.core + io.entgra.device.mgt.core.device.mgt.common + + + @@ -124,6 +130,7 @@ io.entgra.device.mgt.core.identity.jwt.client.extension.dto;version="[5.0,6)", io.entgra.device.mgt.core.identity.jwt.client.extension.exception;version="[5.0,6)", io.entgra.device.mgt.core.identity.jwt.client.extension.service;version="[5.0,6)", + io.entgra.device.mgt.core.device.mgt.common;version="[5.0,6)", org.apache.commons.lang;version="[2.6,3)", org.apache.commons.logging;version="[1.2,2)", org.json.simple;version="[1.1,2)", diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java index ad58ab4f33..6ab71de887 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/APIManagementProviderServiceImpl.java @@ -24,6 +24,10 @@ import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplication import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException; import io.entgra.device.mgt.core.apimgt.application.extension.internal.APIApplicationManagerExtensionDataHolder; import io.entgra.device.mgt.core.apimgt.application.extension.util.APIManagerUtil; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataKeyAlreadyExistsException; +import io.entgra.device.mgt.core.device.mgt.common.exceptions.MetadataManagementException; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.Metadata; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService; import io.entgra.device.mgt.core.identity.jwt.client.extension.JWTClient; import io.entgra.device.mgt.core.identity.jwt.client.extension.dto.AccessTokenInfo; import io.entgra.device.mgt.core.identity.jwt.client.extension.exception.JWTClientException; @@ -41,6 +45,7 @@ import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.Unexpected import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.eclipse.jetty.http.MetaData; import org.wso2.carbon.apimgt.api.APIAdmin; import org.wso2.carbon.apimgt.api.APIConsumer; import org.wso2.carbon.apimgt.api.APIManagementException; @@ -159,13 +164,47 @@ public class APIManagementProviderServiceImpl implements APIManagementProviderSe } } + MetadataManagementService metadataManagementService = APIApplicationManagerExtensionDataHolder.getInstance().getMetadataManagementService(); if (isNewApplication) { ApplicationKey applicationKey = consumerRESTAPIServices.generateApplicationKeys(applicationInfo, application); ApiApplicationKey apiApplicationKey = new ApiApplicationKey(); apiApplicationKey.setConsumerKey(applicationKey.getConsumerKey()); apiApplicationKey.setConsumerSecret(applicationKey.getConsumerSecret()); - return apiApplicationKey; + + Metadata metaData = new Metadata(); + metaData.setMetaKey(applicationName); + String metaValue = application.getApplicationId() + ":" + applicationKey.getKeyMappingId(); + metaData.setMetaValue(metaValue); + try { + metadataManagementService.createMetadata(metaData); + return apiApplicationKey; + } catch (MetadataManagementException e) { + String msg = "Error occurred while creating the meta data entry for mata key: " + applicationName; + log.error(msg, e); + throw new APIManagerException(msg, e); + } catch (MetadataKeyAlreadyExistsException e) { + String msg = "Found duplicate meta value entry for meta key: " + applicationName; + log.error(msg, e); + throw new APIManagerException(msg, e); + } } else { + try { + Metadata metaData = metadataManagementService.retrieveMetadata(applicationName); + if (metaData == null) { + String msg = + "Couldn't find application key data from meta data mgt service. Meta key: " + applicationName; + log.error(msg); + throw new APIManagerException(msg); + } + String[] metaValues = metaData.getMetaValue().split(":"); + String applicationId = metaValues[0]; + String keyMappingId = metaValues[1]; + //todo call the API key retrieving call, return apiApplicationKey; + } catch (MetadataManagementException e) { + String msg = "Error occurred while getting meta data for meta key: " + applicationName; + log.error(msg, e); + throw new APIManagerException(msg, e); + } return null; } } catch (APIServicesException e) { diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/internal/APIApplicationManagerExtensionDataHolder.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/internal/APIApplicationManagerExtensionDataHolder.java index 1074edd9d7..d6a3bb2fc5 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/internal/APIApplicationManagerExtensionDataHolder.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/internal/APIApplicationManagerExtensionDataHolder.java @@ -20,6 +20,7 @@ package io.entgra.device.mgt.core.apimgt.application.extension.internal; import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProviderService; import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices; import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService; import org.wso2.carbon.context.PrivilegedCarbonContext; import io.entgra.device.mgt.core.identity.jwt.client.extension.service.JWTClientManagerService; import org.wso2.carbon.registry.core.service.TenantRegistryLoader; @@ -39,6 +40,7 @@ public class APIApplicationManagerExtensionDataHolder { private JWTClientManagerService jwtClientManagerService; private ConsumerRESTAPIServices consumerRESTAPIServices; private APIApplicationServices apiApplicationServices; + private MetadataManagementService metadataManagementService; private APIApplicationManagerExtensionDataHolder() { } @@ -123,4 +125,12 @@ public class APIApplicationManagerExtensionDataHolder { public void setApiApplicationServices(APIApplicationServices apiApplicationServices) { this.apiApplicationServices = apiApplicationServices; } + + public MetadataManagementService getMetadataManagementService() { + return metadataManagementService; + } + + public void setMetadataManagementService(MetadataManagementService metadataManagementService) { + this.metadataManagementService = metadataManagementService; + } } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/internal/APIApplicationManagerExtensionServiceComponent.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/internal/APIApplicationManagerExtensionServiceComponent.java index f761bfbf38..b3dc6dbd9d 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/internal/APIApplicationManagerExtensionServiceComponent.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.application.extension/src/main/java/io/entgra/device/mgt/core/apimgt/application/extension/internal/APIApplicationManagerExtensionServiceComponent.java @@ -21,6 +21,7 @@ import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationService import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices; import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProviderService; import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProviderServiceImpl; +import io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; @@ -178,4 +179,34 @@ public class APIApplicationManagerExtensionServiceComponent { } APIApplicationManagerExtensionDataHolder.getInstance().setApiApplicationServices(null); } + + /** + * Sets Meta Data Mgt service. + * + * @param metadataManagementService An instance of MetadataManagementService + */ + @Reference( + name = "meta.data.mgt.service", + service = io.entgra.device.mgt.core.device.mgt.common.metadata.mgt.MetadataManagementService.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetAMetaMgtServices") + protected void setMetaMgtServices(MetadataManagementService metadataManagementService) { + if (log.isDebugEnabled()) { + log.debug("Setting Meta data mgt Service"); + } + APIApplicationManagerExtensionDataHolder.getInstance().setMetadataManagementService(metadataManagementService); + } + + /** + * Unset Meta Data Mgt service + * + * @param metadataManagementService An instance of MetadataManagementService + */ + protected void unsetAMetaMgtServices(MetadataManagementService metadataManagementService) { + if (log.isDebugEnabled()) { + log.debug("Unsetting Meta Data mgt Service"); + } + APIApplicationManagerExtensionDataHolder.getInstance().setMetadataManagementService(null); + } } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/api/DCRRequest.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/api/DCRRequest.java index c8171451f6..8f492c2234 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/api/DCRRequest.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension.api/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/api/DCRRequest.java @@ -32,6 +32,8 @@ public class DCRRequest { @XmlElement(required = true) private String username; @XmlElement(required = true) + private String password; + @XmlElement(required = true) private String grantTypes; @XmlElement private String callBackUrl; @@ -98,4 +100,8 @@ public class DCRRequest { public void setValidityPeriod(int validityPeriod) { this.validityPeriod = validityPeriod; } + + public String getPassword() { return password; } + + public void setPassword(String password) { this.password = password; } } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/pom.xml b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/pom.xml index 5d2d371df5..53374f48c6 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/pom.xml +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/pom.xml @@ -55,6 +55,11 @@ io.entgra.device.mgt.core io.entgra.device.mgt.core.device.mgt.common + + io.entgra.device.mgt.core + io.entgra.device.mgt.core.apimgt.extension.rest.api + + @@ -108,6 +113,7 @@ org.wso2.carbon.apimgt.keymgt.model;version="[9.28,10)", org.wso2.carbon.apimgt.keymgt.model.entity;version="[9.28,10)", org.wso2.carbon.apimgt.keymgt.service;version="[9.28,10)", + io.entgra.device.mgt.core.apimgt.extension.rest.api, org.wso2.carbon.context;version="[4.8,5)", org.wso2.carbon.user.api;version="[1.0,2)", org.wso2.carbon.user.core.service;version="[4.8,5)", diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/internal/KeyMgtDataHolder.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/internal/KeyMgtDataHolder.java index fd711781a1..96e96000be 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/internal/KeyMgtDataHolder.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/internal/KeyMgtDataHolder.java @@ -18,12 +18,16 @@ package io.entgra.device.mgt.core.apimgt.keymgt.extension.internal; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices; import io.entgra.device.mgt.core.apimgt.keymgt.extension.service.KeyMgtService; public class KeyMgtDataHolder { private static final KeyMgtDataHolder thisInstance = new KeyMgtDataHolder(); private KeyMgtService keyMgtService; + private ConsumerRESTAPIServices consumerRESTAPIServices; + private APIApplicationServices apiApplicationServices; public static KeyMgtDataHolder getInstance() { return thisInstance; @@ -37,4 +41,20 @@ public class KeyMgtDataHolder { this.keyMgtService = keyMgtService; } + + public ConsumerRESTAPIServices getConsumerRESTAPIServices() { + return consumerRESTAPIServices; + } + + public void setConsumerRESTAPIServices(ConsumerRESTAPIServices consumerRESTAPIServices) { + this.consumerRESTAPIServices = consumerRESTAPIServices; + } + + public APIApplicationServices getApiApplicationServices() { + return apiApplicationServices; + } + + public void setApiApplicationServices(APIApplicationServices apiApplicationServices) { + this.apiApplicationServices = apiApplicationServices; + } } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/internal/KeyMgtServiceComponent.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/internal/KeyMgtServiceComponent.java index 395bdc0107..96a05f15f5 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/internal/KeyMgtServiceComponent.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.keymgt.extension/src/main/java/io/entgra/device/mgt/core/apimgt/keymgt/extension/internal/KeyMgtServiceComponent.java @@ -18,15 +18,16 @@ package io.entgra.device.mgt.core.apimgt.keymgt.extension.internal; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices; +import io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices; import io.entgra.device.mgt.core.apimgt.keymgt.extension.service.KeyMgtService; import io.entgra.device.mgt.core.apimgt.keymgt.extension.service.KeyMgtServiceImpl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; -import org.osgi.service.component.annotations.Activate; -import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.annotations.Deactivate; +import org.osgi.service.component.annotations.*; + @Component( name = "io.entgra.device.mgt.core.apimgt.keymgt.extension.internal.KeyMgtServiceComponent", immediate = true) @@ -63,4 +64,64 @@ public class KeyMgtServiceComponent { log.debug("De-activating Key Management Service Component"); } } + + /** + * Sets APIM Consumer REST API service. + * + * @param consumerRESTAPIServices An instance of ConsumerRESTAPIServices + */ + @Reference( + name = "APIM.consumer.service", + service = io.entgra.device.mgt.core.apimgt.extension.rest.api.ConsumerRESTAPIServices.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetConsumerRESTAPIServices") + protected void setConsumerRESTAPIServices(ConsumerRESTAPIServices consumerRESTAPIServices) { + if (log.isDebugEnabled()) { + log.debug("Setting APIM Consumer REST API Service"); + } + KeyMgtDataHolder.getInstance().setConsumerRESTAPIServices(consumerRESTAPIServices); + } + + /** + * Unset APIM Consumer REST API service + * + * @param consumerRESTAPIServices An instance of ConsumerRESTAPIServices + */ + protected void unsetConsumerRESTAPIServices(ConsumerRESTAPIServices consumerRESTAPIServices) { + if (log.isDebugEnabled()) { + log.debug("Unsetting APIM Consumer REST API Service"); + } + KeyMgtDataHolder.getInstance().setConsumerRESTAPIServices(null); + } + + /** + * Sets DCR REST API service. + * + * @param apiApplicationServices An instance of APIApplicationServices + */ + @Reference( + name = "APIM.application.service", + service = io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices.class, + cardinality = ReferenceCardinality.MANDATORY, + policy = ReferencePolicy.DYNAMIC, + unbind = "unsetAPIApplicationServices") + protected void setAPIApplicationServices(APIApplicationServices apiApplicationServices) { + if (log.isDebugEnabled()) { + log.debug("Setting DCR REST API Service"); + } + KeyMgtDataHolder.getInstance().setApiApplicationServices(apiApplicationServices); + } + + /** + * Unset DCR REST API service + * + * @param apiApplicationServices An instance of APIApplicationServices + */ + protected void unsetAPIApplicationServices(APIApplicationServices apiApplicationServices) { + if (log.isDebugEnabled()) { + log.debug("Unsetting DCR REST API Service"); + } + KeyMgtDataHolder.getInstance().setApiApplicationServices(null); + } }