Fixes in API publishing flow

master
Pasindu Rupasinghe 1 year ago
parent 9767f7e90f
commit 1eda7bd237

@ -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.BadRequestException;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.UnexpectedResponseException; 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.HttpsTrustManagerUtils;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.util.ScopeUtils;
import okhttp3.*; import okhttp3.*;
import okhttp3.Request.Builder; import okhttp3.Request.Builder;
import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpStatus;
@ -144,14 +143,19 @@ public class PublisherRESTAPIServicesImpl implements PublisherRESTAPIServices {
String addNewSharedScopeEndPoint = endPointPrefix + Constants.SCOPE_API_ENDPOINT; String addNewSharedScopeEndPoint = endPointPrefix + Constants.SCOPE_API_ENDPOINT;
ScopeUtils scopeUtil = new ScopeUtils(); JSONArray bindings = new JSONArray();
scopeUtil.setKey(scope.getKey()); for (String str : scope.getRoles()) {
scopeUtil.setName(scope.getName()); bindings.put(str);
scopeUtil.setDescription(scope.getDescription()); }
scopeUtil.setRoles(scope.getRoles());
String scopeString = scopeUtil.toJSON(); 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() Request request = new Request.Builder()
.url(addNewSharedScopeEndPoint) .url(addNewSharedScopeEndPoint)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER .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(); String updateScopeUrl = endPointPrefix + Constants.SCOPE_API_ENDPOINT + scope.getId();
ScopeUtils scopeUtil = new ScopeUtils(); JSONArray bindings = new JSONArray();
scopeUtil.setKey(scope.getKey()); for (String str : scope.getRoles()) {
scopeUtil.setName(scope.getName()); bindings.put(str);
scopeUtil.setDescription(scope.getDescription()); }
scopeUtil.setRoles(scope.getRoles());
String scopeString = scopeUtil.toJSON(); 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() Request request = new Request.Builder()
.url(updateScopeUrl) .url(updateScopeUrl)
.addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER .addHeader(Constants.AUTHORIZATION_HEADER_NAME, Constants.AUTHORIZATION_HEADER_PREFIX_BEARER

@ -18,7 +18,7 @@
package io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIInfo; package io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIInfo;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects; import java.util.*;
public class Scope implements Serializable{ public class Scope implements Serializable{
@ -26,7 +26,7 @@ public class Scope implements Serializable{
String key; String key;
String name; String name;
String roles; List<String> roles;
String description; String description;
String id; String id;
int usageCount; int usageCount;
@ -47,12 +47,12 @@ public class Scope implements Serializable{
this.name = name; this.name = name;
} }
public String getRoles() { public List<String> getRoles() {
return roles; return roles;
} }
public void setRoles(String roles) { public void setRoles(List<String> roles) {
this.roles = roles; this.roles = removeDuplicatesFromRoleString(roles);
} }
public String getDescription() { public String getDescription() {
@ -97,4 +97,12 @@ public class Scope implements Serializable{
public int hashCode() { public int hashCode() {
return Objects.hash(key, name, roles, description, id); return Objects.hash(key, name, roles, description, id);
} }
private static List<String> removeDuplicatesFromRoleString(List<String> roles) {
Set<String> roleSet = new HashSet<>();
for(String role : roles) {
roleSet.add(role.trim());
}
return new ArrayList<>(roleSet);
}
} }

@ -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<String> roleSet = new HashSet<>();
for(String role : roles) {
roleSet.add(role.trim());
}
return String.join(",", roleSet);
}
}

@ -553,36 +553,33 @@ public class APIPublisherServiceImpl implements APIPublisherService {
// scopeMapping[3] != null ? StringUtils.trim(scopeMapping[3]) : StringUtils.EMPTY); // scopeMapping[3] != null ? StringUtils.trim(scopeMapping[3]) : StringUtils.EMPTY);
String permission = scopeMapping[3] != null ? StringUtils.trim(scopeMapping[3]) : StringUtils.EMPTY; String permission = scopeMapping[3] != null ? StringUtils.trim(scopeMapping[3]) : StringUtils.EMPTY;
String roleString = ""; List<String> rolesList = new ArrayList<>();
for (int i = 4; i < scopeMapping.length; i++) { for (int i = 4; i < scopeMapping.length; i++) {
if (scopeMapping[i] != null && StringUtils.trim(scopeMapping[i]).equals("Yes")) { 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)) { if (rolePermissions.containsKey(roles.get(i)) && StringUtils.isNotEmpty(permission)) {
rolePermissions.get(roles.get(i)).add(permission); rolePermissions.get(roles.get(i)).add(permission);
} }
} }
} }
if (roleString.length() > 1) { scope.setRoles(rolesList);
roleString = roleString.substring(1); // remove first , (comma)
}
scope.setRoles(roleString);
//Set scope id which related to the scope key //Set scope id which related to the scope key
JSONArray scopeList = (JSONArray) scopeObject.get("list"); JSONArray scopeList = (JSONArray) scopeObject.get("list");
for (int i = 0; i < scopeList.length(); i++) { for (int i = 0; i < scopeList.length(); i++) {
JSONObject scopeObj = scopeList.getJSONObject(i); JSONObject scopeObj = scopeList.getJSONObject(i);
if (scopeObj.getString("name").equals(scopeMapping[2] != null ? if (scopeObj.getString("name").equals(StringUtils.trim(scopeMapping[2]))) {
StringUtils.trim(scopeMapping[2]) : StringUtils.EMPTY)) {
scope.setId(scopeObj.getString("id")); scope.setId(scopeObj.getString("id"));
scope.setUsageCount(scopeObj.getInt("usageCount"));
// Including already existing roles // Including already existing roles
JSONArray existingRolesArray = (JSONArray) scopeObj.get("bindings"); JSONArray existingRolesArray = (JSONArray) scopeObj.get("bindings");
for (int j = 0; j < existingRolesArray.length(); j++) { 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())) { if (publisherRESTAPIServices.isSharedScopeNameExists(apiApplicationKey, accessTokenInfo, scope.getKey())) {
publisherRESTAPIServices.updateSharedScope(apiApplicationKey, accessTokenInfo, scope); publisherRESTAPIServices.updateSharedScope(apiApplicationKey, accessTokenInfo, scope);

@ -18,11 +18,13 @@
package io.entgra.device.mgt.core.apimgt.webapp.publisher.dto; package io.entgra.device.mgt.core.apimgt.webapp.publisher.dto;
import java.util.List;
public class ApiScope { public class ApiScope {
String key; String key;
String name; String name;
String roles; List<String> roles;
String permissions; String permissions;
String description; String description;
int id; int id;
@ -46,11 +48,11 @@ public class ApiScope {
this.name = name; this.name = name;
} }
public String getRoles() { public List<String> getRoles() {
return this.roles; return this.roles;
} }
public void setRoles(String roles) { public void setRoles(List<String> roles) {
this.roles = roles; this.roles = roles;
} }

@ -228,10 +228,9 @@ public class AnnotationProcessor {
String permissions[]; String permissions[];
StringBuilder aggregatedPermissions; StringBuilder aggregatedPermissions;
String roles[]; String roles[];
StringBuilder aggregatedRoles; List<String> aggregatedRoles;
for (int i = 0; i < annotatedScopes.length; i++) { for (int i = 0; i < annotatedScopes.length; i++) {
aggregatedPermissions = new StringBuilder(); aggregatedPermissions = new StringBuilder();
aggregatedRoles = new StringBuilder();
methodHandler = Proxy.getInvocationHandler(annotatedScopes[i]); methodHandler = Proxy.getInvocationHandler(annotatedScopes[i]);
scope = new ApiScope(); scope = new ApiScope();
scope.setName(invokeMethod(scopeClass scope.setName(invokeMethod(scopeClass
@ -250,11 +249,8 @@ public class AnnotationProcessor {
scope.setPermissions(aggregatedPermissions.toString().trim()); scope.setPermissions(aggregatedPermissions.toString().trim());
roles = (String[]) methodHandler.invoke(annotatedScopes[i], scopeClass roles = (String[]) methodHandler.invoke(annotatedScopes[i], scopeClass
.getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_ROLES, null), null); .getMethod(SWAGGER_ANNOTATIONS_PROPERTIES_ROLES, null), null);
for (String role : roles) { aggregatedRoles = Arrays.asList(roles);
aggregatedRoles.append(role); scope.setRoles(aggregatedRoles);
aggregatedRoles.append(",");
}
scope.setRoles(aggregatedRoles.substring(0, aggregatedRoles.lastIndexOf(",")));
scopes.put(scope.getKey(), scope); scopes.put(scope.getKey(), scope);
} }
return scopes; return scopes;
@ -305,11 +301,13 @@ public class AnnotationProcessor {
// } else { // } else {
// log.warn("Scope is not defined for '" + makeContextURLReady(resourceRootContext) + // log.warn("Scope is not defined for '" + makeContextURLReady(resourceRootContext) +
// makeContextURLReady(subCtx) + "' endpoint, hence assigning the default scope"); // makeContextURLReady(subCtx) + "' endpoint, hence assigning the default scope");
// List<String> roles = new ArrayList<>();
// roles.add(DEFAULT_SCOPE_ROLE);
// scope = new ApiScope(); // scope = new ApiScope();
// scope.setName(DEFAULT_SCOPE_NAME); // scope.setName(DEFAULT_SCOPE_NAME);
// scope.setDescription(DEFAULT_SCOPE_NAME); // scope.setDescription(DEFAULT_SCOPE_NAME);
// scope.setKey(DEFAULT_SCOPE_KEY); // scope.setKey(DEFAULT_SCOPE_KEY);
// scope.setRoles(DEFAULT_SCOPE_ROLE); // scope.setRoles(roles);
// scope.setPermissions(DEFAULT_SCOPE_PERMISSION); // scope.setPermissions(DEFAULT_SCOPE_PERMISSION);
// resource.setScope(scope); // resource.setScope(scope);
// } // }
@ -543,11 +541,13 @@ public class AnnotationProcessor {
} else { } else {
// log.warn("Scope is not defined for '" + makeContextURLReady(resourceRootContext) + // log.warn("Scope is not defined for '" + makeContextURLReady(resourceRootContext) +
// makeContextURLReady(subCtx) + "' endpoint, hence assigning the default scope"); // makeContextURLReady(subCtx) + "' endpoint, hence assigning the default scope");
List<String> roles = new ArrayList<>();
roles.add(DEFAULT_SCOPE_ROLE);
scope = new ApiScope(); scope = new ApiScope();
scope.setName(DEFAULT_SCOPE_NAME); scope.setName(DEFAULT_SCOPE_NAME);
scope.setDescription(DEFAULT_SCOPE_NAME); scope.setDescription(DEFAULT_SCOPE_NAME);
scope.setKey(DEFAULT_SCOPE_KEY); scope.setKey(DEFAULT_SCOPE_KEY);
scope.setRoles(DEFAULT_SCOPE_ROLE); scope.setRoles(roles);
scope.setPermissions(DEFAULT_SCOPE_PERMISSION); scope.setPermissions(DEFAULT_SCOPE_PERMISSION);
apiResource.setScope(scope); apiResource.setScope(scope);
} }

@ -39,7 +39,7 @@ public class TestUtils {
ApiScope scope = new ApiScope(); ApiScope scope = new ApiScope();
scope.setKey("win:ops:reboot"); scope.setKey("win:ops:reboot");
scope.setName("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"); scope.setDescription("Lock reset on Windows devices");
template.setScope(scope); template.setScope(scope);
uriTemplates.add(template); uriTemplates.add(template);

Loading…
Cancel
Save