Add validation for policy

Adding validation logic for policy APIs and added a new validate API to validate feature lists
reporting
inoshperera 5 years ago
parent ebbae0a6eb
commit 08ee99f034

@ -35,6 +35,7 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.Policy;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.PriorityUpdatedPolicyWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.PriorityUpdatedPolicyWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.ProfileFeature;
import org.wso2.carbon.device.mgt.jaxrs.util.Constants; import org.wso2.carbon.device.mgt.jaxrs.util.Constants;
import javax.validation.Valid; import javax.validation.Valid;
@ -216,6 +217,75 @@ public interface PolicyManagementService {
required = true) required = true)
@Valid PolicyWrapper policy); @Valid PolicyWrapper policy);
@POST
@Path("/validate")
@ApiOperation(
consumes = MediaType.APPLICATION_JSON,
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Validate a policy",
notes = "Validate a policy",
tags = "Device Policy Management",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = Constants.SCOPE, value = "perm:policies:manage")
})
}
)
@ApiResponses(
value = {
@ApiResponse(
code = 200,
message = "OK. \n Successfully validated the policy.",
responseHeaders = {
@ResponseHeader(
name = "Content-Location",
description = "The URL of the added policy."),
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "ETag",
description = "Entity Tag of the response resource.\n" +
"Used by caches, or in conditional requests."),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource was last modified.\n" +
"Used by caches, or in conditional requests.")
}
),
@ApiResponse(
code = 303,
message = "See Other. \n The source can be retrieved from the URL specified in the " +
"location header",
responseHeaders = {
@ResponseHeader(
name = "Content-Location",
description = "The Source URL of the document.")}),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid request or validation error.",
response = ErrorResponse.class),
@ApiResponse(
code = 401,
message = "Not Found. \n The user that is currently logged in is not " +
"authorized to validate policies.",
response = ErrorResponse.class),
@ApiResponse(
code = 415,
message = "Unsupported media type. \n The format of the requested entity was not " +
"supported."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n " +
"Server error occurred while validating a policy.",
response = ErrorResponse.class)
})
Response validatePolicy(@ApiParam(
name = "profileFeaturesList",
value = "The properties required to validate a policy.",
required = true) List<ProfileFeature> profileFeaturesList);
@GET @GET
@ApiOperation( @ApiOperation(
produces = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON,

@ -48,6 +48,7 @@ import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyList; import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyList;
import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.PriorityUpdatedPolicyWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.PriorityUpdatedPolicyWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.ProfileFeature;
import org.wso2.carbon.device.mgt.jaxrs.service.api.PolicyManagementService; import org.wso2.carbon.device.mgt.jaxrs.service.api.PolicyManagementService;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.FilteringUtil; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.FilteringUtil;
import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil;
@ -78,7 +79,11 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
@POST @POST
@Override @Override
public Response addPolicy(@Valid PolicyWrapper policyWrapper) { public Response addPolicy(@Valid PolicyWrapper policyWrapper) {
RequestValidationUtil.validatePolicyDetails(policyWrapper); List<org.wso2.carbon.policy.mgt.common.ProfileFeature> features = RequestValidationUtil
.validatePolicyDetails(policyWrapper);
if (features != null && features.size() > 0) { // validation failure results;
return Response.status(Response.Status.BAD_REQUEST).entity(features).build();
}
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
try { try {
@ -213,7 +218,11 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
@Path("/{id}") @Path("/{id}")
@Override @Override
public Response updatePolicy(@PathParam("id") int id, @Valid PolicyWrapper policyWrapper) { public Response updatePolicy(@PathParam("id") int id, @Valid PolicyWrapper policyWrapper) {
RequestValidationUtil.validatePolicyDetails(policyWrapper); List<org.wso2.carbon.policy.mgt.common.ProfileFeature> features = RequestValidationUtil
.validatePolicyDetails(policyWrapper);
if (features != null && features.size() > 0) { // validation failure results;
return Response.status(Response.Status.BAD_REQUEST).entity(features).build();
}
PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService(); PolicyManagerService policyManagementService = DeviceMgtAPIUtils.getPolicyManagementService();
try { try {
Policy policy = this.getPolicyFromWrapper(policyWrapper); Policy policy = this.getPolicyFromWrapper(policyWrapper);
@ -460,4 +469,18 @@ public class PolicyManagementServiceImpl implements PolicyManagementService {
return Response.status(Response.Status.OK).entity(targetPolicies).build(); return Response.status(Response.Status.OK).entity(targetPolicies).build();
} }
@POST
@Path("/validate")
@Override
public Response validatePolicy(List<ProfileFeature> profileFeaturesList) {
List<org.wso2.carbon.policy.mgt.common.ProfileFeature> features
= RequestValidationUtil.validateProfileFeatures(profileFeaturesList);
if (features != null && features.size() > 0) { // validation failure results;
return Response.status(Response.Status.BAD_REQUEST).entity(features).build();
}
return Response.status(Response.Status.OK).entity("Valid request").build();
}
} }

@ -22,12 +22,18 @@ import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpStatus; import org.apache.http.HttpStatus;
import org.wso2.carbon.device.mgt.core.dao.impl.device.GenericDeviceDAOImpl;
import org.wso2.carbon.device.mgt.jaxrs.beans.Scope;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; import org.wso2.carbon.device.mgt.common.notification.mgt.Notification;
import org.wso2.carbon.device.mgt.jaxrs.beans.*; import org.wso2.carbon.device.mgt.jaxrs.beans.ApplicationWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse;
import org.wso2.carbon.device.mgt.jaxrs.beans.OldPasswordResetWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.PolicyWrapper;
import org.wso2.carbon.device.mgt.jaxrs.beans.ProfileFeature;
import org.wso2.carbon.device.mgt.jaxrs.beans.RoleInfo;
import org.wso2.carbon.device.mgt.jaxrs.beans.Scope;
import org.wso2.carbon.device.mgt.jaxrs.util.Constants;
import org.wso2.carbon.policy.mgt.common.PolicyPayloadValidator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -273,13 +279,56 @@ public class RequestValidationUtil {
} }
} }
public static void validatePolicyDetails(PolicyWrapper policyWrapper) { public static List<org.wso2.carbon.policy.mgt.common.ProfileFeature> validatePolicyDetails(PolicyWrapper policyWrapper) {
if (policyWrapper == null) { if (policyWrapper == null) {
throw new InputValidationException( throw new InputValidationException(
new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Policy is empty.").build()); new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage("Policy is empty.").build());
} }
return validateProfileFeatures(policyWrapper.getProfile().getProfileFeaturesList());
}
public static List<org.wso2.carbon.policy.mgt.common.ProfileFeature> validateProfileFeatures
(List<ProfileFeature> profileFeatures) {
List<org.wso2.carbon.policy.mgt.common.ProfileFeature> features
= new ArrayList<>();
String deviceType = null;
for (ProfileFeature profileFeature : profileFeatures) {
deviceType = profileFeature.getDeviceTypeId();
org.wso2.carbon.policy.mgt.common.ProfileFeature feature = new org
.wso2.carbon.policy.mgt.common.ProfileFeature();
feature.setContent(profileFeature.getContent());
feature.setDeviceType(profileFeature.getDeviceTypeId());
feature.setFeatureCode(profileFeature.getFeatureCode());
feature.setPayLoad(profileFeature.getPayLoad());
features.add(feature);
}
try {
Class<?> clz;
switch (deviceType) {
case Constants.ANDROID:
clz = Class.forName(Constants.ANDROID_POLICY_VALIDATOR);
PolicyPayloadValidator enrollmentNotifier = (PolicyPayloadValidator) clz.newInstance();
return enrollmentNotifier.validate(features);
default:
log.error("No policy validator found for device type " + deviceType);
}
} catch (InstantiationException e) {
log.error("Error when creating an instance of validator related to deviceType " +
deviceType);
} catch (IllegalAccessException e) {
log.error("Error when accessing an instance of validator related to deviceType " +
deviceType);
} catch (ClassNotFoundException e) {
log.error("Error when loading an instance of validator related to deviceType " +
deviceType);
}
return null;
} }
public static void validatePolicyIds(List<Integer> policyIds) { public static void validatePolicyIds(List<Integer> policyIds) {
if (policyIds == null || policyIds.size() == 0) { if (policyIds == null || policyIds.size() == 0) {
throw new InputValidationException( throw new InputValidationException(

@ -53,6 +53,12 @@ public class Constants {
public static final String DEFAULT_SIMPLE_DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z"; public static final String DEFAULT_SIMPLE_DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss Z";
public static final int DEFAULT_PAGE_LIMIT = 50; public static final int DEFAULT_PAGE_LIMIT = 50;
public static final String FORWARD_SLASH = "/"; public static final String FORWARD_SLASH = "/";
public static final String ANDROID = "android";
public static final String ANDROID_POLICY_VALIDATOR = "io.entgra.proprietary.platform.android." +
"core.polcy.AndroidPolicyPayloadValidator";
public static final String IOS = "ios";
public static final String WINDOWS = "windows";
public final class ErrorMessages { public final class ErrorMessages {

@ -0,0 +1,8 @@
package org.wso2.carbon.policy.mgt.common;
import java.util.List;
public interface PolicyPayloadValidator {
List<ProfileFeature> validate(List<ProfileFeature> features);
}

@ -0,0 +1,74 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. 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 org.wso2.carbon.policy.mgt.common;
import java.io.Serializable;
public class ProfileFeature implements Serializable {
private boolean isValid;
private String featureCode;
private String deviceType;
private Object content;
private String payLoad;
public boolean isValid() {
return isValid;
}
public void setValid(boolean valid) {
isValid = valid;
}
public String getDeviceType() {
return deviceType;
}
public String getFeatureCode() {
return featureCode;
}
public void setFeatureCode(String featureCode) {
this.featureCode = featureCode;
}
public String getDeviceTypeId() {
return deviceType;
}
public void setDeviceType(String deviceType) {
this.deviceType = deviceType;
}
public String getPayLoad() {
return payLoad;
}
public void setPayLoad(String payLoad) {
this.payLoad = payLoad;
}
public Object getContent() {
return content;
}
public void setContent(Object content) {
this.content = content;
}
}
Loading…
Cancel
Save