From 3f1933cfe1f5cdb6d3fefdff9248c9d4430849c2 Mon Sep 17 00:00:00 2001 From: Michael Mokry Date: Fri, 1 Dec 2017 11:39:27 -0600 Subject: Modified the code to fix various pushPolicy issues - Added modifications per Pam's review Change-Id: I5d67ee529cbc5245e7d1f8dbec6f2f2d453ce299 Issue-ID: POLICY-486 Signed-off-by: Michael Mokry (cherry picked from commit b6bae924c4a794cd772ac1524089fc8739e310b1) --- .../services/CreateUpdateDictionaryService.java | 31 ++++-- .../pdp/rest/api/services/ListConfigService.java | 6 +- .../policy/pdp/rest/api/services/PAPServices.java | 3 + .../pdp/rest/api/services/PushPolicyService.java | 36 +------ .../policy/pdp/rest/api/utils/PolicyApiUtils.java | 106 +++++++++++++++++++++ 5 files changed, 136 insertions(+), 46 deletions(-) (limited to 'ONAP-PDP-REST/src/main/java') diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/CreateUpdateDictionaryService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/CreateUpdateDictionaryService.java index 9d2577426..5f6a4fc90 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/CreateUpdateDictionaryService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/CreateUpdateDictionaryService.java @@ -41,6 +41,7 @@ public class CreateUpdateDictionaryService { private String message = null; private Boolean updateFlag = false; private DictionaryParameters dictionaryParameters = null; + private JsonObject json = null; public CreateUpdateDictionaryService( DictionaryParameters dictionaryParameters, String requestID, @@ -62,6 +63,7 @@ public class CreateUpdateDictionaryService { } this.dictionaryParameters.setRequestID(requestUUID); } + try{ run(); specialCheck(); @@ -105,20 +107,14 @@ public class CreateUpdateDictionaryService { } else { operation = "create"; } - JsonObject json = null; - try{ - json = PolicyApiUtils.stringToJsonObject(dictionaryParameters.getDictionaryJson()); - } catch(JsonException| IllegalStateException e){ - message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper Dictionary JSON object : " + dictionaryParameters.getDictionaryJson(); - LOGGER.error(message, e); - return message; - } + String dictionaryFields = json.toString(); PAPServices papServices = new PAPServices(); return (String) papServices.callPAP(new ByteArrayInputStream(dictionaryFields.getBytes()), new String[] {"operation="+operation, "apiflag=api", "dictionaryType="+dictionaryParameters.getDictionary()}, dictionaryParameters.getRequestID(), "dictionaryItem"); } private boolean getValidation() { + LOGGER.info("Start validating create or update dictionary request."); if(dictionaryParameters==null){ message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Dictionary Parameters are not given."; return false; @@ -134,11 +130,28 @@ public class CreateUpdateDictionaryService { if(dictionaryParameters.getDictionaryJson()==null || dictionaryParameters.getDictionaryJson().isEmpty()){ message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Dictionary JSON given."; return false; - } + } if (updateFlag && "MicroServiceDictionary".equalsIgnoreCase(dictionaryParameters.getDictionary())&& !dictionaryParameters.getDictionaryJson().contains("initialFields")){ message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Mising the required field initialFields."; return false; } + + try{ + json = PolicyApiUtils.stringToJsonObject(dictionaryParameters.getDictionaryJson()); + String result = PolicyApiUtils.validateDictionaryJsonFields(json.getJsonObject("dictionaryFields"), dictionaryParameters.getDictionary()); + + if(!"success".equals(result)) { + message = result; + return false; + } + + }catch(JsonException| IllegalStateException e){ + message = XACMLErrorConstants.ERROR_DATA_ISSUE+ " improper Dictionary JSON object : " + dictionaryParameters.getDictionaryJson(); + LOGGER.error(message, e); + return false; + } + + LOGGER.info("dictionary API request validation complete and valid."); return true; } diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ListConfigService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ListConfigService.java index 26f491963..b1cf79b1e 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ListConfigService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/ListConfigService.java @@ -23,6 +23,7 @@ import java.util.ArrayList; import java.util.Collection; import org.onap.policy.api.ConfigRequestParameters; +import org.onap.policy.api.PolicyConfigStatus; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; import org.onap.policy.pdp.rest.api.models.PolicyConfig; @@ -43,11 +44,12 @@ public class ListConfigService { results = new ArrayList(); status = HttpStatus.OK; for(PolicyConfig policyConfig : policyConfigs){ - if(policyConfig.getPolicyConfigMessage()!=null && policyConfig.getPolicyConfigMessage().contains("PE300")){ + if(policyConfig.getPolicyConfigMessage()!=null && policyConfig.getPolicyConfigMessage().contains("PE300") + && policyConfigs.size()<=1 && policyConfig.getPolicyConfigStatus().equals(PolicyConfigStatus.CONFIG_NOT_FOUND)){ results.add(policyConfig.getPolicyConfigMessage()); status = HttpStatus.BAD_REQUEST; } else { - results.add("Policy Name: " + policyConfig.getPolicyName()); + results.add(policyConfig.getPolicyName()); } } } diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PAPServices.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PAPServices.java index f7c175c0c..72d8fdeec 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PAPServices.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PAPServices.java @@ -420,6 +420,9 @@ public class PAPServices { + connection.getHeaderField("message") + " Please check the pdpGroup you are requesting to move the policy to."; LOGGER.error(response); + } else if ("policyNotAvailableForEdit".equals(connection.getHeaderField("error"))) { + response = XACMLErrorConstants.ERROR_DATA_ISSUE + + connection.getHeaderField("message"); } } else if (connection.getResponseCode() == 409 && connection.getHeaderField("error") != null) { if ("modelExistsDB".equals(connection.getHeaderField("error"))) { diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PushPolicyService.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PushPolicyService.java index f2c6c221c..773f0d86a 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PushPolicyService.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/services/PushPolicyService.java @@ -109,49 +109,15 @@ public class PushPolicyService { } try { LOGGER.debug("StdPDPPolicy object contains: " + selectedPolicy.getId() + ", " + selectedPolicy.getName() + ", " + selectedPolicy.getLocation().toString()); - response = copyPolicy(selectedPolicy, pdpGroup, clientScope, pushPolicyParameters.getRequestID()); + response = (String) papServices.callPAP(selectedPolicy, new String[]{"groupId=" + pdpGroup, "policyId="+selectedPolicy.getId(), "apiflag=api", "operation=POST"}, pushPolicyParameters.getRequestID(), clientScope); } catch (PAPException e) { LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW+e.getMessage()); throw new PolicyException(e); } - LOGGER.debug("copyPolicy response: " + response); - if(response.contains("successfully")){ - response = (String) papServices.callPAP(selectedPolicy, new String[]{"groupId=" + pdpGroup, "policyId="+selectedPolicy.getId(), "apiflag=addPolicyToGroup", "operation=PUT"}, pushPolicyParameters.getRequestID(), clientScope); - } LOGGER.debug("Final API response: " + response); return response; } - private String copyPolicy(PDPPolicy policy, String group, String policyType, UUID requestID) throws PAPException { - String response = null; - if (policy == null || group == null) { - throw new PAPException("Null input policy="+policy+" group="+group); - } - try { - StdPAPPolicy location = new StdPAPPolicy(policy.getLocation()); - response = copyFile(policy.getId(), group, location, policyType, requestID); - } catch (Exception e) { - String message = "Unable to PUT policy '" + policy.getId() + "', e:" + e; - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e); - throw new PAPException(message); - } - return response; - } - - private String copyFile(String policyId, String group, StdPAPPolicy location, String clientScope, UUID requestID) throws PAPException { - String response = null; - // send the policy file to the PAP Servlet - PAPServices papService = new PAPServices(); - try { - response = (String) papService.callPAP(location, new String[] {"groupId=" + group, "policyId="+policyId, "apiflag=api", "operation=post"}, requestID, clientScope); - } catch (Exception e) { - String message = "Unable to PUT policy '" + policyId + "', e:" + e; - LOGGER.error(XACMLErrorConstants.ERROR_PROCESS_FLOW + message, e); - throw new PAPException(message); - } - return response; - } - private boolean getValidation() { // While Validating, extract the required values. if (pushPolicyParameters.getPolicyName() != null diff --git a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/utils/PolicyApiUtils.java b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/utils/PolicyApiUtils.java index cd107d0ba..3e6a3e944 100644 --- a/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/utils/PolicyApiUtils.java +++ b/ONAP-PDP-REST/src/main/java/org/onap/policy/pdp/rest/api/utils/PolicyApiUtils.java @@ -22,6 +22,7 @@ package org.onap.policy.pdp.rest.api.utils; import java.io.StringReader; import javax.json.Json; +import javax.json.JsonArray; import javax.json.JsonException; import javax.json.JsonObject; import javax.json.JsonReader; @@ -29,13 +30,17 @@ import javax.json.stream.JsonParsingException; import org.onap.policy.common.logging.flexlogger.FlexLogger; import org.onap.policy.common.logging.flexlogger.Logger; +import org.onap.policy.utils.PolicyUtils; +import org.onap.policy.xacml.api.XACMLErrorConstants; import com.google.common.base.CharMatcher; public class PolicyApiUtils { private static Logger LOGGER = FlexLogger.getLogger(PolicyApiUtils.class .getName()); + private static final String SUCCESS = "success"; + public static Boolean validateNONASCIICharactersAndAllowSpaces( String jsonString) { Boolean isValidForm = false; @@ -69,4 +74,105 @@ public class PolicyApiUtils { jsonReader.close(); return object; } + + public static String validateDictionaryJsonFields(JsonObject json, String dictionary) { + + LOGGER.info("Validating DictionaryJsonField values"); + String message; + + if("Action".equals(dictionary.trim())){ + if(json.containsKey("attributeName")){ + if(json.getString("attributeName")==null || json.getString("attributeName").trim().isEmpty()){ + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Attribute Name provided."; + return message; + } + if(!SUCCESS.equals(PolicyUtils.policySpecialCharValidator(json.getString("attributeName").trim()))){ + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Attribute Name value."; + return message; + } + }else{ + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing attributeName key in the dictionaryJson parameter."; + return message; + } + if(json.containsKey("type")){ + if(json.getString("type")==null || json.getString("type").trim().isEmpty()){ + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Type provided."; + return message; + } + if(!"REST".equals(json.getString("type").trim())){ + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Type value."; + return message; + } + }else{ + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing type key in the dictionaryJson parameter."; + return message; + } + if(json.containsKey("method")){ + if(json.getString("method")==null || json.getString("method").trim().isEmpty()){ + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Method provided."; + return message; + } + if("GET".equals(json.getString("method").trim()) + || "PUT".equals(json.getString("method").trim()) + || "POST".equals(json.getString("method").trim())){ + + message = SUCCESS; + + }else{ + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Invalid Method value."; + return message; + } + }else{ + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing method key in the dictionaryJson parameter."; + return message; + } + if(json.containsKey("url")){ + if(json.getString("url")==null || json.getString("url").trim().isEmpty()){ + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No URL provided."; + return message; + } + }else{ + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing url key in the dictionaryJson parameter."; + return message; + } + if(json.containsKey("body")){ + if(json.getString("body")==null || json.getString("body").trim().isEmpty()){ + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "No Body provided."; + return message; + } + }else{ + message = XACMLErrorConstants.ERROR_DATA_ISSUE + "Missing body key in the dictionaryJson parameter."; + return message; + } + if(json.containsKey("headers")){ + JsonArray array = json.getJsonArray("headers"); + + for (int i = 0;i