Fixes in API publishing flow

tenantviewfix
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.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

@ -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<String> roles;
String description;
String id;
int usageCount;
@ -47,12 +47,12 @@ public class Scope implements Serializable{
this.name = name;
}
public String getRoles() {
public List<String> getRoles() {
return roles;
}
public void setRoles(String roles) {
this.roles = roles;
public void setRoles(List<String> 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<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);
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++) {
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);

@ -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<String> roles;
String permissions;
String description;
int id;
@ -46,11 +48,11 @@ public class ApiScope {
this.name = name;
}
public String getRoles() {
public List<String> getRoles() {
return this.roles;
}
public void setRoles(String roles) {
public void setRoles(List<String> roles) {
this.roles = roles;
}

@ -228,10 +228,9 @@ public class AnnotationProcessor {
String permissions[];
StringBuilder aggregatedPermissions;
String roles[];
StringBuilder aggregatedRoles;
List<String> 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<String> 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<String> 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);
}

@ -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);

Loading…
Cancel
Save