From 1eda7bd237c2af2e64b52492343bb66cf9010d03 Mon Sep 17 00:00:00 2001 From: pasindu Date: Tue, 3 Oct 2023 09:17:46 +0530 Subject: [PATCH] Fixes in API publishing flow --- .../api/PublisherRESTAPIServicesImpl.java | 39 ++++---- .../extension/rest/api/dto/APIInfo/Scope.java | 18 ++-- .../extension/rest/api/util/ScopeUtils.java | 90 ------------------- .../publisher/APIPublisherServiceImpl.java | 17 ++-- .../apimgt/webapp/publisher/dto/ApiScope.java | 8 +- .../lifecycle/util/AnnotationProcessor.java | 18 ++-- .../webapp/publisher/utils/TestUtils.java | 2 +- 7 files changed, 59 insertions(+), 133 deletions(-) delete mode 100644 components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.extension.rest.api/src/main/java/io/entgra/device/mgt/core/apimgt/extension/rest/api/util/ScopeUtils.java 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/PublisherRESTAPIServicesImpl.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/PublisherRESTAPIServicesImpl.java index 40a8e92c61..90724d3cf1 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/PublisherRESTAPIServicesImpl.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/PublisherRESTAPIServicesImpl.java @@ -32,7 +32,6 @@ import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIService 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.util.HttpsTrustManagerUtils; -import io.entgra.device.mgt.core.apimgt.extension.rest.api.util.ScopeUtils; import okhttp3.*; import okhttp3.Request.Builder; import org.apache.commons.httpclient.HttpStatus; @@ -144,14 +143,19 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { String addNewSharedScopeEndPoint = endPointPrefix + Constants.SCOPE_API_ENDPOINT; - ScopeUtils scopeUtil = new ScopeUtils(); - scopeUtil.setKey(scope.getKey()); - scopeUtil.setName(scope.getName()); - scopeUtil.setDescription(scope.getDescription()); - scopeUtil.setRoles(scope.getRoles()); - String scopeString = scopeUtil.toJSON(); + JSONArray bindings = new JSONArray(); + for (String str : scope.getRoles()) { + bindings.put(str); + } + + JSONObject payload = new JSONObject(); + payload.put("name", scope.getKey()); + payload.put("displayName", scope.getName()); + payload.put("description", scope.getDescription()); + payload.put("bindings", bindings); + payload.put("usageCount", scope.getUsageCount()); - RequestBody requestBody = RequestBody.create(JSON, scopeString); + RequestBody requestBody = RequestBody.create(JSON, payload.toString()); Request request = new Request.Builder() .url(addNewSharedScopeEndPoint) .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER @@ -191,14 +195,19 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices { String updateScopeUrl = endPointPrefix + Constants.SCOPE_API_ENDPOINT + scope.getId(); - ScopeUtils scopeUtil = new ScopeUtils(); - scopeUtil.setKey(scope.getKey()); - scopeUtil.setName(scope.getName()); - scopeUtil.setDescription(scope.getDescription()); - scopeUtil.setRoles(scope.getRoles()); - String scopeString = scopeUtil.toJSON(); + JSONArray bindings = new JSONArray(); + for (String str : scope.getRoles()) { + bindings.put(str); + } + + JSONObject payload = new JSONObject(); + payload.put("name", scope.getKey()); + payload.put("displayName", scope.getName()); + payload.put("description", scope.getDescription()); + payload.put("bindings", bindings); + payload.put("usageCount", scope.getUsageCount()); - RequestBody requestBody = RequestBody.create(JSON, scopeString); + RequestBody requestBody = RequestBody.create(JSON, payload.toString()); Request request = new Request.Builder() .url(updateScopeUrl) .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER 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/dto/APIInfo/Scope.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/dto/APIInfo/Scope.java index 01d3f53d36..1e414675cd 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/dto/APIInfo/Scope.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/dto/APIInfo/Scope.java @@ -18,7 +18,7 @@ package io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIInfo; import java.io.Serializable; -import java.util.Objects; +import java.util.*; public class Scope implements Serializable{ @@ -26,7 +26,7 @@ public class Scope implements Serializable{ String key; String name; - String roles; + List roles; String description; String id; int usageCount; @@ -47,12 +47,12 @@ public class Scope implements Serializable{ this.name = name; } - public String getRoles() { + public List getRoles() { return roles; } - public void setRoles(String roles) { - this.roles = roles; + public void setRoles(List roles) { + this.roles = removeDuplicatesFromRoleString(roles); } public String getDescription() { @@ -97,4 +97,12 @@ public class Scope implements Serializable{ public int hashCode() { return Objects.hash(key, name, roles, description, id); } + + private static List removeDuplicatesFromRoleString(List roles) { + Set roleSet = new HashSet<>(); + for(String role : roles) { + roleSet.add(role.trim()); + } + return new ArrayList<>(roleSet); + } } 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/util/ScopeUtils.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/util/ScopeUtils.java deleted file mode 100644 index 58cff08553..0000000000 --- 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/util/ScopeUtils.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (c) 2018 - 2023, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. - * - * Entgra (Pvt) Ltd. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package io.entgra.device.mgt.core.apimgt.extension.rest.api.util; - -import java.util.HashSet; -import java.util.Set; - -/** - * This class represents the scope data. - */ -public class ScopeUtils { - - private String key; - private String name; - private String roles; - private String description; - private int id; - - public ScopeUtils() { - } - - public String getKey() { - return key; - } - - public void setKey(String key) { - this.key = key; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getRoles() { - return roles; - } - - public void setRoles(String roles) { - this.roles = removeDuplicatesFromRoleString(roles); - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String toJSON() { - String jsonString = "{\n" + - " \"name\":\"" + key + "\",\n" + - " \"displayName\":\"" + name + "\",\n" + - " \"description\":\"" + description + "\",\n" + - " \"bindings\":[\n" + - " \"" + roles + "\"\n" + - " ]\n" + - "}"; - return jsonString; - } - - private static String removeDuplicatesFromRoleString(String roleString) { - String[] roles = roleString.split(","); - Set roleSet = new HashSet<>(); - for(String role : roles) { - roleSet.add(role.trim()); - } - return String.join(",", roleSet); - } -} 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 69f20fa020..305b542777 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 @@ -553,36 +553,33 @@ public class APIPublisherServiceImpl implements APIPublisherService { // scopeMapping[3] != null ? StringUtils.trim(scopeMapping[3]) : StringUtils.EMPTY); String permission = scopeMapping[3] != null ? StringUtils.trim(scopeMapping[3]) : StringUtils.EMPTY; - String roleString = ""; + List rolesList = new ArrayList<>(); for (int i = 4; i < scopeMapping.length; i++) { if (scopeMapping[i] != null && StringUtils.trim(scopeMapping[i]).equals("Yes")) { - roleString = roleString + "," + roles.get(i); + rolesList.add(roles.get(i)); if (rolePermissions.containsKey(roles.get(i)) && StringUtils.isNotEmpty(permission)) { rolePermissions.get(roles.get(i)).add(permission); } } } - if (roleString.length() > 1) { - roleString = roleString.substring(1); // remove first , (comma) - } - scope.setRoles(roleString); + scope.setRoles(rolesList); //Set scope id which related to the scope key JSONArray scopeList = (JSONArray) scopeObject.get("list"); for (int i = 0; i < scopeList.length(); i++) { JSONObject scopeObj = scopeList.getJSONObject(i); - if (scopeObj.getString("name").equals(scopeMapping[2] != null ? - StringUtils.trim(scopeMapping[2]) : StringUtils.EMPTY)) { + if (scopeObj.getString("name").equals(StringUtils.trim(scopeMapping[2]))) { scope.setId(scopeObj.getString("id")); + scope.setUsageCount(scopeObj.getInt("usageCount")); // Including already existing roles JSONArray existingRolesArray = (JSONArray) scopeObj.get("bindings"); for (int j = 0; j < existingRolesArray.length(); j++) { - roleString = roleString + "," + existingRolesArray.get(j); + rolesList.add(existingRolesArray.getString(j)); } } } - scope.setRoles(roleString); + scope.setRoles(rolesList); if (publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, scope.getKey())) { publisherRESTAPIServices.updateSharedScope(apiApplicationKey, accessTokenInfo, scope); 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/dto/ApiScope.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/dto/ApiScope.java index 5d178a0eb3..ea5f6d3047 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/dto/ApiScope.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/dto/ApiScope.java @@ -18,11 +18,13 @@ package io.entgra.device.mgt.core.apimgt.webapp.publisher.dto; +import java.util.List; + public class ApiScope { String key; String name; - String roles; + List roles; String permissions; String description; int id; @@ -46,11 +48,11 @@ public class ApiScope { this.name = name; } - public String getRoles() { + public List getRoles() { return this.roles; } - public void setRoles(String roles) { + public void setRoles(List roles) { this.roles = roles; } 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/lifecycle/util/AnnotationProcessor.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/lifecycle/util/AnnotationProcessor.java index 704de6442c..b044c9f72a 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/lifecycle/util/AnnotationProcessor.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/main/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/lifecycle/util/AnnotationProcessor.java @@ -228,10 +228,9 @@ public class AnnotationProcessor { String permissions[]; StringBuilder aggregatedPermissions; String roles[]; - StringBuilder aggregatedRoles; + List aggregatedRoles; for (int i = 0; i < annotatedScopes.length; i++) { aggregatedPermissions = new StringBuilder(); - aggregatedRoles = new StringBuilder(); methodHandler = Proxy.getInvocationHandler(annotatedScopes[i]); scope = new ApiScope(); scope.setName(invokeMethod(scopeClass @@ -250,11 +249,8 @@ public class AnnotationProcessor { scope.setPermissions(aggregatedPermissions.toString().trim()); roles = (String[]) methodHandler.invoke(annotatedScopes[i], scopeClass .getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_ROLES, null), null); - for (String role : roles) { - aggregatedRoles.append(role); - aggregatedRoles.append(","); - } - scope.setRoles(aggregatedRoles.substring(0, aggregatedRoles.lastIndexOf(","))); + aggregatedRoles = Arrays.asList(roles); + scope.setRoles(aggregatedRoles); scopes.put(scope.getKey(), scope); } return scopes; @@ -305,11 +301,13 @@ public class AnnotationProcessor { // } else { // log.warn("Scope is not defined for '" + makeContextURLReady(resourceRootContext) + // makeContextURLReady(subCtx) + "' endpoint, hence assigning the default scope"); +// List roles = new ArrayList<>(); +// roles.add(DEFAULT_SCOPE_ROLE); // scope = new ApiScope(); // scope.setName(DEFAULT_SCOPE_NAME); // scope.setDescription(DEFAULT_SCOPE_NAME); // scope.setKey(DEFAULT_SCOPE_KEY); -// scope.setRoles(DEFAULT_SCOPE_ROLE); +// scope.setRoles(roles); // scope.setPermissions(DEFAULT_SCOPE_PERMISSION); // resource.setScope(scope); // } @@ -543,11 +541,13 @@ public class AnnotationProcessor { } else { // log.warn("Scope is not defined for '" + makeContextURLReady(resourceRootContext) + // makeContextURLReady(subCtx) + "' endpoint, hence assigning the default scope"); + List roles = new ArrayList<>(); + roles.add(DEFAULT_SCOPE_ROLE); scope = new ApiScope(); scope.setName(DEFAULT_SCOPE_NAME); scope.setDescription(DEFAULT_SCOPE_NAME); scope.setKey(DEFAULT_SCOPE_KEY); - scope.setRoles(DEFAULT_SCOPE_ROLE); + scope.setRoles(roles); scope.setPermissions(DEFAULT_SCOPE_PERMISSION); apiResource.setScope(scope); } diff --git a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/test/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/utils/TestUtils.java b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/test/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/utils/TestUtils.java index 988bd9c5f7..85f0d520f9 100644 --- a/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/test/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/utils/TestUtils.java +++ b/components/apimgt-extensions/io.entgra.device.mgt.core.apimgt.webapp.publisher/src/test/java/io/entgra/device/mgt/core/apimgt/webapp/publisher/utils/TestUtils.java @@ -39,7 +39,7 @@ public class TestUtils { ApiScope scope = new ApiScope(); scope.setKey("win:ops:reboot"); scope.setName("Reboot"); - scope.setRoles("/permission/admin/device-mgt/devices/owning-device/operations/windows/reboot"); + scope.setPermissions("/permission/admin/device-mgt/devices/owning-device/operations/windows/reboot"); scope.setDescription("Lock reset on Windows devices"); template.setScope(scope); uriTemplates.add(template);